forked from edgefarm/vault-plugin-secrets-nats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths_test.go
108 lines (88 loc) · 2.74 KB
/
paths_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
package natsbackend
import (
"context"
"testing"
"github.com/hashicorp/vault/sdk/logical"
"github.com/nats-io/jwt/v2"
"github.com/nats-io/nkeys"
"github.com/stretchr/testify/assert"
)
func cOperatorJWT(operatorSeed string) string {
operatorKey, _ := nkeys.FromSeed([]byte(operatorSeed))
pub, _ := operatorKey.PublicKey()
claim := jwt.NewOperatorClaims(pub)
encoded, _ := claim.Encode(operatorKey)
return encoded
}
// we need to test importing nkeys and jwts to issue new accounts and users
func TestXxx(t *testing.T) {
b, reqStorage := getTestBackend(t)
t.Run("import operator nkey", func(t *testing.T) {
// create operator seed
seed := genOperatorSeed()
// import operator nkey
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: "nkey/operator/op123",
Storage: reqStorage,
Data: map[string]interface{}{
"seed": seed,
},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
// read operator nkey
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "nkey/operator/op123",
Storage: reqStorage,
})
// check operator nkey is the same as imported
assert.NoError(t, err)
assert.False(t, resp.IsError())
// import operator jwt
operatorJWT := cOperatorJWT(seed)
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: "jwt/operator/op123",
Storage: reqStorage,
Data: map[string]interface{}{
"jwt": operatorJWT,
},
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
// issue account from operator
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.CreateOperation,
Path: "issue/operator/op123/account/acc123",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
// get issued account
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "issue/operator/op123/account/acc123",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
// get issued account jwt
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "jwt/operator/op123/account/acc123",
Storage: reqStorage,
})
assert.NoError(t, err)
assert.False(t, resp.IsError())
// unmarshal jwt
j, _ := jwt.Decode(resp.Data["jwt"].(string))
iss := j.Claims().Issuer
// get pubkey from operator seed
operatorKey, _ := nkeys.FromSeed([]byte(seed))
pub, _ := operatorKey.PublicKey()
// check account is issued by operator pubkey
assert.Equal(t, pub, iss)
})
}