Skip to content

Commit 1c5f0de

Browse files
allmightyspiffGitHub Enterprise
authored andcommitted
Merge pull request #676 from Brian-Flores/issue674
New Command: `ibmcloud sl user vpn-password`
2 parents 378bf3f + e78c0c0 commit 1c5f0de

7 files changed

Lines changed: 254 additions & 15 deletions

File tree

.secrets.baseline

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "(.*test.*)|(vendor)|(go.sum)|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2023-03-03T20:07:59Z",
6+
"generated_at": "2023-04-06T16:07:51Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -302,15 +302,15 @@
302302
"hashed_secret": "bbccdf2efb33b52e6c9d0a14dd70b2d415fbea6e",
303303
"is_secret": false,
304304
"is_verified": false,
305-
"line_number": 4759,
305+
"line_number": 4743,
306306
"type": "Secret Keyword",
307307
"verified_result": null
308308
},
309309
{
310310
"hashed_secret": "7d44c33cf9a82ab9bd5300d266784504e9772177",
311311
"is_secret": false,
312312
"is_verified": false,
313-
"line_number": 4760,
313+
"line_number": 4744,
314314
"type": "Secret Keyword",
315315
"verified_result": null
316316
}
@@ -320,7 +320,7 @@
320320
"hashed_secret": "bbccdf2efb33b52e6c9d0a14dd70b2d415fbea6e",
321321
"is_secret": false,
322322
"is_verified": false,
323-
"line_number": 4760,
323+
"line_number": 4796,
324324
"type": "Secret Keyword",
325325
"verified_result": null
326326
}
@@ -338,15 +338,15 @@
338338
"hashed_secret": "bbccdf2efb33b52e6c9d0a14dd70b2d415fbea6e",
339339
"is_secret": false,
340340
"is_verified": false,
341-
"line_number": 4759,
341+
"line_number": 4743,
342342
"type": "Secret Keyword",
343343
"verified_result": null
344344
},
345345
{
346346
"hashed_secret": "8927bd748f26a7258a01e318a7e1e7585458a228",
347347
"is_secret": false,
348348
"is_verified": false,
349-
"line_number": 4760,
349+
"line_number": 4744,
350350
"type": "Secret Keyword",
351351
"verified_result": null
352352
}
@@ -356,7 +356,7 @@
356356
"hashed_secret": "bbccdf2efb33b52e6c9d0a14dd70b2d415fbea6e",
357357
"is_secret": false,
358358
"is_verified": false,
359-
"line_number": 4759,
359+
"line_number": 4743,
360360
"type": "Secret Keyword",
361361
"verified_result": null
362362
}
@@ -366,7 +366,7 @@
366366
"hashed_secret": "bbccdf2efb33b52e6c9d0a14dd70b2d415fbea6e",
367367
"is_secret": false,
368368
"is_verified": false,
369-
"line_number": 4759,
369+
"line_number": 4743,
370370
"type": "Secret Keyword",
371371
"verified_result": null
372372
}
@@ -376,15 +376,15 @@
376376
"hashed_secret": "bbccdf2efb33b52e6c9d0a14dd70b2d415fbea6e",
377377
"is_secret": false,
378378
"is_verified": false,
379-
"line_number": 4759,
379+
"line_number": 4743,
380380
"type": "Secret Keyword",
381381
"verified_result": null
382382
},
383383
{
384384
"hashed_secret": "d3ac7a4ef1a838b4134f2f6e7f3c0d249d74b674",
385385
"is_secret": false,
386386
"is_verified": false,
387-
"line_number": 4760,
387+
"line_number": 4744,
388388
"type": "Secret Keyword",
389389
"verified_result": null
390390
}
@@ -394,15 +394,15 @@
394394
"hashed_secret": "bbccdf2efb33b52e6c9d0a14dd70b2d415fbea6e",
395395
"is_secret": false,
396396
"is_verified": false,
397-
"line_number": 4759,
397+
"line_number": 4743,
398398
"type": "Secret Keyword",
399399
"verified_result": null
400400
},
401401
{
402402
"hashed_secret": "d3ac7a4ef1a838b4134f2f6e7f3c0d249d74b674",
403403
"is_secret": false,
404404
"is_verified": false,
405-
"line_number": 4760,
405+
"line_number": 4744,
406406
"type": "Secret Keyword",
407407
"verified_result": null
408408
}
@@ -412,7 +412,7 @@
412412
"hashed_secret": "bbccdf2efb33b52e6c9d0a14dd70b2d415fbea6e",
413413
"is_secret": false,
414414
"is_verified": false,
415-
"line_number": 4759,
415+
"line_number": 4743,
416416
"type": "Secret Keyword",
417417
"verified_result": null
418418
}
@@ -422,7 +422,7 @@
422422
"hashed_secret": "bbccdf2efb33b52e6c9d0a14dd70b2d415fbea6e",
423423
"is_secret": false,
424424
"is_verified": false,
425-
"line_number": 4759,
425+
"line_number": 4743,
426426
"type": "Secret Keyword",
427427
"verified_result": null
428428
}
@@ -432,7 +432,7 @@
432432
"hashed_secret": "bbccdf2efb33b52e6c9d0a14dd70b2d415fbea6e",
433433
"is_secret": false,
434434
"is_verified": false,
435-
"line_number": 4759,
435+
"line_number": 4743,
436436
"type": "Secret Keyword",
437437
"verified_result": null
438438
}
@@ -446,6 +446,16 @@
446446
"type": "Secret Keyword",
447447
"verified_result": null
448448
}
449+
],
450+
"plugin/managers/user.go": [
451+
{
452+
"hashed_secret": "c2a6b03f190dfb2b4aa91f8af8d477a9bc3401dc",
453+
"is_secret": false,
454+
"is_verified": false,
455+
"line_number": 369,
456+
"type": "Secret Keyword",
457+
"verified_result": null
458+
}
449459
]
450460
},
451461
"version": "0.13.1+ibm.47.dss",

