clientauth.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /*
  14. Package auth defines a file format for holding authentication
  15. information needed by clients of Kubernetes. Typically,
  16. a Kubernetes cluster will put auth info for the admin in a known
  17. location when it is created, and will (soon) put it in a known
  18. location within a Container's file tree for Containers that
  19. need access to the Kubernetes API.
  20. Having a defined format allows:
  21. - clients to be implmented in multiple languages
  22. - applications which link clients to be portable across
  23. clusters with different authentication styles (e.g.
  24. some may use SSL Client certs, others may not, etc)
  25. - when the format changes, applications only
  26. need to update this code.
  27. The file format is json, marshalled from a struct authcfg.Info.
  28. Clinet libraries in other languages should use the same format.
  29. It is not intended to store general preferences, such as default
  30. namespace, output options, etc. CLIs (such as kubectl) and UIs should
  31. develop their own format and may wish to inline the authcfg.Info type.
  32. The authcfg.Info is just a file format. It is distinct from
  33. client.Config which holds options for creating a client.Client.
  34. Helper functions are provided in this package to fill in a
  35. client.Client from an authcfg.Info.
  36. Example:
  37. import (
  38. "pkg/client"
  39. "pkg/client/auth"
  40. )
  41. info, err := auth.LoadFromFile(filename)
  42. if err != nil {
  43. // handle error
  44. }
  45. clientConfig = client.Config{}
  46. clientConfig.Host = "example.com:4901"
  47. clientConfig = info.MergeWithConfig()
  48. client := client.New(clientConfig)
  49. client.Pods(ns).List()
  50. */
  51. package auth
  52. // TODO: need a way to rotate Tokens. Therefore, need a way for client object to be reset when the authcfg is updated.
  53. import (
  54. "encoding/json"
  55. "io/ioutil"
  56. "os"
  57. restclient "k8s.io/client-go/rest"
  58. )
  59. // Info holds Kubernetes API authorization config. It is intended
  60. // to be read/written from a file as a JSON object.
  61. type Info struct {
  62. User string
  63. Password string
  64. CAFile string
  65. CertFile string
  66. KeyFile string
  67. BearerToken string
  68. Insecure *bool
  69. }
  70. // LoadFromFile parses an Info object from a file path.
  71. // If the file does not exist, then os.IsNotExist(err) == true
  72. func LoadFromFile(path string) (*Info, error) {
  73. var info Info
  74. if _, err := os.Stat(path); os.IsNotExist(err) {
  75. return nil, err
  76. }
  77. data, err := ioutil.ReadFile(path)
  78. if err != nil {
  79. return nil, err
  80. }
  81. err = json.Unmarshal(data, &info)
  82. if err != nil {
  83. return nil, err
  84. }
  85. return &info, err
  86. }
  87. // MergeWithConfig returns a copy of a client.Config with values from the Info.
  88. // The fields of client.Config with a corresponding field in the Info are set
  89. // with the value from the Info.
  90. func (info Info) MergeWithConfig(c restclient.Config) (restclient.Config, error) {
  91. var config restclient.Config = c
  92. config.Username = info.User
  93. config.Password = info.Password
  94. config.CAFile = info.CAFile
  95. config.CertFile = info.CertFile
  96. config.KeyFile = info.KeyFile
  97. config.BearerToken = info.BearerToken
  98. if info.Insecure != nil {
  99. config.Insecure = *info.Insecure
  100. }
  101. return config, nil
  102. }
  103. func (info Info) Complete() bool {
  104. return len(info.User) > 0 ||
  105. len(info.CertFile) > 0 ||
  106. len(info.BearerToken) > 0
  107. }