sigs.k8s.io/cluster-api@v1.7.1/bootstrap/kubeadm/types/upstreamv1beta1/bootstraptokenstring.go (about)

     1  /*
     2  Copyright 2018 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 upstreamv1beta1
    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  //
    33  // +kubebuilder:validation:Type=string
    34  type BootstrapTokenString struct {
    35  	ID     string `json:"-"`
    36  	Secret string `json:"-"`
    37  }
    38  
    39  // MarshalJSON implements the json.Marshaler interface.
    40  func (bts BootstrapTokenString) MarshalJSON() ([]byte, error) {
    41  	return []byte(fmt.Sprintf("%q", bts.String())), nil
    42  }
    43  
    44  // UnmarshalJSON implements the json.Unmarshaller interface.
    45  func (bts *BootstrapTokenString) UnmarshalJSON(b []byte) error {
    46  	// If the token is represented as "", just return quickly without an error
    47  	if len(b) == 0 {
    48  		return nil
    49  	}
    50  
    51  	// Remove unnecessary " characters coming from the JSON parser
    52  	token := strings.ReplaceAll(string(b), `"`, ``)
    53  	// Convert the string Token to a BootstrapTokenString object
    54  	newbts, err := NewBootstrapTokenString(token)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	bts.ID = newbts.ID
    59  	bts.Secret = newbts.Secret
    60  	return nil
    61  }
    62  
    63  // String returns the string representation of the BootstrapTokenString.
    64  func (bts BootstrapTokenString) String() string {
    65  	if bts.ID != "" && bts.Secret != "" {
    66  		return bootstraputil.TokenFromIDAndSecret(bts.ID, bts.Secret)
    67  	}
    68  	return ""
    69  }
    70  
    71  // NewBootstrapTokenString converts the given Bootstrap Token as a string
    72  // to the BootstrapTokenString object used for serialization/deserialization
    73  // and internal usage. It also automatically validates that the given token
    74  // is of the right format.
    75  func NewBootstrapTokenString(token string) (*BootstrapTokenString, error) {
    76  	substrs := bootstraputil.BootstrapTokenRegexp.FindStringSubmatch(token)
    77  	// TODO: Add a constant for the 3 value here, and explain better why it's needed (other than because how the regexp parsin works)
    78  	if len(substrs) != 3 {
    79  		return nil, errors.Errorf("the bootstrap token %q was not of the form %q", token, bootstrapapi.BootstrapTokenPattern)
    80  	}
    81  
    82  	return &BootstrapTokenString{ID: substrs[1], Secret: substrs[2]}, nil
    83  }
    84  
    85  // NewBootstrapTokenStringFromIDAndSecret is a wrapper around NewBootstrapTokenString
    86  // that allows the caller to specify the ID and Secret separately.
    87  func NewBootstrapTokenStringFromIDAndSecret(id, secret string) (*BootstrapTokenString, error) {
    88  	return NewBootstrapTokenString(bootstraputil.TokenFromIDAndSecret(id, secret))
    89  }