util.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "regexp"
  16. "time"
  17. "k8s.io/klog"
  18. "k8s.io/api/core/v1"
  19. bootstrapapi "k8s.io/cluster-bootstrap/token/api"
  20. )
  21. var namePattern = `^` + regexp.QuoteMeta(bootstrapapi.BootstrapTokenSecretPrefix) + `([a-z0-9]{6})$`
  22. var nameRegExp = regexp.MustCompile(namePattern)
  23. // getSecretString gets a string value from a secret. If there is an error or
  24. // if the key doesn't exist, an empty string is returned.
  25. func getSecretString(secret *v1.Secret, key string) string {
  26. data, ok := secret.Data[key]
  27. if !ok {
  28. return ""
  29. }
  30. return string(data)
  31. }
  32. // parseSecretName parses the name of the secret to extract the secret ID.
  33. func parseSecretName(name string) (secretID string, ok bool) {
  34. r := nameRegExp.FindStringSubmatch(name)
  35. if r == nil {
  36. return "", false
  37. }
  38. return r[1], true
  39. }
  40. func validateSecretForSigning(secret *v1.Secret) (tokenID, tokenSecret string, ok bool) {
  41. nameTokenID, ok := parseSecretName(secret.Name)
  42. if !ok {
  43. klog.V(3).Infof("Invalid secret name: %s. Must be of form %s<secret-id>.", secret.Name, bootstrapapi.BootstrapTokenSecretPrefix)
  44. return "", "", false
  45. }
  46. tokenID = getSecretString(secret, bootstrapapi.BootstrapTokenIDKey)
  47. if len(tokenID) == 0 {
  48. klog.V(3).Infof("No %s key in %s/%s Secret", bootstrapapi.BootstrapTokenIDKey, secret.Namespace, secret.Name)
  49. return "", "", false
  50. }
  51. if nameTokenID != tokenID {
  52. klog.V(3).Infof("Token ID (%s) doesn't match secret name: %s", tokenID, nameTokenID)
  53. return "", "", false
  54. }
  55. tokenSecret = getSecretString(secret, bootstrapapi.BootstrapTokenSecretKey)
  56. if len(tokenSecret) == 0 {
  57. klog.V(3).Infof("No %s key in %s/%s Secret", bootstrapapi.BootstrapTokenSecretKey, secret.Namespace, secret.Name)
  58. return "", "", false
  59. }
  60. // Ensure this secret hasn't expired. The TokenCleaner should remove this
  61. // but if that isn't working or it hasn't gotten there yet we should check
  62. // here.
  63. if isSecretExpired(secret) {
  64. return "", "", false
  65. }
  66. // Make sure this secret can be used for signing
  67. okToSign := getSecretString(secret, bootstrapapi.BootstrapTokenUsageSigningKey)
  68. if okToSign != "true" {
  69. return "", "", false
  70. }
  71. return tokenID, tokenSecret, true
  72. }
  73. // isSecretExpired returns true if the Secret is expired.
  74. func isSecretExpired(secret *v1.Secret) bool {
  75. expiration := getSecretString(secret, bootstrapapi.BootstrapTokenExpirationKey)
  76. if len(expiration) > 0 {
  77. expTime, err2 := time.Parse(time.RFC3339, expiration)
  78. if err2 != nil {
  79. klog.V(3).Infof("Unparseable expiration time (%s) in %s/%s Secret: %v. Treating as expired.",
  80. expiration, secret.Namespace, secret.Name, err2)
  81. return true
  82. }
  83. if time.Now().After(expTime) {
  84. klog.V(3).Infof("Expired bootstrap token in %s/%s Secret: %v",
  85. secret.Namespace, secret.Name, expiration)
  86. return true
  87. }
  88. }
  89. return false
  90. }