clientauth.go 3.8 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 implemented 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. Client 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. "os"
  56. restclient "k8s.io/client-go/rest"
  57. )
  58. // Info holds Kubernetes API authorization config. It is intended
  59. // to be read/written from a file as a JSON object.
  60. type Info struct {
  61. User string
  62. Password string `datapolicy:"password"`
  63. CAFile string
  64. CertFile string
  65. KeyFile string
  66. BearerToken string `datapolicy:"token"`
  67. Insecure *bool
  68. }
  69. // LoadFromFile parses an Info object from a file path.
  70. // If the file does not exist, then os.IsNotExist(err) == true
  71. func LoadFromFile(path string) (*Info, error) {
  72. var info Info
  73. if _, err := os.Stat(path); os.IsNotExist(err) {
  74. return nil, err
  75. }
  76. data, err := os.ReadFile(path)
  77. if err != nil {
  78. return nil, err
  79. }
  80. err = json.Unmarshal(data, &info)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return &info, err
  85. }
  86. // MergeWithConfig returns a copy of a client.Config with values from the Info.
  87. // The fields of client.Config with a corresponding field in the Info are set
  88. // with the value from the Info.
  89. func (info Info) MergeWithConfig(c restclient.Config) (restclient.Config, error) {
  90. var config = c
  91. config.Username = info.User
  92. config.Password = info.Password
  93. config.CAFile = info.CAFile
  94. config.CertFile = info.CertFile
  95. config.KeyFile = info.KeyFile
  96. config.BearerToken = info.BearerToken
  97. if info.Insecure != nil {
  98. config.Insecure = *info.Insecure
  99. }
  100. return config, nil
  101. }
  102. // Complete returns true if the Kubernetes API authorization info is complete.
  103. func (info Info) Complete() bool {
  104. return len(info.User) > 0 ||
  105. len(info.CertFile) > 0 ||
  106. len(info.BearerToken) > 0
  107. }