Repository navigation
Expand file tree
/
Copy pathkuma.go
More file actions
266 lines (222 loc) · 7.07 KB
/
Copy pathkuma.go
File metadata and controls
266 lines (222 loc) · 7.07 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
package kuma
import (
"context"
"encoding/base64"
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const (
kumaTokenAccountType = "kuma_token"
kumaRevocationSecret = "kuma_revocations"
kumaTokenUser = "token_user"
kumaTokenDataplane = "token_dataplane"
kumaMeshSecretDataplaneRevocation = "dataplane-token-revocations-"
kumaGlobalSecretUserRevocation = "user-token-revocations"
)
// kumaToken defines a secret to store for a given role
// and how it should be revoked or renewed.
func (b *kumaBackend) kumaToken() *framework.Secret {
return &framework.Secret{
Type: kumaTokenAccountType,
Fields: map[string]*framework.FieldSchema{
"kuma_token": {
Type: framework.TypeString,
Description: "Kuma access token",
},
},
Revoke: b.tokenRevoke,
Renew: b.tokenRenew,
}
}
// tokenRevoke removes the token from the Vault storage API and calls the client to revoke the robot account
func (b *kumaBackend) tokenRevoke(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
client, err := b.getClient(ctx, req.Storage)
if err != nil {
return nil, err
}
if client == nil {
return nil, fmt.Errorf("error getting Kuma client")
}
var tokenJTI string
// We passed the jti using InternalData from when we first created
// the secret.
jti, ok := req.Secret.InternalData["jti"]
if !ok {
return nil, fmt.Errorf("jti is missing on the lease")
}
tokenJTI, ok = jti.(string)
if !ok {
return nil, fmt.Errorf("unable convert jti")
}
var tokenMesh string
// We passed the mesh using InternalData from when we first created
// the secret.
mes, ok := req.Secret.InternalData["mesh"]
if !ok {
return nil, fmt.Errorf("mesh is missing on the lease")
}
tokenMesh, ok = mes.(string)
if !ok {
return nil, fmt.Errorf("unable convert mesh")
}
var tokenType string
// We passed the jti using InternalData from when we first created
// the secret.
typ, ok := req.Secret.InternalData["type"]
if !ok {
return nil, fmt.Errorf("type is missing on the lease")
}
tokenType, ok = typ.(string)
if !ok {
return nil, fmt.Errorf("unable convert type")
}
var tokenExpiry int64
// We passed the jti using InternalData from when we first created
// the secret.
exp, ok := req.Secret.InternalData["expiry"]
if !ok {
return nil, fmt.Errorf("expiry is missing on the lease")
}
// convert the expiry
switch exp.(type) {
case int:
tokenExpiry = int64(exp.(int))
case int64:
tokenExpiry = exp.(int64)
case float64:
tokenExpiry = int64(exp.(float64))
default:
return nil, fmt.Errorf("unable to convert expiry to int64 %s", reflect.TypeOf(exp))
}
// if the ttl on the token has not elapsed we need to add this token to
// the revocation list
b.Logger().Info("Revoking Token", "jti", tokenJTI, "type", tokenType, "mesh", tokenMesh, "expiry", time.Unix(0, tokenExpiry).String())
if time.Now().Sub(time.Unix(0, tokenExpiry)) < 0 {
b.Logger().Info("Token has not expired, revoke token in Kuma API", "jti", tokenJTI, "type", tokenType, "mesh", tokenMesh, "expiry", time.Unix(0, tokenExpiry).String())
if err := revokeToken(ctx, client, req.Storage, tokenType, tokenJTI, tokenMesh, tokenExpiry); err != nil {
return nil, fmt.Errorf("error revoking kuma token: %w", err)
}
}
return nil, nil
}
type RevocationListSecret struct {
Tokens []RevocationToken `json:"tokens"`
}
type RevocationToken struct {
JTI string `json:"jti"`
Mesh string `json:"mesh"`
Expiry int64 `json:"expiry"`
Type string `json:"type"`
}
// revokeToken adds the jti for the token to the revocation list and
func revokeToken(ctx context.Context, c *kumaClient, storage logical.Storage, tokenType, jti, mesh string, expiry int64) error {
revList := &RevocationListSecret{}
// first get the existing revocation list secret
if tokenType == kumaTokenDataplane {
err := revokeDataPlaneToken(ctx, c, jti, mesh)
if err != nil {
return err
}
}
if tokenType == kumaTokenUser {
err := revokeUserToken(ctx, c, jti)
if err != nil {
return err
}
}
// we now need to add the token details to the internal secret, so that we can clean the
// revocation list later
se, err := storage.Get(ctx, kumaRevocationSecret)
if err != nil {
return fmt.Errorf("unable to get revocations from internal storage: %s", err)
}
if se != nil {
se.DecodeJSON(revList)
}
// add this token to the list
revList.Tokens = append(
revList.Tokens,
RevocationToken{
JTI: jti,
Mesh: mesh,
Expiry: expiry,
Type: tokenType,
})
// update the secret
se, err = logical.StorageEntryJSON(kumaRevocationSecret, revList)
if err != nil {
return fmt.Errorf("unable to marshal revocation list: %s", err)
}
err = storage.Put(ctx, se)
if err != nil {
return fmt.Errorf("unable to store revocation list: %s", err)
}
return nil
}
func revokeDataPlaneToken(ctx context.Context, c *kumaClient, jti, mesh string) error {
jtis := []string{}
data, err := c.meshSecretClient.Get(mesh, kumaMeshSecretDataplaneRevocation+mesh)
if err != nil && err != SecretNotFound {
return fmt.Errorf("unable to get revocation token secret: %s", err)
}
// if we have existing secret merge
if err == nil {
jtiBytes, _ := base64.StdEncoding.DecodeString(data)
jtis = strings.Split(string(jtiBytes), ",")
}
jtis = append(jtis, jti)
data = base64.StdEncoding.EncodeToString([]byte(strings.Join(jtis, ",")))
err = c.meshSecretClient.Put(mesh, kumaMeshSecretDataplaneRevocation+mesh, string(data))
if err != nil {
return fmt.Errorf("unable to add jti to revocation list: %s", err)
}
return nil
}
func revokeUserToken(ctx context.Context, c *kumaClient, jti string) error {
jtis := []string{}
data, err := c.globalSecretClient.Get(kumaGlobalSecretUserRevocation)
if err != nil && err != GlobalSecretNotFound {
return fmt.Errorf("unable to get revocation token secret: %s", err)
}
// if we have existing secret merge
if err == nil {
jtiBytes, _ := base64.StdEncoding.DecodeString(data)
jtis = strings.Split(string(jtiBytes), ",")
}
jtis = append(jtis, jti)
data = base64.StdEncoding.EncodeToString([]byte(strings.Join(jtis, ",")))
err = c.globalSecretClient.Put(kumaGlobalSecretUserRevocation, string(data))
if err != nil {
return fmt.Errorf("unable to add jti to revocation list: %s", err)
}
return nil
}
// tokenRenew
func (b *kumaBackend) tokenRenew(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
roleRaw, ok := req.Secret.InternalData["role"]
if !ok {
return nil, fmt.Errorf("secret is missing role internal data")
}
// get the role entry
role := roleRaw.(string)
roleEntry, err := b.getRole(ctx, req.Storage, role)
if err != nil {
return nil, fmt.Errorf("error retrieving role: %w", err)
}
if roleEntry == nil {
return nil, errors.New("error retrieving role: role is nil")
}
resp := &logical.Response{Secret: req.Secret}
if roleEntry.TTL > 0 {
resp.Secret.TTL = roleEntry.TTL
}
if roleEntry.MaxTTL > 0 {
resp.Secret.MaxTTL = roleEntry.MaxTTL
}
return resp, nil
}