blob: 990a440c66d0838150564bcb5c526e61e520c630 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package api
18
19import (
20 "fmt"
21
22 "k8s.io/apimachinery/pkg/runtime"
23)
24
25// Where possible, json tags match the cli argument names.
26// Top level config objects and all values required for proper functioning are not "omitempty". Any truly optional piece of config is allowed to be omitted.
27
28// Config holds the information needed to build connect to remote kubernetes clusters as a given user
29// IMPORTANT if you add fields to this struct, please update IsConfigEmpty()
30// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
31type Config struct {
32 // Legacy field from pkg/api/types.go TypeMeta.
33 // TODO(jlowdermilk): remove this after eliminating downstream dependencies.
34 // +optional
35 Kind string `json:"kind,omitempty"`
36 // Legacy field from pkg/api/types.go TypeMeta.
37 // TODO(jlowdermilk): remove this after eliminating downstream dependencies.
38 // +optional
39 APIVersion string `json:"apiVersion,omitempty"`
40 // Preferences holds general information to be use for cli interactions
41 Preferences Preferences `json:"preferences"`
42 // Clusters is a map of referencable names to cluster configs
43 Clusters map[string]*Cluster `json:"clusters"`
44 // AuthInfos is a map of referencable names to user configs
45 AuthInfos map[string]*AuthInfo `json:"users"`
46 // Contexts is a map of referencable names to context configs
47 Contexts map[string]*Context `json:"contexts"`
48 // CurrentContext is the name of the context that you would like to use by default
49 CurrentContext string `json:"current-context"`
50 // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
51 // +optional
52 Extensions map[string]runtime.Object `json:"extensions,omitempty"`
53}
54
55// IMPORTANT if you add fields to this struct, please update IsConfigEmpty()
56type Preferences struct {
57 // +optional
58 Colors bool `json:"colors,omitempty"`
59 // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
60 // +optional
61 Extensions map[string]runtime.Object `json:"extensions,omitempty"`
62}
63
64// Cluster contains information about how to communicate with a kubernetes cluster
65type Cluster struct {
66 // LocationOfOrigin indicates where this object came from. It is used for round tripping config post-merge, but never serialized.
67 LocationOfOrigin string
68 // Server is the address of the kubernetes cluster (https://hostname:port).
69 Server string `json:"server"`
70 // InsecureSkipTLSVerify skips the validity check for the server's certificate. This will make your HTTPS connections insecure.
71 // +optional
72 InsecureSkipTLSVerify bool `json:"insecure-skip-tls-verify,omitempty"`
73 // CertificateAuthority is the path to a cert file for the certificate authority.
74 // +optional
75 CertificateAuthority string `json:"certificate-authority,omitempty"`
76 // CertificateAuthorityData contains PEM-encoded certificate authority certificates. Overrides CertificateAuthority
77 // +optional
78 CertificateAuthorityData []byte `json:"certificate-authority-data,omitempty"`
79 // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
80 // +optional
81 Extensions map[string]runtime.Object `json:"extensions,omitempty"`
82}
83
84// AuthInfo contains information that describes identity information. This is use to tell the kubernetes cluster who you are.
85type AuthInfo struct {
86 // LocationOfOrigin indicates where this object came from. It is used for round tripping config post-merge, but never serialized.
87 LocationOfOrigin string
88 // ClientCertificate is the path to a client cert file for TLS.
89 // +optional
90 ClientCertificate string `json:"client-certificate,omitempty"`
91 // ClientCertificateData contains PEM-encoded data from a client cert file for TLS. Overrides ClientCertificate
92 // +optional
93 ClientCertificateData []byte `json:"client-certificate-data,omitempty"`
94 // ClientKey is the path to a client key file for TLS.
95 // +optional
96 ClientKey string `json:"client-key,omitempty"`
97 // ClientKeyData contains PEM-encoded data from a client key file for TLS. Overrides ClientKey
98 // +optional
99 ClientKeyData []byte `json:"client-key-data,omitempty"`
100 // Token is the bearer token for authentication to the kubernetes cluster.
101 // +optional
102 Token string `json:"token,omitempty"`
103 // TokenFile is a pointer to a file that contains a bearer token (as described above). If both Token and TokenFile are present, Token takes precedence.
104 // +optional
105 TokenFile string `json:"tokenFile,omitempty"`
106 // Impersonate is the username to act-as.
107 // +optional
108 Impersonate string `json:"act-as,omitempty"`
109 // ImpersonateGroups is the groups to imperonate.
110 // +optional
111 ImpersonateGroups []string `json:"act-as-groups,omitempty"`
112 // ImpersonateUserExtra contains additional information for impersonated user.
113 // +optional
114 ImpersonateUserExtra map[string][]string `json:"act-as-user-extra,omitempty"`
115 // Username is the username for basic authentication to the kubernetes cluster.
116 // +optional
117 Username string `json:"username,omitempty"`
118 // Password is the password for basic authentication to the kubernetes cluster.
119 // +optional
120 Password string `json:"password,omitempty"`
121 // AuthProvider specifies a custom authentication plugin for the kubernetes cluster.
122 // +optional
123 AuthProvider *AuthProviderConfig `json:"auth-provider,omitempty"`
124 // Exec specifies a custom exec-based authentication plugin for the kubernetes cluster.
125 // +optional
126 Exec *ExecConfig `json:"exec,omitempty"`
127 // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
128 // +optional
129 Extensions map[string]runtime.Object `json:"extensions,omitempty"`
130}
131
132// Context is a tuple of references to a cluster (how do I communicate with a kubernetes cluster), a user (how do I identify myself), and a namespace (what subset of resources do I want to work with)
133type Context struct {
134 // LocationOfOrigin indicates where this object came from. It is used for round tripping config post-merge, but never serialized.
135 LocationOfOrigin string
136 // Cluster is the name of the cluster for this context
137 Cluster string `json:"cluster"`
138 // AuthInfo is the name of the authInfo for this context
139 AuthInfo string `json:"user"`
140 // Namespace is the default namespace to use on unspecified requests
141 // +optional
142 Namespace string `json:"namespace,omitempty"`
143 // Extensions holds additional information. This is useful for extenders so that reads and writes don't clobber unknown fields
144 // +optional
145 Extensions map[string]runtime.Object `json:"extensions,omitempty"`
146}
147
148// AuthProviderConfig holds the configuration for a specified auth provider.
149type AuthProviderConfig struct {
150 Name string `json:"name"`
151 // +optional
152 Config map[string]string `json:"config,omitempty"`
153}
154
155var _ fmt.Stringer = new(AuthProviderConfig)
156var _ fmt.GoStringer = new(AuthProviderConfig)
157
158// GoString implements fmt.GoStringer and sanitizes sensitive fields of
159// AuthProviderConfig to prevent accidental leaking via logs.
160func (c AuthProviderConfig) GoString() string {
161 return c.String()
162}
163
164// String implements fmt.Stringer and sanitizes sensitive fields of
165// AuthProviderConfig to prevent accidental leaking via logs.
166func (c AuthProviderConfig) String() string {
167 cfg := "<nil>"
168 if c.Config != nil {
169 cfg = "--- REDACTED ---"
170 }
171 return fmt.Sprintf("api.AuthProviderConfig{Name: %q, Config: map[string]string{%s}}", c.Name, cfg)
172}
173
174// ExecConfig specifies a command to provide client credentials. The command is exec'd
175// and outputs structured stdout holding credentials.
176//
177// See the client.authentiction.k8s.io API group for specifications of the exact input
178// and output format
179type ExecConfig struct {
180 // Command to execute.
181 Command string `json:"command"`
182 // Arguments to pass to the command when executing it.
183 // +optional
184 Args []string `json:"args"`
185 // Env defines additional environment variables to expose to the process. These
186 // are unioned with the host's environment, as well as variables client-go uses
187 // to pass argument to the plugin.
188 // +optional
189 Env []ExecEnvVar `json:"env"`
190
191 // Preferred input version of the ExecInfo. The returned ExecCredentials MUST use
192 // the same encoding version as the input.
193 APIVersion string `json:"apiVersion,omitempty"`
194}
195
196var _ fmt.Stringer = new(ExecConfig)
197var _ fmt.GoStringer = new(ExecConfig)
198
199// GoString implements fmt.GoStringer and sanitizes sensitive fields of
200// ExecConfig to prevent accidental leaking via logs.
201func (c ExecConfig) GoString() string {
202 return c.String()
203}
204
205// String implements fmt.Stringer and sanitizes sensitive fields of ExecConfig
206// to prevent accidental leaking via logs.
207func (c ExecConfig) String() string {
208 var args []string
209 if len(c.Args) > 0 {
210 args = []string{"--- REDACTED ---"}
211 }
212 env := "[]ExecEnvVar(nil)"
213 if len(c.Env) > 0 {
214 env = "[]ExecEnvVar{--- REDACTED ---}"
215 }
216 return fmt.Sprintf("api.AuthProviderConfig{Command: %q, Args: %#v, Env: %s, APIVersion: %q}", c.Command, args, env, c.APIVersion)
217}
218
219// ExecEnvVar is used for setting environment variables when executing an exec-based
220// credential plugin.
221type ExecEnvVar struct {
222 Name string `json:"name"`
223 Value string `json:"value"`
224}
225
226// NewConfig is a convenience function that returns a new Config object with non-nil maps
227func NewConfig() *Config {
228 return &Config{
229 Preferences: *NewPreferences(),
230 Clusters: make(map[string]*Cluster),
231 AuthInfos: make(map[string]*AuthInfo),
232 Contexts: make(map[string]*Context),
233 Extensions: make(map[string]runtime.Object),
234 }
235}
236
237// NewContext is a convenience function that returns a new Context
238// object with non-nil maps
239func NewContext() *Context {
240 return &Context{Extensions: make(map[string]runtime.Object)}
241}
242
243// NewCluster is a convenience function that returns a new Cluster
244// object with non-nil maps
245func NewCluster() *Cluster {
246 return &Cluster{Extensions: make(map[string]runtime.Object)}
247}
248
249// NewAuthInfo is a convenience function that returns a new AuthInfo
250// object with non-nil maps
251func NewAuthInfo() *AuthInfo {
252 return &AuthInfo{
253 Extensions: make(map[string]runtime.Object),
254 ImpersonateUserExtra: make(map[string][]string),
255 }
256}
257
258// NewPreferences is a convenience function that returns a new
259// Preferences object with non-nil maps
260func NewPreferences() *Preferences {
261 return &Preferences{Extensions: make(map[string]runtime.Object)}
262}