]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | package acctest |
2 | ||
3 | import ( | |
4 | "bufio" | |
5 | "bytes" | |
6 | crand "crypto/rand" | |
7 | "crypto/rsa" | |
8 | "crypto/x509" | |
9 | "encoding/pem" | |
10 | "fmt" | |
11 | "math/rand" | |
12 | "strings" | |
13 | "time" | |
14 | ||
15 | "golang.org/x/crypto/ssh" | |
16 | ) | |
17 | ||
18 | // Helpers for generating random tidbits for use in identifiers to prevent | |
19 | // collisions in acceptance tests. | |
20 | ||
21 | // RandInt generates a random integer | |
22 | func RandInt() int { | |
23 | reseed() | |
24 | return rand.New(rand.NewSource(time.Now().UnixNano())).Int() | |
25 | } | |
26 | ||
27 | // RandomWithPrefix is used to generate a unique name with a prefix, for | |
28 | // randomizing names in acceptance tests | |
29 | func RandomWithPrefix(name string) string { | |
30 | reseed() | |
31 | return fmt.Sprintf("%s-%d", name, rand.New(rand.NewSource(time.Now().UnixNano())).Int()) | |
32 | } | |
33 | ||
34 | func RandIntRange(min int, max int) int { | |
35 | reseed() | |
36 | source := rand.New(rand.NewSource(time.Now().UnixNano())) | |
37 | rangeMax := max - min | |
38 | ||
39 | return int(source.Int31n(int32(rangeMax))) | |
40 | } | |
41 | ||
42 | // RandString generates a random alphanumeric string of the length specified | |
43 | func RandString(strlen int) string { | |
44 | return RandStringFromCharSet(strlen, CharSetAlphaNum) | |
45 | } | |
46 | ||
47 | // RandStringFromCharSet generates a random string by selecting characters from | |
48 | // the charset provided | |
49 | func RandStringFromCharSet(strlen int, charSet string) string { | |
50 | reseed() | |
51 | result := make([]byte, strlen) | |
52 | for i := 0; i < strlen; i++ { | |
53 | result[i] = charSet[rand.Intn(len(charSet))] | |
54 | } | |
55 | return string(result) | |
56 | } | |
57 | ||
58 | // RandSSHKeyPair generates a public and private SSH key pair. The public key is | |
59 | // returned in OpenSSH format, and the private key is PEM encoded. | |
60 | func RandSSHKeyPair(comment string) (string, string, error) { | |
61 | privateKey, err := rsa.GenerateKey(crand.Reader, 1024) | |
62 | if err != nil { | |
63 | return "", "", err | |
64 | } | |
65 | ||
66 | var privateKeyBuffer bytes.Buffer | |
67 | privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)} | |
68 | if err := pem.Encode(bufio.NewWriter(&privateKeyBuffer), privateKeyPEM); err != nil { | |
69 | return "", "", err | |
70 | } | |
71 | ||
72 | publicKey, err := ssh.NewPublicKey(&privateKey.PublicKey) | |
73 | if err != nil { | |
74 | return "", "", err | |
75 | } | |
76 | keyMaterial := strings.TrimSpace(string(ssh.MarshalAuthorizedKey(publicKey))) | |
77 | return fmt.Sprintf("%s %s", keyMaterial, comment), privateKeyBuffer.String(), nil | |
78 | } | |
79 | ||
80 | // Seeds random with current timestamp | |
81 | func reseed() { | |
82 | rand.Seed(time.Now().UTC().UnixNano()) | |
83 | } | |
84 | ||
85 | const ( | |
86 | // CharSetAlphaNum is the alphanumeric character set for use with | |
87 | // RandStringFromCharSet | |
88 | CharSetAlphaNum = "abcdefghijklmnopqrstuvwxyz012346789" | |
89 | ||
90 | // CharSetAlpha is the alphabetical character set for use with | |
91 | // RandStringFromCharSet | |
92 | CharSetAlpha = "abcdefghijklmnopqrstuvwxyz" | |
93 | ) |