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
=== modified file 'names.go'
--- names.go 2013-07-10 16:57:25 +0000
+++ names.go 2013-07-11 14:09:25 +0000
@@ -14,6 +14,12 @@
14 return string(chars[index])14 return string(chars[index])
15}15}
1616
17const (
18 upperCaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
19 letters = "abcdefghijklmnopqrstuvwxyz"
20 digits = "0123456789"
21)
22
17// makeRandomIdentifier creates an arbitrary identifier of the given length,23// makeRandomIdentifier creates an arbitrary identifier of the given length,
18// consisting of only ASCII digits and lower-case ASCII letters.24// consisting of only ASCII digits and lower-case ASCII letters.
19// The identifier will start with the given prefix. The prefix must be no25// The identifier will start with the given prefix. The prefix must be no
@@ -22,9 +28,7 @@
22func makeRandomIdentifier(prefix string, length int) string {28func makeRandomIdentifier(prefix string, length int) string {
23 // Only digits and lower-case ASCII letters are accepted.29 // Only digits and lower-case ASCII letters are accepted.
24 const (30 const (
25 letters = "abcdefghijklmnopqrstuvwxyz"31 chars = letters + digits
26 digits = "0123456789"
27 chars = letters + digits
28 )32 )
2933
30 if len(prefix) > length {34 if len(prefix) > length {
@@ -122,3 +126,28 @@
122func MakeRandomVirtualNetworkName(prefix string) string {126func MakeRandomVirtualNetworkName(prefix string) string {
123 return makeRandomIdentifier(prefix, 20)127 return makeRandomIdentifier(prefix, 20)
124}128}
129
130const (
131 // Valid passwords must be 6-72 characters long.
132 passwordSize = 50
133)
134
135// MakeRandomPassword generates a pseudo-random password for a Linux Virtual
136// Machine.
137//
138// Be sure your random generator is initialized, or this will always produce
139// the same sequence!
140func MakeRandomPassword() string {
141 const chars = letters + digits + upperCaseLetters
142
143 upperCaseLetter := pickOne(upperCaseLetters)
144 letter := pickOne(letters)
145 digit := pickOne(digits)
146 // Make sure the password has at least one letter, one upper-case letter
147 // and a digit to meet Azure's password complexity requirements.
148 password := letter + upperCaseLetter + digit
149 for len(password) < passwordSize {
150 password += pickOne(chars)
151 }
152 return password
153}
125154
=== modified file 'names_test.go'
--- names_test.go 2013-07-10 16:57:25 +0000
+++ names_test.go 2013-07-11 14:09:25 +0000
@@ -6,6 +6,7 @@
6import (6import (
7 . "launchpad.net/gocheck"7 . "launchpad.net/gocheck"
8 "math/rand"8 "math/rand"
9 "strings"
9)10)
1011
11type namesSuite struct{}12type namesSuite struct{}
@@ -73,3 +74,23 @@
73func (*namesSuite) TestMakeRandomVirtualNetworkName(c *C) {74func (*namesSuite) TestMakeRandomVirtualNetworkName(c *C) {
74 c.Check(MakeRandomVirtualNetworkName(""), Not(HasLen), 0)75 c.Check(MakeRandomVirtualNetworkName(""), Not(HasLen), 0)
75}76}
77
78func assertIsAzureValidPassword(c *C, password string) {
79 c.Check(MakeRandomPassword(), HasLen, passwordSize)
80 if !strings.ContainsAny(password, upperCaseLetters) {
81 c.Errorf("Password %v does not contain a single upper-case letter!", password)
82 }
83 if !strings.ContainsAny(password, letters) {
84 c.Errorf("Password %v does not contain a single lower-case letter!", password)
85 }
86 if !strings.ContainsAny(password, digits) {
87 c.Errorf("Password %v does not contain a single digit!", password)
88 }
89}
90
91func (*namesSuite) TestMakeRandomPassword(c *C) {
92 for index := 0; index < 100; index += 1 {
93 password := MakeRandomPassword()
94 assertIsAzureValidPassword(c, password)
95 }
96}

Subscribers

People subscribed via source and target branches

to all changes: