2 Copyright 2014 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
18 Package auth defines a file format for holding authentication
19 information needed by clients of Kubernetes. Typically,
20 a Kubernetes cluster will put auth info for the admin in a known
21 location when it is created, and will (soon) put it in a known
22 location within a Container's file tree for Containers that
23 need access to the Kubernetes API.
25 Having a defined format allows:
26 - clients to be implemented in multiple languages
27 - applications which link clients to be portable across
28 clusters with different authentication styles (e.g.
29 some may use SSL Client certs, others may not, etc)
30 - when the format changes, applications only
31 need to update this code.
33 The file format is json, marshalled from a struct authcfg.Info.
35 Clinet libraries in other languages should use the same format.
37 It is not intended to store general preferences, such as default
38 namespace, output options, etc. CLIs (such as kubectl) and UIs should
39 develop their own format and may wish to inline the authcfg.Info type.
41 The authcfg.Info is just a file format. It is distinct from
42 client.Config which holds options for creating a client.Client.
43 Helper functions are provided in this package to fill in a
44 client.Client from an authcfg.Info.
53 info, err := auth.LoadFromFile(filename)
57 clientConfig = client.Config{}
58 clientConfig.Host = "example.com:4901"
59 clientConfig = info.MergeWithConfig()
60 client := client.New(clientConfig)
61 client.Pods(ns).List()
65 // TODO: need a way to rotate Tokens. Therefore, need a way for client object to be reset when the authcfg is updated.
71 restclient "k8s.io/client-go/rest"
74 // Info holds Kubernetes API authorization config. It is intended
75 // to be read/written from a file as a JSON object.
86 // LoadFromFile parses an Info object from a file path.
87 // If the file does not exist, then os.IsNotExist(err) == true
88 func LoadFromFile(path string) (*Info, error) {
90 if _, err := os.Stat(path); os.IsNotExist(err) {
93 data, err := ioutil.ReadFile(path)
97 err = json.Unmarshal(data, &info)
104 // MergeWithConfig returns a copy of a client.Config with values from the Info.
105 // The fields of client.Config with a corresponding field in the Info are set
106 // with the value from the Info.
107 func (info Info) MergeWithConfig(c restclient.Config) (restclient.Config, error) {
108 var config restclient.Config = c
109 config.Username = info.User
110 config.Password = info.Password
111 config.CAFile = info.CAFile
112 config.CertFile = info.CertFile
113 config.KeyFile = info.KeyFile
114 config.BearerToken = info.BearerToken
115 if info.Insecure != nil {
116 config.Insecure = *info.Insecure
121 func (info Info) Complete() bool {
122 return len(info.User) > 0 ||
123 len(info.CertFile) > 0 ||
124 len(info.BearerToken) > 0