keyring.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package credentialprovider
  14. import (
  15. "net"
  16. "net/url"
  17. "path/filepath"
  18. "sort"
  19. "strings"
  20. "k8s.io/klog"
  21. "k8s.io/apimachinery/pkg/util/sets"
  22. )
  23. // DockerKeyring tracks a set of docker registry credentials, maintaining a
  24. // reverse index across the registry endpoints. A registry endpoint is made
  25. // up of a host (e.g. registry.example.com), but it may also contain a path
  26. // (e.g. registry.example.com/foo) This index is important for two reasons:
  27. // - registry endpoints may overlap, and when this happens we must find the
  28. // most specific match for a given image
  29. // - iterating a map does not yield predictable results
  30. type DockerKeyring interface {
  31. Lookup(image string) ([]LazyAuthConfiguration, bool)
  32. }
  33. // BasicDockerKeyring is a trivial map-backed implementation of DockerKeyring
  34. type BasicDockerKeyring struct {
  35. index []string
  36. creds map[string][]LazyAuthConfiguration
  37. }
  38. // lazyDockerKeyring is an implementation of DockerKeyring that lazily
  39. // materializes its dockercfg based on a set of dockerConfigProviders.
  40. type lazyDockerKeyring struct {
  41. Providers []DockerConfigProvider
  42. }
  43. // AuthConfig contains authorization information for connecting to a Registry
  44. // This type mirrors "github.com/docker/docker/api/types.AuthConfig"
  45. type AuthConfig struct {
  46. Username string `json:"username,omitempty"`
  47. Password string `json:"password,omitempty"`
  48. Auth string `json:"auth,omitempty"`
  49. // Email is an optional value associated with the username.
  50. // This field is deprecated and will be removed in a later
  51. // version of docker.
  52. Email string `json:"email,omitempty"`
  53. ServerAddress string `json:"serveraddress,omitempty"`
  54. // IdentityToken is used to authenticate the user and get
  55. // an access token for the registry.
  56. IdentityToken string `json:"identitytoken,omitempty"`
  57. // RegistryToken is a bearer token to be sent to a registry
  58. RegistryToken string `json:"registrytoken,omitempty"`
  59. }
  60. // LazyAuthConfiguration wraps dockertypes.AuthConfig, potentially deferring its
  61. // binding. If Provider is non-nil, it will be used to obtain new credentials
  62. // by calling LazyProvide() on it.
  63. type LazyAuthConfiguration struct {
  64. AuthConfig
  65. Provider DockerConfigProvider
  66. }
  67. func DockerConfigEntryToLazyAuthConfiguration(ident DockerConfigEntry) LazyAuthConfiguration {
  68. return LazyAuthConfiguration{
  69. AuthConfig: AuthConfig{
  70. Username: ident.Username,
  71. Password: ident.Password,
  72. Email: ident.Email,
  73. },
  74. }
  75. }
  76. func (dk *BasicDockerKeyring) Add(cfg DockerConfig) {
  77. if dk.index == nil {
  78. dk.index = make([]string, 0)
  79. dk.creds = make(map[string][]LazyAuthConfiguration)
  80. }
  81. for loc, ident := range cfg {
  82. var creds LazyAuthConfiguration
  83. if ident.Provider != nil {
  84. creds = LazyAuthConfiguration{
  85. Provider: ident.Provider,
  86. }
  87. } else {
  88. creds = DockerConfigEntryToLazyAuthConfiguration(ident)
  89. }
  90. value := loc
  91. if !strings.HasPrefix(value, "https://") && !strings.HasPrefix(value, "http://") {
  92. value = "https://" + value
  93. }
  94. parsed, err := url.Parse(value)
  95. if err != nil {
  96. klog.Errorf("Entry %q in dockercfg invalid (%v), ignoring", loc, err)
  97. continue
  98. }
  99. // The docker client allows exact matches:
  100. // foo.bar.com/namespace
  101. // Or hostname matches:
  102. // foo.bar.com
  103. // It also considers /v2/ and /v1/ equivalent to the hostname
  104. // See ResolveAuthConfig in docker/registry/auth.go.
  105. effectivePath := parsed.Path
  106. if strings.HasPrefix(effectivePath, "/v2/") || strings.HasPrefix(effectivePath, "/v1/") {
  107. effectivePath = effectivePath[3:]
  108. }
  109. var key string
  110. if (len(effectivePath) > 0) && (effectivePath != "/") {
  111. key = parsed.Host + effectivePath
  112. } else {
  113. key = parsed.Host
  114. }
  115. dk.creds[key] = append(dk.creds[key], creds)
  116. dk.index = append(dk.index, key)
  117. }
  118. eliminateDupes := sets.NewString(dk.index...)
  119. dk.index = eliminateDupes.List()
  120. // Update the index used to identify which credentials to use for a given
  121. // image. The index is reverse-sorted so more specific paths are matched
  122. // first. For example, if for the given image "quay.io/coreos/etcd",
  123. // credentials for "quay.io/coreos" should match before "quay.io".
  124. sort.Sort(sort.Reverse(sort.StringSlice(dk.index)))
  125. }
  126. const (
  127. defaultRegistryHost = "index.docker.io"
  128. defaultRegistryKey = defaultRegistryHost + "/v1/"
  129. )
  130. // isDefaultRegistryMatch determines whether the given image will
  131. // pull from the default registry (DockerHub) based on the
  132. // characteristics of its name.
  133. func isDefaultRegistryMatch(image string) bool {
  134. parts := strings.SplitN(image, "/", 2)
  135. if len(parts[0]) == 0 {
  136. return false
  137. }
  138. if len(parts) == 1 {
  139. // e.g. library/ubuntu
  140. return true
  141. }
  142. if parts[0] == "docker.io" || parts[0] == "index.docker.io" {
  143. // resolve docker.io/image and index.docker.io/image as default registry
  144. return true
  145. }
  146. // From: http://blog.docker.com/2013/07/how-to-use-your-own-registry/
  147. // Docker looks for either a “.” (domain separator) or “:” (port separator)
  148. // to learn that the first part of the repository name is a location and not
  149. // a user name.
  150. return !strings.ContainsAny(parts[0], ".:")
  151. }
  152. // url.Parse require a scheme, but ours don't have schemes. Adding a
  153. // scheme to make url.Parse happy, then clear out the resulting scheme.
  154. func parseSchemelessUrl(schemelessUrl string) (*url.URL, error) {
  155. parsed, err := url.Parse("https://" + schemelessUrl)
  156. if err != nil {
  157. return nil, err
  158. }
  159. // clear out the resulting scheme
  160. parsed.Scheme = ""
  161. return parsed, nil
  162. }
  163. // split the host name into parts, as well as the port
  164. func splitUrl(url *url.URL) (parts []string, port string) {
  165. host, port, err := net.SplitHostPort(url.Host)
  166. if err != nil {
  167. // could not parse port
  168. host, port = url.Host, ""
  169. }
  170. return strings.Split(host, "."), port
  171. }
  172. // overloaded version of urlsMatch, operating on strings instead of URLs.
  173. func urlsMatchStr(glob string, target string) (bool, error) {
  174. globUrl, err := parseSchemelessUrl(glob)
  175. if err != nil {
  176. return false, err
  177. }
  178. targetUrl, err := parseSchemelessUrl(target)
  179. if err != nil {
  180. return false, err
  181. }
  182. return urlsMatch(globUrl, targetUrl)
  183. }
  184. // check whether the given target url matches the glob url, which may have
  185. // glob wild cards in the host name.
  186. //
  187. // Examples:
  188. // globUrl=*.docker.io, targetUrl=blah.docker.io => match
  189. // globUrl=*.docker.io, targetUrl=not.right.io => no match
  190. //
  191. // Note that we don't support wildcards in ports and paths yet.
  192. func urlsMatch(globUrl *url.URL, targetUrl *url.URL) (bool, error) {
  193. globUrlParts, globPort := splitUrl(globUrl)
  194. targetUrlParts, targetPort := splitUrl(targetUrl)
  195. if globPort != targetPort {
  196. // port doesn't match
  197. return false, nil
  198. }
  199. if len(globUrlParts) != len(targetUrlParts) {
  200. // host name does not have the same number of parts
  201. return false, nil
  202. }
  203. if !strings.HasPrefix(targetUrl.Path, globUrl.Path) {
  204. // the path of the credential must be a prefix
  205. return false, nil
  206. }
  207. for k, globUrlPart := range globUrlParts {
  208. targetUrlPart := targetUrlParts[k]
  209. matched, err := filepath.Match(globUrlPart, targetUrlPart)
  210. if err != nil {
  211. return false, err
  212. }
  213. if !matched {
  214. // glob mismatch for some part
  215. return false, nil
  216. }
  217. }
  218. // everything matches
  219. return true, nil
  220. }
  221. // Lookup implements the DockerKeyring method for fetching credentials based on image name.
  222. // Multiple credentials may be returned if there are multiple potentially valid credentials
  223. // available. This allows for rotation.
  224. func (dk *BasicDockerKeyring) Lookup(image string) ([]LazyAuthConfiguration, bool) {
  225. // range over the index as iterating over a map does not provide a predictable ordering
  226. ret := []LazyAuthConfiguration{}
  227. for _, k := range dk.index {
  228. // both k and image are schemeless URLs because even though schemes are allowed
  229. // in the credential configurations, we remove them in Add.
  230. if matched, _ := urlsMatchStr(k, image); matched {
  231. ret = append(ret, dk.creds[k]...)
  232. }
  233. }
  234. if len(ret) > 0 {
  235. return ret, true
  236. }
  237. // Use credentials for the default registry if provided, and appropriate
  238. if isDefaultRegistryMatch(image) {
  239. if auth, ok := dk.creds[defaultRegistryHost]; ok {
  240. return auth, true
  241. }
  242. }
  243. return []LazyAuthConfiguration{}, false
  244. }
  245. // Lookup implements the DockerKeyring method for fetching credentials
  246. // based on image name.
  247. func (dk *lazyDockerKeyring) Lookup(image string) ([]LazyAuthConfiguration, bool) {
  248. keyring := &BasicDockerKeyring{}
  249. for _, p := range dk.Providers {
  250. keyring.Add(p.Provide(image))
  251. }
  252. return keyring.Lookup(image)
  253. }
  254. type FakeKeyring struct {
  255. auth []LazyAuthConfiguration
  256. ok bool
  257. }
  258. func (f *FakeKeyring) Lookup(image string) ([]LazyAuthConfiguration, bool) {
  259. return f.auth, f.ok
  260. }
  261. // UnionDockerKeyring delegates to a set of keyrings.
  262. type UnionDockerKeyring []DockerKeyring
  263. func (k UnionDockerKeyring) Lookup(image string) ([]LazyAuthConfiguration, bool) {
  264. authConfigs := []LazyAuthConfiguration{}
  265. for _, subKeyring := range k {
  266. if subKeyring == nil {
  267. continue
  268. }
  269. currAuthResults, _ := subKeyring.Lookup(image)
  270. authConfigs = append(authConfigs, currAuthResults...)
  271. }
  272. return authConfigs, (len(authConfigs) > 0)
  273. }