-
Notifications
You must be signed in to change notification settings - Fork 7
/
paths_issue_account_test.go
769 lines (699 loc) · 24.2 KB
/
paths_issue_account_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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
package natsbackend
import (
"context"
"encoding/json"
"fmt"
"testing"
accountv1 "github.com/edgefarm/vault-plugin-secrets-nats/pkg/claims/account/v1alpha1"
"github.com/edgefarm/vault-plugin-secrets-nats/pkg/stm"
"github.com/hashicorp/vault/sdk/logical"
"github.com/nats-io/jwt/v2"
"github.com/stretchr/testify/assert"
)
func TestCRUDAccountIssue(t *testing.T) {
b, reqStorage := getTestBackend(t)
t.Run("Test initial state of account issuer", func(t *testing.T) {
path := "issue/operator/op1/account/ac1"
// first create operator issue to be able to create account issue
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: "issue/operator/op1",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
// call read/delete/list without creating the issue
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: path,
Storage: reqStorage,
})
assert.NoError(t, err)
assert.True(t, resp.IsError())
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.DeleteOperation,
Path: path,
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "issue/operator/op1/account/",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, resp.Data, map[string]interface{}{})
})
t.Run("Test CRUD logic for account issuer", func(t *testing.T) {
/////////////////////////
// Prepare the test data
/////////////////////////
var path string = "issue/operator/op1/account/ac1"
var request map[string]interface{}
var expected IssueAccountData
var current IssueAccountData
// first create operator issue to be able to create account issue
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: "issue/operator/op1",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
//////////////////////////
// That will be requested
//////////////////////////
stm.StructToMap(&IssueAccountParameters{}, &request)
//////////////////////////
// That will be expected
//////////////////////////
expected = IssueAccountData{
Operator: "op1",
Account: "ac1",
UseSigningKey: "",
Claims: accountv1.AccountClaims{},
Status: IssueAccountStatus{
Account: IssueStatus{
Nkey: true,
JWT: true,
},
},
}
/////////////////////////////
// create the issue only
// with defaults and read it
/////////////////////////////
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: path,
Storage: reqStorage,
Data: request,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
//////////////////////////
// read the created issue
//////////////////////////
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: path,
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
//////////////////////////////////
// Compare the expected and current
//////////////////////////////////
stm.MapToStruct(resp.Data, ¤t)
assert.Equal(t, expected, current)
//////////////////////////
// That will be requested
//////////////////////////
issue := IssueAccountParameters{
Claims: accountv1.AccountClaims{
Account: accountv1.Account{
Limits: accountv1.OperatorLimits{
AccountLimits: accountv1.AccountLimits{
Imports: 10,
},
},
},
},
}
tmp, err := json.Marshal(issue)
assert.Nil(t, err)
json.Unmarshal(tmp, &request)
//////////////////////////
// That will be expected
//////////////////////////
expected = IssueAccountData{
Operator: "op1",
Account: "ac1",
Claims: accountv1.AccountClaims{
Account: accountv1.Account{
Limits: accountv1.OperatorLimits{
AccountLimits: accountv1.AccountLimits{
Imports: 10,
},
},
},
},
Status: IssueAccountStatus{
Account: IssueStatus{
Nkey: true,
JWT: true,
},
},
}
//////////////////////////////////
// Update with the requested data
//////////////////////////////////
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: path,
Storage: reqStorage,
Data: request,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
//////////////////////////////////
// Read the updated data back
//////////////////////////////////
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: path,
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
//////////////////////////////////
// Compare the expected and current
//////////////////////////////////
stm.MapToStruct(resp.Data, ¤t)
assert.Equal(t, expected, current)
//////////////////////////////////
// List the issues
//////////////////////////////////
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "issue/operator/op1/account/",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
//////////////////////////////////
// Check, only one key is listed
//////////////////////////////////
assert.Equal(t, map[string]interface{}{"keys": []string{"ac1"}}, resp.Data)
/////////////////////////
// Then delete the key
/////////////////////////
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.DeleteOperation,
Path: path,
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
//////////////////////////
// ... and try to read it
//////////////////////////
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: path,
Storage: reqStorage,
})
assert.NoError(t, err)
assert.True(t, resp.IsError())
//////////////////////////
// Then recreate the key
//////////////////////////
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: path,
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
//////////////////////////
// ... read the key
//////////////////////////
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: path,
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
//////////////////////////
// ... and delete again
//////////////////////////
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.DeleteOperation,
Path: path,
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
})
t.Run("Test CRUD for multiple account jwts", func(t *testing.T) {
// first create operator issue to be able to create account issue
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: "issue/operator/op1",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
// create 3 keys
for i := 0; i < 3; i++ {
path := fmt.Sprintf("issue/operator/op1/account/ac%d", i)
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: path,
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
}
// list the keys
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "issue/operator/op1/account/",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{
"keys": []string{"ac0", "ac1", "ac2"},
}, resp.Data)
// delete the keys
for i := 0; i < 3; i++ {
path := fmt.Sprintf("issue/operator/op1/account/ac%d", i)
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.DeleteOperation,
Path: path,
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
}
// list the keys
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "issue/operator/op1/account/",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{}, resp.Data)
})
}
func TestAccountBeforeOperatorCreatingSysAccount(t *testing.T) {
b, reqStorage := getTestBackend(t)
// Test name: Account before Operator creating SYS account
// Test overwiew: This test creates an account issue before the operator is created. Once the operator is created,
// it also creates the SYS account.
// Test steps:
// 1. The account for ac1 issue should create a nkey but no JWT because of the missing system account.
// 2. During creating of the operator issue, the operators nkey and JWT is created. The operator also creates
// the sys account and puts the sys account's information in it's JWT.
// 3. After creating the operator all accounts JWTs are created referencing the operators public key as issuer in the JWT claims.
t.Run("Account before Operator creating SYS account", func(t *testing.T) {
path := "issue/operator/op1/account"
request := map[string]interface{}{}
var current JWTData
var acountJWT JWTData
// 1. The accounts for ac1 and ac2 issue should create a nkey but no JWT because of the missing system account.
// 1.1a create the account
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: path + "/ac1",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.True(t, resp.IsError())
// 1.1b create the account
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: path + "/ac2",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.True(t, resp.IsError())
// 1.2 list the accounts - ac1 should be present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "issue/operator/op1/account/",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{
"keys": []string{"ac1", "ac2"},
}, resp.Data)
// 1.3 list the JWTs for accounts - none present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "jwt/operator/op1/account/",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{}, resp.Data)
// 2. During creating of the operator issue, the operators nkey and JWT is created. The operator also creates
// the sys account and puts the sys account's information in it's JWT.
// 2.1 create operator and let it create the sys account
stm.StructToMap(&IssueOperatorParameters{
CreateSystemAccount: true,
}, &request)
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: "issue/operator/op1",
Storage: reqStorage,
Data: request,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
// 2.2 list the operators - op1 should be present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "issue/operator/",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{
"keys": []string{"op1"},
}, resp.Data)
// 2.3 list the JWTs for operators - op1 should be present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "jwt/operator/",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{
"keys": []string{"op1"},
}, resp.Data)
// 2.4 list the nkeys for operators - op1 should be present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "nkey/operator/",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{
"keys": []string{"op1"},
}, resp.Data)
// 2.5 check the operator JWT - sys account information present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "jwt/operator/op1",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
stm.MapToStruct(resp.Data, ¤t)
op1Claims, err := jwt.DecodeOperatorClaims(current.JWT)
assert.NoError(t, err)
// get sys account public key
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "jwt/operator/op1/account/" + DefaultSysAccountName,
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
var sysAccount JWTData
stm.MapToStruct(resp.Data, &sysAccount)
sysAccountClaims, err := jwt.DecodeAccountClaims(sysAccount.JWT)
assert.NoError(t, err)
assert.Equal(t, op1Claims.SystemAccount, sysAccountClaims.Subject)
// 3. After creating the operator all accounts JWTs are created referencing the operators public key as issuer in the JWT claims.
// 3.1 list the accounts - ac1, ac2 and SYS should be present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "issue/operator/op1/account/",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{
"keys": []string{"ac1", "ac2", DefaultSysAccountName},
}, resp.Data)
// 3.2 list the JWTs for accounts - ac1 and SYS should be present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "jwt/operator/op1/account/",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{
"keys": []string{"ac1", "ac2", DefaultSysAccountName},
}, resp.Data)
// 3.3a check account acc1 JWT that issuer is operator public key
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "jwt/operator/op1/account/ac1",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
stm.MapToStruct(resp.Data, &acountJWT)
ac1Claims, err := jwt.DecodeAccountClaims(acountJWT.JWT)
assert.NoError(t, err)
assert.Equal(t, ac1Claims.Issuer, op1Claims.Subject)
// 3.3b check accounts acc2 JWT that issuer is operator public key
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "jwt/operator/op1/account/ac1",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
stm.MapToStruct(resp.Data, &acountJWT)
ac2Claims, err := jwt.DecodeAccountClaims(acountJWT.JWT)
assert.NoError(t, err)
assert.Equal(t, ac2Claims.Issuer, op1Claims.Subject)
})
}
func TestAccountBeforeOperatorAndSysAccount(t *testing.T) {
b, reqStorage := getTestBackend(t)
// Test name: Account before Operator and SYS account
// Test overwiew: This test creates an account issue before the operator and a system account is created.
// Test steps:
// 1. The account for ac1 issue should create a nkey but no JWT because of the missing system account.
// 2. During creating of the operator issue, the operators nkey and JWT is created. However the operators JWT
// is missing information about the system account.
// 3. After creating the system account, the operators JWT is updated with the system account information and
// all accounts JWTs are created referencing the operators public key as issuer in the JWT claims.
t.Run("Account before Operator and SYS account", func(t *testing.T) {
path := "issue/operator/op1/account/ac1"
var current JWTData
// 1. The account for ac1 issue should create a nkey but no JWT because of the missing system account.
// 1.1 create the account
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: path,
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.True(t, resp.IsError())
// 1.2 list the accounts - ac1 should be present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "issue/operator/op1/account/",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{
"keys": []string{"ac1"},
}, resp.Data)
// 1.3 list the JWTs for accounts - none present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "jwt/operator/op1/account/",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{}, resp.Data)
// 2. During creating of the operator issue, the operators nkey and JWT is created. However the operators JWT
// is missing information about the system account.
// 2.1 create operator with no sys account
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: "issue/operator/op1",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
// 2.2 list the operators - op1 should be present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "issue/operator/",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{
"keys": []string{"op1"},
}, resp.Data)
// 2.3 list the JWTs for operators - op1 should be present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "jwt/operator/",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{
"keys": []string{"op1"},
}, resp.Data)
// 2.4 list the nkeys for operators - op1 should be present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "nkey/operator/",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{
"keys": []string{"op1"},
}, resp.Data)
// 2.5 check the operator JWT - no sys account information present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "jwt/operator/op1",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
stm.MapToStruct(resp.Data, ¤t)
op1Claims, err := jwt.DecodeOperatorClaims(current.JWT)
assert.NoError(t, err)
assert.Equal(t, op1Claims.SystemAccount, "")
// 3. After creating the system account, the operators JWT is updated with the system account information and
// all accounts JWTs are created referencing the operators public key as issuer in the JWT claims.
// 3.1 create sys account
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: "issue/operator/op1/account/" + DefaultSysAccountName,
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
// 3.2 list the accounts - ac1 and SYS should be present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "issue/operator/op1/account/",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{
"keys": []string{"ac1", DefaultSysAccountName},
}, resp.Data)
// 3.3 list the JWTs for accounts - ac1 and SYS should be present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "jwt/operator/op1/account/",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Equal(t, map[string]interface{}{
"keys": []string{"ac1", DefaultSysAccountName},
}, resp.Data)
// 3.4 check the operator JWT - sys account information present
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "jwt/operator/op1",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
stm.MapToStruct(resp.Data, ¤t)
op1Claims, err = jwt.DecodeOperatorClaims(current.JWT)
assert.NoError(t, err)
// get sys account public key
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "jwt/operator/op1/account/" + DefaultSysAccountName,
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
var sysAccount JWTData
stm.MapToStruct(resp.Data, &sysAccount)
sysAccountClaims, err := jwt.DecodeAccountClaims(sysAccount.JWT)
assert.NoError(t, err)
assert.Equal(t, op1Claims.SystemAccount, sysAccountClaims.Subject)
// 3.5 check account ac1 JWT that issuer is operator public key
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "jwt/operator/op1/account/ac1",
Storage: reqStorage,
Data: map[string]interface{}{},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
var ac1 JWTData
stm.MapToStruct(resp.Data, &ac1)
ac1Claims, err := jwt.DecodeAccountClaims(ac1.JWT)
assert.NoError(t, err)
assert.Equal(t, ac1Claims.Issuer, op1Claims.Subject)
})
}
func Test_UnmarshalIssueAccountParameters(t *testing.T) {
assert := assert.New(t)
jsonClaims :=
`{
"Account": {
"Limits": {
"Subs": -1,
"Conn": -1,
"LeafNodeConn": -1,
"Data": -1,
"Payload": -1,
"WildcardExports": true,
"Imports": -1,
"Exports": -1
},
"Exports": [
{
"Name": "account-monitoring-streams",
"Subject": "$SYS.ACCOUNT.*.>",
"Type": "stream",
"AccountTokenPosition": 3,
"Info": {
"Description": "Account specific monitoring stream",
"InfoURL": "https://docs.nats.io/nats-server/configuration/sys_accounts"
}
},
{
"Name": "account-monitoring-services",
"Subject": "$SYS.ACCOUNT.*.*",
"Type": "service",
"ResponseType": "Stream",
"AccountTokenPosition": 4,
"Info": {
"Description": "Account specific monitoring stream",
"InfoURL": "https://docs.nats.io/nats-server/configuration/sys_accounts"
}
}
]
}
}`
claims := &accountv1.AccountClaims{}
err := json.Unmarshal([]byte(jsonClaims), claims)
assert.Nil(err)
fmt.Printf("%+v\n", claims)
}