...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs/checksums.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs

     1  /*
     2  Copyright 2019 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 componentconfigs
    18  
    19  import (
    20  	"crypto/sha256"
    21  	"fmt"
    22  	"sort"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  
    26  	"k8s.io/kubernetes/cmd/kubeadm/app/constants"
    27  )
    28  
    29  // ChecksumForConfigMap calculates a checksum for the supplied config map. The exact algorithm depends on hash and prefix parameters
    30  func ChecksumForConfigMap(cm *v1.ConfigMap) string {
    31  	hash := sha256.New()
    32  
    33  	// Since maps are not ordered we need to make sure we order them somehow so we'll always get the same checksums
    34  	// for the same config maps. The solution here is to extract the keys into a slice and sort them.
    35  	// Then iterate over that slice to fetch the values to be hashed.
    36  	keys := []string{}
    37  	for key := range cm.Data {
    38  		keys = append(keys, key)
    39  	}
    40  	sort.Strings(keys)
    41  
    42  	for _, key := range keys {
    43  		hash.Write([]byte(cm.Data[key]))
    44  	}
    45  
    46  	// Do the same as above, but for binaryData this time.
    47  	keys = []string{}
    48  	for key := range cm.BinaryData {
    49  		keys = append(keys, key)
    50  	}
    51  	sort.Strings(keys)
    52  
    53  	for _, key := range keys {
    54  		hash.Write(cm.BinaryData[key])
    55  	}
    56  
    57  	return fmt.Sprintf("sha256:%x", hash.Sum(nil))
    58  }
    59  
    60  // SignConfigMap calculates the supplied config map checksum and annotates it with it
    61  func SignConfigMap(cm *v1.ConfigMap) {
    62  	if cm.Annotations == nil {
    63  		cm.Annotations = map[string]string{}
    64  	}
    65  	cm.Annotations[constants.ComponentConfigHashAnnotationKey] = ChecksumForConfigMap(cm)
    66  }
    67  
    68  // VerifyConfigMapSignature returns true if the config map has checksum annotation and it matches; false otherwise
    69  func VerifyConfigMapSignature(cm *v1.ConfigMap) bool {
    70  	signature, ok := cm.Annotations[constants.ComponentConfigHashAnnotationKey]
    71  	if !ok {
    72  		return false
    73  	}
    74  	return signature == ChecksumForConfigMap(cm)
    75  }
    76  

View as plain text