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