This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
gatekeeper_test.go
361 lines (327 loc) · 9.59 KB
/
gatekeeper_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
package gatekeeper
import (
"encoding/json"
"os"
"testing"
"time"
"github.com/nemosupremo/vault-gatekeeper/policy"
"github.com/nemosupremo/vault-gatekeeper/scheduler"
"github.com/nemosupremo/vault-gatekeeper/scheduler/mock"
"github.com/nemosupremo/vault-gatekeeper/vault"
"github.com/nemosupremo/vault-gatekeeper/vault/unsealer"
"github.com/franela/goreq"
"github.com/segmentio/ksuid"
"github.com/spf13/viper"
)
const rootPolicy = `{
"*":{
"roles":["wildcard"],
"num_uses": 1
},
"x":{
"roles":["invalid"],
"num_uses": 1
}
}`
const subPolicy = `{
"foo":{
"roles":["bar"],
"num_uses": 2
},
"x":{
"roles":["valid"],
"num_uses": 1
}
}`
var vaultToken = os.Getenv("VAULT_TOKEN")
var vaultAddr = os.Getenv("VAULT_ADDR")
func init() {
if vaultAddr == "" {
vaultAddr = "http://localhost:8200"
}
viper.SetDefault("vault-addr", vaultAddr)
}
func TestLoadPolicyV2(t *testing.T) {
secretPath := ksuid.New().String()
r, err := vault.Request{
goreq.Request{
Uri: vault.Path("/v1/sys/mounts/" + secretPath),
Method: "POST",
Body: struct {
Type string `json:"type"`
Options map[string]string `json:"options"`
}{"kv", map[string]string{"version": "2"}},
MaxRedirects: 10,
RedirectHeaders: true,
}.WithHeader("X-Vault-Token", vaultToken),
}.Do()
if err != nil || (r.StatusCode != 200 && r.StatusCode != 204) {
t.Fatalf("failed to mount v2 secret backend.")
}
pathKey := ksuid.New().String()
for i, policy := range []string{rootPolicy, subPolicy} {
var path string
switch i {
case 0:
path = "/v1/" + secretPath + "/data/" + pathKey
case 1:
path = "/v1/" + secretPath + "/data/" + pathKey + "/foo/bar"
default:
t.Fatalf("misconfigured test.")
}
if err := installPolicy(path, policy); err != nil {
if verr, ok := err.(vault.Error); ok {
t.Fatalf("Could not upload policy to vault: %v", verr)
} else {
t.Fatalf("Failed to upload policy to vault: %v", err)
}
}
}
g := &Gatekeeper{}
g.config.PolicyPath = "/v1/" + secretPath + "/data/" + pathKey
g.config.Vault.KvVersion = "2"
g.Token = vaultToken
if policies, err := g.loadPolicies(); err == nil {
mustGet := func(p *policy.Policy, ok bool) *policy.Policy {
if ok {
return p
}
t.Fatalf("Did not find a matching policy")
return nil
}
if !mustGet(policies.Get("default")).Has("wildcard") {
t.Fatalf("Expected default role to have wildcard.")
}
if !mustGet(policies.Get("foo")).Has("bar") {
t.Fatalf("Expected foo policy to have bar role.")
}
if mustGet(policies.Get("x")).Has("invalid") {
t.Fatalf("Expected x policy to not have invalid role.")
}
if !mustGet(policies.Get("x")).Has("valid") {
t.Fatalf("Expected x policy to have valid role.")
}
} else {
t.Fatalf("Loading policies failed: %v", err)
}
}
func TestLoadPolicyV1(t *testing.T) {
secretPath := ksuid.New().String()
r, err := vault.Request{
goreq.Request{
Uri: vault.Path("/v1/sys/mounts/" + secretPath),
Method: "POST",
Body: struct {
Type string `json:"type"`
Options map[string]string `json:"options"`
}{"kv", map[string]string{"version": "1"}},
MaxRedirects: 10,
RedirectHeaders: true,
}.WithHeader("X-Vault-Token", vaultToken),
}.Do()
if err != nil || (r.StatusCode != 200 && r.StatusCode != 204) {
t.Fatalf("failed to mount v1 secret backend.")
}
pathKey := ksuid.New().String()
for i, policy := range []string{rootPolicy, subPolicy} {
var path string
switch i {
case 0:
path = "/v1/" + secretPath + "/" + pathKey
case 1:
path = "/v1/" + secretPath + "/" + pathKey + "/foo/bar"
default:
t.Fatalf("misconfigured test.")
}
if err := installPolicy(path, policy); err != nil {
if verr, ok := err.(vault.Error); ok {
t.Fatalf("Could not upload policy to vault: %v", verr)
} else {
t.Fatalf("Failed to upload policy to vault: %v", err)
}
}
}
g := &Gatekeeper{}
g.config.PolicyPath = "/v1/" + secretPath + "/" + pathKey
g.config.Vault.KvVersion = "1"
g.Token = vaultToken
if policies, err := g.loadPolicies(); err == nil {
mustGet := func(p *policy.Policy, ok bool) *policy.Policy {
if ok {
return p
}
t.Fatalf("Did not find a matching policy")
return nil
}
if !mustGet(policies.Get("default")).Has("wildcard") {
t.Fatalf("Expected default role to have wildcard.")
}
if !mustGet(policies.Get("foo")).Has("bar") {
t.Fatalf("Expected foo policy to have bar role.")
}
if mustGet(policies.Get("x")).Has("invalid") {
t.Fatalf("Expected x policy to not have invalid role.")
}
if !mustGet(policies.Get("x")).Has("valid") {
t.Fatalf("Expected x policy to have valid role.")
}
} else {
t.Fatalf("Loading policies failed: %v", err)
}
}
func installPolicy(path string, policy string) error {
r, err := vault.Request{
goreq.Request{
Uri: vault.Path(path),
MaxRedirects: 10,
RedirectHeaders: true,
Body: struct {
Data json.RawMessage `json:"data"`
}{json.RawMessage(policy)},
ContentType: "application/json",
Method: "POST",
}.WithHeader("X-Vault-Token", vaultToken),
}.Do()
if err == nil {
defer r.Body.Close()
switch r.StatusCode {
case 200, 204:
return nil
default:
var e vault.Error
e.Code = r.StatusCode
if err := r.Body.FromJsonTo(&e); err == nil {
return e
} else {
return err
}
}
} else {
return err
}
}
func createAuthEndpoint(authType string) (string, error) {
authPath := ksuid.New().String()
r, err := vault.Request{
goreq.Request{
Uri: vault.Path("/v1/sys/auth/" + authPath),
Method: "POST",
Body: struct {
Type string `json:"type"`
}{authType},
MaxRedirects: 10,
RedirectHeaders: true,
}.WithHeader("X-Vault-Token", vaultToken),
}.Do()
if err == nil {
defer r.Body.Close()
if r.StatusCode == 200 || r.StatusCode == 204 {
return authPath, nil
} else {
var e vault.Error
e.Code = r.StatusCode
if err := r.Body.FromJsonTo(&e); err == nil {
return "", e
} else {
return "", err
}
}
} else {
return "", err
}
}
const mockPolicy = `{
"mock:*":{
"roles":["test_role"],
"num_uses":1
},
"mock:special":{
"roles":["test_role", "{{name}}"],
"num_uses":1
}
}`
func TestRequestToken(t *testing.T) {
mock.ValidTaskId = ksuid.New().String()
var authPath string
if ap, err := createAuthEndpoint("approle"); err == nil {
authPath = ap
} else {
t.Fatalf("Failed to initialize approle endpoint: %v", err)
}
policyPath := "v1/secret/data/" + ksuid.New().String()
for _, appRoleName := range []string{"mock", "test_role", "special"} {
r, err := vault.Request{goreq.Request{
Uri: vault.Path("/v1/auth/" + authPath + "/role/" + appRoleName),
MaxRedirects: 10,
RedirectHeaders: true,
Body: struct {
Policies string `json:"policies"`
}{"unseal"},
ContentType: "application/json",
Method: "POST",
}.WithHeader("X-Vault-Token", vaultToken)}.Do()
if err != nil || (r.StatusCode != 200 && r.StatusCode != 204) {
t.Fatalf("failed to create app role for testing")
}
}
if err := installPolicy(policyPath, mockPolicy); err != nil {
if verr, ok := err.(vault.Error); ok {
t.Fatalf("Could not upload policy to vault: %v", verr)
} else {
t.Fatalf("Failed to upload policy to vault: %v", err)
}
}
conf := Config{
Schedulers: []string{"mock"},
Store: "memory",
PolicyPath: policyPath,
MaxTaskLife: 1 * time.Minute,
Unsealer: unsealer.TokenUnsealer{vaultToken},
}
conf.Vault.Address = vaultAddr
conf.Vault.KvVersion = "2"
conf.Vault.AppRoleMount = authPath
if g, err := NewGatekeeper(conf); err == nil && g.IsUnsealed() {
if token, _, err := g.RequestToken("mock", mock.ValidTaskId, "", ""); err == nil {
if _, err := (unsealer.WrappedTokenUnsealer{token}).Token(); err != nil {
t.Fatalf("Wrapped token requested from gatekeeper could not be unwrapped: %v", err)
}
} else {
t.Fatalf("Failed to request token: %v", err)
}
if _, _, err := g.RequestToken("mock", mock.ValidTaskId, "", ""); err != ErrMaxTokensGiven {
t.Fatalf("Token request should have failed with ErrMaxTokensGiven: %v", err)
}
mock.ValidTaskId = ksuid.New().String()
if _, _, err := g.RequestToken("mock", mock.ValidTaskId, "super-role", ""); err != ErrRoleMismatch {
t.Fatalf("Token request should have failed with ErrRoleMismatch: %v", err)
}
mock.ValidTaskId = ksuid.New().String()
if _, _, err := g.RequestToken("mock", mock.ValidTaskId, "{{name}}", ""); err != ErrRoleMismatch {
t.Fatalf("Token request should have failed with ErrRoleMismatch: %v", err)
}
mock.ValidTaskId = "special"
if _, _, err := g.RequestToken("mock", mock.ValidTaskId, "{{name}}", ""); err != nil {
t.Fatalf("Token request should have succeeded with {{name}}: %v", err)
}
mock.ValidTaskId = "localhost"
g.config.HostCheck = true
if _, _, err := g.RequestToken("mock", mock.ValidTaskId, "", "localhost"); err != nil {
t.Fatalf("Token request should have succeeded: %v", err)
}
if _, _, err := g.RequestToken("mock", mock.ValidTaskId, "", "172.217.9.78"); err != ErrHostMismatch {
t.Fatalf("Token request should have failed with ErrHostMismatch: %v", err)
}
g.config.HostCheck = false
if _, _, err := g.RequestToken("mock", ksuid.New().String(), "", ""); err != scheduler.ErrTaskNotFound {
t.Fatalf("Unknown task should have failed: %v", err)
}
if _, _, err := g.RequestToken("mock", "expired", "", ""); err != ErrTaskNotFresh {
t.Fatalf("Expired task should have returned task not fresh: %v", err)
}
} else if err == nil {
t.Fatalf("Failed to create gatekeeper instance: could not unseal.")
} else {
t.Fatalf("Failed to create gatekeeper instance: %v", err)
}
}