File: bundle_test.go

package info (click to toggle)
golang-github-spiffe-go-spiffe 2.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,116 kB
  • sloc: makefile: 157
file content (319 lines) | stat: -rw-r--r-- 7,832 bytes parent folder | download | duplicates (2)
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
package jwtbundle_test

import (
	"crypto"
	"os"
	"testing"

	"github.com/spiffe/go-spiffe/v2/bundle/jwtbundle"
	"github.com/spiffe/go-spiffe/v2/internal/test"
	"github.com/spiffe/go-spiffe/v2/internal/test/errstrings"
	"github.com/spiffe/go-spiffe/v2/spiffeid"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

type testFile struct {
	filePath  string
	keysCount int
}

var (
	td        = spiffeid.RequireTrustDomainFromString("example.org")
	testFiles = map[string]testFile{
		"valid 1": {
			filePath:  "testdata/jwks_valid_1.json",
			keysCount: 1,
		},
		"valid 2": {
			filePath:  "testdata/jwks_valid_2.json",
			keysCount: 2,
		},
		"non existent file": {
			filePath: "testdata/does-not-exist.json",
		},
		"missing kid": {
			filePath: "testdata/jwks_missing_kid.json",
		},
	}
)

func TestNew(t *testing.T) {
	b := jwtbundle.New(td)
	require.NotNil(t, b)
	require.Len(t, b.JWTAuthorities(), 0)
	require.Equal(t, td, b.TrustDomain())
}

func TestFromJWTAuthorities(t *testing.T) {
	jwtAuthorities := map[string]crypto.PublicKey{
		"key-1": "test-1",
		"key-2": "test-2",
	}
	b := jwtbundle.FromJWTAuthorities(td, jwtAuthorities)
	require.NotNil(t, b)
	assert.Equal(t, b.JWTAuthorities(), jwtAuthorities)
}

func TestLoad(t *testing.T) {
	testCases := []struct {
		tf  testFile
		err string
	}{
		{
			tf: testFiles["valid 1"],
		},
		{
			tf: testFiles["valid 2"],
		},
		{
			tf:  testFiles["non existent file"],
			err: "jwtbundle: unable to read JWT bundle: open testdata/does-not-exist.json: " + errstrings.FileNotFound,
		},
		{
			tf:  testFiles["missing kid"],
			err: "jwtbundle: error adding authority 1 of JWKS: keyID cannot be empty",
		},
	}

	for _, testCase := range testCases {
		t.Run(testCase.tf.filePath, func(t *testing.T) {
			bundle, err := jwtbundle.Load(td, testCase.tf.filePath)
			if testCase.err != "" {
				require.EqualError(t, err, testCase.err)
				return
			}
			require.NoError(t, err)
			require.NotNil(t, bundle)
			assert.Len(t, bundle.JWTAuthorities(), testCase.tf.keysCount)
		})
	}
}

func TestRead(t *testing.T) {
	testCases := []struct {
		tf  testFile
		err string
	}{
		{
			tf: testFiles["valid 1"],
		},
		{
			tf: testFiles["valid 2"],
		},
		{
			tf:  testFiles["non existent file"],
			err: "jwtbundle: unable to read: invalid argument",
		},
		{
			tf:  testFiles["missing kid"],
			err: "jwtbundle: error adding authority 1 of JWKS: keyID cannot be empty",
		},
	}

	for _, testCase := range testCases {
		t.Run(testCase.tf.filePath, func(t *testing.T) {
			// we expect the Open call to fail in some cases
			file, _ := os.Open(testCase.tf.filePath)
			defer file.Close()

			bundle, err := jwtbundle.Read(td, file)
			if testCase.err != "" {
				require.EqualError(t, err, testCase.err)
				return
			}
			require.NoError(t, err)
			require.NotNil(t, bundle)
			assert.Len(t, bundle.JWTAuthorities(), testCase.tf.keysCount)
		})
	}
}

func TestParse(t *testing.T) {
	testCases := []struct {
		tf  testFile
		err string
	}{
		{
			tf: testFiles["valid 1"],
		},
		{
			tf: testFiles["valid 2"],
		},
		{
			tf:  testFiles["non existent file"],
			err: "jwtbundle: unable to parse JWKS: unexpected end of JSON input",
		},
		{
			tf:  testFiles["missing kid"],
			err: "jwtbundle: error adding authority 1 of JWKS: keyID cannot be empty",
		},
	}

	for _, testCase := range testCases {
		t.Run(testCase.tf.filePath, func(t *testing.T) {
			// we expect the ReadFile call to fail in some cases
			bundleBytes, _ := os.ReadFile(testCase.tf.filePath)

			bundle, err := jwtbundle.Parse(td, bundleBytes)
			if testCase.err != "" {
				require.EqualError(t, err, testCase.err)
				return
			}
			require.NoError(t, err)
			require.NotNil(t, bundle)
			assert.Len(t, bundle.JWTAuthorities(), testCase.tf.keysCount)
		})
	}
}

func TestTrustDomain(t *testing.T) {
	b := jwtbundle.New(td)
	btd := b.TrustDomain()
	require.Equal(t, td, btd)
}

func TestJWTAuthoritiesCRUD(t *testing.T) {
	// Test AddJWTAuthority (missing authority)
	b := jwtbundle.New(td)
	err := b.AddJWTAuthority("", "test-1")
	require.EqualError(t, err, "jwtbundle: keyID cannot be empty")

	// Test AddJWTAuthority (new authority)
	err = b.AddJWTAuthority("key-1", "test-1")
	require.NoError(t, err)

	// Test JWTAuthorities
	jwtAuthorities := b.JWTAuthorities()
	require.Equal(t, map[string]crypto.PublicKey{"key-1": "test-1"}, jwtAuthorities)

	err = b.AddJWTAuthority("key-2", "test-2")
	require.NoError(t, err)

	jwtAuthorities = b.JWTAuthorities()
	require.Equal(t, map[string]crypto.PublicKey{
		"key-1": "test-1",
		"key-2": "test-2",
	}, jwtAuthorities)

	// Test FindJWTAuthority
	authority, ok := b.FindJWTAuthority("key-1")
	require.True(t, ok)
	require.Equal(t, "test-1", authority)

	authority, ok = b.FindJWTAuthority("key-3")
	require.False(t, ok)
	require.Nil(t, authority)

	require.Equal(t, true, b.HasJWTAuthority("key-1"))
	b.RemoveJWTAuthority("key-3")

	require.Equal(t, 2, len(b.JWTAuthorities()))
	require.Equal(t, true, b.HasJWTAuthority("key-1"))
	require.Equal(t, true, b.HasJWTAuthority("key-2"))

	// Test RemoveJWTAuthority
	b.RemoveJWTAuthority("key-2")
	require.Equal(t, 1, len(b.JWTAuthorities()))
	require.Equal(t, true, b.HasJWTAuthority("key-1"))

	// Test AddJWTAuthority (update authority)
	err = b.AddJWTAuthority("key-1", "test-1-updated")
	require.NoError(t, err)
	jwtAuthorities = b.JWTAuthorities()
	require.Equal(t, map[string]crypto.PublicKey{
		"key-1": "test-1-updated",
	}, jwtAuthorities)
}

func TestMarshal(t *testing.T) {
	// Load a bundle to marshal
	bundle, err := jwtbundle.Load(td, "testdata/jwks_valid_2.json")
	require.NoError(t, err)

	// Marshal the bundle
	bundleBytesMarshal, err := bundle.Marshal()
	require.NoError(t, err)
	require.NotNil(t, bundleBytesMarshal)

	// Parse the marshaled bundle
	bundleParsed, err := jwtbundle.Parse(td, bundleBytesMarshal)
	require.NoError(t, err)

	// Assert that the marshaled bundle is equal to the parsed bundle
	assert.Equal(t, bundleParsed, bundle)
}

func TestGetJWTBundleForTrustDomain(t *testing.T) {
	b := jwtbundle.New(td)
	b1, err := b.GetJWTBundleForTrustDomain(td)
	require.NoError(t, err)
	require.Equal(t, b, b1)

	td2 := spiffeid.RequireTrustDomainFromString("example-2.org")
	b2, err := b.GetJWTBundleForTrustDomain(td2)
	require.Nil(t, b2)
	require.EqualError(t, err, `jwtbundle: no JWT bundle for trust domain "example-2.org"`)
}

func TestEqual(t *testing.T) {
	ca1 := test.NewCA(t, td)
	ca2 := test.NewCA(t, td2)

	empty := jwtbundle.New(td)
	empty2 := jwtbundle.New(td2)

	jwtAuthorities1 := jwtbundle.FromJWTAuthorities(td, ca1.JWTAuthorities())
	jwtAuthorities2 := jwtbundle.FromJWTAuthorities(td, ca2.JWTAuthorities())

	for _, tt := range []struct {
		name        string
		a           *jwtbundle.Bundle
		b           *jwtbundle.Bundle
		expectEqual bool
	}{
		{
			name:        "empty equal",
			a:           empty,
			b:           empty,
			expectEqual: true,
		},
		{
			name:        "different trust domains",
			a:           empty,
			b:           empty2,
			expectEqual: false,
		},
		{
			name:        "JWT authorities equal",
			a:           jwtAuthorities1,
			b:           jwtAuthorities1,
			expectEqual: true,
		},
		{
			name:        "JWT authorities empty and not empty",
			a:           empty,
			b:           jwtAuthorities1,
			expectEqual: false,
		},
		{
			name:        "JWT authorities not empty but not equal",
			a:           jwtAuthorities1,
			b:           jwtAuthorities2,
			expectEqual: false,
		},
	} {
		t.Run(tt.name, func(t *testing.T) {
			require.Equal(t, tt.expectEqual, tt.a.Equal(tt.b))
		})
	}
}

func TestClone(t *testing.T) {
	// Load a bundle to clone
	original, err := jwtbundle.Load(td, "testdata/jwks_valid_2.json")
	require.NoError(t, err)

	cloned := original.Clone()
	require.True(t, original.Equal(cloned))
}