-
Notifications
You must be signed in to change notification settings - Fork 7
/
paths_jwt_user.go
219 lines (192 loc) · 6.29 KB
/
paths_jwt_user.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
package natsbackend
import (
"context"
"fmt"
"github.com/edgefarm/vault-plugin-secrets-nats/pkg/stm"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/nats-io/jwt/v2"
"github.com/rs/zerolog/log"
)
func pathUserJWT(b *NatsBackend) []*framework.Path {
return []*framework.Path{
{
Pattern: "jwt/operator/" + framework.GenericNameRegex("operator") + "/account/" + framework.GenericNameRegex("account") + "/user/" + framework.GenericNameRegex("user") + "$",
Fields: map[string]*framework.FieldSchema{
"operator": {
Type: framework.TypeString,
Description: "operator identifier",
Required: false,
},
"account": {
Type: framework.TypeString,
Description: "account identifier",
Required: false,
},
"user": {
Type: framework.TypeString,
Description: "user identifier",
Required: false,
},
"jwt": {
Type: framework.TypeString,
Description: "User JWT to import.",
Required: false,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.CreateOperation: &framework.PathOperation{
Callback: b.pathAddUserJWT,
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathAddUserJWT,
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathReadUserJWT,
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathDeleteUserJWT,
},
},
HelpSynopsis: `Manages user JWT's.`,
HelpDescription: ``,
},
{
Pattern: "jwt/operator/" + framework.GenericNameRegex("operator") + "/account/" + framework.GenericNameRegex("account") + "/user/?$",
Fields: map[string]*framework.FieldSchema{
"operator": {
Type: framework.TypeString,
Description: "operator identifier",
Required: false,
},
"account": {
Type: framework.TypeString,
Description: "account identifier",
Required: false,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ListOperation: &framework.PathOperation{
Callback: b.pathListUserJWTs,
},
},
HelpSynopsis: "pathRoleListHelpSynopsis",
HelpDescription: "pathRoleListHelpDescription",
},
}
}
func (b *NatsBackend) pathAddUserJWT(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
err := data.Validate()
if err != nil {
return logical.ErrorResponse(InvalidParametersError), logical.ErrInvalidRequest
}
var params JWTParameters
err = stm.MapToStruct(data.Raw, ¶ms)
if err != nil {
return logical.ErrorResponse(DecodeFailedError), logical.ErrInvalidRequest
}
err = addUserJWT(ctx, req.Storage, params)
if err != nil {
return logical.ErrorResponse(AddingJWTFailedError), nil
}
return nil, nil
}
func (b *NatsBackend) pathReadUserJWT(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
err := data.Validate()
if err != nil {
return logical.ErrorResponse(InvalidParametersError), logical.ErrInvalidRequest
}
var params JWTParameters
err = stm.MapToStruct(data.Raw, ¶ms)
if err != nil {
return logical.ErrorResponse(DecodeFailedError), logical.ErrInvalidRequest
}
jwt, err := readUserJWT(ctx, req.Storage, params)
if err != nil {
return logical.ErrorResponse(ReadingJWTFailedError), nil
}
if jwt == nil {
return logical.ErrorResponse(JwtNotFoundError), nil
}
return createResponseJWTData(jwt)
}
func (b *NatsBackend) pathListUserJWTs(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
err := data.Validate()
if err != nil {
return logical.ErrorResponse(InvalidParametersError), logical.ErrInvalidRequest
}
var params JWTParameters
err = stm.MapToStruct(data.Raw, ¶ms)
if err != nil {
return logical.ErrorResponse(DecodeFailedError), logical.ErrInvalidRequest
}
entries, err := listUserJWTs(ctx, req.Storage, params)
if err != nil {
return logical.ErrorResponse(ListJWTsFailedError), nil
}
return logical.ListResponse(entries), nil
}
func (b *NatsBackend) pathDeleteUserJWT(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
err := data.Validate()
if err != nil {
return logical.ErrorResponse(InvalidParametersError), logical.ErrInvalidRequest
}
var params JWTParameters
err = stm.MapToStruct(data.Raw, ¶ms)
if err != nil {
return logical.ErrorResponse(DecodeFailedError), logical.ErrInvalidRequest
}
// when a key is given, store it
err = deleteUserJWT(ctx, req.Storage, params)
if err != nil {
return logical.ErrorResponse(DeleteJWTFailedError), nil
}
return nil, nil
}
func readUserJWT(ctx context.Context, storage logical.Storage, params JWTParameters) (*JWTStorage, error) {
path := getUserJWTPath(params.Operator, params.Account, params.User)
return readJWT(ctx, storage, path)
}
func deleteUserJWT(ctx context.Context, storage logical.Storage, params JWTParameters) error {
path := getUserJWTPath(params.Operator, params.Account, params.User)
return deleteJWT(ctx, storage, path)
}
func addUserJWT(ctx context.Context, storage logical.Storage, params JWTParameters) error {
log.Info().
Str("operator", params.Operator).Str("account", params.Account).Str("user", params.User).
Msg("create/update user jwt")
if params.JWT == "" {
return fmt.Errorf("user JWT is required")
} else {
err := validateJWT[jwt.UserClaims](params.JWT)
if err != nil {
return err
}
}
path := getUserJWTPath(params.Operator, params.Account, params.User)
err := addJWT(ctx, storage, path, params)
if err != nil {
return err
}
iParams := IssueUserParameters{
Operator: params.Operator,
Account: params.Account,
User: params.User,
}
issue, err := readUserIssue(ctx, storage, iParams)
if err != nil {
return err
}
if issue == nil {
//ignore error, try to create issue
addUserIssue(ctx, storage, iParams)
}
return nil
}
func listUserJWTs(ctx context.Context, storage logical.Storage, params JWTParameters) ([]string, error) {
path := getUserJWTPath(params.Operator, params.Account, "")
return listJWTs(ctx, storage, path)
}
func getUserJWTPath(operator string, account string, user string) string {
return "jwt/operator/" + operator + "/account/" + account + "/user/" + user
}