-
Notifications
You must be signed in to change notification settings - Fork 989
Expand file tree
/
Copy pathauth_command_test.go
More file actions
469 lines (394 loc) · 14.4 KB
/
Copy pathauth_command_test.go
File metadata and controls
469 lines (394 loc) · 14.4 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package v7_test
import (
"errors"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/api/uaa"
"code.cloudfoundry.org/cli/api/uaa/constant"
"code.cloudfoundry.org/cli/api/uaa/uaaversion"
"code.cloudfoundry.org/cli/command/commandfakes"
"code.cloudfoundry.org/cli/command/translatableerror"
. "code.cloudfoundry.org/cli/command/v7"
"code.cloudfoundry.org/cli/command/v7/v7fakes"
"code.cloudfoundry.org/cli/util/ui"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
)
var _ = Describe("auth Command", func() {
var (
cmd AuthCommand
testUI *ui.UI
fakeActor *v7fakes.FakeActor
fakeConfig *commandfakes.FakeConfig
binaryName string
err error
k8sLoginPrompts map[string]coreconfig.AuthPrompt
)
BeforeEach(func() {
testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
fakeActor = new(v7fakes.FakeActor)
fakeConfig = new(commandfakes.FakeConfig)
cmd = AuthCommand{
BaseCommand: BaseCommand{
UI: testUI,
Config: fakeConfig,
Actor: fakeActor,
},
}
binaryName = "faceman"
fakeConfig.BinaryNameReturns(binaryName)
fakeConfig.UAAOAuthClientReturns("cf")
fakeConfig.APIVersionReturns("3.160.0")
k8sLoginPrompts = map[string]coreconfig.AuthPrompt{
"k8s-auth-info": {
Entries: []string{"myuser"},
},
}
})
JustBeforeEach(func() {
err = cmd.Execute(nil)
})
When("--origin are set", func() {
BeforeEach(func() {
cmd.Origin = "some-origin"
})
When("the UAA is below the minimum API version", func() {
BeforeEach(func() {
fakeActor.GetUAAAPIVersionReturns(uaaversion.InvalidUAAClientVersion, nil)
})
It("returns an API version error", func() {
Expect(err).To(MatchError(translatableerror.MinimumUAAAPIVersionNotMetError{
Command: "Option '--origin'",
MinimumVersion: uaaversion.MinUAAClientVersion,
}))
})
})
When("--client-credentials set", func() {
BeforeEach(func() {
cmd.ClientCredentials = true
fakeActor.GetUAAAPIVersionReturns(uaaversion.MinUAAClientVersion, nil)
})
It("returns an ArgumentCombinationError", func() {
Expect(err).To(MatchError(translatableerror.ArgumentCombinationError{
Args: []string{"--client-credentials", "--origin"},
}))
})
})
})
When("credentials are missing", func() {
When("username and password are both missing", func() {
It("raises an error", func() {
Expect(err).To(MatchError(translatableerror.MissingCredentialsError{
MissingUsername: true,
MissingPassword: true,
}))
})
})
When("username is missing", func() {
BeforeEach(func() {
cmd.RequiredArgs.Password = "mypassword"
})
It("raises an error", func() {
Expect(err).To(MatchError(translatableerror.MissingCredentialsError{
MissingUsername: true,
}))
})
})
When("password is missing", func() {
BeforeEach(func() {
cmd.RequiredArgs.Username = "myuser"
})
It("raises an error", func() {
Expect(err).To(MatchError(translatableerror.MissingCredentialsError{
MissingPassword: true,
}))
})
When("authenticating against Korifi", func() {
BeforeEach(func() {
fakeConfig.IsCFOnK8sReturns(true)
fakeActor.GetLoginPromptsReturns(k8sLoginPrompts, nil)
})
It("succeeds", func() {
Expect(err).NotTo(HaveOccurred())
})
})
})
})
When("there is an auth error", func() {
BeforeEach(func() {
cmd.RequiredArgs.Username = "foo"
cmd.RequiredArgs.Password = "bar"
fakeConfig.TargetReturns("some-api-target")
fakeActor.AuthenticateReturns(uaa.UnauthorizedError{Message: "some message"})
})
It("returns a BadCredentialsError", func() {
Expect(err).To(MatchError(uaa.UnauthorizedError{Message: "some message"}))
})
})
When("there is an account locked error", func() {
BeforeEach(func() {
cmd.RequiredArgs.Username = "foo"
cmd.RequiredArgs.Password = "bar"
fakeConfig.TargetReturns("some-api-target")
fakeActor.AuthenticateReturns(uaa.AccountLockedError{Message: "some message"})
})
It("returns a BadCredentialsError", func() {
Expect(err).To(MatchError(uaa.AccountLockedError{Message: "some message"}))
})
})
When("there is a non-auth error", func() {
var expectedError error
BeforeEach(func() {
cmd.RequiredArgs.Username = "foo"
cmd.RequiredArgs.Password = "bar"
fakeConfig.TargetReturns("some-api-target")
expectedError = errors.New("my humps")
fakeActor.AuthenticateReturns(expectedError)
})
It("returns the error", func() {
Expect(err).To(MatchError(expectedError))
})
})
When("there are no input errors", func() {
var (
testID string
testSecret string
)
BeforeEach(func() {
testID = "hello"
testSecret = "goodbye"
fakeConfig.TargetReturns("some-api-target")
})
When("--client-credentials is set", func() {
BeforeEach(func() {
cmd.ClientCredentials = true
cmd.RequiredArgs.Username = testID
cmd.RequiredArgs.Password = testSecret
})
It("outputs API target information and clears the targeted org and space", func() {
Expect(err).ToNot(HaveOccurred())
Expect(testUI.Out).To(Say("API endpoint: %s", fakeConfig.Target()))
Expect(testUI.Out).To(Say(`Authenticating\.\.\.`))
Expect(testUI.Out).To(Say("OK"))
Expect(testUI.Out).To(Say("Use '%s target' to view or set your target org and space", binaryName))
Expect(fakeActor.AuthenticateCallCount()).To(Equal(1))
credentials, origin, grantType := fakeActor.AuthenticateArgsForCall(0)
ID := credentials["client_id"]
secret := credentials["client_secret"]
Expect(ID).To(Equal(testID))
Expect(secret).To(Equal(testSecret))
Expect(origin).To(BeEmpty())
Expect(grantType).To(Equal(constant.GrantTypeClientCredentials))
})
})
When("--client-credentials is not set", func() {
When("username and password are only provided as arguments", func() {
BeforeEach(func() {
cmd.RequiredArgs.Username = testID
cmd.RequiredArgs.Password = testSecret
})
When("the API version is older than the minimum supported API version for the v9 CLI", func() {
BeforeEach(func() {
fakeConfig.APIVersionReturns("3.83.0")
})
It("warns that the user is targeting an unsupported API version and that things may not work correctly", func() {
Expect(err).ToNot(HaveOccurred())
Expect(testUI.Out).To(Say("API endpoint: %s", fakeConfig.Target()))
Expect(testUI.Err).To(Say("Warning: Your targeted API's version \\(3.83.0\\) is less than the minimum supported API version \\(3.160.0\\). Some commands may not function correctly."))
})
})
When("the API version is empty", func() {
BeforeEach(func() {
fakeConfig.APIVersionReturns("")
})
It("prints a warning message", func() {
Expect(err).ToNot(HaveOccurred())
Expect(testUI.Err).To(Say("Warning: unable to determine whether targeted API's version meets minimum supported."))
})
})
It("should NOT warn that the user is targeting an unsupported API version", func() {
Expect(testUI.Err).ToNot(Say("is less than the minimum supported API version"))
})
It("outputs API target information and clears the targeted org and space", func() {
Expect(err).ToNot(HaveOccurred())
Expect(testUI.Out).To(Say("API endpoint: %s", fakeConfig.Target()))
Expect(testUI.Out).To(Say(`Authenticating\.\.\.`))
Expect(testUI.Out).To(Say("OK"))
Expect(testUI.Out).To(Say("Use '%s target' to view or set your target org and space", binaryName))
Expect(fakeActor.AuthenticateCallCount()).To(Equal(1))
credentials, origin, grantType := fakeActor.AuthenticateArgsForCall(0)
username := credentials["username"]
password := credentials["password"]
Expect(username).To(Equal(testID))
Expect(password).To(Equal(testSecret))
Expect(origin).To(BeEmpty())
Expect(grantType).To(Equal(constant.GrantTypePassword))
})
})
When("--assertion is set", func() {
BeforeEach(func() {
cmd.ClientCredentials = false
cmd.Assertion = "jwt-token"
cmd.RequiredArgs.Username = testID
cmd.RequiredArgs.Password = testSecret
})
It("outputs API target information and clears the targeted org and space", func() {
Expect(err).ToNot(HaveOccurred())
Expect(testUI.Out).To(Say("API endpoint: %s", fakeConfig.Target()))
Expect(testUI.Out).To(Say(`Authenticating\.\.\.`))
Expect(testUI.Out).To(Say("OK"))
Expect(testUI.Out).To(Say("Use '%s target' to view or set your target org and space", binaryName))
Expect(fakeActor.AuthenticateCallCount()).To(Equal(1))
credentials, origin, grantType := fakeActor.AuthenticateArgsForCall(0)
ID := credentials["client_id"]
secret := credentials["client_secret"]
Expect(ID).To(Equal(testID))
Expect(secret).To(Equal(testSecret))
Expect(origin).To(BeEmpty())
Expect(grantType).To(Equal(constant.GrantTypeJwtBearer))
})
})
When("--assertion and --client-credentials is set", func() {
BeforeEach(func() {
cmd.ClientCredentials = true
cmd.Assertion = "client-jwt-token"
cmd.RequiredArgs.Username = testID
cmd.RequiredArgs.Password = testSecret
})
It("outputs API target information and clears the targeted org and space", func() {
Expect(err).ToNot(HaveOccurred())
Expect(testUI.Out).To(Say("API endpoint: %s", fakeConfig.Target()))
Expect(testUI.Out).To(Say(`Authenticating\.\.\.`))
Expect(testUI.Out).To(Say("OK"))
Expect(testUI.Out).To(Say("Use '%s target' to view or set your target org and space", binaryName))
Expect(fakeActor.AuthenticateCallCount()).To(Equal(1))
credentials, origin, grantType := fakeActor.AuthenticateArgsForCall(0)
ID := credentials["client_id"]
secret := credentials["client_secret"]
clientAssertion := credentials["client_assertion"]
clientAssertionType := credentials["client_assertion_type"]
Expect(ID).To(Equal(testID))
Expect(secret).To(BeEmpty())
Expect(origin).To(BeEmpty())
Expect(secret).To(Equal(""))
Expect(clientAssertion).To(Equal("client-jwt-token"))
Expect(clientAssertionType).To(Equal("urn:ietf:params:oauth:client-assertion-type:jwt-bearer"))
Expect(grantType).To(Equal(constant.GrantTypeClientCredentials))
})
})
When("the username and password are provided in env variables", func() {
var (
envUsername string
envPassword string
)
BeforeEach(func() {
envUsername = "banana"
envPassword = "potato"
fakeConfig.CFUsernameReturns(envUsername)
fakeConfig.CFPasswordReturns(envPassword)
})
When("username and password are not also provided as arguments", func() {
It("authenticates with the values in the env variables", func() {
Expect(err).ToNot(HaveOccurred())
Expect(fakeActor.AuthenticateCallCount()).To(Equal(1))
credentials, origin, _ := fakeActor.AuthenticateArgsForCall(0)
username := credentials["username"]
password := credentials["password"]
Expect(username).To(Equal(envUsername))
Expect(password).To(Equal(envPassword))
Expect(origin).To(BeEmpty())
})
})
When("username and password are also provided as arguments", func() {
BeforeEach(func() {
cmd.RequiredArgs.Username = testID
cmd.RequiredArgs.Password = testSecret
})
It("authenticates with the values from the command line args", func() {
Expect(err).ToNot(HaveOccurred())
Expect(fakeActor.AuthenticateCallCount()).To(Equal(1))
credentials, origin, _ := fakeActor.AuthenticateArgsForCall(0)
username := credentials["username"]
password := credentials["password"]
Expect(username).To(Equal(testID))
Expect(password).To(Equal(testSecret))
Expect(origin).To(BeEmpty())
})
})
})
When("authenticating against Korifi", func() {
BeforeEach(func() {
cmd.RequiredArgs.Username = "myuser"
fakeConfig.IsCFOnK8sReturns(true)
fakeActor.GetLoginPromptsReturns(k8sLoginPrompts, nil)
})
When("the specified username doesn't match any k8s context", func() {
BeforeEach(func() {
cmd.RequiredArgs.Username = "some-unknown-user"
})
It("errors", func() {
Expect(err).To(MatchError(errors.New("kubernetes user not found in configuration: some-unknown-user")))
})
})
When("the prompts don't contain k8s authentication information", func() {
BeforeEach(func() {
k8sLoginPrompts = map[string]coreconfig.AuthPrompt{
"some-other-auth-info": {
Entries: []string{"myuser"},
},
}
fakeActor.GetLoginPromptsReturns(k8sLoginPrompts, nil)
})
It("errors", func() {
Expect(err).To(MatchError(errors.New("kubernetes login context is missing")))
})
})
})
})
When("a user has manually added their client credentials to the config file", func() {
BeforeEach(func() {
fakeConfig.UAAOAuthClientReturns("AClientsId")
})
When("the --client-credentials flag is not set", func() {
BeforeEach(func() {
cmd.ClientCredentials = false
cmd.RequiredArgs.Username = "some-username"
cmd.RequiredArgs.Password = "some-password"
})
It("fails with an error indicating manual client credentials are no longer supported in the config file", func() {
Expect(err).To(MatchError(translatableerror.ManualClientCredentialsError{}))
})
})
})
})
When("already logged in via client credentials", func() {
BeforeEach(func() {
fakeConfig.UAAGrantTypeReturns("client_credentials")
})
When("authenticating as a user", func() {
BeforeEach(func() {
cmd.ClientCredentials = false
cmd.RequiredArgs.Username = "some-username"
cmd.RequiredArgs.Password = "some-password"
})
It("returns an already logged in error", func() {
Expect(err).To(MatchError("Service account currently logged in. Use 'cf logout' to log out service account and try again."))
Expect(fakeConfig.UAAGrantTypeCallCount()).To(Equal(1))
})
It("the returned error is translatable", func() {
Expect(err).To(MatchError(translatableerror.PasswordGrantTypeLogoutRequiredError{}))
})
})
When("authenticating as a client", func() {
BeforeEach(func() {
cmd.ClientCredentials = true
cmd.RequiredArgs.Username = "some-client-id"
cmd.RequiredArgs.Password = "some-client-secret"
})
It("does not error", func() {
Expect(err).ToNot(HaveOccurred())
Expect(fakeConfig.UAAGrantTypeCallCount()).To(Equal(0))
})
})
})
})