sigs.k8s.io/cluster-api@v1.7.1/bootstrap/kubeadm/types/upstreamv1beta3/bootstraptokenstring.go (about) 1 /* 2 Copyright 2021 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 upstreamv1beta3 18 19 import ( 20 "fmt" 21 "strings" 22 23 "github.com/pkg/errors" 24 bootstrapapi "k8s.io/cluster-bootstrap/token/api" 25 bootstraputil "k8s.io/cluster-bootstrap/token/util" 26 ) 27 28 // BootstrapTokenString is a token of the format abcdef.abcdef0123456789 that is used 29 // for both validation of the practically of the API server from a joining node's point 30 // of view and as an authentication method for the node in the bootstrap phase of 31 // "kubeadm join". This token is and should be short-lived. 32 type BootstrapTokenString struct { 33 ID string `json:"-" datapolicy:"token"` 34 Secret string `json:"-" datapolicy:"token"` 35 } 36 37 // MarshalJSON implements the json.Marshaler interface. 38 func (bts BootstrapTokenString) MarshalJSON() ([]byte, error) { 39 return []byte(fmt.Sprintf("%q", bts.String())), nil 40 } 41 42 // UnmarshalJSON implements the json.Unmarshaller interface. 43 func (bts *BootstrapTokenString) UnmarshalJSON(b []byte) error { 44 // If the token is represented as "", just return quickly without an error 45 if len(b) == 0 { 46 return nil 47 } 48 49 // Remove unnecessary " characters coming from the JSON parser 50 token := strings.ReplaceAll(string(b), `"`, ``) 51 // Convert the string Token to a BootstrapTokenString object 52 newbts, err := NewBootstrapTokenString(token) 53 if err != nil { 54 return err 55 } 56 bts.ID = newbts.ID 57 bts.Secret = newbts.Secret 58 return nil 59 } 60 61 // String returns the string representation of the BootstrapTokenString. 62 func (bts BootstrapTokenString) String() string { 63 if bts.ID != "" && bts.Secret != "" { 64 return bootstraputil.TokenFromIDAndSecret(bts.ID, bts.Secret) 65 } 66 return "" 67 } 68 69 // NewBootstrapTokenString converts the given Bootstrap Token as a string 70 // to the BootstrapTokenString object used for serialization/deserialization 71 // and internal usage. It also automatically validates that the given token 72 // is of the right format. 73 func NewBootstrapTokenString(token string) (*BootstrapTokenString, error) { 74 substrs := bootstraputil.BootstrapTokenRegexp.FindStringSubmatch(token) 75 // TODO: Add a constant for the 3 value here, and explain better why it's needed (other than because how the regexp parsin works) 76 if len(substrs) != 3 { 77 return nil, errors.Errorf("the bootstrap token %q was not of the form %q", token, bootstrapapi.BootstrapTokenPattern) 78 } 79 80 return &BootstrapTokenString{ID: substrs[1], Secret: substrs[2]}, nil 81 } 82 83 // NewBootstrapTokenStringFromIDAndSecret is a wrapper around NewBootstrapTokenString 84 // that allows the caller to specify the ID and Secret separately. 85 func NewBootstrapTokenStringFromIDAndSecret(id, secret string) (*BootstrapTokenString, error) { 86 return NewBootstrapTokenString(bootstraputil.TokenFromIDAndSecret(id, secret)) 87 }