-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathspiffe_test.go
More file actions
470 lines (399 loc) · 11.5 KB
/
spiffe_test.go
File metadata and controls
470 lines (399 loc) · 11.5 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0
package client
import (
"errors"
"slices"
"sync"
"testing"
"time"
eventsv1 "github.com/agntcy/dir/api/events/v1"
routingv1 "github.com/agntcy/dir/api/routing/v1"
searchv1 "github.com/agntcy/dir/api/search/v1"
signv1 "github.com/agntcy/dir/api/sign/v1"
storev1 "github.com/agntcy/dir/api/store/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
const (
// Test server constants for SPIFFE tests.
spiffeTestServerBufnet = "bufnet"
spiffeTestCleanupWait = 10 * time.Millisecond
)
// ============================================================================
// Issue 3: SPIFFE Sources Resource Leaks Tests
// ============================================================================
// mockCloser is a mock implementation of io.Closer for testing.
type mockCloser struct {
closed bool
closeErr error
closeChan chan struct{} // Signal when Close() is called
}
func newMockCloser() *mockCloser {
return &mockCloser{
closeChan: make(chan struct{}, 1),
}
}
func (m *mockCloser) Close() error {
m.closed = true
select {
case m.closeChan <- struct{}{}:
default:
}
return m.closeErr
}
// orderTrackingCloser wraps a closer and tracks when it's closed.
type orderTrackingCloser struct {
name string
closeOrder *[]string
mu *sync.Mutex
}
func (o *orderTrackingCloser) Close() error {
o.mu.Lock()
*o.closeOrder = append(*o.closeOrder, o.name)
o.mu.Unlock()
return nil
}
// TestClientClose_ClosesSPIFFESources tests that Close() properly closes all SPIFFE sources.
func TestClientClose_ClosesSPIFFESources(t *testing.T) {
tests := []struct {
name string
bundleSrc *mockCloser
x509Src *mockCloser
jwtSource *mockCloser
wantClosed []string // Which sources should be closed
}{
{
name: "all sources present",
bundleSrc: newMockCloser(),
x509Src: newMockCloser(),
jwtSource: newMockCloser(),
wantClosed: []string{"jwtSource", "x509Src", "bundleSrc"},
},
{
name: "only bundleSrc",
bundleSrc: newMockCloser(),
x509Src: nil,
jwtSource: nil,
wantClosed: []string{"bundleSrc"},
},
{
name: "only x509Src",
bundleSrc: nil,
x509Src: newMockCloser(),
jwtSource: nil,
wantClosed: []string{"x509Src"},
},
{
name: "only jwtSource",
bundleSrc: nil,
x509Src: nil,
jwtSource: newMockCloser(),
wantClosed: []string{"jwtSource"},
},
{
name: "no sources",
bundleSrc: nil,
x509Src: nil,
jwtSource: nil,
wantClosed: []string{},
},
{
name: "x509 auth pattern (bundleSrc + x509Src)",
bundleSrc: newMockCloser(),
x509Src: newMockCloser(),
jwtSource: nil,
wantClosed: []string{"x509Src", "bundleSrc"},
},
{
name: "jwt auth pattern (bundleSrc + jwtSource)",
bundleSrc: newMockCloser(),
x509Src: nil,
jwtSource: newMockCloser(),
wantClosed: []string{"jwtSource", "bundleSrc"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := &Client{}
// Only set non-nil sources to avoid Go interface nil gotcha
if tt.bundleSrc != nil {
client.bundleSrc = tt.bundleSrc
}
if tt.x509Src != nil {
client.x509Src = tt.x509Src
}
if tt.jwtSource != nil {
client.jwtSource = tt.jwtSource
}
// Close the client
if err := client.Close(); err != nil {
t.Errorf("Close() returned error: %v", err)
}
// Verify expected sources were closed
checkClosed := func(src *mockCloser, name string) {
if src == nil {
// If source is nil, it shouldn't be in wantClosed list
if contains(tt.wantClosed, name) {
t.Errorf("%s was nil but expected to be closed", name)
}
return
}
shouldBeClosed := contains(tt.wantClosed, name)
if src.closed != shouldBeClosed {
t.Errorf("%s.closed = %v, want %v", name, src.closed, shouldBeClosed)
}
}
checkClosed(tt.bundleSrc, "bundleSrc")
checkClosed(tt.x509Src, "x509Src")
checkClosed(tt.jwtSource, "jwtSource")
})
}
}
// TestClientClose_SPIFFESourcesCloseOrder tests the order of closing SPIFFE sources.
func TestClientClose_SPIFFESourcesCloseOrder(t *testing.T) {
// Track close order
var (
closeOrder []string
orderMu sync.Mutex
)
// Create closers that record their close order
jwtSource := &orderTrackingCloser{name: "jwtSource", closeOrder: &closeOrder, mu: &orderMu}
x509Src := &orderTrackingCloser{name: "x509Src", closeOrder: &closeOrder, mu: &orderMu}
bundleSrc := &orderTrackingCloser{name: "bundleSrc", closeOrder: &closeOrder, mu: &orderMu}
client := &Client{
jwtSource: jwtSource,
x509Src: x509Src,
bundleSrc: bundleSrc,
}
// Close the client
if err := client.Close(); err != nil {
t.Errorf("Close() returned error: %v", err)
}
// Verify close order: jwtSource → x509Src → bundleSrc
// This order is important because sources may depend on each other
expectedOrder := []string{"jwtSource", "x509Src", "bundleSrc"}
orderMu.Lock()
defer orderMu.Unlock()
if len(closeOrder) != len(expectedOrder) {
t.Errorf("Close order length = %d, want %d (got %v)", len(closeOrder), len(expectedOrder), closeOrder)
}
for i, want := range expectedOrder {
if i >= len(closeOrder) {
t.Errorf("Missing close call for %s at position %d", want, i)
continue
}
if closeOrder[i] != want {
t.Errorf("Close order[%d] = %s, want %s", i, closeOrder[i], want)
}
}
}
// TestClientClose_SPIFFESourceErrorHandling tests error handling when closing SPIFFE sources.
func TestClientClose_SPIFFESourceErrorHandling(t *testing.T) {
tests := []struct {
name string
bundleErr error
x509Err error
jwtErr error
wantErrCount int
wantErrSubstr string
}{
{
name: "no errors",
bundleErr: nil,
x509Err: nil,
jwtErr: nil,
wantErrCount: 0,
wantErrSubstr: "",
},
{
name: "jwt source error",
bundleErr: nil,
x509Err: nil,
jwtErr: errors.New("jwt close failed"),
wantErrCount: 1,
wantErrSubstr: "JWT source",
},
{
name: "x509 source error",
bundleErr: nil,
x509Err: errors.New("x509 close failed"),
jwtErr: nil,
wantErrCount: 1,
wantErrSubstr: "X.509 source",
},
{
name: "bundle source error",
bundleErr: errors.New("bundle close failed"),
x509Err: nil,
jwtErr: nil,
wantErrCount: 1,
wantErrSubstr: "bundle source",
},
{
name: "all sources error",
bundleErr: errors.New("bundle close failed"),
x509Err: errors.New("x509 close failed"),
jwtErr: errors.New("jwt close failed"),
wantErrCount: 3,
wantErrSubstr: "client close errors",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bundleSrc := newMockCloser()
bundleSrc.closeErr = tt.bundleErr
x509Src := newMockCloser()
x509Src.closeErr = tt.x509Err
jwtSource := newMockCloser()
jwtSource.closeErr = tt.jwtErr
client := &Client{
bundleSrc: bundleSrc,
x509Src: x509Src,
jwtSource: jwtSource,
}
err := client.Close()
// Test case expects no error
if tt.wantErrCount == 0 {
if err != nil {
t.Errorf("Close() returned error when none expected: %v", err)
}
return
}
// Test case expects an error
if err == nil {
t.Errorf("Close() returned nil, want error")
return
}
// Verify error message contains expected substring
if tt.wantErrSubstr != "" && !containsSubstring(err.Error(), tt.wantErrSubstr) {
t.Errorf("Close() error = %q, want substring %q", err.Error(), tt.wantErrSubstr)
}
// Verify all sources were attempted to be closed despite errors
if !bundleSrc.closed {
t.Error("bundleSrc was not closed")
}
if !x509Src.closed {
t.Error("x509Src was not closed")
}
if !jwtSource.closed {
t.Error("jwtSource was not closed")
}
})
}
}
// TestClientClose_SPIFFESourcesWithConnection tests that sources are closed before connection.
func TestClientClose_SPIFFESourcesWithConnection(t *testing.T) {
// Create test server
server, lis := createTestServer(t)
defer server.Stop()
// Create connection
conn, err := grpc.NewClient(
spiffeTestServerBufnet,
grpc.WithContextDialer(bufDialer(lis)),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
if err != nil {
t.Fatalf("Failed to create gRPC client: %v", err)
}
// Create mock SPIFFE sources
bundleSrc := newMockCloser()
x509Src := newMockCloser()
jwtSource := newMockCloser()
client := &Client{
StoreServiceClient: storev1.NewStoreServiceClient(conn),
RoutingServiceClient: routingv1.NewRoutingServiceClient(conn),
SearchServiceClient: searchv1.NewSearchServiceClient(conn),
SyncServiceClient: storev1.NewSyncServiceClient(conn),
SignServiceClient: signv1.NewSignServiceClient(conn),
EventServiceClient: eventsv1.NewEventServiceClient(conn),
config: &Config{
ServerAddress: spiffeTestServerBufnet,
},
conn: conn,
bundleSrc: bundleSrc,
x509Src: x509Src,
jwtSource: jwtSource,
}
// Close the client
if err := client.Close(); err != nil {
t.Errorf("Close() returned error: %v", err)
}
// Verify all SPIFFE sources were closed
if !jwtSource.closed {
t.Error("jwtSource was not closed")
}
if !x509Src.closed {
t.Error("x509Src was not closed")
}
if !bundleSrc.closed {
t.Error("bundleSrc was not closed")
}
// Verify connection state
time.Sleep(spiffeTestCleanupWait)
finalState := conn.GetState()
t.Logf("Final connection state: %v", finalState)
}
// TestClientClose_PartialSPIFFESources tests closing when only some sources are present.
func TestClientClose_PartialSPIFFESources(t *testing.T) {
// Test X.509 auth pattern (bundleSrc + x509Src, no jwtSource)
t.Run("x509 auth pattern", func(t *testing.T) {
bundleSrc := newMockCloser()
x509Src := newMockCloser()
client := &Client{
bundleSrc: bundleSrc,
x509Src: x509Src,
jwtSource: nil, // Not used in X.509 auth
}
if err := client.Close(); err != nil {
t.Errorf("Close() returned error: %v", err)
}
if !bundleSrc.closed {
t.Error("bundleSrc was not closed")
}
if !x509Src.closed {
t.Error("x509Src was not closed")
}
})
// Test JWT auth pattern (bundleSrc + jwtSource, no x509Src)
t.Run("jwt auth pattern", func(t *testing.T) {
bundleSrc := newMockCloser()
jwtSource := newMockCloser()
client := &Client{
bundleSrc: bundleSrc,
x509Src: nil, // Not used in JWT auth
jwtSource: jwtSource,
}
if err := client.Close(); err != nil {
t.Errorf("Close() returned error: %v", err)
}
if !bundleSrc.closed {
t.Error("bundleSrc was not closed")
}
if !jwtSource.closed {
t.Error("jwtSource was not closed")
}
})
}
// ============================================================================
// Helper functions
// ============================================================================
// contains checks if a string slice contains a value.
func contains(slice []string, val string) bool {
return slices.Contains(slice, val)
}
// containsSubstring checks if a string contains a substring.
func containsSubstring(s, substr string) bool {
return len(s) > 0 && len(substr) > 0 &&
(s == substr || len(s) >= len(substr) &&
(s[:len(substr)] == substr || s[len(s)-len(substr):] == substr ||
len(s) > len(substr) && findSubstring(s, substr)))
}
func findSubstring(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}