@@ -22,6 +22,7 @@ import (
22
22
"go.step.sm/crypto/kms/softkms"
23
23
"go.step.sm/crypto/minica"
24
24
"go.step.sm/crypto/pemutil"
25
+ "go.step.sm/crypto/x509util"
25
26
"go.step.sm/linkedca"
26
27
)
27
28
@@ -37,6 +38,7 @@ func Test_challengeValidationController_Validate(t *testing.T) {
37
38
}
38
39
type response struct {
39
40
Allow bool `json:"allow"`
41
+ Data any `json:"data"`
40
42
}
41
43
nokServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
42
44
req := & request {}
@@ -60,11 +62,22 @@ func Test_challengeValidationController_Validate(t *testing.T) {
60
62
if assert .NotNil (t , req .Request ) {
61
63
assert .Equal (t , []byte {1 }, req .Request .Raw )
62
64
}
63
- b , err := json .Marshal (response {Allow : true })
65
+ resp := response {Allow : true }
66
+ if r .Header .Get ("X-Smallstep-Webhook-Id" ) == "webhook-id-2" {
67
+ resp .Data = map [string ]any {
68
+ "ID" : "2adcbfec-5e4a-4b93-8913-640e24faf101" ,
69
+
70
+ }
71
+ }
72
+ b , err := json .Marshal (resp )
64
73
require .NoError (t , err )
65
74
w .WriteHeader (200 )
66
75
w .Write (b )
67
76
}))
77
+ t .Cleanup (func () {
78
+ nokServer .Close ()
79
+ okServer .Close ()
80
+ })
68
81
type fields struct {
69
82
client * http.Client
70
83
webhooks []* Webhook
@@ -78,7 +91,7 @@ func Test_challengeValidationController_Validate(t *testing.T) {
78
91
name string
79
92
fields fields
80
93
args args
81
- server * httptest. Server
94
+ want x509util. TemplateData
82
95
expErr error
83
96
}{
84
97
{
@@ -134,7 +147,6 @@ func Test_challengeValidationController_Validate(t *testing.T) {
134
147
challenge : "not-allowed" ,
135
148
transactionID : "transaction-1" ,
136
149
},
137
- server : nokServer ,
138
150
expErr : errors .New ("webhook server did not allow request" ),
139
151
},
140
152
{
@@ -154,26 +166,58 @@ func Test_challengeValidationController_Validate(t *testing.T) {
154
166
challenge : "challenge" ,
155
167
transactionID : "transaction-1" ,
156
168
},
157
- server : okServer ,
169
+ want : x509util.TemplateData {
170
+ x509util .WebhooksKey : map [string ]any {
171
+ "webhook-name-1" : nil ,
172
+ },
173
+ },
174
+ },
175
+ {
176
+ name : "ok with data" ,
177
+ fields : fields {http .DefaultClient , []* Webhook {
178
+ {
179
+ ID : "webhook-id-2" ,
180
+ Name : "webhook-name-2" ,
181
+ Secret : "MTIzNAo=" ,
182
+ Kind : linkedca .Webhook_SCEPCHALLENGE .String (),
183
+ CertType : linkedca .Webhook_X509 .String (),
184
+ URL : okServer .URL ,
185
+ },
186
+ }},
187
+ args : args {
188
+ provisionerName : "my-scep-provisioner" ,
189
+ challenge : "challenge" ,
190
+ transactionID : "transaction-1" ,
191
+ },
192
+ want : x509util.TemplateData {
193
+ x509util .WebhooksKey : map [string ]any {
194
+ "webhook-name-2" : map [string ]any {
195
+ "ID" : "2adcbfec-5e4a-4b93-8913-640e24faf101" ,
196
+
197
+ },
198
+ },
199
+ },
158
200
},
159
201
}
160
202
for _ , tt := range tests {
161
203
t .Run (tt .name , func (t * testing.T ) {
162
204
c := newChallengeValidationController (tt .fields .client , tt .fields .webhooks )
163
-
164
- if tt .server != nil {
165
- defer tt .server .Close ()
166
- }
167
-
168
205
ctx := context .Background ()
169
- err := c .Validate (ctx , dummyCSR , tt .args .provisionerName , tt .args .challenge , tt .args .transactionID )
170
-
206
+ got , err := c .Validate (ctx , dummyCSR , tt .args .provisionerName , tt .args .challenge , tt .args .transactionID )
171
207
if tt .expErr != nil {
172
208
assert .EqualError (t , err , tt .expErr .Error ())
173
209
return
174
210
}
175
-
176
211
assert .NoError (t , err )
212
+ data := x509util.TemplateData {}
213
+ for _ , o := range got {
214
+ if m , ok := o .(TemplateDataModifier ); ok {
215
+ m .Modify (data )
216
+ } else {
217
+ t .Errorf ("Validate() got = %T, want TemplateDataModifier" , o )
218
+ }
219
+ }
220
+ assert .Equal (t , tt .want , data )
177
221
})
178
222
}
179
223
}
@@ -257,6 +301,7 @@ func TestSCEP_ValidateChallenge(t *testing.T) {
257
301
}
258
302
type response struct {
259
303
Allow bool `json:"allow"`
304
+ Data any `json:"data"`
260
305
}
261
306
okServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
262
307
req := & request {}
@@ -268,11 +313,19 @@ func TestSCEP_ValidateChallenge(t *testing.T) {
268
313
if assert .NotNil (t , req .Request ) {
269
314
assert .Equal (t , []byte {1 }, req .Request .Raw )
270
315
}
271
- b , err := json .Marshal (response {Allow : true })
316
+ resp := response {Allow : true }
317
+ if r .Header .Get ("X-Smallstep-Webhook-Id" ) == "webhook-id-2" {
318
+ resp .Data = map [string ]any {
319
+ "ID" : "2adcbfec-5e4a-4b93-8913-640e24faf101" ,
320
+
321
+ }
322
+ }
323
+ b , err := json .Marshal (resp )
272
324
require .NoError (t , err )
273
325
w .WriteHeader (200 )
274
326
w .Write (b )
275
327
}))
328
+ t .Cleanup (okServer .Close )
276
329
type args struct {
277
330
challenge string
278
331
transactionID string
@@ -282,6 +335,7 @@ func TestSCEP_ValidateChallenge(t *testing.T) {
282
335
p * SCEP
283
336
server * httptest.Server
284
337
args args
338
+ want x509util.TemplateData
285
339
expErr error
286
340
}{
287
341
{"ok/webhooks" , & SCEP {
@@ -299,9 +353,43 @@ func TestSCEP_ValidateChallenge(t *testing.T) {
299
353
},
300
354
},
301
355
},
302
- }, okServer , args {"webhook-challenge" , "webhook-transaction-1" },
303
- nil ,
304
- },
356
+ }, okServer , args {"webhook-challenge" , "webhook-transaction-1" }, x509util.TemplateData {
357
+ x509util .WebhooksKey : map [string ]any {
358
+ "webhook-name-1" : nil ,
359
+ },
360
+ }, nil },
361
+ {"ok/with-data" , & SCEP {
362
+ Name : "SCEP" ,
363
+ Type : "SCEP" ,
364
+ Options : & Options {
365
+ Webhooks : []* Webhook {
366
+ {
367
+ ID : "webhook-id-1" ,
368
+ Name : "webhook-name-1" ,
369
+ Secret : "MTIzNAo=" ,
370
+ Kind : linkedca .Webhook_SCEPCHALLENGE .String (),
371
+ CertType : linkedca .Webhook_X509 .String (),
372
+ URL : okServer .URL ,
373
+ },
374
+ {
375
+ ID : "webhook-id-2" ,
376
+ Name : "webhook-name-2" ,
377
+ Secret : "MTIzNAo=" ,
378
+ Kind : linkedca .Webhook_SCEPCHALLENGE .String (),
379
+ CertType : linkedca .Webhook_X509 .String (),
380
+ URL : okServer .URL ,
381
+ },
382
+ },
383
+ },
384
+ }, okServer , args {"webhook-challenge" , "webhook-transaction-1" }, x509util.TemplateData {
385
+ x509util .WebhooksKey : map [string ]any {
386
+ "webhook-name-1" : nil ,
387
+ "webhook-name-2" : map [string ]any {
388
+ "ID" : "2adcbfec-5e4a-4b93-8913-640e24faf101" ,
389
+
390
+ },
391
+ },
392
+ }, nil },
305
393
{"fail/webhooks-secret-configuration" , & SCEP {
306
394
Name : "SCEP" ,
307
395
Type : "SCEP" ,
@@ -317,60 +405,53 @@ func TestSCEP_ValidateChallenge(t *testing.T) {
317
405
},
318
406
},
319
407
},
320
- }, nil , args {"webhook-challenge" , "webhook-transaction-1" },
321
- errors .New ("failed executing webhook request: illegal base64 data at input byte 0" ),
322
- },
408
+ }, nil , args {"webhook-challenge" , "webhook-transaction-1" }, nil , errors .New ("failed executing webhook request: illegal base64 data at input byte 0" )},
323
409
{"ok/static-challenge" , & SCEP {
324
410
Name : "SCEP" ,
325
411
Type : "SCEP" ,
326
412
Options : & Options {},
327
413
ChallengePassword : "secret-static-challenge" ,
328
- }, nil , args {"secret-static-challenge" , "static-transaction-1" },
329
- nil ,
330
- },
414
+ }, nil , args {"secret-static-challenge" , "static-transaction-1" }, x509util.TemplateData {}, nil },
331
415
{"fail/wrong-static-challenge" , & SCEP {
332
416
Name : "SCEP" ,
333
417
Type : "SCEP" ,
334
418
Options : & Options {},
335
419
ChallengePassword : "secret-static-challenge" ,
336
- }, nil , args {"the-wrong-challenge-secret" , "static-transaction-1" },
337
- errors .New ("invalid challenge password provided" ),
338
- },
420
+ }, nil , args {"the-wrong-challenge-secret" , "static-transaction-1" }, nil , errors .New ("invalid challenge password provided" )},
339
421
{"ok/no-challenge" , & SCEP {
340
422
Name : "SCEP" ,
341
423
Type : "SCEP" ,
342
424
Options : & Options {},
343
425
ChallengePassword : "" ,
344
- }, nil , args {"" , "static-transaction-1" },
345
- nil ,
346
- },
426
+ }, nil , args {"" , "static-transaction-1" }, x509util.TemplateData {}, nil },
347
427
{"fail/no-challenge-but-provided" , & SCEP {
348
428
Name : "SCEP" ,
349
429
Type : "SCEP" ,
350
430
Options : & Options {},
351
431
ChallengePassword : "" ,
352
- }, nil , args {"a-challenge-value" , "static-transaction-1" },
353
- errors .New ("invalid challenge password provided" ),
354
- },
432
+ }, nil , args {"a-challenge-value" , "static-transaction-1" }, nil , errors .New ("invalid challenge password provided" )},
355
433
}
356
434
for _ , tt := range tests {
357
435
t .Run (tt .name , func (t * testing.T ) {
358
-
359
- if tt .server != nil {
360
- defer tt .server .Close ()
361
- }
362
-
363
436
err := tt .p .Init (Config {Claims : globalProvisionerClaims , WebhookClient : http .DefaultClient })
364
437
require .NoError (t , err )
365
438
ctx := context .Background ()
366
439
367
- err = tt .p .ValidateChallenge (ctx , dummyCSR , tt .args .challenge , tt .args .transactionID )
440
+ got , err : = tt .p .ValidateChallenge (ctx , dummyCSR , tt .args .challenge , tt .args .transactionID )
368
441
if tt .expErr != nil {
369
442
assert .EqualError (t , err , tt .expErr .Error ())
370
443
return
371
444
}
372
-
373
445
assert .NoError (t , err )
446
+ data := x509util.TemplateData {}
447
+ for _ , o := range got {
448
+ if m , ok := o .(TemplateDataModifier ); ok {
449
+ m .Modify (data )
450
+ } else {
451
+ t .Errorf ("Validate() got = %T, want TemplateDataModifier" , o )
452
+ }
453
+ }
454
+ assert .Equal (t , tt .want , data )
374
455
})
375
456
}
376
457
}
0 commit comments