Skip to content

Commit 7db5469

Browse files
Updated secrets baseline
1 parent d2df65c commit 7db5469

5 files changed

Lines changed: 62 additions & 188 deletions

File tree

.secrets.baseline

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "plugin/i18n/v1Resources/|plugin/i18n/v2Resources/|(.*test.*)|(vendor)|(go.sum)|bin/|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2024-07-22T22:42:28Z",
6+
"generated_at": "2024-09-04T21:46:16Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -270,23 +270,23 @@
270270
"hashed_secret": "df4bb9b1035a1847159d5c655c1d11a00508a609",
271271
"is_secret": false,
272272
"is_verified": false,
273-
"line_number": 93,
273+
"line_number": 89,
274274
"type": "Secret Keyword",
275275
"verified_result": null
276276
},
277277
{
278-
"hashed_secret": "53aa77492eb716085c45d2c5873f9e47abd66bf2",
278+
"hashed_secret": "09d3c49efe52ba11e94d7bdd18d2801a7830f583",
279279
"is_secret": false,
280280
"is_verified": false,
281-
"line_number": 95,
281+
"line_number": 91,
282282
"type": "Secret Keyword",
283283
"verified_result": null
284284
},
285285
{
286286
"hashed_secret": "18a6fefdd2d6204456b0733cc47be1397f284fa4",
287287
"is_secret": false,
288288
"is_verified": false,
289-
"line_number": 98,
289+
"line_number": 94,
290290
"type": "Secret Keyword",
291291
"verified_result": null
292292
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require (
3434
github.com/pmezard/go-difflib v1.0.0 // indirect
3535
github.com/rivo/uniseg v0.4.7 // indirect
3636
github.com/rogpeppe/go-internal v1.9.0 // indirect
37+
github.com/sethvargo/go-password v0.3.1 // indirect
3738
github.com/softlayer/xmlrpc v0.0.0-20200409220501-5f089df7cb7e // indirect
3839
golang.org/x/crypto v0.24.0 // indirect
3940
golang.org/x/mod v0.18.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f
6666
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
6767
github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8=
6868
github.com/sclevine/spec v1.4.0/go.mod h1:LvpgJaFyvQzRvc1kaDs0bulYwzC70PbiYjC4QnFHkOM=
69+
github.com/sethvargo/go-password v0.3.1 h1:WqrLTjo7X6AcVYfC6R7GtSyuUQR9hGyAj/f1PYQZCJU=
70+
github.com/sethvargo/go-password v0.3.1/go.mod h1:rXofC1zT54N7R8K/h1WDUdkf9BOx5OptoxrMBcrXzvs=
6971
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
7072
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
7173
github.com/smartystreets/goconvey v1.6.7 h1:I6tZjLXD2Q1kjvNbIzB1wvQBsXmKXiVrhpRE8ZjP5jY=

plugin/commands/user/create.go

Lines changed: 4 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package user
22

33
import (
4-
crand "crypto/rand"
5-
"encoding/binary"
64
"encoding/json"
75
"fmt"
8-
"log"
9-
"math/rand"
106
"reflect"
11-
"strings"
127

8+
gopass "github.com/sethvargo/go-password/password"
139
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
1410

1511
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/terminal"
@@ -92,7 +88,7 @@ func (cmd *CreateCommand) Run(args []string) error {
9288

9389
password := cmd.Password
9490
if password == "generate" {
95-
password = string(GeneratePassword(23, 4))
91+
password = gopass.MustGenerate(18, 4, 4, false, false)
9692
}
9793

9894
vpnPassword := cmd.VpnPassword
@@ -139,54 +135,9 @@ func printUser(user datatypes.User_Customer, password string, ui terminal.UI) {
139135
table.Print()
140136
}
141137

142-
// random source leveraging crypto/rand to provide
143-
// true non-determinstic
144-
type cryptoSource struct{}
145-
146-
func (s cryptoSource) Seed(seed int64) {}
147-
148-
func (s cryptoSource) Int63() int64 {
149-
return int64(s.Uint64() & ^uint64(1<<63))
150-
}
151-
152-
func (s cryptoSource) Uint64() (v uint64) {
153-
err := binary.Read(crand.Reader, binary.BigEndian, &v)
154-
if err != nil {
155-
log.Fatal(err)
156-
}
157-
return v
158-
}
159-
160-
// GeneratePassword will create a random password
161-
// Returns a 23 character random string
162-
// 0 only number
163-
// 1 lower and upper
164-
// 2 upper
165-
// 3 special
166-
// 4 all
167-
func GeneratePassword(size int, kind int) []byte {
168-
ikind, kinds, result := kind, [][]int{{10, 48}, {26, 97}, {26, 65}, {10, 38}}, make([]byte, size)
169-
isAll := kind > 3 || kind < 0
170-
171-
// #nosec G404: Use "crypto/rand" as the seed, which should resolve the pseudo "math/rand"
172-
rnd := rand.New(&cryptoSource{})
173-
generate := true
174-
for generate {
175-
result = make([]byte, size)
176-
for i := 0; i < size; i++ {
177-
if isAll { // random ikind
178-
ikind = rnd.Intn(4)
179-
}
180-
scope, base := kinds[ikind][0], kinds[ikind][1]
181-
result[i] = uint8(base + rnd.Intn(scope))
182-
}
183-
generate = !IsValidPassword(string(result))
184-
}
185-
return result
186-
}
187-
138+
// Values of B get copied into A
139+
// A <--- B
188140
func StructAssignment(A, B interface{}) { //a =b
189-
190141
av := reflect.ValueOf(A).Elem()
191142
at := av.Type()
192143

@@ -202,34 +153,3 @@ func StructAssignment(A, B interface{}) { //a =b
202153
}
203154
}
204155
}
205-
206-
func IsValidPassword(output string) bool {
207-
output = strings.TrimSpace(output)
208-
var uppercase, lowercase, number, simbol, lenght bool
209-
//Verify lenght is 23
210-
if len(output) == 23 {
211-
lenght = true
212-
}
213-
for _, char := range output {
214-
//Verify exist uppercase
215-
if int(char) >= 65 && int(char) <= 90 {
216-
uppercase = true
217-
}
218-
//Verify exist lowercase
219-
if int(char) >= 97 && int(char) <= 122 {
220-
lowercase = true
221-
}
222-
//Verify exist number
223-
if int(char) >= 48 && int(char) <= 57 {
224-
number = true
225-
}
226-
//Verify exist simbol
227-
if int(char) >= 33 && int(char) <= 47 {
228-
simbol = true
229-
}
230-
}
231-
if uppercase && lowercase && number && simbol && lenght {
232-
return true
233-
}
234-
return false
235-
}

plugin/commands/user/create_test.go

Lines changed: 50 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -40,57 +40,54 @@ var _ = Describe("Create", func() {
4040
}
4141
fakeUserManager.CreateUserReturns(testUser, nil)
4242
})
43-
Describe("user create", func() {
44-
Context("user create with not enough parameters", func() {
45-
It("return error", func() {
43+
Describe("User Create Command", func() {
44+
Context("Invalid Paramter Checks", func() {
45+
It("Needs one argument", func() {
4646
err := testhelpers.RunCobraCommand(cliCommand.Command)
4747
Expect(err).To(HaveOccurred())
48-
Expect(strings.Contains(err.Error(), "Incorrect Usage: This command requires one argument")).To(BeTrue())
48+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: This command requires one argument"))
4949
})
5050
})
51-
52-
Context("create user with fail confirmation", func() {
53-
It("return error", func() {
51+
Context("Input Checks", func() {
52+
It("Not Y/N", func() {
5453
fakeUI.Inputs("123456")
5554
err := testhelpers.RunCobraCommand(cliCommand.Command, "createdUser@email.com", "--email", "createdUser@email.com", "--password", "MyPassWord")
5655
Expect(err).To(HaveOccurred())
5756
Expect(fakeUI.Outputs()).To(ContainSubstring("You are about to create the following user: createdUser@email.com. Do you wish to continue?"))
5857
Expect(err.Error()).To(ContainSubstring("input must be 'y', 'n', 'yes' or 'no'"))
5958
})
60-
})
61-
62-
Context("create user with No confirmation", func() {
63-
It("return error", func() {
59+
It("No confirmation", func() {
6460
fakeUI.Inputs("No")
6561
err := testhelpers.RunCobraCommand(cliCommand.Command, "createdUser@email.com", "--email", "createdUser@email.com", "--password", "MyPassWord")
6662
Expect(err).NotTo(HaveOccurred())
6763
Expect(fakeUI.Outputs()).To(ContainSubstring("You are about to create the following user: createdUser@email.com. Do you wish to continue?"))
6864
Expect(fakeUI.Outputs()).To(ContainSubstring("Aborted."))
6965
})
7066
})
71-
72-
Context("User Create error", func() {
73-
It("return error", func() {
67+
Context("Error Handling", func() {
68+
It("API Error", func() {
7469
fakeUserManager.CreateUserReturns(datatypes.User_Customer{}, errors.New("Internal server error"))
7570
err := testhelpers.RunCobraCommand(cliCommand.Command, "createdUser@email.com", "--email", "createdUser@email.com", "--password", "MyPassWord", "-f")
7671
Expect(err).To(HaveOccurred())
7772
Expect(err.Error()).To(ContainSubstring("Failed to add user."))
7873
})
74+
It("Bad Template", func() {
75+
err := testhelpers.RunCobraCommand(cliCommand.Command, "createdUser@email.com", "--email", "createdUser@email.com", "--password", "MyPassWord", "-f", "--template", ``)
76+
Expect(err).To(HaveOccurred())
77+
Expect(err.Error()).To(ContainSubstring("Unable to unmarshal template json: unexpected end of JSON input"))
78+
})
7979
})
8080

81-
Context("Basic User Create usage", func() {
82-
It("Create a user", func() {
81+
Context("Happy Path Tests", func() {
82+
It("Create a user --force", func() {
8383
err := testhelpers.RunCobraCommand(cliCommand.Command, "createdUser@email.com", "--email", "createdUser@email.com", "--password", "MyPassWord", "-f")
8484
Expect(err).NotTo(HaveOccurred())
8585
Expect(fakeUI.Outputs()).To(ContainSubstring("name value"))
8686
Expect(fakeUI.Outputs()).To(ContainSubstring("Username createdUser"))
8787
Expect(fakeUI.Outputs()).To(ContainSubstring("Email createdUser@email.com"))
8888
Expect(fakeUI.Outputs()).To(ContainSubstring("Password MyPassWord"))
8989
})
90-
})
91-
92-
Context("User Create", func() {
93-
It("Create a user", func() {
90+
It("Create a user with confirmation", func() {
9491
fakeUI.Inputs("Y")
9592
err := testhelpers.RunCobraCommand(cliCommand.Command, "createdUser@email.com", "--email", "createdUser@email.com", "--password", "MyPassWord")
9693
Expect(err).NotTo(HaveOccurred())
@@ -100,10 +97,7 @@ var _ = Describe("Create", func() {
10097
Expect(fakeUI.Outputs()).To(ContainSubstring("Email createdUser@email.com"))
10198
Expect(fakeUI.Outputs()).To(ContainSubstring("Password MyPassWord"))
10299
})
103-
})
104-
105-
Context("User Create from user", func() {
106-
It("Create a user", func() {
100+
It("Create a user from another user", func() {
107101
fakeUI.Inputs("Y")
108102
err := testhelpers.RunCobraCommand(cliCommand.Command, "createdUser@email.com", "--from-user", "456", "--password", "MyPassWord", "-f")
109103
Expect(err).NotTo(HaveOccurred())
@@ -112,18 +106,7 @@ var _ = Describe("Create", func() {
112106
Expect(fakeUI.Outputs()).To(ContainSubstring("Email createdUser@email.com"))
113107
Expect(fakeUI.Outputs()).To(ContainSubstring("Password MyPassWord"))
114108
})
115-
})
116-
117-
Context("User Create from wrong template", func() {
118-
It("Create a user", func() {
119-
err := testhelpers.RunCobraCommand(cliCommand.Command, "createdUser@email.com", "--email", "createdUser@email.com", "--password", "MyPassWord", "-f", "--template", ``)
120-
Expect(err).To(HaveOccurred())
121-
Expect(err.Error()).To(ContainSubstring("Unable to unmarshal template json: unexpected end of JSON input"))
122-
})
123-
})
124-
125-
Context("User Create from template", func() {
126-
It("Create a user", func() {
109+
It("Create a user from a template", func() {
127110
testUser := datatypes.User_Customer{
128111
Id: sl.Int(6666),
129112
Username: sl.String("createdUser"),
@@ -138,12 +121,8 @@ var _ = Describe("Create", func() {
138121
Expect(fakeUI.Outputs()).To(ContainSubstring("Username createdUser"))
139122
Expect(fakeUI.Outputs()).To(ContainSubstring("Email createdUser@email.com"))
140123
Expect(fakeUI.Outputs()).To(ContainSubstring("Password MyPassWord"))
141-
142124
})
143-
})
144-
145-
Context("User Create with generated password", func() {
146-
It("Create a user", func() {
125+
It("Create a user with a generated password", func() {
147126
err := testhelpers.RunCobraCommand(cliCommand.Command, "createdUser@email.com", "--email", "createdUser@email.com", "--password", "generate", "-f")
148127
Expect(err).NotTo(HaveOccurred())
149128
Expect(fakeUI.Outputs()).To(ContainSubstring("name value"))
@@ -152,64 +131,36 @@ var _ = Describe("Create", func() {
152131
Expect(fakeUI.Outputs()).To(ContainSubstring("Password"))
153132
})
154133
})
155-
156134
})
157135

158-
Describe("structAssignment", func() {
159-
160-
A1 := "11"
161-
A2 := "12"
162-
B1 := "21"
163-
B2 := "22"
164-
var S1, S2 datatypes.User_Customer
165-
Context("structAssignment", func() {
166-
BeforeEach(func() {
167-
S1 = datatypes.User_Customer{Address1: &A1, Address2: &A2}
168-
S2 = datatypes.User_Customer{Address1: &B1, Address2: nil}
169-
})
170-
171-
It("return succ", func() {
172-
user.StructAssignment(&S1, &S2)
173-
Expect(*S1.Address1).To(Equal("21"))
174-
Expect(*S1.Address2).To(Equal("12"))
175-
})
176-
})
177-
Context("structAssignment", func() {
178-
BeforeEach(func() {
179-
S1 = datatypes.User_Customer{Address1: &A1, Address2: &A2}
180-
S2 = datatypes.User_Customer{Address1: &B1, Address2: &B2}
181-
})
182-
183-
It("return succ", func() {
184-
user.StructAssignment(&S1, &S2)
185-
Expect(*S1.Address1).To(Equal("21"))
186-
Expect(*S1.Address2).To(Equal("22"))
187-
})
188-
})
189-
Context("structAssignment", func() {
190-
BeforeEach(func() {
191-
S1 = datatypes.User_Customer{Address1: nil, Address2: &A2}
192-
S2 = datatypes.User_Customer{Address1: &B1, Address2: &B2}
193-
})
194-
195-
It("return succ", func() {
196-
user.StructAssignment(&S1, &S2)
197-
Expect(*S1.Address1).To(Equal("21"))
198-
Expect(*S1.Address2).To(Equal("22"))
199-
})
200-
})
201-
202-
Context("structAssignment", func() {
203-
BeforeEach(func() {
204-
S1 = datatypes.User_Customer{Address1: &A1, Address2: &A2}
205-
S2 = datatypes.User_Customer{Address1: nil, Address2: &B2}
206-
})
207-
208-
It("return succ", func() {
209-
user.StructAssignment(&S1, &S2)
210-
Expect(*S1.Address1).To(Equal("11"))
211-
Expect(*S1.Address2).To(Equal("22"))
212-
})
213-
})
214-
})
136+
// dataValues are a set of 4 strings we set Default and UserValues to
137+
// expected is a set of 2 strings that we check were set properly
138+
DescribeTable("StructAssignment Tests",
139+
func(dataValues []string, expected []string) {
140+
Expect(len(dataValues)).To(Equal(4))
141+
Expect(len(expected)).To(Equal(2))
142+
Default := datatypes.User_Customer{Address1: &dataValues[0], Address2: &dataValues[1]}
143+
UserValues := datatypes.User_Customer{Address1: &dataValues[2], Address2: &dataValues[3]}
144+
// Can't set nil in the dataValues value because its a string, so we just do this
145+
if dataValues[0] == "nil" {
146+
Default.Address1 = nil
147+
}
148+
if dataValues[1] == "nil" {
149+
Default.Address2 = nil
150+
}
151+
if dataValues[2] == "nil" {
152+
UserValues.Address1 = nil
153+
}
154+
if dataValues[3] == "nil" {
155+
UserValues.Address2 = nil
156+
}
157+
user.StructAssignment(&Default, &UserValues)
158+
Expect(*Default.Address1).To(Equal(expected[0]))
159+
Expect(*Default.Address2).To(Equal(expected[1]))
160+
},
161+
Entry("Test1", []string{"Def1", "Def2", "UserInput1", "nil"}, []string{"UserInput1", "Def2"}),
162+
Entry("Test2", []string{"Def1", "Def2", "nil", "UserInput2"}, []string{"Def1", "UserInput2"}),
163+
Entry("Test3", []string{"Def1", "nil", "UserInput1", "UserInput2"}, []string{"UserInput1", "UserInput2"}),
164+
Entry("Test4", []string{"nil", "Def2", "UserInput1", "UserInput2"}, []string{"UserInput1", "UserInput2"}),
165+
)
215166
})

0 commit comments

Comments
 (0)