registrycredentials.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright 2020 Docker, Inc.
  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 convert
  14. import (
  15. "net/url"
  16. "os"
  17. "strings"
  18. "github.com/Azure/azure-sdk-for-go/profiles/latest/containerinstance/mgmt/containerinstance"
  19. "github.com/Azure/go-autorest/autorest/to"
  20. compose "github.com/compose-spec/compose-go/types"
  21. "github.com/docker/cli/cli/config"
  22. "github.com/docker/cli/cli/config/configfile"
  23. "github.com/docker/cli/cli/config/types"
  24. )
  25. // Specific username from ACR docs : https://github.com/Azure/acr/blob/master/docs/AAD-OAuth.md#getting-credentials-programatically
  26. const (
  27. tokenUsername = "00000000-0000-0000-0000-000000000000"
  28. dockerHub = "index.docker.io"
  29. )
  30. type registryConfLoader interface {
  31. getAllRegistryCredentials() (map[string]types.AuthConfig, error)
  32. }
  33. type cliRegistryConfLoader struct {
  34. cfg *configfile.ConfigFile
  35. }
  36. func (c cliRegistryConfLoader) getAllRegistryCredentials() (map[string]types.AuthConfig, error) {
  37. return c.cfg.GetAllCredentials()
  38. }
  39. func newCliRegistryConfLoader() cliRegistryConfLoader {
  40. return cliRegistryConfLoader{
  41. cfg: config.LoadDefaultConfigFile(os.Stderr),
  42. }
  43. }
  44. func getRegistryCredentials(project compose.Project, registryLoader registryConfLoader) ([]containerinstance.ImageRegistryCredential, error) {
  45. allCreds, err := registryLoader.getAllRegistryCredentials()
  46. if err != nil {
  47. return nil, err
  48. }
  49. usedRegistries := map[string]bool{}
  50. for _, service := range project.Services {
  51. imageName := service.Image
  52. tokens := strings.Split(imageName, "/")
  53. registry := tokens[0]
  54. if len(tokens) == 1 { // ! image names can include "." ...
  55. registry = dockerHub
  56. } else if !strings.Contains(registry, ".") {
  57. registry = dockerHub
  58. }
  59. usedRegistries[registry] = true
  60. }
  61. var registryCreds []containerinstance.ImageRegistryCredential
  62. for name, oneCred := range allCreds {
  63. parsedURL, err := url.Parse(name)
  64. // Credentials can contain some garbage, we don't return the error here
  65. // because we don't care about these garbage creds.
  66. if err != nil {
  67. continue
  68. }
  69. hostname := parsedURL.Host
  70. if hostname == "" {
  71. hostname = parsedURL.Path
  72. }
  73. if _, ok := usedRegistries[hostname]; ok {
  74. if oneCred.Password != "" {
  75. aciCredential := containerinstance.ImageRegistryCredential{
  76. Server: to.StringPtr(hostname),
  77. Password: to.StringPtr(oneCred.Password),
  78. Username: to.StringPtr(oneCred.Username),
  79. }
  80. registryCreds = append(registryCreds, aciCredential)
  81. } else if oneCred.IdentityToken != "" {
  82. userName := tokenUsername
  83. if oneCred.Username != "" {
  84. userName = oneCred.Username
  85. }
  86. aciCredential := containerinstance.ImageRegistryCredential{
  87. Server: to.StringPtr(hostname),
  88. Password: to.StringPtr(oneCred.IdentityToken),
  89. Username: to.StringPtr(userName),
  90. }
  91. registryCreds = append(registryCreds, aciCredential)
  92. }
  93. }
  94. }
  95. return registryCreds, nil
  96. }