util.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Copyright 2016 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 bootstrap
  14. import (
  15. "time"
  16. "k8s.io/klog"
  17. "k8s.io/api/core/v1"
  18. bootstrapapi "k8s.io/cluster-bootstrap/token/api"
  19. bootstrapsecretutil "k8s.io/cluster-bootstrap/util/secrets"
  20. )
  21. func validateSecretForSigning(secret *v1.Secret) (tokenID, tokenSecret string, ok bool) {
  22. nameTokenID, ok := bootstrapsecretutil.ParseName(secret.Name)
  23. if !ok {
  24. klog.V(3).Infof("Invalid secret name: %s. Must be of form %s<secret-id>.", secret.Name, bootstrapapi.BootstrapTokenSecretPrefix)
  25. return "", "", false
  26. }
  27. tokenID = bootstrapsecretutil.GetData(secret, bootstrapapi.BootstrapTokenIDKey)
  28. if len(tokenID) == 0 {
  29. klog.V(3).Infof("No %s key in %s/%s Secret", bootstrapapi.BootstrapTokenIDKey, secret.Namespace, secret.Name)
  30. return "", "", false
  31. }
  32. if nameTokenID != tokenID {
  33. klog.V(3).Infof("Token ID (%s) doesn't match secret name: %s", tokenID, nameTokenID)
  34. return "", "", false
  35. }
  36. tokenSecret = bootstrapsecretutil.GetData(secret, bootstrapapi.BootstrapTokenSecretKey)
  37. if len(tokenSecret) == 0 {
  38. klog.V(3).Infof("No %s key in %s/%s Secret", bootstrapapi.BootstrapTokenSecretKey, secret.Namespace, secret.Name)
  39. return "", "", false
  40. }
  41. // Ensure this secret hasn't expired. The TokenCleaner should remove this
  42. // but if that isn't working or it hasn't gotten there yet we should check
  43. // here.
  44. if bootstrapsecretutil.HasExpired(secret, time.Now()) {
  45. return "", "", false
  46. }
  47. // Make sure this secret can be used for signing
  48. okToSign := bootstrapsecretutil.GetData(secret, bootstrapapi.BootstrapTokenUsageSigningKey)
  49. if okToSign != "true" {
  50. return "", "", false
  51. }
  52. return tokenID, tokenSecret, true
  53. }