plugin/commands/user/user.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command {
2828
cobraCmd.AddCommand(NewDeviceAccessCommand(sl).Command)
2929
cobraCmd.AddCommand(NewVpnSubnetCommand(sl).Command)
3030
cobraCmd.AddCommand(NewVpnManualCommand(sl).Command)
31+
cobraCmd.AddCommand(NewVpnPasswordCommand(sl).Command)
3132
return cobraCmd
3233
}
3334

plugin/commands/user/user_suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var availableCommands = []string{
3232
"permissions",
3333
"remove-access",
3434
"vpn-manual",
35+
"vpn-password",
3536
"vpn-subnet",
3637
}
3738

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package user
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/spf13/cobra"
7+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
8+
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
9+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
10+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
11+
)
12+
13+
type VpnPasswordCommand struct {
14+
*metadata.SoftlayerCommand
15+
UserManager managers.UserManager
16+
Command *cobra.Command
17+
Password string
18+
}
19+
20+
func NewVpnPasswordCommand(sl *metadata.SoftlayerCommand) (cmd *VpnPasswordCommand) {
21+
thisCmd := &VpnPasswordCommand{
22+
SoftlayerCommand: sl,
23+
UserManager: managers.NewUserManager(sl.Session),
24+
}
25+
26+
cobraCmd := &cobra.Command{
27+
Use: "vpn-password " + T("IDENTIFIER"),
28+
Short: T("Set the user VPN password."),
29+
Args: metadata.OneArgs,
30+
RunE: func(cmd *cobra.Command, args []string) error {
31+
return thisCmd.Run(args)
32+
},
33+
}
34+
35+
cobraCmd.Flags().StringVar(&thisCmd.Password, "password", "", T("Your new VPN password [required]"))
36+
37+
//#nosec G104 -- This is a false positive
38+
cobraCmd.MarkFlagRequired("password")
39+
40+
thisCmd.Command = cobraCmd
41+
return thisCmd
42+
}
43+
44+
func (cmd *VpnPasswordCommand) Run(args []string) error {
45+
userID, err := strconv.Atoi(args[0])
46+
if err != nil {
47+
return errors.NewInvalidSoftlayerIdInputError("User ID")
48+
}
49+
50+
success, err := cmd.UserManager.UpdateVpnPassword(userID, cmd.Password)
51+
if err != nil {
52+
return errors.NewAPIError(T("Failed to update user vpn password."), err.Error(), 2)
53+
}
54+
if success {
55+
cmd.UI.Ok()
56+
cmd.UI.Print(T("Successfully updated user VPN password."))
57+
}
58+
return nil
59+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package user_test
2+
3+
import (
4+
"errors"
5+
6+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
7+
. "github.com/onsi/ginkgo"
8+
. "github.com/onsi/gomega"
9+
"github.com/softlayer/softlayer-go/session"
10+
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/user"
12+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
14+
)
15+
16+
var _ = Describe("user vpn-password", func() {
17+
var (
18+
fakeUI *terminal.FakeUI
19+
cliCommand *user.VpnPasswordCommand
20+
fakeSession *session.Session
21+
slCommand *metadata.SoftlayerCommand
22+
fakeUserManager *testhelpers.FakeUserManager
23+
)
24+
BeforeEach(func() {
25+
fakeUI = terminal.NewFakeUI()
26+
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
27+
fakeUserManager = new(testhelpers.FakeUserManager)
28+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
29+
cliCommand = user.NewVpnPasswordCommand(slCommand)
30+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
31+
cliCommand.UserManager = fakeUserManager
32+
})
33+
34+
Describe("user vpn-password", func() {
35+
36+
Context("Return error", func() {
37+
It("Set command without Argument", func() {
38+
err := testhelpers.RunCobraCommand(cliCommand.Command)
39+
Expect(err).To(HaveOccurred())
40+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: This command requires one argument"))
41+
})
42+
43+
It("Set command with an invalid user Id", func() {
44+
err := testhelpers.RunCobraCommand(cliCommand.Command, "abcde", "--password=Mypassword1.")
45+
Expect(err).To(HaveOccurred())
46+
Expect(err.Error()).To(ContainSubstring("Invalid input for 'User ID'. It must be a positive integer."))
47+
})
48+
49+
It("Set without any option", func() {
50+
err := testhelpers.RunCobraCommand(cliCommand.Command, "111111")
51+
Expect(err).To(HaveOccurred())
52+
Expect(err.Error()).To(ContainSubstring(`required flag(s) "password" not set`))
53+
})
54+
})
55+
56+
Context("Return error", func() {
57+
BeforeEach(func() {
58+
fakeUserManager.UpdateVpnPasswordReturns(false, errors.New("Failed to update user vpn password."))
59+
})
60+
It("Failed edit user", func() {
61+
err := testhelpers.RunCobraCommand(cliCommand.Command, "111111", "--password=Mypassword1.")
62+
Expect(err).To(HaveOccurred())
63+
Expect(err.Error()).To(ContainSubstring("Failed to update user vpn password."))
64+
})
65+
})
66+
67+
Context("Return no error", func() {
68+
BeforeEach(func() {
69+
fakeUserManager.UpdateVpnPasswordReturns(true, nil)
70+
})
71+
It("enable", func() {
72+
err := testhelpers.RunCobraCommand(cliCommand.Command, "111111", "--password=Mypassword1.")
73+
Expect(err).NotTo(HaveOccurred())
74+
Expect(fakeUI.Outputs()).To(ContainSubstring("OK"))
75+
Expect(fakeUI.Outputs()).To(ContainSubstring("Successfully updated user VPN password"))
76+
})
77+
})
78+
})
79+
})

plugin/managers/user.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type UserManager interface {
5858
UpdateVpnUser(userId int) (bool, error)
5959
GetOverrides(userId int) ([]datatypes.Network_Service_Vpn_Overrides, error)
6060
DeleteUserVpnOverride(overrideId int) (bool, error)
61+
UpdateVpnPassword(userID int, password string) (bool, error)
6162
}
6263

6364
type userManager struct {
@@ -362,3 +363,10 @@ func (u userManager) DeleteUserVpnOverride(overrideId int) (bool, error) {
362363
networkServiceVpnOverridesService := services.GetNetworkServiceVpnOverridesService(u.Session)
363364
return networkServiceVpnOverridesService.Id(overrideId).DeleteObject()
364365
}
366+
367+
// Update a user’s VPN password.
368+
// int userID: The user customer identifier.
369+
// string password: New password
370+
func (u userManager) UpdateVpnPassword(userID int, password string) (bool, error) {
371+
return u.UserCustomerService.Id(userID).UpdateVpnPassword(&password)
372+
}

0 commit comments

Comments
 (0)