summaryrefslogtreecommitdiff
path: root/pkg/registries/registries.go
blob: 5c4ecd020e43663db37cb0c1f521f1efa69760da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package registries

import (
	"os"
	"path/filepath"
	"strings"

	"github.com/containers/image/pkg/sysregistriesv2"
	"github.com/containers/image/types"
	"github.com/containers/libpod/pkg/rootless"
	"github.com/docker/distribution/reference"
	"github.com/pkg/errors"
)

// userRegistriesFile is the path to the per user registry configuration file.
var userRegistriesFile = filepath.Join(os.Getenv("HOME"), ".config/containers/registries.conf")

// SystemRegistriesConfPath returns an appropriate value for types.SystemContext.SystemRegistriesConfPath
// (possibly "", which is not an error), taking into account rootless mode and environment variable overrides.
//
// FIXME: This should be centralized in a global SystemContext initializer inherited throughout the code,
// not haphazardly called throughout the way it is being called now.
func SystemRegistriesConfPath() string {
	if envOverride := os.Getenv("REGISTRIES_CONFIG_PATH"); len(envOverride) > 0 {
		return envOverride
	}

	if rootless.IsRootless() {
		if _, err := os.Stat(userRegistriesFile); err == nil {
			return userRegistriesFile
		}
	}

	return ""
}

func getRegistries() ([]sysregistriesv2.Registry, error) {
	registries, err := sysregistriesv2.GetRegistries(&types.SystemContext{SystemRegistriesConfPath: SystemRegistriesConfPath()})
	if err != nil {
		return nil, errors.Wrapf(err, "unable to parse the registries.conf file")
	}
	return registries, nil
}

// GetRegistries obtains the list of search registries defined in the global registries file.
func GetRegistries() ([]string, error) {
	var searchRegistries []string
	registries, err := getRegistries()
	if err != nil {
		return nil, err
	}
	for _, reg := range registries {
		if reg.Search {
			searchRegistries = append(searchRegistries, reg.Location)
		}
	}
	return searchRegistries, nil
}

// GetBlockedRegistries obtains the list of blocked registries defined in the global registries file.
func GetBlockedRegistries() ([]string, error) {
	var blockedRegistries []string
	registries, err := getRegistries()
	if err != nil {
		return nil, err
	}
	for _, reg := range registries {
		if reg.Blocked {
			blockedRegistries = append(blockedRegistries, reg.Location)
		}
	}
	return blockedRegistries, nil
}

// GetInsecureRegistries obtains the list of insecure registries from the global registration file.
func GetInsecureRegistries() ([]string, error) {
	var insecureRegistries []string
	registries, err := getRegistries()
	if err != nil {
		return nil, err
	}
	for _, reg := range registries {
		if reg.Insecure {
			insecureRegistries = append(insecureRegistries, reg.Location)
		}
	}
	return insecureRegistries, nil
}

// GetRegistry returns the registry name from a string if specified
func GetRegistry(image string) (string, error) {
	// It is possible to only have the registry name in the format "myregistry/"
	// if so, just trim the "/" from the end and return the registry name
	if strings.HasSuffix(image, "/") {
		return strings.TrimSuffix(image, "/"), nil
	}
	imgRef, err := reference.Parse(image)
	if err != nil {
		return "", err
	}
	return reference.Domain(imgRef.(reference.Named)), nil
}