-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_test.go
257 lines (217 loc) · 9.27 KB
/
server_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
// 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 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("ParseServerTokenWithKeyfile", func() {
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("could not parse server id token: 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("could not parse server id token: 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"))
})
})
})