...

Source file src/k8s.io/client-go/tools/clientcmd/auth_loaders.go

Documentation: k8s.io/client-go/tools/clientcmd

     1  /*
     2  Copyright 2014 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 clientcmd
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"io"
    23  	"os"
    24  
    25  	"golang.org/x/term"
    26  
    27  	clientauth "k8s.io/client-go/tools/auth"
    28  )
    29  
    30  // AuthLoaders are used to build clientauth.Info objects.
    31  type AuthLoader interface {
    32  	// LoadAuth takes a path to a config file and can then do anything it needs in order to return a valid clientauth.Info
    33  	LoadAuth(path string) (*clientauth.Info, error)
    34  }
    35  
    36  // default implementation of an AuthLoader
    37  type defaultAuthLoader struct{}
    38  
    39  // LoadAuth for defaultAuthLoader simply delegates to clientauth.LoadFromFile
    40  func (*defaultAuthLoader) LoadAuth(path string) (*clientauth.Info, error) {
    41  	return clientauth.LoadFromFile(path)
    42  }
    43  
    44  type PromptingAuthLoader struct {
    45  	reader io.Reader
    46  }
    47  
    48  // LoadAuth parses an AuthInfo object from a file path. It prompts user and creates file if it doesn't exist.
    49  func (a *PromptingAuthLoader) LoadAuth(path string) (*clientauth.Info, error) {
    50  	// Prompt for user/pass and write a file if none exists.
    51  	if _, err := os.Stat(path); os.IsNotExist(err) {
    52  		authPtr, err := a.Prompt()
    53  		if err != nil {
    54  			return nil, err
    55  		}
    56  		auth := *authPtr
    57  		data, err := json.Marshal(auth)
    58  		if err != nil {
    59  			return &auth, err
    60  		}
    61  		err = os.WriteFile(path, data, 0600)
    62  		return &auth, err
    63  	}
    64  	authPtr, err := clientauth.LoadFromFile(path)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	return authPtr, nil
    69  }
    70  
    71  // Prompt pulls the user and password from a reader
    72  func (a *PromptingAuthLoader) Prompt() (*clientauth.Info, error) {
    73  	var err error
    74  	auth := &clientauth.Info{}
    75  	auth.User, err = promptForString("Username", a.reader, true)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	auth.Password, err = promptForString("Password", nil, false)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	return auth, nil
    84  }
    85  
    86  func promptForString(field string, r io.Reader, show bool) (result string, err error) {
    87  	fmt.Printf("Please enter %s: ", field)
    88  	if show {
    89  		_, err = fmt.Fscan(r, &result)
    90  	} else {
    91  		var data []byte
    92  		if term.IsTerminal(int(os.Stdin.Fd())) {
    93  			data, err = term.ReadPassword(int(os.Stdin.Fd()))
    94  			result = string(data)
    95  		} else {
    96  			return "", fmt.Errorf("error reading input for %s", field)
    97  		}
    98  	}
    99  	return result, err
   100  }
   101  
   102  // NewPromptingAuthLoader is an AuthLoader that parses an AuthInfo object from a file path. It prompts user and creates file if it doesn't exist.
   103  func NewPromptingAuthLoader(reader io.Reader) *PromptingAuthLoader {
   104  	return &PromptingAuthLoader{reader}
   105  }
   106  
   107  // NewDefaultAuthLoader returns a default implementation of an AuthLoader that only reads from a config file
   108  func NewDefaultAuthLoader() AuthLoader {
   109  	return &defaultAuthLoader{}
   110  }
   111  

View as plain text