Merge lp:~rvb/gwacl/random-pw into lp:gwacl

Proposed by Raphaël Badin on 2013-07-11
Status: Merged
Approved by: Raphaël Badin on 2013-07-11
Approved revision: 179
Merged at revision: 178
Proposed branch: lp:~rvb/gwacl/random-pw
Merge into: lp:gwacl
Diff against target: 92 lines (+53/-3)
2 files modified
names.go (+32/-3)
names_test.go (+21/-0)
To merge this branch: bzr merge lp:~rvb/gwacl/random-pw
Reviewer Review Type Date Requested Status
Gavin Panella 2013-07-11 Approve on 2013-07-11
Review via email: mp+174200@code.launchpad.net

Commit message

Add MakeRandomPassword utility.

Description of the change

Add utility to create a valid Azure password. We could do this in much clever ways but I think that this serves our purpose well enough and is pretty readable.

To post a comment you must log in.
Gavin Panella (allenap) wrote :

Looks good.

[1]

+const (
+    // Valid passwords must be 6-72 characters long.
+    passwordSize = 50
+)

According to http://www.universetoday.com/36302/atoms-in-the-universe/
this means that there are more possible password combinations than
there are atoms in the universe.

[2]

+    // Only digits and lower-case ASCII letters are accepted.
+    const (
+        chars = letters + digits + upperCaseLetters
+    )
+

The comment is incorrect. Also, you could dispense with the braces and
the blank line to try and claw back some of the losses incurred from
Go's syntax.

[3]

+    id := letter + upperCaseLetter + digit
+    for len(id) < passwordSize {
+        id += pickOne(chars)
+    }
+    return id

s/id/password/ ?

review: Approve
Raphaël Badin (rvb) wrote :

> Looks good.

Thanks for the review!

> [1]
>
> +const (
> +    // Valid passwords must be 6-72 characters long.
> +    passwordSize = 50
> +)
>
> According to http://www.universetoday.com/36302/atoms-in-the-universe/
> this means that there are more possible password combinations than
> there are atoms in the universe.

The more the merrier!
>
>
> [2]
>
> +    // Only digits and lower-case ASCII letters are accepted.
> +    const (
> +        chars = letters + digits + upperCaseLetters
> +    )
> +
>
> The comment is incorrect. Also, you could dispense with the braces and
> the blank line to try and claw back some of the losses incurred from
> Go's syntax.

Fixed.

> [3]
>
> +    id := letter + upperCaseLetter + digit
> +    for len(id) < passwordSize {
> +        id += pickOne(chars)
> +    }
> +    return id
>
> s/id/password/ ?

Okay, done.

lp:~rvb/gwacl/random-pw updated on 2013-07-11
179. By Raphaël Badin on 2013-07-11

Review fixes.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'names.go'
2--- names.go 2013-07-10 16:57:25 +0000
3+++ names.go 2013-07-11 14:09:25 +0000
4@@ -14,6 +14,12 @@
5 return string(chars[index])
6 }
7
8+const (
9+ upperCaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
10+ letters = "abcdefghijklmnopqrstuvwxyz"
11+ digits = "0123456789"
12+)
13+
14 // makeRandomIdentifier creates an arbitrary identifier of the given length,
15 // consisting of only ASCII digits and lower-case ASCII letters.
16 // The identifier will start with the given prefix. The prefix must be no
17@@ -22,9 +28,7 @@
18 func makeRandomIdentifier(prefix string, length int) string {
19 // Only digits and lower-case ASCII letters are accepted.
20 const (
21- letters = "abcdefghijklmnopqrstuvwxyz"
22- digits = "0123456789"
23- chars = letters + digits
24+ chars = letters + digits
25 )
26
27 if len(prefix) > length {
28@@ -122,3 +126,28 @@
29 func MakeRandomVirtualNetworkName(prefix string) string {
30 return makeRandomIdentifier(prefix, 20)
31 }
32+
33+const (
34+ // Valid passwords must be 6-72 characters long.
35+ passwordSize = 50
36+)
37+
38+// MakeRandomPassword generates a pseudo-random password for a Linux Virtual
39+// Machine.
40+//
41+// Be sure your random generator is initialized, or this will always produce
42+// the same sequence!
43+func MakeRandomPassword() string {
44+ const chars = letters + digits + upperCaseLetters
45+
46+ upperCaseLetter := pickOne(upperCaseLetters)
47+ letter := pickOne(letters)
48+ digit := pickOne(digits)
49+ // Make sure the password has at least one letter, one upper-case letter
50+ // and a digit to meet Azure's password complexity requirements.
51+ password := letter + upperCaseLetter + digit
52+ for len(password) < passwordSize {
53+ password += pickOne(chars)
54+ }
55+ return password
56+}
57
58=== modified file 'names_test.go'
59--- names_test.go 2013-07-10 16:57:25 +0000
60+++ names_test.go 2013-07-11 14:09:25 +0000
61@@ -6,6 +6,7 @@
62 import (
63 . "launchpad.net/gocheck"
64 "math/rand"
65+ "strings"
66 )
67
68 type namesSuite struct{}
69@@ -73,3 +74,23 @@
70 func (*namesSuite) TestMakeRandomVirtualNetworkName(c *C) {
71 c.Check(MakeRandomVirtualNetworkName(""), Not(HasLen), 0)
72 }
73+
74+func assertIsAzureValidPassword(c *C, password string) {
75+ c.Check(MakeRandomPassword(), HasLen, passwordSize)
76+ if !strings.ContainsAny(password, upperCaseLetters) {
77+ c.Errorf("Password %v does not contain a single upper-case letter!", password)
78+ }
79+ if !strings.ContainsAny(password, letters) {
80+ c.Errorf("Password %v does not contain a single lower-case letter!", password)
81+ }
82+ if !strings.ContainsAny(password, digits) {
83+ c.Errorf("Password %v does not contain a single digit!", password)
84+ }
85+}
86+
87+func (*namesSuite) TestMakeRandomPassword(c *C) {
88+ for index := 0; index < 100; index += 1 {
89+ password := MakeRandomPassword()
90+ assertIsAzureValidPassword(c, password)
91+ }
92+}

Subscribers

People subscribed via source and target branches

to all changes: