secrets.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. Copyright 2018 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 secrets
  14. import (
  15. "encoding/json"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/kubernetes/pkg/credentialprovider"
  18. )
  19. // MakeDockerKeyring inspects the passedSecrets to see if they contain any DockerConfig secrets. If they do,
  20. // then a DockerKeyring is built based on every hit and unioned with the defaultKeyring.
  21. // If they do not, then the default keyring is returned
  22. func MakeDockerKeyring(passedSecrets []v1.Secret, defaultKeyring credentialprovider.DockerKeyring) (credentialprovider.DockerKeyring, error) {
  23. passedCredentials := []credentialprovider.DockerConfig{}
  24. for _, passedSecret := range passedSecrets {
  25. if dockerConfigJSONBytes, dockerConfigJSONExists := passedSecret.Data[v1.DockerConfigJsonKey]; (passedSecret.Type == v1.SecretTypeDockerConfigJson) && dockerConfigJSONExists && (len(dockerConfigJSONBytes) > 0) {
  26. dockerConfigJSON := credentialprovider.DockerConfigJson{}
  27. if err := json.Unmarshal(dockerConfigJSONBytes, &dockerConfigJSON); err != nil {
  28. return nil, err
  29. }
  30. passedCredentials = append(passedCredentials, dockerConfigJSON.Auths)
  31. } else if dockercfgBytes, dockercfgExists := passedSecret.Data[v1.DockerConfigKey]; (passedSecret.Type == v1.SecretTypeDockercfg) && dockercfgExists && (len(dockercfgBytes) > 0) {
  32. dockercfg := credentialprovider.DockerConfig{}
  33. if err := json.Unmarshal(dockercfgBytes, &dockercfg); err != nil {
  34. return nil, err
  35. }
  36. passedCredentials = append(passedCredentials, dockercfg)
  37. }
  38. }
  39. if len(passedCredentials) > 0 {
  40. basicKeyring := &credentialprovider.BasicDockerKeyring{}
  41. for _, currCredentials := range passedCredentials {
  42. basicKeyring.Add(currCredentials)
  43. }
  44. return credentialprovider.UnionDockerKeyring{basicKeyring, defaultKeyring}, nil
  45. }
  46. return defaultKeyring, nil
  47. }