github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/registries/registries.go (about) 1 package registries 2 3 // TODO: this package should not exist anymore. Users should either use 4 // c/image's `sysregistriesv2` package directly OR, even better, we cache a 5 // config in libpod's image runtime so we don't need to parse the 6 // registries.conf files redundantly. 7 8 import ( 9 "os" 10 "path/filepath" 11 12 "github.com/containers/image/v5/pkg/sysregistriesv2" 13 "github.com/containers/image/v5/types" 14 "github.com/containers/podman/v2/pkg/rootless" 15 "github.com/pkg/errors" 16 ) 17 18 // userRegistriesFile is the path to the per user registry configuration file. 19 var userRegistriesFile = filepath.Join(os.Getenv("HOME"), ".config/containers/registries.conf") 20 21 // SystemRegistriesConfPath returns an appropriate value for types.SystemContext.SystemRegistriesConfPath 22 // (possibly "", which is not an error), taking into account rootless mode and environment variable overrides. 23 // 24 // FIXME: This should be centralized in a global SystemContext initializer inherited throughout the code, 25 // not haphazardly called throughout the way it is being called now. 26 func SystemRegistriesConfPath() string { 27 if envOverride := os.Getenv("REGISTRIES_CONFIG_PATH"); len(envOverride) > 0 { 28 return envOverride 29 } 30 31 if rootless.IsRootless() { 32 if _, err := os.Stat(userRegistriesFile); err == nil { 33 return userRegistriesFile 34 } 35 } 36 37 return "" 38 } 39 40 // GetRegistriesData obtains the list of registries 41 func GetRegistriesData() ([]sysregistriesv2.Registry, error) { 42 registries, err := sysregistriesv2.GetRegistries(&types.SystemContext{SystemRegistriesConfPath: SystemRegistriesConfPath()}) 43 if err != nil { 44 return nil, errors.Wrapf(err, "unable to parse the registries.conf file") 45 } 46 return registries, nil 47 } 48 49 // GetRegistries obtains the list of search registries defined in the global registries file. 50 func GetRegistries() ([]string, error) { 51 return sysregistriesv2.UnqualifiedSearchRegistries(&types.SystemContext{SystemRegistriesConfPath: SystemRegistriesConfPath()}) 52 } 53 54 // GetBlockedRegistries obtains the list of blocked registries defined in the global registries file. 55 func GetBlockedRegistries() ([]string, error) { 56 var blockedRegistries []string 57 registries, err := GetRegistriesData() 58 if err != nil { 59 return nil, err 60 } 61 for _, reg := range registries { 62 if reg.Blocked { 63 blockedRegistries = append(blockedRegistries, reg.Prefix) 64 } 65 } 66 return blockedRegistries, nil 67 } 68 69 // GetInsecureRegistries obtains the list of insecure registries from the global registration file. 70 func GetInsecureRegistries() ([]string, error) { 71 var insecureRegistries []string 72 registries, err := GetRegistriesData() 73 if err != nil { 74 return nil, err 75 } 76 for _, reg := range registries { 77 if reg.Insecure { 78 insecureRegistries = append(insecureRegistries, reg.Prefix) 79 } 80 } 81 return insecureRegistries, nil 82 }