Repository navigation
Expand file tree
/
Copy pathmiddleware_test.go
More file actions
357 lines (299 loc) · 10 KB
/
Copy pathmiddleware_test.go
File metadata and controls
357 lines (299 loc) · 10 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
package pact
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestMiddlewareVerifiesValidRequest(t *testing.T) {
// Setup: create identities and delegation
human, _ := NewIdentity(EntityHuman, "alice")
agent, _ := NewIdentity(EntityAgent, "test-agent")
delegation, _ := NewDelegation(DelegationRequest{
From: human,
To: agent,
Capabilities: []string{"api:read"},
TTL: 1 * time.Hour,
})
chain := DelegationChain{*delegation}
// Create a handler that checks the context
var gotResult *VerificationResult
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotResult = FromContext(r.Context())
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
})
// Wrap with middleware
protected := Middleware(MiddlewareConfig{
VerifyOptions: VerifyOptions{
RequiredCapability: "api:read",
},
})(handler)
// Create and sign a request
req := httptest.NewRequest("GET", "https://api.example.com/data", nil)
req.Host = "api.example.com"
if err := SignRequest(req, agent, chain); err != nil {
t.Fatalf("failed to sign request: %v", err)
}
rr := httptest.NewRecorder()
protected.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
if gotResult == nil {
t.Fatal("expected verification result in context")
}
if !gotResult.Valid {
t.Fatalf("expected valid result, got error: %s", gotResult.Error)
}
if gotResult.AgentID != agent.ID {
t.Fatalf("expected agent ID %s, got %s", agent.ID, gotResult.AgentID)
}
if gotResult.RootAuthority != human.ID {
t.Fatalf("expected root %s, got %s", human.ID, gotResult.RootAuthority)
}
}
func TestMiddlewareRejectsUnsignedRequest(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Fatal("handler should not be called for unsigned request")
})
protected := Middleware(MiddlewareConfig{})(handler)
req := httptest.NewRequest("GET", "https://api.example.com/data", nil)
rr := httptest.NewRecorder()
protected.ServeHTTP(rr, req)
if rr.Code != http.StatusUnauthorized {
t.Fatalf("expected 401, got %d", rr.Code)
}
}
func TestMiddlewareOptionalPassthrough(t *testing.T) {
var handlerCalled bool
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handlerCalled = true
result := FromContext(r.Context())
if result != nil {
t.Fatal("expected no verification result for unsigned request in optional mode")
}
w.WriteHeader(http.StatusOK)
})
protected := Middleware(MiddlewareConfig{
Optional: true,
})(handler)
// Request without Pact headers
req := httptest.NewRequest("GET", "https://api.example.com/data", nil)
rr := httptest.NewRecorder()
protected.ServeHTTP(rr, req)
if !handlerCalled {
t.Fatal("handler should be called in optional mode")
}
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", rr.Code)
}
}
func TestMiddlewareRejectsInsufficientCapability(t *testing.T) {
human, _ := NewIdentity(EntityHuman, "alice")
agent, _ := NewIdentity(EntityAgent, "test-agent")
delegation, _ := NewDelegation(DelegationRequest{
From: human,
To: agent,
Capabilities: []string{"api:read"},
TTL: 1 * time.Hour,
})
chain := DelegationChain{*delegation}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Fatal("handler should not be called for insufficient capability")
})
// Require write capability, but delegation only grants read
protected := Middleware(MiddlewareConfig{
VerifyOptions: VerifyOptions{
RequiredCapability: "api:write",
},
})(handler)
req := httptest.NewRequest("POST", "https://api.example.com/data", nil)
req.Host = "api.example.com"
SignRequest(req, agent, chain)
rr := httptest.NewRecorder()
protected.ServeHTTP(rr, req)
if rr.Code != http.StatusUnauthorized {
t.Fatalf("expected 401, got %d", rr.Code)
}
if !strings.Contains(rr.Body.String(), "lacks required capability") {
t.Fatalf("expected capability error, got: %s", rr.Body.String())
}
}
func TestMiddlewareDynamicCapability(t *testing.T) {
human, _ := NewIdentity(EntityHuman, "alice")
agent, _ := NewIdentity(EntityAgent, "test-agent")
delegation, _ := NewDelegation(DelegationRequest{
From: human,
To: agent,
Capabilities: []string{"api:read", "api:write"},
TTL: 1 * time.Hour,
})
chain := DelegationChain{*delegation}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
protected := Middleware(MiddlewareConfig{
CapabilityForRequest: func(r *http.Request) string {
if r.Method == "GET" {
return "api:read"
}
return "api:write"
},
})(handler)
// GET should work (requires read, has read)
getReq := httptest.NewRequest("GET", "https://api.example.com/data", nil)
getReq.Host = "api.example.com"
SignRequest(getReq, agent, chain)
rr := httptest.NewRecorder()
protected.ServeHTTP(rr, getReq)
if rr.Code != http.StatusOK {
t.Fatalf("GET expected 200, got %d: %s", rr.Code, rr.Body.String())
}
// POST should work (requires write, has write)
postReq := httptest.NewRequest("POST", "https://api.example.com/data", strings.NewReader("body"))
postReq.Host = "api.example.com"
SignRequest(postReq, agent, chain)
rr = httptest.NewRecorder()
protected.ServeHTTP(rr, postReq)
if rr.Code != http.StatusOK {
t.Fatalf("POST expected 200, got %d: %s", rr.Code, rr.Body.String())
}
}
func TestMiddlewareOnFailedCallback(t *testing.T) {
var failedCalled bool
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Should be called because OnFailed returns true (permissive mode)
result := FromContext(r.Context())
if result == nil {
t.Fatal("expected result in context even in permissive failure")
}
w.WriteHeader(http.StatusOK)
})
protected := Middleware(MiddlewareConfig{
VerifyOptions: VerifyOptions{
RequiredCapability: "admin:nuke",
},
OnFailed: func(w http.ResponseWriter, r *http.Request, result *VerificationResult) bool {
failedCalled = true
// Return true = continue to handler anyway (permissive/audit mode)
return true
},
})(handler)
human, _ := NewIdentity(EntityHuman, "alice")
agent, _ := NewIdentity(EntityAgent, "test-agent")
delegation, _ := NewDelegation(DelegationRequest{
From: human,
To: agent,
Capabilities: []string{"api:read"},
TTL: 1 * time.Hour,
})
chain := DelegationChain{*delegation}
req := httptest.NewRequest("GET", "https://api.example.com/data", nil)
req.Host = "api.example.com"
SignRequest(req, agent, chain)
rr := httptest.NewRecorder()
protected.ServeHTTP(rr, req)
if !failedCalled {
t.Fatal("OnFailed should have been called")
}
if rr.Code != http.StatusOK {
t.Fatalf("expected 200 in permissive mode, got %d", rr.Code)
}
}
func TestMiddlewareOnVerifiedCallback(t *testing.T) {
var verifiedAgent string
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
protected := Middleware(MiddlewareConfig{
OnVerified: func(r *http.Request, result *VerificationResult) {
verifiedAgent = result.AgentID
},
})(handler)
human, _ := NewIdentity(EntityHuman, "alice")
agent, _ := NewIdentity(EntityAgent, "test-agent")
delegation, _ := NewDelegation(DelegationRequest{
From: human,
To: agent,
Capabilities: []string{"*"},
TTL: 1 * time.Hour,
})
chain := DelegationChain{*delegation}
req := httptest.NewRequest("GET", "https://api.example.com/data", nil)
req.Host = "api.example.com"
SignRequest(req, agent, chain)
rr := httptest.NewRecorder()
protected.ServeHTTP(rr, req)
if verifiedAgent != agent.ID {
t.Fatalf("OnVerified should have captured agent ID %s, got %s", agent.ID, verifiedAgent)
}
}
func TestRequireCapabilityShorthand(t *testing.T) {
human, _ := NewIdentity(EntityHuman, "alice")
agent, _ := NewIdentity(EntityAgent, "test-agent")
delegation, _ := NewDelegation(DelegationRequest{
From: human,
To: agent,
Capabilities: []string{"deploy:create"},
TTL: 1 * time.Hour,
})
chain := DelegationChain{*delegation}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
protected := RequireCapability("deploy:create")(handler)
req := httptest.NewRequest("POST", "https://api.example.com/deploy", nil)
req.Host = "api.example.com"
SignRequest(req, agent, chain)
rr := httptest.NewRecorder()
protected.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
}
func TestMiddlewareEndToEndWithServer(t *testing.T) {
// Full end-to-end: real HTTP server with Pact middleware
human, _ := NewIdentity(EntityHuman, "alice")
agent, _ := NewIdentity(EntityAgent, "test-agent")
delegation, _ := NewDelegation(DelegationRequest{
From: human,
To: agent,
Capabilities: []string{"api:read"},
TTL: 1 * time.Hour,
})
chain := DelegationChain{*delegation}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
result := MustFromContext(r.Context())
w.WriteHeader(http.StatusOK)
io.WriteString(w, "agent="+result.AgentID)
})
protected := Middleware(MiddlewareConfig{
VerifyOptions: VerifyOptions{
RequiredCapability: "api:read",
TrustedRoots: map[string]bool{human.ID: true},
},
})(handler)
server := httptest.NewServer(protected)
defer server.Close()
// Create a signed request to the test server
req, _ := http.NewRequest("GET", server.URL+"/data", nil)
req.Host = "api.example.com" // Override for consistent signing
if err := SignRequest(req, agent, chain); err != nil {
t.Fatalf("failed to sign: %v", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("request failed: %v", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", resp.StatusCode, string(body))
}
if !strings.Contains(string(body), agent.ID) {
t.Fatalf("expected agent ID in response, got: %s", string(body))
}
}