k8s.io/kubernetes@v1.29.3/pkg/controller/bootstrap/util.go (about) 1 /* 2 Copyright 2016 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 bootstrap 18 19 import ( 20 "context" 21 "time" 22 23 "k8s.io/klog/v2" 24 25 "k8s.io/api/core/v1" 26 bootstrapapi "k8s.io/cluster-bootstrap/token/api" 27 bootstrapsecretutil "k8s.io/cluster-bootstrap/util/secrets" 28 ) 29 30 func validateSecretForSigning(ctx context.Context, secret *v1.Secret) (tokenID, tokenSecret string, ok bool) { 31 logger := klog.FromContext(ctx) 32 nameTokenID, ok := bootstrapsecretutil.ParseName(secret.Name) 33 if !ok { 34 logger.V(3).Info("Invalid secret name, must be of the form "+bootstrapapi.BootstrapTokenSecretPrefix+"<secret-id>", "secretName", secret.Name) 35 return "", "", false 36 } 37 38 tokenID = bootstrapsecretutil.GetData(secret, bootstrapapi.BootstrapTokenIDKey) 39 if len(tokenID) == 0 { 40 logger.V(3).Info("No key in Secret", "key", bootstrapapi.BootstrapTokenIDKey, "secret", klog.KObj(secret)) 41 return "", "", false 42 } 43 44 if nameTokenID != tokenID { 45 logger.V(3).Info("Token ID doesn't match secret name", "tokenID", tokenID, "secretName", secret.Name) 46 return "", "", false 47 } 48 49 tokenSecret = bootstrapsecretutil.GetData(secret, bootstrapapi.BootstrapTokenSecretKey) 50 if len(tokenSecret) == 0 { 51 logger.V(3).Info("No key in secret", "key", bootstrapapi.BootstrapTokenSecretKey, "secret", klog.KObj(secret)) 52 return "", "", false 53 } 54 55 // Ensure this secret hasn't expired. The TokenCleaner should remove this 56 // but if that isn't working or it hasn't gotten there yet we should check 57 // here. 58 if bootstrapsecretutil.HasExpired(secret, time.Now()) { 59 return "", "", false 60 } 61 62 // Make sure this secret can be used for signing 63 okToSign := bootstrapsecretutil.GetData(secret, bootstrapapi.BootstrapTokenUsageSigningKey) 64 if okToSign != "true" { 65 return "", "", false 66 } 67 68 return tokenID, tokenSecret, true 69 }