auth_loaders.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. clientauth "k8s.io/kubernetes/pkg/client/unversioned/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. var auth clientauth.Info
  39. // Prompt for user/pass and write a file if none exists.
  40. if _, err := os.Stat(path); os.IsNotExist(err) {
  41. auth = *a.Prompt()
  42. data, err := json.Marshal(auth)
  43. if err != nil {
  44. return &auth, err
  45. }
  46. err = ioutil.WriteFile(path, data, 0600)
  47. return &auth, err
  48. }
  49. authPtr, err := clientauth.LoadFromFile(path)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return authPtr, nil
  54. }
  55. // Prompt pulls the user and password from a reader
  56. func (a *PromptingAuthLoader) Prompt() *clientauth.Info {
  57. auth := &clientauth.Info{}
  58. auth.User = promptForString("Username", a.reader)
  59. auth.Password = promptForString("Password", a.reader)
  60. return auth
  61. }
  62. func promptForString(field string, r io.Reader) string {
  63. fmt.Printf("Please enter %s: ", field)
  64. var result string
  65. fmt.Fscan(r, &result)
  66. return result
  67. }
  68. // NewPromptingAuthLoader is an AuthLoader that parses an AuthInfo object from a file path. It prompts user and creates file if it doesn't exist.
  69. func NewPromptingAuthLoader(reader io.Reader) *PromptingAuthLoader {
  70. return &PromptingAuthLoader{reader}
  71. }
  72. // NewDefaultAuthLoader returns a default implementation of an AuthLoader that only reads from a config file
  73. func NewDefaultAuthLoader() AuthLoader {
  74. return &defaultAuthLoader{}
  75. }