keyring.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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) ([]AuthConfig, bool)
  32. }
  33. // BasicDockerKeyring is a trivial map-backed implementation of DockerKeyring
  34. type BasicDockerKeyring struct {
  35. index []string
  36. creds map[string][]AuthConfig
  37. }
  38. // providersDockerKeyring is an implementation of DockerKeyring that
  39. // materializes its dockercfg based on a set of dockerConfigProviders.
  40. type providersDockerKeyring 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. func (dk *BasicDockerKeyring) Add(cfg DockerConfig) {
  61. if dk.index == nil {
  62. dk.index = make([]string, 0)
  63. dk.creds = make(map[string][]AuthConfig)
  64. }
  65. for loc, ident := range cfg {
  66. creds := AuthConfig{
  67. Username: ident.Username,
  68. Password: ident.Password,
  69. Email: ident.Email,
  70. }
  71. value := loc
  72. if !strings.HasPrefix(value, "https://") && !strings.HasPrefix(value, "http://") {
  73. value = "https://" + value
  74. }
  75. parsed, err := url.Parse(value)
  76. if err != nil {
  77. klog.Errorf("Entry %q in dockercfg invalid (%v), ignoring", loc, err)
  78. continue
  79. }
  80. // The docker client allows exact matches:
  81. // foo.bar.com/namespace
  82. // Or hostname matches:
  83. // foo.bar.com
  84. // It also considers /v2/ and /v1/ equivalent to the hostname
  85. // See ResolveAuthConfig in docker/registry/auth.go.
  86. effectivePath := parsed.Path
  87. if strings.HasPrefix(effectivePath, "/v2/") || strings.HasPrefix(effectivePath, "/v1/") {
  88. effectivePath = effectivePath[3:]
  89. }
  90. var key string
  91. if (len(effectivePath) > 0) && (effectivePath != "/") {
  92. key = parsed.Host + effectivePath
  93. } else {
  94. key = parsed.Host
  95. }
  96. dk.creds[key] = append(dk.creds[key], creds)
  97. dk.index = append(dk.index, key)
  98. }
  99. eliminateDupes := sets.NewString(dk.index...)
  100. dk.index = eliminateDupes.List()
  101. // Update the index used to identify which credentials to use for a given
  102. // image. The index is reverse-sorted so more specific paths are matched
  103. // first. For example, if for the given image "quay.io/coreos/etcd",
  104. // credentials for "quay.io/coreos" should match before "quay.io".
  105. sort.Sort(sort.Reverse(sort.StringSlice(dk.index)))
  106. }
  107. const (
  108. defaultRegistryHost = "index.docker.io"
  109. defaultRegistryKey = defaultRegistryHost + "/v1/"
  110. )
  111. // isDefaultRegistryMatch determines whether the given image will
  112. // pull from the default registry (DockerHub) based on the
  113. // characteristics of its name.
  114. func isDefaultRegistryMatch(image string) bool {
  115. parts := strings.SplitN(image, "/", 2)
  116. if len(parts[0]) == 0 {
  117. return false
  118. }
  119. if len(parts) == 1 {
  120. // e.g. library/ubuntu
  121. return true
  122. }
  123. if parts[0] == "docker.io" || parts[0] == "index.docker.io" {
  124. // resolve docker.io/image and index.docker.io/image as default registry
  125. return true
  126. }
  127. // From: http://blog.docker.com/2013/07/how-to-use-your-own-registry/
  128. // Docker looks for either a “.” (domain separator) or “:” (port separator)
  129. // to learn that the first part of the repository name is a location and not
  130. // a user name.
  131. return !strings.ContainsAny(parts[0], ".:")
  132. }
  133. // url.Parse require a scheme, but ours don't have schemes. Adding a
  134. // scheme to make url.Parse happy, then clear out the resulting scheme.
  135. func parseSchemelessUrl(schemelessUrl string) (*url.URL, error) {
  136. parsed, err := url.Parse("https://" + schemelessUrl)
  137. if err != nil {
  138. return nil, err
  139. }
  140. // clear out the resulting scheme
  141. parsed.Scheme = ""
  142. return parsed, nil
  143. }
  144. // split the host name into parts, as well as the port
  145. func splitUrl(url *url.URL) (parts []string, port string) {
  146. host, port, err := net.SplitHostPort(url.Host)
  147. if err != nil {
  148. // could not parse port
  149. host, port = url.Host, ""
  150. }
  151. return strings.Split(host, "."), port
  152. }
  153. // overloaded version of urlsMatch, operating on strings instead of URLs.
  154. func urlsMatchStr(glob string, target string) (bool, error) {
  155. globUrl, err := parseSchemelessUrl(glob)
  156. if err != nil {
  157. return false, err
  158. }
  159. targetUrl, err := parseSchemelessUrl(target)
  160. if err != nil {
  161. return false, err
  162. }
  163. return urlsMatch(globUrl, targetUrl)
  164. }
  165. // check whether the given target url matches the glob url, which may have
  166. // glob wild cards in the host name.
  167. //
  168. // Examples:
  169. // globUrl=*.docker.io, targetUrl=blah.docker.io => match
  170. // globUrl=*.docker.io, targetUrl=not.right.io => no match
  171. //
  172. // Note that we don't support wildcards in ports and paths yet.
  173. func urlsMatch(globUrl *url.URL, targetUrl *url.URL) (bool, error) {
  174. globUrlParts, globPort := splitUrl(globUrl)
  175. targetUrlParts, targetPort := splitUrl(targetUrl)
  176. if globPort != targetPort {
  177. // port doesn't match
  178. return false, nil
  179. }
  180. if len(globUrlParts) != len(targetUrlParts) {
  181. // host name does not have the same number of parts
  182. return false, nil
  183. }
  184. if !strings.HasPrefix(targetUrl.Path, globUrl.Path) {
  185. // the path of the credential must be a prefix
  186. return false, nil
  187. }
  188. for k, globUrlPart := range globUrlParts {
  189. targetUrlPart := targetUrlParts[k]
  190. matched, err := filepath.Match(globUrlPart, targetUrlPart)
  191. if err != nil {
  192. return false, err
  193. }
  194. if !matched {
  195. // glob mismatch for some part
  196. return false, nil
  197. }
  198. }
  199. // everything matches
  200. return true, nil
  201. }
  202. // Lookup implements the DockerKeyring method for fetching credentials based on image name.
  203. // Multiple credentials may be returned if there are multiple potentially valid credentials
  204. // available. This allows for rotation.
  205. func (dk *BasicDockerKeyring) Lookup(image string) ([]AuthConfig, bool) {
  206. // range over the index as iterating over a map does not provide a predictable ordering
  207. ret := []AuthConfig{}
  208. for _, k := range dk.index {
  209. // both k and image are schemeless URLs because even though schemes are allowed
  210. // in the credential configurations, we remove them in Add.
  211. if matched, _ := urlsMatchStr(k, image); matched {
  212. ret = append(ret, dk.creds[k]...)
  213. }
  214. }
  215. if len(ret) > 0 {
  216. return ret, true
  217. }
  218. // Use credentials for the default registry if provided, and appropriate
  219. if isDefaultRegistryMatch(image) {
  220. if auth, ok := dk.creds[defaultRegistryHost]; ok {
  221. return auth, true
  222. }
  223. }
  224. return []AuthConfig{}, false
  225. }
  226. // Lookup implements the DockerKeyring method for fetching credentials
  227. // based on image name.
  228. func (dk *providersDockerKeyring) Lookup(image string) ([]AuthConfig, bool) {
  229. keyring := &BasicDockerKeyring{}
  230. for _, p := range dk.Providers {
  231. keyring.Add(p.Provide(image))
  232. }
  233. return keyring.Lookup(image)
  234. }
  235. type FakeKeyring struct {
  236. auth []AuthConfig
  237. ok bool
  238. }
  239. func (f *FakeKeyring) Lookup(image string) ([]AuthConfig, bool) {
  240. return f.auth, f.ok
  241. }
  242. // UnionDockerKeyring delegates to a set of keyrings.
  243. type UnionDockerKeyring []DockerKeyring
  244. func (k UnionDockerKeyring) Lookup(image string) ([]AuthConfig, bool) {
  245. authConfigs := []AuthConfig{}
  246. for _, subKeyring := range k {
  247. if subKeyring == nil {
  248. continue
  249. }
  250. currAuthResults, _ := subKeyring.Lookup(image)
  251. authConfigs = append(authConfigs, currAuthResults...)
  252. }
  253. return authConfigs, (len(authConfigs) > 0)
  254. }