forked from openshift/origin
/
loginoptions.go
339 lines (275 loc) · 9.53 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
package cmd
import (
"fmt"
"io"
"os"
"sort"
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"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/openshift/origin/pkg/client"
"github.com/openshift/origin/pkg/cmd/cli/config"
"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"
// Helper for the login and setup process, gathers all information required for a
// successful login and eventual update of config files.
// Depending on the Reader present it can be interactive, asking for terminal input in
// case of any missing information.
// Notice that some methods mutate this object so it should not be reused. The Config
// provided as a pointer will also mutate (handle new auth tokens, etc).
type LoginOptions struct {
// flags and printing helpers
Username string
Password string
Project string
// infra
ClientConfig kclientcmd.ClientConfig
Config *kclient.Config
Reader io.Reader
// flow controllers
gatheredServerInfo bool
gatheredAuthInfo bool
gatheredProjectInfo bool
// Optional, if provided will only try to save in it
PathToSaveConfig string
}
// Gather all required information in a comprehensive order.
func (o *LoginOptions) GatherInfo() error {
if err := o.gatherServerInfo(); err != nil {
return err
}
if err := o.gatherAuthInfo(); err != nil {
return err
}
if err := o.gatherProjectInfo(); err != nil {
return err
}
return nil
}
// Makes sure it has all the needed information about the server we are connecting to,
// particularly the host address and certificate information. For every information not
// present ask for interactive user input. Will also ping the server to make sure we can
// connect to it, and if any problem is found (e.g. certificate issues), ask the user about
// connecting insecurely.
func (o *LoginOptions) gatherServerInfo() error {
// we need to have a server to talk to
if util.IsTerminal(o.Reader) {
for !o.serverProvided() {
defaultServer := defaultClusterURL
promptMsg := fmt.Sprintf("OpenShift server [%s]: ", defaultServer)
server := util.PromptForStringWithDefault(o.Reader, defaultServer, promptMsg)
kclientcmd.DefaultCluster = clientcmdapi.Cluster{Server: server}
}
}
// we know the server we are expected to use
clientCfg, err := o.ClientConfig.ClientConfig()
if err != nil {
return err
}
// ping to check if server is reachable
osClient, err := client.New(clientCfg)
if err != nil {
return err
}
result := osClient.Get().AbsPath("/osapi").Do()
if result.Error() != nil {
// certificate issue, prompt user for insecure connection
if clientcmd.IsCertificateAuthorityUnknown(result.Error()) {
fmt.Println("The server uses a certificate signed by an unknown authority.")
fmt.Println("You can bypass the certificate check, but any data you send to the server could be intercepted by others.")
clientCfg.Insecure = util.PromptForBool(os.Stdin, "Use insecure connections? (y/n): ")
if !clientCfg.Insecure {
return fmt.Errorf(clientcmd.GetPrettyMessageFor(result.Error()))
}
fmt.Println()
}
}
// we have all info we need, now we can have a proper Config
o.Config = clientCfg
o.gatheredServerInfo = true
return nil
}
// 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 {
if err := o.assertGatheredServerInfo(); err != nil {
return err
}
if me, err := o.whoAmI(); err == nil && (!o.usernameProvided() || (o.usernameProvided() && o.Username == me.Name)) {
o.Username = me.Name
fmt.Printf("Already logged into %q as %q.\n", o.Config.Host, o.Username)
} else {
// if not, we need to log in again
o.Config.BearerToken = ""
token, err := tokencmd.RequestToken(o.Config, o.Reader, o.Username, o.Password)
if err != nil {
return err
}
o.Config.BearerToken = token
me, err := o.whoAmI()
if err != nil {
return err
}
o.Username = me.Name
fmt.Println("Login successful.\n")
}
// TODO investigate about the safety and intent of the proposal below
// if trying to log in an user that's not the currently logged in, try to reuse an existing token
// if o.usernameProvided() {
// glog.V(5).Infof("Checking existing authentication info for '%v'...\n", o.Username)
// for _, ctx := range rawCfg.Contexts {
// authInfo := rawCfg.AuthInfos[ctx.AuthInfo]
// clusterInfo := rawCfg.Clusters[ctx.Cluster]
// if ctx.AuthInfo == o.Username && clusterInfo.Server == o.Server && len(authInfo.Token) > 0 { // only token for now
// glog.V(5).Infof("Authentication exists for '%v' on '%v', trying to use it...\n", o.Server, o.Username)
// o.Config.BearerToken = authInfo.Token
// if me, err := whoami(o.Config); err == nil && usernameFromUser(me) == o.Username {
// o.Username = usernameFromUser(me)
// return nil
// }
// glog.V(5).Infof("Token %v no longer valid for '%v', can't use it\n", authInfo.Token, o.Username)
// }
// }
// }
o.gatheredAuthInfo = true
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 {
if err := o.assertGatheredAuthInfo(); err != nil {
return err
}
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:
// TODO most users will not be allowed to run the suggested commands below, so we should check it and/or
// have a server endpoint that allows an admin to describe to users how to request projects
fmt.Printf(`You don't have any projects. If you have access to create a new project, run
$ openshift ex new-project <projectname> --admin=%q
To be added as an admin to an existing project, run
$ openshift ex policy add-role-to-user admin %q -n <projectname>
`, o.Username, o.Username)
case 1:
o.Project = projectsItems[0].Name
fmt.Printf("Using project %q\n", o.Project)
default:
projects := []string{}
for _, project := range projectsItems {
projects = append(projects, project.Name)
}
namespace, err := o.ClientConfig.Namespace()
if err != nil {
return err
}
current, err := oClient.Projects().Get(namespace)
if err == nil {
o.Project = current.Name
fmt.Printf("Using project %q\n", o.Project)
o.gatheredProjectInfo = true
return nil
}
if !kerrors.IsNotFound(err) && !clientcmd.IsForbidden(err) {
return err
}
sort.StringSlice(projects).Sort()
fmt.Printf("You have access to the following projects and can switch between them with 'osc project <projectname>':\n\n")
for _, p := range projects {
if o.Project == p {
fmt.Printf(" * %s (current)\n", p)
} else {
fmt.Printf(" * %s\n", p)
}
}
fmt.Println()
}
o.gatheredProjectInfo = true
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() (created bool, err error) {
if len(o.Username) == 0 {
return false, fmt.Errorf("Insufficient data to merge configuration.")
}
var configStore *config.ConfigStore
if len(o.PathToSaveConfig) > 0 {
configStore, err = config.LoadFrom(o.PathToSaveConfig)
if err != nil {
return created, err
}
} else {
configStore, err = config.LoadWithLoadingRules()
if err != nil {
configStore, err = config.CreateEmpty()
if err != nil {
return created, err
}
created = true
}
}
rawCfg, err := o.ClientConfig.RawConfig()
if err != nil {
return created, err
}
return created, configStore.SaveToFile(o.Username, o.Project, o.Config, rawCfg)
}
func (o *LoginOptions) whoAmI() (*api.User, error) {
oClient, err := client.New(o.Config)
if err != nil {
return nil, err
}
me, err := oClient.Users().Get("~")
if err != nil {
return nil, err
}
return me, nil
}
func (o *LoginOptions) assertGatheredServerInfo() error {
if !o.gatheredServerInfo {
return fmt.Errorf("Must gather server info first.")
}
return nil
}
func (o *LoginOptions) assertGatheredAuthInfo() error {
if !o.gatheredAuthInfo {
return fmt.Errorf("Must gather auth info first.")
}
return nil
}
func (o *LoginOptions) assertGatheredProjectInfo() error {
if !o.gatheredProjectInfo {
return fmt.Errorf("Must gather project info first.")
}
return nil
}
func (o *LoginOptions) usernameProvided() bool {
return len(o.Username) > 0
}
func (o *LoginOptions) passwordProvided() bool {
return len(o.Password) > 0
}
func (o *LoginOptions) serverProvided() bool {
_, err := o.ClientConfig.ClientConfig()
return err == nil || !clientcmd.IsNoServerFound(err)
}