forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loginoptions.go
361 lines (298 loc) · 10.5 KB
/
loginoptions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package cmd
import (
"bytes"
"fmt"
"os"
"path/filepath"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
kclientcmd "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
kubecmdconfig "github.com/GoogleCloudPlatform/kubernetes/pkg/kubectl/cmd/config"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/cli/config"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
"github.com/openshift/origin/pkg/cmd/util/tokencmd"
"github.com/openshift/origin/pkg/user/api"
)
const defaultClusterURL = "https://localhost:8443"
// Gather all required information in a comprehensive order.
func (o *LoginOptions) GatherInfo() error {
if err := o.gatherAuthInfo(); err != nil {
return err
}
if err := o.gatherProjectInfo(); err != nil {
return err
}
return nil
}
// getClientConfig returns back the current clientConfig as we know it. If there is no clientConfig, it builds one with enough information
// to talk to a server. This may involve user prompts. This method is not threadsafe.
func (o *LoginOptions) getClientConfig() (*kclient.Config, error) {
if o.Config != nil {
return o.Config, nil
}
clientConfig := &kclient.Config{}
// if someone specified a server, use it as the default
if len(o.Server) > 0 {
clientConfig.Host = o.Server
} else {
// we need to have a server to talk to
if cmdutil.IsTerminal(o.Reader) {
for !o.serverProvided() {
defaultServer := defaultClusterURL
promptMsg := fmt.Sprintf("OpenShift server [%s]: ", defaultServer)
o.Server = cmdutil.PromptForStringWithDefault(o.Reader, defaultServer, promptMsg)
clientConfig.Host = o.Server
}
}
}
if len(o.CAFile) > 0 {
clientConfig.CAFile = o.CAFile
} else {
// check all cluster stanzas to see if we already have one with this URL that contains a client cert
for _, cluster := range o.StartingKubeConfig.Clusters {
if cluster.Server == clientConfig.Host {
if len(cluster.CertificateAuthority) > 0 {
clientConfig.CAFile = cluster.CertificateAuthority
break
}
if len(cluster.CertificateAuthorityData) > 0 {
clientConfig.CAData = cluster.CertificateAuthorityData
break
}
}
}
}
// ping to check if server is reachable
osClient, err := client.New(clientConfig)
if err != nil {
return nil, err
}
result := osClient.Get().AbsPath("/osapi").Do()
if result.Error() != nil {
switch {
case o.InsecureTLS:
clientConfig.Insecure = true
// certificate issue, prompt user for insecure connection
case clientcmd.IsCertificateAuthorityUnknown(result.Error()):
// check to see if we already have a cluster stanza that tells us to use --insecure for this particular server. If we don't, then prompt
clientConfigToTest := *clientConfig
clientConfigToTest.Insecure = true
matchingClusters := getMatchingClusters(clientConfigToTest, *o.StartingKubeConfig)
if len(matchingClusters) > 0 {
clientConfig.Insecure = true
} else if cmdutil.IsTerminal(o.Reader) {
fmt.Fprintln(o.Out, "The server uses a certificate signed by an unknown authority.")
fmt.Fprintln(o.Out, "You can bypass the certificate check, but any data you send to the server could be intercepted by others.")
clientConfig.Insecure = cmdutil.PromptForBool(os.Stdin, "Use insecure connections? (y/n): ")
if !clientConfig.Insecure {
return nil, fmt.Errorf(clientcmd.GetPrettyMessageFor(result.Error()))
}
fmt.Fprintln(o.Out)
}
default:
return nil, result.Error()
}
}
o.Config = clientConfig
return o.Config, nil
}
// getMatchingClusters examines the kubeconfig for all clusters that point to the same server
func getMatchingClusters(clientConfig kclient.Config, kubeconfig clientcmdapi.Config) util.StringSet {
ret := util.StringSet{}
for key, cluster := range kubeconfig.Clusters {
if (cluster.Server == clientConfig.Host) && (cluster.InsecureSkipTLSVerify == clientConfig.Insecure) && (cluster.CertificateAuthority == clientConfig.CAFile) && (bytes.Compare(cluster.CertificateAuthorityData, clientConfig.CAData) == 0) {
ret.Insert(key)
}
}
return ret
}
// Negotiate a bearer token with the auth server, or try to reuse one based on the
// information already present. In case of any missing information, ask for user input
// (usually username and password, interactive depending on the Reader).
func (o *LoginOptions) gatherAuthInfo() error {
directClientConfig, err := o.getClientConfig()
if err != nil {
return err
}
// make a copy and use it to avoid mutating the original
t := *directClientConfig
clientConfig := &t
// make sure we have a username before continuing
if !o.usernameProvided() {
if cmdutil.IsTerminal(o.Reader) {
for !o.usernameProvided() {
o.Username = cmdutil.PromptForString(o.Reader, "Username: ")
}
}
}
// search all valid contexts with matching server stanzas to see if we have a matching user stanza
kubeconfig := *o.StartingKubeConfig
matchingClusters := getMatchingClusters(*clientConfig, kubeconfig)
for key, context := range o.StartingKubeConfig.Contexts {
if matchingClusters.Has(context.Cluster) {
clientcmdConfig := kclientcmd.NewDefaultClientConfig(kubeconfig, &kclientcmd.ConfigOverrides{CurrentContext: key})
if kubeconfigClientConfig, err := clientcmdConfig.ClientConfig(); err == nil {
if osClient, err := client.New(kubeconfigClientConfig); err == nil {
if me, err := whoAmI(osClient); err == nil && (o.Username == me.Name) {
o.Config.BearerToken = kubeconfigClientConfig.BearerToken
o.Config.CertFile = kubeconfigClientConfig.CertFile
o.Config.CertData = kubeconfigClientConfig.CertData
o.Config.KeyFile = kubeconfigClientConfig.KeyFile
o.Config.KeyData = kubeconfigClientConfig.KeyData
if key == o.StartingKubeConfig.CurrentContext {
fmt.Fprintf(o.Out, "Already logged into %q as %q.\n", o.Config.Host, o.Username)
}
return nil
}
}
}
}
}
// if kubeconfig doesn't already have a matching user stanza...
clientConfig.BearerToken = ""
clientConfig.CertData = []byte{}
clientConfig.KeyData = []byte{}
clientConfig.CertFile = o.CertFile
clientConfig.KeyFile = o.KeyFile
token, err := tokencmd.RequestToken(o.Config, o.Reader, o.Username, o.Password)
if err != nil {
return err
}
clientConfig.BearerToken = token
osClient, err := client.New(clientConfig)
if err != nil {
return err
}
me, err := whoAmI(osClient)
if err != nil {
return err
}
o.Username = me.Name
o.Config = clientConfig
fmt.Fprintln(o.Out, "Login successful.\n")
return nil
}
// Discover the projects available for the stabilished session and take one to use. It
// fails in case of no existing projects, and print out useful information in case of
// multiple projects.
// Requires o.Username to be set.
func (o *LoginOptions) gatherProjectInfo() error {
me, err := o.whoAmI()
if err != nil {
return err
}
if o.Username != me.Name {
return fmt.Errorf("current user, %v, does not match expected user %v", me.Name, o.Username)
}
oClient, err := client.New(o.Config)
if err != nil {
return err
}
projects, err := oClient.Projects().List(labels.Everything(), fields.Everything())
if err != nil {
return err
}
projectsItems := projects.Items
switch len(projectsItems) {
case 0:
fmt.Fprintf(o.Out, `You don't have any projects. You can try to create a new project, by running
$ osc new-project <projectname>
`)
case 1:
o.Project = projectsItems[0].Name
fmt.Fprintf(o.Out, "Using project %q.\n", o.Project)
default:
projects := util.StringSet{}
for _, project := range projectsItems {
projects.Insert(project.Name)
}
namespace := o.DefaultNamespace
if !projects.Has(namespace) {
if namespace != kapi.NamespaceDefault && projects.Has(kapi.NamespaceDefault) {
namespace = kapi.NamespaceDefault
} else {
namespace = projects.List()[0]
}
}
if current, err := oClient.Projects().Get(namespace); err == nil {
o.Project = current.Name
fmt.Fprintf(o.Out, "Using project %q.\n", o.Project)
} else if !kerrors.IsNotFound(err) && !clientcmd.IsForbidden(err) {
return err
}
fmt.Fprintf(o.Out, "\nYou have access to the following projects and can switch between them with 'osc project <projectname>':\n\n")
for _, p := range projects.List() {
if o.Project == p {
fmt.Fprintf(o.Out, " * %s (current)\n", p)
} else {
fmt.Fprintf(o.Out, " * %s\n", p)
}
}
fmt.Fprintln(o.Out)
}
return nil
}
// Save all the information present in this helper to a config file. An explicit config
// file path can be provided, if not use the established conventions about config
// loading rules. Will create a new config file if one can't be found at all. Will only
// succeed if all required info is present.
func (o *LoginOptions) SaveConfig() (bool, error) {
if len(o.Username) == 0 {
return false, fmt.Errorf("Insufficient data to merge configuration.")
}
globalExistedBefore := true
if _, err := os.Stat(o.PathOptions.GlobalFile); os.IsNotExist(err) {
globalExistedBefore = false
}
newConfig := config.CreateConfig(o.Username, o.Project, o.Config)
cwd, err := os.Getwd()
if err != nil {
return false, err
}
baseDir, err := cmdutil.MakeAbs(filepath.Dir(o.PathOptions.GetDefaultFilename()), cwd)
if err != nil {
return false, err
}
if err := config.RelativizeClientConfigPaths(&newConfig, baseDir); err != nil {
return false, err
}
configToWrite, err := config.MergeConfig(*o.StartingKubeConfig, newConfig)
if err != nil {
return false, err
}
if err := kubecmdconfig.ModifyConfig(o.PathOptions, *configToWrite); err != nil {
return false, err
}
created := false
if _, err := os.Stat(o.PathOptions.GlobalFile); err == nil {
created = created || !globalExistedBefore
}
return created, nil
}
func whoAmI(client *client.Client) (*api.User, error) {
me, err := client.Users().Get("~")
if err != nil {
return nil, err
}
return me, nil
}
func (o LoginOptions) whoAmI() (*api.User, error) {
client, err := client.New(o.Config)
if err != nil {
return nil, err
}
return whoAmI(client)
}
func (o *LoginOptions) usernameProvided() bool {
return len(o.Username) > 0
}
func (o *LoginOptions) serverProvided() bool {
return (len(o.Server) > 0)
}