...

Source file src/k8s.io/kubernetes/pkg/credentialprovider/secrets/secrets.go

Documentation: k8s.io/kubernetes/pkg/credentialprovider/secrets

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package secrets
    18  
    19  import (
    20  	"encoding/json"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	"k8s.io/kubernetes/pkg/credentialprovider"
    24  )
    25  
    26  // MakeDockerKeyring inspects the passedSecrets to see if they contain any DockerConfig secrets.  If they do,
    27  // then a DockerKeyring is built based on every hit and unioned with the defaultKeyring.
    28  // If they do not, then the default keyring is returned
    29  func MakeDockerKeyring(passedSecrets []v1.Secret, defaultKeyring credentialprovider.DockerKeyring) (credentialprovider.DockerKeyring, error) {
    30  	passedCredentials := []credentialprovider.DockerConfig{}
    31  	for _, passedSecret := range passedSecrets {
    32  		if dockerConfigJSONBytes, dockerConfigJSONExists := passedSecret.Data[v1.DockerConfigJsonKey]; (passedSecret.Type == v1.SecretTypeDockerConfigJson) && dockerConfigJSONExists && (len(dockerConfigJSONBytes) > 0) {
    33  			dockerConfigJSON := credentialprovider.DockerConfigJSON{}
    34  			if err := json.Unmarshal(dockerConfigJSONBytes, &dockerConfigJSON); err != nil {
    35  				return nil, err
    36  			}
    37  
    38  			passedCredentials = append(passedCredentials, dockerConfigJSON.Auths)
    39  		} else if dockercfgBytes, dockercfgExists := passedSecret.Data[v1.DockerConfigKey]; (passedSecret.Type == v1.SecretTypeDockercfg) && dockercfgExists && (len(dockercfgBytes) > 0) {
    40  			dockercfg := credentialprovider.DockerConfig{}
    41  			if err := json.Unmarshal(dockercfgBytes, &dockercfg); err != nil {
    42  				return nil, err
    43  			}
    44  
    45  			passedCredentials = append(passedCredentials, dockercfg)
    46  		}
    47  	}
    48  
    49  	if len(passedCredentials) > 0 {
    50  		basicKeyring := &credentialprovider.BasicDockerKeyring{}
    51  		for _, currCredentials := range passedCredentials {
    52  			basicKeyring.Add(currCredentials)
    53  		}
    54  		return credentialprovider.UnionDockerKeyring{basicKeyring, defaultKeyring}, nil
    55  	}
    56  
    57  	return defaultKeyring, nil
    58  }
    59  

View as plain text