Debian Codegraph @

cmd/nebula-cert/print_test.go

raw
1 package main
2
3 import (
4 "bytes"
5 "crypto/ed25519"
6 "crypto/rand"
7 "encoding/hex"
8 "net/netip"
9 "os"
10 "testing"
11 "time"
12
13 "github.com/slackhq/nebula/cert"
14 "github.com/stretchr/testify/assert"
15 "github.com/stretchr/testify/require"
16 )
17
18 func Test_printSummary(t *testing.T) {
19 assert.Equal(t, "print <flags>: prints details about a certificate", printSummary())
20 }
21
22 func Test_printHelp(t *testing.T) {
23 ob := &bytes.Buffer{}
24 printHelp(ob)
25 assert.Equal(
26 t,
27 "Usage of "+os.Args[0]+" print <flags>: prints details about a certificate\n"+
28 " Pass \"-\" to any path flag to read from stdin or write to stdout.\n"+
29 " -json\n"+
30 " \tOptional: outputs certificates in json format\n"+
31 " -out-qr string\n"+
32 " \tOptional: output a qr code image (png) of the certificate\n"+
33 " -path string\n"+
34 " \tRequired: path to the certificate\n",
35 ob.String(),
36 )
37 }
38
39 func Test_printCert(t *testing.T) {
40 // Orient our local time and avoid headaches
41 time.Local = time.UTC
42 ob := &bytes.Buffer{}
43 eb := &bytes.Buffer{}
44
45 // no path
46 err := printCert([]string{}, ob, eb)
47 assert.Empty(t, ob.String())
48 assert.Empty(t, eb.String())
49 assertHelpError(t, err, "-path is required")
50
51 // no cert at path
52 ob.Reset()
53 eb.Reset()
54 err = printCert([]string{"-path", "does_not_exist"}, ob, eb)
55 assert.Empty(t, ob.String())
56 assert.Empty(t, eb.String())
57 require.EqualError(t, err, "unable to read cert; open does_not_exist: "+NoSuchFileError)
58
59 // invalid cert at path
60 ob.Reset()
61 eb.Reset()
62 tf, err := os.CreateTemp("", "print-cert")
63 require.NoError(t, err)
64 defer os.Remove(tf.Name())
65
66 tf.WriteString("-----BEGIN NOPE-----")
67 err = printCert([]string{"-path", tf.Name()}, ob, eb)
68 assert.Empty(t, ob.String())
69 assert.Empty(t, eb.String())
70 require.EqualError(t, err, "error while unmarshaling cert: input did not contain a valid PEM encoded block")
71
72 // test multiple certs
73 ob.Reset()
74 eb.Reset()
75 tf.Truncate(0)
76 tf.Seek(0, 0)
77 ca, caKey := NewTestCaCert("test ca", nil, nil, time.Time{}, time.Time{}, nil, nil, nil)
78 c, _ := NewTestCert(ca, caKey, "test", time.Time{}, time.Time{}, []netip.Prefix{netip.MustParsePrefix("10.0.0.123/8")}, nil, []string{"hi"})
79
80 p, _ := c.MarshalPEM()
81 tf.Write(p)
82 tf.Write(p)
83 tf.Write(p)
84
85 err = printCert([]string{"-path", tf.Name()}, ob, eb)
86 fp, _ := c.Fingerprint()
87 pk := hex.EncodeToString(c.PublicKey())
88 sig := hex.EncodeToString(c.Signature())
89 require.NoError(t, err)
90 assert.Equal(
91 t,
92 //"NebulaCertificate {\n\tDetails {\n\t\tName: test\n\t\tIps: []\n\t\tSubnets: []\n\t\tGroups: [\n\t\t\t\"hi\"\n\t\t]\n\t\tNot before: 0001-01-01 00:00:00 +0000 UTC\n\t\tNot After: 0001-01-01 00:00:00 +0000 UTC\n\t\tIs CA: false\n\t\tIssuer: "+c.Issuer()+"\n\t\tPublic key: "+pk+"\n\t\tCurve: CURVE25519\n\t}\n\tFingerprint: "+fp+"\n\tSignature: "+sig+"\n}\nNebulaCertificate {\n\tDetails {\n\t\tName: test\n\t\tIps: []\n\t\tSubnets: []\n\t\tGroups: [\n\t\t\t\"hi\"\n\t\t]\n\t\tNot before: 0001-01-01 00:00:00 +0000 UTC\n\t\tNot After: 0001-01-01 00:00:00 +0000 UTC\n\t\tIs CA: false\n\t\tIssuer: "+c.Issuer()+"\n\t\tPublic key: "+pk+"\n\t\tCurve: CURVE25519\n\t}\n\tFingerprint: "+fp+"\n\tSignature: "+sig+"\n}\nNebulaCertificate {\n\tDetails {\n\t\tName: test\n\t\tIps: []\n\t\tSubnets: []\n\t\tGroups: [\n\t\t\t\"hi\"\n\t\t]\n\t\tNot before: 0001-01-01 00:00:00 +0000 UTC\n\t\tNot After: 0001-01-01 00:00:00 +0000 UTC\n\t\tIs CA: false\n\t\tIssuer: "+c.Issuer()+"\n\t\tPublic key: "+pk+"\n\t\tCurve: CURVE25519\n\t}\n\tFingerprint: "+fp+"\n\tSignature: "+sig+"\n}\n",
93 `{
94 "details": {
95 "curve": "CURVE25519",
96 "groups": [
97 "hi"
98 ],
99 "isCa": false,
100 "issuer": "`+c.Issuer()+`",
101 "name": "test",
102 "networks": [
103 "10.0.0.123/8"
104 ],
105 "notAfter": "0001-01-01T00:00:00Z",
106 "notBefore": "0001-01-01T00:00:00Z",
107 "publicKey": "`+pk+`",
108 "unsafeNetworks": []
109 },
110 "fingerprint": "`+fp+`",
111 "signature": "`+sig+`",
112 "version": 1
113 }
114 {
115 "details": {
116 "curve": "CURVE25519",
117 "groups": [
118 "hi"
119 ],
120 "isCa": false,
121 "issuer": "`+c.Issuer()+`",
122 "name": "test",
123 "networks": [
124 "10.0.0.123/8"
125 ],
126 "notAfter": "0001-01-01T00:00:00Z",
127 "notBefore": "0001-01-01T00:00:00Z",
128 "publicKey": "`+pk+`",
129 "unsafeNetworks": []
130 },
131 "fingerprint": "`+fp+`",
132 "signature": "`+sig+`",
133 "version": 1
134 }
135 {
136 "details": {
137 "curve": "CURVE25519",
138 "groups": [
139 "hi"
140 ],
141 "isCa": false,
142 "issuer": "`+c.Issuer()+`",
143 "name": "test",
144 "networks": [
145 "10.0.0.123/8"
146 ],
147 "notAfter": "0001-01-01T00:00:00Z",
148 "notBefore": "0001-01-01T00:00:00Z",
149 "publicKey": "`+pk+`",
150 "unsafeNetworks": []
151 },
152 "fingerprint": "`+fp+`",
153 "signature": "`+sig+`",
154 "version": 1
155 }
156 `,
157 ob.String(),
158 )
159 assert.Empty(t, eb.String())
160
161 // test json
162 ob.Reset()
163 eb.Reset()
164 tf.Truncate(0)
165 tf.Seek(0, 0)
166 tf.Write(p)
167 tf.Write(p)
168 tf.Write(p)
169
170 err = printCert([]string{"-json", "-path", tf.Name()}, ob, eb)
171 fp, _ = c.Fingerprint()
172 pk = hex.EncodeToString(c.PublicKey())
173 sig = hex.EncodeToString(c.Signature())
174 require.NoError(t, err)
175 assert.Equal(
176 t,
177 `[{"details":{"curve":"CURVE25519","groups":["hi"],"isCa":false,"issuer":"`+c.Issuer()+`","name":"test","networks":["10.0.0.123/8"],"notAfter":"0001-01-01T00:00:00Z","notBefore":"0001-01-01T00:00:00Z","publicKey":"`+pk+`","unsafeNetworks":[]},"fingerprint":"`+fp+`","signature":"`+sig+`","version":1},{"details":{"curve":"CURVE25519","groups":["hi"],"isCa":false,"issuer":"`+c.Issuer()+`","name":"test","networks":["10.0.0.123/8"],"notAfter":"0001-01-01T00:00:00Z","notBefore":"0001-01-01T00:00:00Z","publicKey":"`+pk+`","unsafeNetworks":[]},"fingerprint":"`+fp+`","signature":"`+sig+`","version":1},{"details":{"curve":"CURVE25519","groups":["hi"],"isCa":false,"issuer":"`+c.Issuer()+`","name":"test","networks":["10.0.0.123/8"],"notAfter":"0001-01-01T00:00:00Z","notBefore":"0001-01-01T00:00:00Z","publicKey":"`+pk+`","unsafeNetworks":[]},"fingerprint":"`+fp+`","signature":"`+sig+`","version":1}]
178 `,
179 ob.String(),
180 )
181 assert.Empty(t, eb.String())
182
183 // read cert from stdin
184 ob.Reset()
185 eb.Reset()
186 withStdin(t, bytes.NewReader(p))
187 err = printCert([]string{"-json", "-path", "-"}, ob, eb)
188 require.NoError(t, err)
189 assert.Equal(
190 t,
191 `[{"details":{"curve":"CURVE25519","groups":["hi"],"isCa":false,"issuer":"`+c.Issuer()+`","name":"test","networks":["10.0.0.123/8"],"notAfter":"0001-01-01T00:00:00Z","notBefore":"0001-01-01T00:00:00Z","publicKey":"`+pk+`","unsafeNetworks":[]},"fingerprint":"`+fp+`","signature":"`+sig+`","version":1}]
192 `,
193 ob.String(),
194 )
195 assert.Empty(t, eb.String())
196
197 // -out-qr - sends only the PNG to stdout, suppressing the cert dump
198 ob.Reset()
199 eb.Reset()
200 withStdin(t, bytes.NewReader(p))
201 err = printCert([]string{"-path", "-", "-out-qr", "-"}, ob, eb)
202 require.NoError(t, err)
203 assert.Empty(t, eb.String())
204 stdout := ob.Bytes()
205 require.NotEmpty(t, stdout)
206 // PNG magic, no PEM/JSON noise prepended
207 assert.Equal(t, []byte{0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a}, stdout[:8])
208 assert.NotContains(t, string(stdout), "NebulaCertificate")
209 assert.NotContains(t, string(stdout), `"details"`)
210
211 // json + out-qr - still suppresses json
212 ob.Reset()
213 eb.Reset()
214 withStdin(t, bytes.NewReader(p))
215 err = printCert([]string{"-json", "-path", "-", "-out-qr", "-"}, ob, eb)
216 require.NoError(t, err)
217 assert.Empty(t, eb.String())
218 assert.Equal(t, []byte{0x89, 'P', 'N', 'G'}, ob.Bytes()[:4])
219 assert.NotContains(t, ob.String(), `"details"`)
220 }
221
222 // NewTestCaCert will generate a CA cert
223 func NewTestCaCert(name string, pubKey, privKey []byte, before, after time.Time, networks, unsafeNetworks []netip.Prefix, groups []string) (cert.Certificate, []byte) {
224 var err error
225 if pubKey == nil || privKey == nil {
226 pubKey, privKey, err = ed25519.GenerateKey(rand.Reader)
227 if err != nil {
228 panic(err)
229 }
230 }
231
232 t := &cert.TBSCertificate{
233 Version: cert.Version1,
234 Name: name,
235 NotBefore: time.Unix(before.Unix(), 0),
236 NotAfter: time.Unix(after.Unix(), 0),
237 PublicKey: pubKey,
238 Networks: networks,
239 UnsafeNetworks: unsafeNetworks,
240 Groups: groups,
241 IsCA: true,
242 }
243
244 c, err := t.Sign(nil, cert.Curve_CURVE25519, privKey)
245 if err != nil {
246 panic(err)
247 }
248
249 return c, privKey
250 }
251
252 func NewTestCert(ca cert.Certificate, signerKey []byte, name string, before, after time.Time, networks, unsafeNetworks []netip.Prefix, groups []string) (cert.Certificate, []byte) {
253 if before.IsZero() {
254 before = ca.NotBefore()
255 }
256
257 if after.IsZero() {
258 after = ca.NotAfter()
259 }
260
261 if len(networks) == 0 {
262 networks = []netip.Prefix{netip.MustParsePrefix("10.0.0.123/8")}
263 }
264
265 pub, rawPriv := x25519Keypair()
266 nc := &cert.TBSCertificate{
267 Version: cert.Version1,
268 Name: name,
269 Networks: networks,
270 UnsafeNetworks: unsafeNetworks,
271 Groups: groups,
272 NotBefore: time.Unix(before.Unix(), 0),
273 NotAfter: time.Unix(after.Unix(), 0),
274 PublicKey: pub,
275 IsCA: false,
276 }
277
278 c, err := nc.Sign(ca, ca.Curve(), signerKey)
279 if err != nil {
280 panic(err)
281 }
282
283 return c, rawPriv
284 }