-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver_test.go
More file actions
351 lines (296 loc) · 12.8 KB
/
Copy pathserver_test.go
File metadata and controls
351 lines (296 loc) · 12.8 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright (c) 2021-2023, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package tokens
import (
"crypto/ed25519"
"crypto/rand"
"encoding/hex"
"os"
"runtime"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("ServerClaims", func() {
var (
pubK ed25519.PublicKey
priK ed25519.PrivateKey
err error
)
BeforeEach(func() {
pubK, priK, err = ed25519.GenerateKey(rand.Reader)
Expect(err).ToNot(HaveOccurred())
})
Describe("NewServerClaims", func() {
It("Should require identity", func() {
_, err := NewServerClaims("", nil, "", nil, nil, nil, "", 0)
Expect(err).To(MatchError("identity is required"))
})
It("Should require collectives", func() {
_, err := NewServerClaims("ginkgo.example.net", nil, "", nil, nil, nil, "", 0)
Expect(err).To(MatchError("at least one collective is required"))
})
It("Should require public key", func() {
_, err := NewServerClaims("ginkgo.example.net", []string{"choria"}, "", nil, nil, nil, "", 0)
Expect(err).To(MatchError("public key is required"))
})
It("Should require validity", func() {
_, err := NewServerClaims("ginkgo.example.net", []string{"choria"}, "", nil, nil, pubK, "", 0)
Expect(err).To(MatchError("validity is required"))
})
It("Should create a valid token", func() {
perms := &ServerPermissions{Submission: true}
claims, err := NewServerClaims("ginkgo.example.net", []string{"choria"}, "ginkgo_org", perms, []string{"choria.registration"}, pubK, "ginkgo issuer", 365*24*time.Hour)
Expect(err).ToNot(HaveOccurred())
Expect(claims.ChoriaIdentity).To(Equal("ginkgo.example.net"))
Expect(claims.Purpose).To(Equal(ServerPurpose))
Expect(claims.Permissions.Submission).To(BeTrue())
Expect(claims.Collectives).To(Equal([]string{"choria"}))
Expect(claims.PublicKey).To(Equal(hex.EncodeToString(pubK)))
Expect(claims.OrganizationUnit).To(Equal("ginkgo_org"))
Expect(claims.Issuer).To(Equal("ginkgo issuer"))
Expect(claims.AdditionalPublishSubjects).To(Equal([]string{"choria.registration"}))
Expect(claims.IssuedAt.Time).To(BeTemporally("~", time.Now(), time.Second))
Expect(claims.ExpiresAt.Time).To(BeTemporally("~", time.Now().Add(365*24*time.Hour), time.Second))
})
})
Describe("IsMatchingPublicKey", func() {
var claims *ServerClaims
var err error
BeforeEach(func() {
perms := &ServerPermissions{Submission: true}
claims, err = NewServerClaims("ginkgo.example.net", []string{"choria"}, "ginkgo_org", perms, []string{"choria.registration"}, pubK, "ginkgo issuer", 365*24*time.Hour)
Expect(err).ToNot(HaveOccurred())
})
It("Should detect incorrect length public keys", func() {
match, err := claims.IsMatchingPublicKey(nil)
Expect(match).To(BeFalse())
Expect(err).To(MatchError("invalid size for public key"))
claims.PublicKey = claims.PublicKey[2:]
match, err = claims.IsMatchingPublicKey(pubK)
Expect(match).To(BeFalse())
Expect(err).To(MatchError("invalid size for token stored public key"))
claims.PublicKey = ""
match, err = claims.IsMatchingPublicKey(pubK)
Expect(match).To(BeFalse())
Expect(err).To(MatchError("no public key stored in the JWT"))
})
It("Should fail for invalid public keys", func() {
pK, _, err := ed25519.GenerateKey(rand.Reader)
Expect(err).ToNot(HaveOccurred())
match, err := claims.IsMatchingPublicKey(pK)
Expect(err).ToNot(HaveOccurred())
Expect(match).To(BeFalse())
})
It("Should match correct keys", func() {
match, err := claims.IsMatchingPublicKey(pubK)
Expect(err).ToNot(HaveOccurred())
Expect(match).To(BeTrue())
})
})
Describe("IsMatchingSeedFile", func() {
var claims *ServerClaims
var err error
BeforeEach(func() {
perms := &ServerPermissions{Submission: true}
claims, err = NewServerClaims("ginkgo.example.net", []string{"choria"}, "ginkgo_org", perms, []string{"choria.registration"}, pubK, "ginkgo issuer", 365*24*time.Hour)
Expect(err).ToNot(HaveOccurred())
})
It("Should fail for invalid files", func() {
match, err := claims.IsMatchingSeedFile("/nonexisting")
Expect(match).To(BeFalse())
if runtime.GOOS == "windows" {
Expect(err).To(HaveOccurred())
} else {
Expect(err).To(MatchError("open /nonexisting: no such file or directory"))
}
tf, err := os.CreateTemp("", "")
Expect(err).ToNot(HaveOccurred())
defer os.Remove(tf.Name())
tf.WriteString("x")
tf.Close()
match, err = claims.IsMatchingSeedFile(tf.Name())
Expect(match).To(BeFalse())
Expect(err).To(HaveOccurred())
})
It("Should fail for invalid seeds", func() {
_, priK, err := ed25519.GenerateKey(rand.Reader)
Expect(err).ToNot(HaveOccurred())
tf, err := os.CreateTemp("", "")
Expect(err).ToNot(HaveOccurred())
defer os.Remove(tf.Name())
_, err = tf.Write([]byte(hex.EncodeToString(priK.Seed())))
Expect(err).ToNot(HaveOccurred())
tf.Close()
match, err := claims.IsMatchingSeedFile(tf.Name())
Expect(err).ToNot(HaveOccurred())
Expect(match).To(BeFalse())
})
It("Should succeed for correct seeds", func() {
tf, err := os.CreateTemp("", "")
Expect(err).ToNot(HaveOccurred())
defer os.Remove(tf.Name())
_, err = tf.Write([]byte(hex.EncodeToString(priK.Seed())))
Expect(err).ToNot(HaveOccurred())
tf.Close()
match, err := claims.IsMatchingSeedFile(tf.Name())
Expect(err).ToNot(HaveOccurred())
Expect(match).To(BeTrue())
})
})
Describe("IsServerTokenString", func() {
It("Should detect correctly", func() {
pt, err := os.ReadFile("testdata/rsa/good-provisioning.jwt")
Expect(err).ToNot(HaveOccurred())
Expect(IsServerTokenString(string(pt))).To(BeFalse())
perms := &ServerPermissions{Submission: true}
claims, err := NewServerClaims("ginkgo.example.net", []string{"choria"}, "ginkgo_org", perms, nil, pubK, "ginkgo issuer", 365*24*time.Hour)
Expect(err).ToNot(HaveOccurred())
signed, err := SignTokenWithKeyFile(claims, "testdata/rsa/signer-key.pem")
Expect(err).ToNot(HaveOccurred())
Expect(IsServerTokenString(signed)).To(BeTrue())
})
})
Describe("IsServerToken", func() {
It("Should detect correctly", func() {
Expect(IsServerToken(StandardClaims{})).To(BeFalse())
Expect(IsServerToken(StandardClaims{Purpose: ServerPurpose})).To(BeTrue())
})
})
Describe("ParseServerTokenUnverified", func() {
It("Should fail for wrong kinds of tokens", func() {
pt, err := os.ReadFile("testdata/rsa/good-provisioning.jwt")
Expect(err).ToNot(HaveOccurred())
_, err = ParseServerTokenUnverified(string(pt))
Expect(err).To(MatchError("not a server token"))
})
It("Should parse valid tokens", func() {
perms := &ServerPermissions{Submission: true}
claims, err := NewServerClaims("ginkgo.example.net", []string{"choria"}, "ginkgo_org", perms, nil, pubK, "ginkgo issuer", 365*24*time.Hour)
Expect(err).ToNot(HaveOccurred())
signed, err := SignTokenWithKeyFile(claims, "testdata/rsa/signer-key.pem")
Expect(err).ToNot(HaveOccurred())
claims = nil
claims, err = ParseServerTokenUnverified(signed)
Expect(err).ToNot(HaveOccurred())
Expect(claims.ChoriaIdentity).To(Equal("ginkgo.example.net"))
})
})
Describe("UnverifiedIdentityFromServerToken", func() {
It("Should fail for invalid tokens", func() {
_, _, err := UnverifiedIdentityFromServerToken("invalid")
Expect(err).To(HaveOccurred())
})
It("Should fail for non-server tokens", func() {
pt, err := os.ReadFile("testdata/rsa/good-provisioning.jwt")
Expect(err).ToNot(HaveOccurred())
_, _, err = UnverifiedIdentityFromServerToken(string(pt))
Expect(err).To(MatchError(ErrNotAServerToken))
})
It("Should fail for empty identity", func() {
claims, err := NewServerClaims("ginkgo.example.net", []string{"choria"}, "ginkgo_org", nil, nil, pubK, "ginkgo issuer", 365*24*time.Hour)
Expect(err).ToNot(HaveOccurred())
claims.ChoriaIdentity = ""
signed, err := SignToken(claims, priK)
Expect(err).ToNot(HaveOccurred())
_, _, err = UnverifiedIdentityFromServerToken(signed)
Expect(err).To(MatchError("invalid identity in token"))
})
It("Should extract identity from valid server tokens", func() {
perms := &ServerPermissions{Submission: true}
claims, err := NewServerClaims("ginkgo.example.net", []string{"choria"}, "ginkgo_org", perms, nil, pubK, "ginkgo issuer", 365*24*time.Hour)
Expect(err).ToNot(HaveOccurred())
signed, err := SignToken(claims, priK)
Expect(err).ToNot(HaveOccurred())
t, identity, err := UnverifiedIdentityFromServerToken(signed)
Expect(err).ToNot(HaveOccurred())
Expect(identity).To(Equal("ginkgo.example.net"))
Expect(t).ToNot(BeNil())
})
})
Describe("ParseServerTokenFileUnverified", func() {
It("Should fail for missing files", func() {
_, err := ParseServerTokenFileUnverified("/nonexisting")
Expect(err).To(HaveOccurred())
})
It("Should fail for non-server tokens", func() {
_, err := ParseServerTokenFileUnverified("testdata/rsa/good-provisioning.jwt")
Expect(err).To(MatchError(ErrNotAServerToken))
})
It("Should parse valid server token files", func() {
perms := &ServerPermissions{Submission: true}
claims, err := NewServerClaims("ginkgo.example.net", []string{"choria"}, "ginkgo_org", perms, nil, pubK, "ginkgo issuer", 365*24*time.Hour)
Expect(err).ToNot(HaveOccurred())
signed, err := SignToken(claims, priK)
Expect(err).ToNot(HaveOccurred())
tf, err := os.CreateTemp("", "")
Expect(err).ToNot(HaveOccurred())
defer os.Remove(tf.Name())
_, err = tf.WriteString(signed)
Expect(err).ToNot(HaveOccurred())
tf.Close()
parsed, err := ParseServerTokenFileUnverified(tf.Name())
Expect(err).ToNot(HaveOccurred())
Expect(parsed.ChoriaIdentity).To(Equal("ginkgo.example.net"))
Expect(parsed.Permissions.Submission).To(BeTrue())
})
})
Describe("ParseServerToken", func() {
It("Should fail for non-server tokens", func() {
pt, err := os.ReadFile("testdata/rsa/good-provisioning.jwt")
Expect(err).ToNot(HaveOccurred())
_, err = ParseServerToken(string(pt), loadRSAPubKey("testdata/rsa/signer-public.pem"))
Expect(err).To(MatchError(ContainSubstring("not a server token")))
})
})
Describe("ParseServerTokenWithKeyfile", func() {
It("Should fail for empty key file path", func() {
_, err := ParseServerTokenWithKeyfile("token", "")
Expect(err).To(MatchError("invalid public key file"))
})
It("Should fail for nonexistent key file", func() {
_, err := ParseServerTokenWithKeyfile("token", "/nonexisting")
Expect(err).To(MatchError(ContainSubstring("could not read validation certificate")))
})
It("Should fail for invalid rsa tokens", func() {
perms := &ServerPermissions{Submission: true}
claims, err := NewServerClaims("ginkgo.example.net", []string{"choria"}, "ginkgo_org", perms, nil, pubK, "ginkgo issuer", 365*24*time.Hour)
Expect(err).ToNot(HaveOccurred())
signed, err := SignTokenWithKeyFile(claims, "testdata/rsa/signer-key.pem")
Expect(err).ToNot(HaveOccurred())
_, err = ParseServerTokenWithKeyfile(signed, "testdata/rsa/other-public.pem")
Expect(err).To(MatchError(ContainSubstring("crypto/rsa: verification error")))
})
It("Should fail for invalid ed25519 tokens", func() {
perms := &ServerPermissions{Submission: true}
claims, err := NewServerClaims("ginkgo.example.net", []string{"choria"}, "ginkgo_org", perms, nil, pubK, "ginkgo issuer", 365*24*time.Hour)
Expect(err).ToNot(HaveOccurred())
signed, err := SignTokenWithKeyFile(claims, "testdata/ed25519/signer.seed")
Expect(err).ToNot(HaveOccurred())
_, err = ParseServerTokenWithKeyfile(signed, "testdata/ed25519/other.public")
Expect(err).To(MatchError(ContainSubstring("ed25519: verification error")))
})
It("Should parse valid rsa tokens", func() {
perms := &ServerPermissions{Submission: true}
claims, err := NewServerClaims("ginkgo.example.net", []string{"choria"}, "ginkgo_org", perms, []string{"additional.subject"}, pubK, "ginkgo issuer", 365*24*time.Hour)
Expect(err).ToNot(HaveOccurred())
signed, err := SignTokenWithKeyFile(claims, "testdata/rsa/signer-key.pem")
Expect(err).ToNot(HaveOccurred())
claims = nil
claims, err = ParseServerTokenWithKeyfile(signed, "testdata/rsa/signer-public.pem")
Expect(err).ToNot(HaveOccurred())
Expect(claims.ChoriaIdentity).To(Equal("ginkgo.example.net"))
})
It("Should parse valid ed25519 tokens", func() {
perms := &ServerPermissions{Submission: true}
claims, err := NewServerClaims("ginkgo.example.net", []string{"choria"}, "ginkgo_org", perms, nil, pubK, "ginkgo issuer", 365*24*time.Hour)
Expect(err).ToNot(HaveOccurred())
signed, err := SignTokenWithKeyFile(claims, "testdata/ed25519/signer.seed")
Expect(err).ToNot(HaveOccurred())
_, err = ParseServerTokenWithKeyfile(signed, "testdata/ed25519/signer.public")
Expect(err).ToNot(HaveOccurred())
Expect(claims.ChoriaIdentity).To(Equal("ginkgo.example.net"))
})
})
})