auth_loaders.go 3.1 KB

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