helpers.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright 2017 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 util
  14. import (
  15. "bufio"
  16. "crypto/rand"
  17. "fmt"
  18. "regexp"
  19. "strings"
  20. "k8s.io/apimachinery/pkg/util/sets"
  21. "k8s.io/cluster-bootstrap/token/api"
  22. )
  23. // TODO(dixudx): refactor this to util/secrets and util/tokens
  24. // validBootstrapTokenChars defines the characters a bootstrap token can consist of
  25. const validBootstrapTokenChars = "0123456789abcdefghijklmnopqrstuvwxyz"
  26. var (
  27. // BootstrapTokenRegexp is a compiled regular expression of TokenRegexpString
  28. BootstrapTokenRegexp = regexp.MustCompile(api.BootstrapTokenPattern)
  29. // BootstrapTokenIDRegexp is a compiled regular expression of TokenIDRegexpString
  30. BootstrapTokenIDRegexp = regexp.MustCompile(api.BootstrapTokenIDPattern)
  31. // BootstrapGroupRegexp is a compiled regular expression of BootstrapGroupPattern
  32. BootstrapGroupRegexp = regexp.MustCompile(api.BootstrapGroupPattern)
  33. )
  34. // GenerateBootstrapToken generates a new, random Bootstrap Token.
  35. func GenerateBootstrapToken() (string, error) {
  36. tokenID, err := randBytes(api.BootstrapTokenIDBytes)
  37. if err != nil {
  38. return "", err
  39. }
  40. tokenSecret, err := randBytes(api.BootstrapTokenSecretBytes)
  41. if err != nil {
  42. return "", err
  43. }
  44. return TokenFromIDAndSecret(tokenID, tokenSecret), nil
  45. }
  46. // randBytes returns a random string consisting of the characters in
  47. // validBootstrapTokenChars, with the length customized by the parameter
  48. func randBytes(length int) (string, error) {
  49. // len("0123456789abcdefghijklmnopqrstuvwxyz") = 36 which doesn't evenly divide
  50. // the possible values of a byte: 256 mod 36 = 4. Discard any random bytes we
  51. // read that are >= 252 so the bytes we evenly divide the character set.
  52. const maxByteValue = 252
  53. var (
  54. b byte
  55. err error
  56. token = make([]byte, length)
  57. )
  58. reader := bufio.NewReaderSize(rand.Reader, length*2)
  59. for i := range token {
  60. for {
  61. if b, err = reader.ReadByte(); err != nil {
  62. return "", err
  63. }
  64. if b < maxByteValue {
  65. break
  66. }
  67. }
  68. token[i] = validBootstrapTokenChars[int(b)%len(validBootstrapTokenChars)]
  69. }
  70. return string(token), nil
  71. }
  72. // TokenFromIDAndSecret returns the full token which is of the form "{id}.{secret}"
  73. func TokenFromIDAndSecret(id, secret string) string {
  74. return fmt.Sprintf("%s.%s", id, secret)
  75. }
  76. // IsValidBootstrapToken returns whether the given string is valid as a Bootstrap Token and
  77. // in other words satisfies the BootstrapTokenRegexp
  78. func IsValidBootstrapToken(token string) bool {
  79. return BootstrapTokenRegexp.MatchString(token)
  80. }
  81. // IsValidBootstrapTokenID returns whether the given string is valid as a Bootstrap Token ID and
  82. // in other words satisfies the BootstrapTokenIDRegexp
  83. func IsValidBootstrapTokenID(tokenID string) bool {
  84. return BootstrapTokenIDRegexp.MatchString(tokenID)
  85. }
  86. // BootstrapTokenSecretName returns the expected name for the Secret storing the
  87. // Bootstrap Token in the Kubernetes API.
  88. func BootstrapTokenSecretName(tokenID string) string {
  89. return fmt.Sprintf("%s%s", api.BootstrapTokenSecretPrefix, tokenID)
  90. }
  91. // ValidateBootstrapGroupName checks if the provided group name is a valid
  92. // bootstrap group name. Returns nil if valid or a validation error if invalid.
  93. // TODO(dixudx): should be moved to util/secrets
  94. func ValidateBootstrapGroupName(name string) error {
  95. if BootstrapGroupRegexp.Match([]byte(name)) {
  96. return nil
  97. }
  98. return fmt.Errorf("bootstrap group %q is invalid (must match %s)", name, api.BootstrapGroupPattern)
  99. }
  100. // ValidateUsages validates that the passed in string are valid usage strings for bootstrap tokens.
  101. func ValidateUsages(usages []string) error {
  102. validUsages := sets.NewString(api.KnownTokenUsages...)
  103. invalidUsages := sets.NewString()
  104. for _, usage := range usages {
  105. if !validUsages.Has(usage) {
  106. invalidUsages.Insert(usage)
  107. }
  108. }
  109. if len(invalidUsages) > 0 {
  110. return fmt.Errorf("invalid bootstrap token usage string: %s, valid usage options: %s", strings.Join(invalidUsages.List(), ","), strings.Join(api.KnownTokenUsages, ","))
  111. }
  112. return nil
  113. }