forked from Masterminds/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_test.go
259 lines (231 loc) · 6.04 KB
/
crypto_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package sprig
import (
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
const (
beginCertificate = "-----BEGIN CERTIFICATE-----"
endCertificate = "-----END CERTIFICATE-----"
)
func TestSha256Sum(t *testing.T) {
tpl := `{{"abc" | sha256sum}}`
if err := runt(tpl, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); err != nil {
t.Error(err)
}
}
func TestSha1Sum(t *testing.T) {
tpl := `{{"abc" | sha1sum}}`
if err := runt(tpl, "a9993e364706816aba3e25717850c26c9cd0d89d"); err != nil {
t.Error(err)
}
}
func TestDerivePassword(t *testing.T) {
expectations := map[string]string{
`{{derivePassword 1 "long" "password" "user" "example.com"}}`: "ZedaFaxcZaso9*",
`{{derivePassword 2 "long" "password" "user" "example.com"}}`: "Fovi2@JifpTupx",
`{{derivePassword 1 "maximum" "password" "user" "example.com"}}`: "pf4zS1LjCg&LjhsZ7T2~",
`{{derivePassword 1 "medium" "password" "user" "example.com"}}`: "ZedJuz8$",
`{{derivePassword 1 "basic" "password" "user" "example.com"}}`: "pIS54PLs",
`{{derivePassword 1 "short" "password" "user" "example.com"}}`: "Zed5",
`{{derivePassword 1 "pin" "password" "user" "example.com"}}`: "6685",
}
for tpl, result := range expectations {
out, err := runRaw(tpl, nil)
if err != nil {
t.Error(err)
}
if 0 != strings.Compare(out, result) {
t.Error("Generated password does not match for", tpl)
}
}
}
// NOTE(bacongobbler): this test is really _slow_ because of how long it takes to compute
// and generate a new crypto key.
func TestGenPrivateKey(t *testing.T) {
// test that calling by default generates an RSA private key
tpl := `{{genPrivateKey ""}}`
out, err := runRaw(tpl, nil)
if err != nil {
t.Error(err)
}
if !strings.Contains(out, "RSA PRIVATE KEY") {
t.Error("Expected RSA PRIVATE KEY")
}
// test all acceptable arguments
tpl = `{{genPrivateKey "rsa"}}`
out, err = runRaw(tpl, nil)
if err != nil {
t.Error(err)
}
if !strings.Contains(out, "RSA PRIVATE KEY") {
t.Error("Expected RSA PRIVATE KEY")
}
tpl = `{{genPrivateKey "dsa"}}`
out, err = runRaw(tpl, nil)
if err != nil {
t.Error(err)
}
if !strings.Contains(out, "DSA PRIVATE KEY") {
t.Error("Expected DSA PRIVATE KEY")
}
tpl = `{{genPrivateKey "ecdsa"}}`
out, err = runRaw(tpl, nil)
if err != nil {
t.Error(err)
}
if !strings.Contains(out, "EC PRIVATE KEY") {
t.Error("Expected EC PRIVATE KEY")
}
// test bad
tpl = `{{genPrivateKey "bad"}}`
out, err = runRaw(tpl, nil)
if err != nil {
t.Error(err)
}
if out != "Unknown type bad" {
t.Error("Expected type 'bad' to be an unknown crypto algorithm")
}
// ensure that we can base64 encode the string
tpl = `{{genPrivateKey "rsa" | b64enc}}`
out, err = runRaw(tpl, nil)
if err != nil {
t.Error(err)
}
}
func TestUUIDGeneration(t *testing.T) {
tpl := `{{uuidv4}}`
out, err := runRaw(tpl, nil)
if err != nil {
t.Error(err)
}
if len(out) != 36 {
t.Error("Expected UUID of length 36")
}
out2, err := runRaw(tpl, nil)
if err != nil {
t.Error(err)
}
if out == out2 {
t.Error("Expected subsequent UUID generations to be different")
}
}
func TestBuildCustomCert(t *testing.T) {
ca, _ := generateCertificateAuthority("example.com", 365)
tpl := fmt.Sprintf(
`{{- $ca := buildCustomCert "%s" "%s"}}
{{- $ca.Cert }}`,
base64.StdEncoding.EncodeToString([]byte(ca.Cert)),
base64.StdEncoding.EncodeToString([]byte(ca.Key)),
)
out, err := runRaw(tpl, nil)
if err != nil {
t.Error(err)
}
tpl2 := fmt.Sprintf(
`{{- $ca := buildCustomCert "%s" "%s"}}
{{- $ca.Cert }}`,
base64.StdEncoding.EncodeToString([]byte("fail")),
base64.StdEncoding.EncodeToString([]byte(ca.Key)),
)
out2, _ := runRaw(tpl2, nil)
assert.Equal(t, out, ca.Cert)
assert.NotEqual(t, out2, ca.Cert)
}
func TestGenCA(t *testing.T) {
const cn = "foo-ca"
tpl := fmt.Sprintf(
`{{- $ca := genCA "%s" 365 }}
{{ $ca.Cert }}
`,
cn,
)
out, err := runRaw(tpl, nil)
if err != nil {
t.Error(err)
}
assert.Contains(t, out, beginCertificate)
assert.Contains(t, out, endCertificate)
decodedCert, _ := pem.Decode([]byte(out))
assert.Nil(t, err)
cert, err := x509.ParseCertificate(decodedCert.Bytes)
assert.Nil(t, err)
assert.Equal(t, cn, cert.Subject.CommonName)
assert.True(t, cert.IsCA)
}
func TestGenSelfSignedCert(t *testing.T) {
const (
cn = "foo.com"
ip1 = "10.0.0.1"
ip2 = "10.0.0.2"
dns1 = "bar.com"
dns2 = "bat.com"
)
tpl := fmt.Sprintf(
`{{- $cert := genSelfSignedCert "%s" (list "%s" "%s") (list "%s" "%s") 365 }}
{{ $cert.Cert }}`,
cn,
ip1,
ip2,
dns1,
dns2,
)
out, err := runRaw(tpl, nil)
if err != nil {
t.Error(err)
}
assert.Contains(t, out, beginCertificate)
assert.Contains(t, out, endCertificate)
decodedCert, _ := pem.Decode([]byte(out))
assert.Nil(t, err)
cert, err := x509.ParseCertificate(decodedCert.Bytes)
assert.Nil(t, err)
assert.Equal(t, cn, cert.Subject.CommonName)
assert.Equal(t, 2, len(cert.IPAddresses))
assert.Equal(t, ip1, cert.IPAddresses[0].String())
assert.Equal(t, ip2, cert.IPAddresses[1].String())
assert.Contains(t, cert.DNSNames, dns1)
assert.Contains(t, cert.DNSNames, dns2)
assert.False(t, cert.IsCA)
}
func TestGenSignedCert(t *testing.T) {
const (
cn = "foo.com"
ip1 = "10.0.0.1"
ip2 = "10.0.0.2"
dns1 = "bar.com"
dns2 = "bat.com"
)
tpl := fmt.Sprintf(
`{{- $ca := genCA "foo" 365 }}
{{- $cert := genSignedCert "%s" (list "%s" "%s") (list "%s" "%s") 365 $ca }}
{{ $cert.Cert }}
`,
cn,
ip1,
ip2,
dns1,
dns2,
)
out, err := runRaw(tpl, nil)
if err != nil {
t.Error(err)
}
assert.Contains(t, out, beginCertificate)
assert.Contains(t, out, endCertificate)
decodedCert, _ := pem.Decode([]byte(out))
assert.Nil(t, err)
cert, err := x509.ParseCertificate(decodedCert.Bytes)
assert.Nil(t, err)
assert.Equal(t, cn, cert.Subject.CommonName)
assert.Equal(t, 2, len(cert.IPAddresses))
assert.Equal(t, ip1, cert.IPAddresses[0].String())
assert.Equal(t, ip2, cert.IPAddresses[1].String())
assert.Contains(t, cert.DNSNames, dns1)
assert.Contains(t, cert.DNSNames, dns2)
assert.False(t, cert.IsCA)
}