github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/registries/registries.go (about) 1 package registries 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 8 "github.com/containers/image/v5/pkg/sysregistriesv2" 9 "github.com/containers/image/v5/types" 10 "github.com/containers/libpod/pkg/rootless" 11 "github.com/docker/distribution/reference" 12 "github.com/pkg/errors" 13 ) 14 15 // userRegistriesFile is the path to the per user registry configuration file. 16 var userRegistriesFile = filepath.Join(os.Getenv("HOME"), ".config/containers/registries.conf") 17 18 // SystemRegistriesConfPath returns an appropriate value for types.SystemContext.SystemRegistriesConfPath 19 // (possibly "", which is not an error), taking into account rootless mode and environment variable overrides. 20 // 21 // FIXME: This should be centralized in a global SystemContext initializer inherited throughout the code, 22 // not haphazardly called throughout the way it is being called now. 23 func SystemRegistriesConfPath() string { 24 if envOverride := os.Getenv("REGISTRIES_CONFIG_PATH"); len(envOverride) > 0 { 25 return envOverride 26 } 27 28 if rootless.IsRootless() { 29 if _, err := os.Stat(userRegistriesFile); err == nil { 30 return userRegistriesFile 31 } 32 } 33 34 return "" 35 } 36 37 // GetRegistriesData obtains the list of registries 38 func GetRegistriesData() ([]sysregistriesv2.Registry, error) { 39 registries, err := sysregistriesv2.GetRegistries(&types.SystemContext{SystemRegistriesConfPath: SystemRegistriesConfPath()}) 40 if err != nil { 41 return nil, errors.Wrapf(err, "unable to parse the registries.conf file") 42 } 43 return registries, nil 44 } 45 46 // GetRegistries obtains the list of search registries defined in the global registries file. 47 func GetRegistries() ([]string, error) { 48 return sysregistriesv2.UnqualifiedSearchRegistries(&types.SystemContext{SystemRegistriesConfPath: SystemRegistriesConfPath()}) 49 } 50 51 // GetBlockedRegistries obtains the list of blocked registries defined in the global registries file. 52 func GetBlockedRegistries() ([]string, error) { 53 var blockedRegistries []string 54 registries, err := GetRegistriesData() 55 if err != nil { 56 return nil, err 57 } 58 for _, reg := range registries { 59 if reg.Blocked { 60 blockedRegistries = append(blockedRegistries, reg.Prefix) 61 } 62 } 63 return blockedRegistries, nil 64 } 65 66 // GetInsecureRegistries obtains the list of insecure registries from the global registration file. 67 func GetInsecureRegistries() ([]string, error) { 68 var insecureRegistries []string 69 registries, err := GetRegistriesData() 70 if err != nil { 71 return nil, err 72 } 73 for _, reg := range registries { 74 if reg.Insecure { 75 insecureRegistries = append(insecureRegistries, reg.Prefix) 76 } 77 } 78 return insecureRegistries, nil 79 } 80 81 // GetRegistry returns the registry name from a string if specified 82 func GetRegistry(image string) (string, error) { 83 // It is possible to only have the registry name in the format "myregistry/" 84 // if so, just trim the "/" from the end and return the registry name 85 if strings.HasSuffix(image, "/") { 86 return strings.TrimSuffix(image, "/"), nil 87 } 88 imgRef, err := reference.Parse(image) 89 if err != nil { 90 return "", err 91 } 92 return reference.Domain(imgRef.(reference.Named)), nil 93 }