registrycredentials.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package convert
  2. import (
  3. "net/url"
  4. "os"
  5. "strings"
  6. "github.com/Azure/azure-sdk-for-go/profiles/latest/containerinstance/mgmt/containerinstance"
  7. "github.com/Azure/go-autorest/autorest/to"
  8. "github.com/docker/cli/cli/config"
  9. "github.com/docker/cli/cli/config/configfile"
  10. "github.com/docker/cli/cli/config/types"
  11. "github.com/docker/api/compose"
  12. )
  13. // Specific username from ACR docs : https://github.com/Azure/acr/blob/master/docs/AAD-OAuth.md#getting-credentials-programatically
  14. const (
  15. tokenUsername = "00000000-0000-0000-0000-000000000000"
  16. dockerHub = "index.docker.io"
  17. )
  18. type registryConfLoader interface {
  19. getAllRegistryCredentials() (map[string]types.AuthConfig, error)
  20. }
  21. type cliRegistryConfLoader struct {
  22. cfg *configfile.ConfigFile
  23. }
  24. func (c cliRegistryConfLoader) getAllRegistryCredentials() (map[string]types.AuthConfig, error) {
  25. return c.cfg.GetAllCredentials()
  26. }
  27. func newCliRegistryConfLoader() cliRegistryConfLoader {
  28. return cliRegistryConfLoader{
  29. cfg: config.LoadDefaultConfigFile(os.Stderr),
  30. }
  31. }
  32. func getRegistryCredentials(project compose.Project, registryLoader registryConfLoader) ([]containerinstance.ImageRegistryCredential, error) {
  33. allCreds, err := registryLoader.getAllRegistryCredentials()
  34. if err != nil {
  35. return nil, err
  36. }
  37. usedRegistries := map[string]bool{}
  38. for _, service := range project.Services {
  39. imageName := service.Image
  40. tokens := strings.Split(imageName, "/")
  41. registry := tokens[0]
  42. if len(tokens) == 1 { // ! image names can include "." ...
  43. registry = dockerHub
  44. } else if !strings.Contains(registry, ".") {
  45. registry = dockerHub
  46. }
  47. usedRegistries[registry] = true
  48. }
  49. var registryCreds []containerinstance.ImageRegistryCredential
  50. for name, oneCred := range allCreds {
  51. parsedURL, err := url.Parse(name)
  52. if err != nil {
  53. return nil, err
  54. }
  55. hostname := parsedURL.Host
  56. if hostname == "" {
  57. hostname = parsedURL.Path
  58. }
  59. if _, ok := usedRegistries[hostname]; ok {
  60. if oneCred.Username != "" {
  61. aciCredential := containerinstance.ImageRegistryCredential{
  62. Server: to.StringPtr(hostname),
  63. Password: to.StringPtr(oneCred.Password),
  64. Username: to.StringPtr(oneCred.Username),
  65. }
  66. registryCreds = append(registryCreds, aciCredential)
  67. } else if oneCred.IdentityToken != "" {
  68. aciCredential := containerinstance.ImageRegistryCredential{
  69. Server: to.StringPtr(hostname),
  70. Password: to.StringPtr(oneCred.IdentityToken),
  71. Username: to.StringPtr(tokenUsername),
  72. }
  73. registryCreds = append(registryCreds, aciCredential)
  74. }
  75. }
  76. }
  77. return registryCreds, nil
  78. }