registrycredentials.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "github.com/docker/cli/cli/config"
  21. "github.com/docker/cli/cli/config/configfile"
  22. "github.com/docker/cli/cli/config/types"
  23. "github.com/docker/api/compose"
  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. if err != nil {
  65. return nil, err
  66. }
  67. hostname := parsedURL.Host
  68. if hostname == "" {
  69. hostname = parsedURL.Path
  70. }
  71. if _, ok := usedRegistries[hostname]; ok {
  72. if oneCred.Username != "" {
  73. aciCredential := containerinstance.ImageRegistryCredential{
  74. Server: to.StringPtr(hostname),
  75. Password: to.StringPtr(oneCred.Password),
  76. Username: to.StringPtr(oneCred.Username),
  77. }
  78. registryCreds = append(registryCreds, aciCredential)
  79. } else if oneCred.IdentityToken != "" {
  80. aciCredential := containerinstance.ImageRegistryCredential{
  81. Server: to.StringPtr(hostname),
  82. Password: to.StringPtr(oneCred.IdentityToken),
  83. Username: to.StringPtr(tokenUsername),
  84. }
  85. registryCreds = append(registryCreds, aciCredential)
  86. }
  87. }
  88. }
  89. return registryCreds, nil
  90. }