auth_loaders.go 3.1 KB

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