-
Notifications
You must be signed in to change notification settings - Fork 14
/
backend_test.go
297 lines (239 loc) · 7.59 KB
/
backend_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
// Copyright © 2019, Oracle and/or its affiliates.
package ociauth
import (
"context"
"fmt"
"os"
"reflect"
"testing"
"time"
"github.com/hashicorp/vault/sdk/logical"
)
const (
DevRole = "devrole"
OpsRole = "opsrole"
KnowledgeWorkerRole = "kwrole"
NonExistentRole = "nonrole"
envVarHomeTenancyID = "HOME_TENANCY_ID"
envVarRoleOCIDList = "ROLE_OCID_LIST"
)
func createHomeTenancy(t *testing.T, backendConfig *logical.BackendConfig, backend logical.Backend) {
t.Helper()
var resp *logical.Response
var err error
configPath := "config"
homeTenancyId := os.Getenv(envVarHomeTenancyID)
if homeTenancyId == "" {
t.Fatalf("%s is not set", envVarHomeTenancyID)
}
configData := map[string]interface{}{
HomeTenancyIdConfigName: homeTenancyId,
}
configReq := &logical.Request{
Operation: logical.CreateOperation,
Storage: backendConfig.StorageView,
Data: configData,
}
configReq.Path = configPath
resp, err = backend.HandleRequest(context.Background(), configReq)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("bad: config creation failed. resp:%#v\n err:%v", resp, err)
}
}
func initTest(t *testing.T) (backend logical.Backend, config *logical.BackendConfig, err error) {
t.Helper()
defaultLeaseTTLVal := time.Hour * 24
maxLeaseTTLVal := time.Hour * 24 * 2
config = &logical.BackendConfig{
Logger: nil,
System: &logical.StaticSystemView{
DefaultLeaseTTLVal: defaultLeaseTTLVal,
MaxLeaseTTLVal: maxLeaseTTLVal,
},
StorageView: &logical.InmemStorage{},
}
backend, err = Factory(context.Background(), config)
if err != nil {
return
}
createHomeTenancy(t, config, backend)
roleOCIDList := os.Getenv(envVarRoleOCIDList)
if roleOCIDList == "" {
return nil, nil, fmt.Errorf("%s is not set", envVarRoleOCIDList)
}
// Create the devRole
devRoleData := map[string]interface{}{
"description": DevRole + " description",
"ocid_list": roleOCIDList,
"token_policies": "policy1,policy2",
"token_ttl": 1500,
}
err = createRole(devRoleData, DevRole, backend, config)
if err != nil {
return
}
// Create the opsRole
opsRoleData := map[string]interface{}{
"description": OpsRole + " description",
"ocid_list": "",
"token_policies": "policy3",
"token_ttl": 1000,
}
err = createRole(opsRoleData, OpsRole, backend, config)
if err != nil {
return
}
// Create the knowledgeWorkerRole
knowledgeWorkerRole := map[string]interface{}{
"description": KnowledgeWorkerRole + " description",
"ocid_list": roleOCIDList,
"token_policies": "policy1,policy5",
"token_ttl": 1000,
}
err = createRole(knowledgeWorkerRole, KnowledgeWorkerRole, backend, config)
if err != nil {
return
}
return
}
func createRole(roleData map[string]interface{}, roleName string, backend logical.Backend, config *logical.BackendConfig) error {
roleRequest := &logical.Request{
Operation: logical.CreateOperation,
Storage: config.StorageView,
Data: roleData,
}
roleRequest.Path = "role/" + roleName
response, err := backend.HandleRequest(context.Background(), roleRequest)
if err != nil || (response != nil && response.IsError()) {
return fmt.Errorf("bad: role creation failed. resp:%#v\n err:%v", response, err)
}
return nil
}
func TestBackEnd_ValidateUserApiKeyLogin(t *testing.T) {
// Skip tests if we are not running acceptance tests
if os.Getenv("VAULT_ACC") == "" {
t.SkipNow()
}
cmdMap := map[string]string{
"auth_type": "apikey",
"role": DevRole,
}
makeRequestAndValidateResponse(t, cmdMap, false, 1500*time.Second, []string{"policy1", "policy2"})
cmdMap = map[string]string{
"auth_type": "apikey",
"role": KnowledgeWorkerRole,
}
makeRequestAndValidateResponse(t, cmdMap, false, 1000*time.Second, []string{"policy1", "policy5"})
}
func TestBackEnd_ValidateUserApiKeyLoginNotInRole(t *testing.T) {
// Skip tests if we are not running acceptance tests
if os.Getenv("VAULT_ACC") == "" {
t.SkipNow()
}
cmdMap := map[string]string{
"auth_type": "apikey",
"role": OpsRole,
}
makeRequestAndValidateResponse(t, cmdMap, true, 1500*time.Second, []string{})
}
func TestBackEnd_ValidateUserApiKeyLoginNonExistentRole(t *testing.T) {
// Skip tests if we are not running acceptance tests
if os.Getenv("VAULT_ACC") == "" {
t.SkipNow()
}
cmdMap := map[string]string{
"auth_type": "apikey",
"role": NonExistentRole,
}
makeRequestAndValidateResponse(t, cmdMap, true, 1500*time.Second, []string{})
}
func TestBackEnd_ValidateInstancePrincipalLogin(t *testing.T) {
// Skip tests if we are not running acceptance tests
if os.Getenv("VAULT_ACC") == "" {
t.SkipNow()
}
cmdMap := map[string]string{
"auth_type": "ip",
"role": DevRole,
}
makeRequestAndValidateResponse(t, cmdMap, false, 1500*time.Second, []string{"policy1", "policy2"})
cmdMap = map[string]string{
"auth_type": "ip",
"role": KnowledgeWorkerRole,
}
makeRequestAndValidateResponse(t, cmdMap, false, 1000*time.Second, []string{"policy1", "policy5"})
}
func TestBackEnd_ValidateInstancePrincipalLoginNotInRole(t *testing.T) {
// Skip tests if we are not running acceptance tests
if os.Getenv("VAULT_ACC") == "" {
t.SkipNow()
}
cmdMap := map[string]string{
"auth_type": "ip",
"role": OpsRole,
}
makeRequestAndValidateResponse(t, cmdMap, true, 1500*time.Second, []string{})
}
func TestBackEnd_ValidateInstancePrincipalLoginNonExistentRole(t *testing.T) {
// Skip tests if we are not running acceptance tests
if os.Getenv("VAULT_ACC") == "" {
t.SkipNow()
}
cmdMap := map[string]string{
"auth_type": "ip",
"role": NonExistentRole,
}
makeRequestAndValidateResponse(t, cmdMap, true, 1500*time.Second, []string{})
}
func makeRequestAndValidateResponse(t *testing.T, cmdMap map[string]string, expectFailure bool, expectedTTL time.Duration, expectedPolicies []string) {
t.Helper()
role := cmdMap["role"]
path := fmt.Sprintf(PathBaseFormat, "oci", role)
signingPath := PathVersionBase + path
backend, config, err := initTest(t)
if err != nil {
t.Fatalf("initTest failed: %s", err)
}
loginData, err := CreateLoginData("http://127.0.0.1", cmdMap, signingPath)
if err != nil {
t.Fatalf("CreateLoginData failed: %s", err)
}
loginRequest := &logical.Request{
Operation: logical.UpdateOperation,
Path: "login/" + role,
Storage: config.StorageView,
Data: loginData,
}
response, err := backend.HandleRequest(context.Background(), loginRequest)
if err != nil {
t.Fatalf("Test failed, got error: resp:%#v\n err:%v", response, err)
}
if response == nil || (response != nil && response.IsError()) {
if expectFailure {
return
} else {
t.Fatalf("Test failure: unexpected response: %#v\n", response)
}
} else {
if expectFailure {
if response.Data["http_status_code"] != 401 {
t.Fatalf("Expected failure, but the request succeeded. Test Failed. Response: %#v\n", response)
}
return
}
}
if response == nil || response.Auth == nil {
t.Fatalf("Failed response is nil")
}
if response.Auth.TTL != expectedTTL {
t.Fatalf("Failed! Expected TTL: %#v Got TTL: %#v resp: %#v\n", response.Auth.TTL, expectedTTL, response)
}
if len(expectedPolicies) != len(response.Auth.Policies) {
t.Fatalf("Failed! Expected Policies: %#v Got Policies: %#v resp: %#v\n", expectedPolicies, response.Auth.Policies, response)
}
expectedPolicyMap := sliceToMap(expectedPolicies)
responsePolicyMap := sliceToMap(response.Auth.Policies)
if !reflect.DeepEqual(responsePolicyMap, expectedPolicyMap) {
t.Fatalf("Failed Policy Comparison! Expected Policies: %#v Got Policies: %#v resp: %#v\n", expectedPolicies, response.Auth.Policies, response)
}
}