-
Notifications
You must be signed in to change notification settings - Fork 14
/
path_role_test.go
143 lines (121 loc) · 3.71 KB
/
path_role_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
// Copyright © 2019, Oracle and/or its affiliates.
package ociauth
import (
"context"
"strconv"
"testing"
"github.com/hashicorp/vault/sdk/logical"
"os"
)
func TestBackend_PathRoles(t *testing.T) {
// Skip tests if we are not running acceptance tests
if os.Getenv("VAULT_ACC") == "" {
t.SkipNow()
}
var resp *logical.Response
var err error
config := logical.TestBackendConfig()
config.StorageView = &logical.InmemStorage{}
b, err := Backend()
if err := b.Setup(context.Background(), config); err != nil {
t.Fatal(err)
}
roleData := map[string]interface{}{
"description": "My dev role",
"ocid_list": "ocid1,ocid2",
"token_policies": "policy1,policy2",
"token_ttl": 1500,
}
roleReq := &logical.Request{
Operation: logical.CreateOperation,
Storage: config.StorageView,
Data: roleData,
}
numRoles := 10
baseRolePath := "role/devrole"
// first create the roles
for i := 1; i <= numRoles; i++ {
roleReq.Path = baseRolePath + strconv.Itoa(i)
resp, err = b.HandleRequest(context.Background(), roleReq)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("Role creation failed. resp:%#v\n err:%v", resp, err)
}
}
// now read the roles
for i := 1; i <= numRoles; i++ {
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: baseRolePath + strconv.Itoa(i),
Storage: config.StorageView,
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("Read roles failed. resp:%#v\n err:%v", resp, err)
}
}
// now update the roles
roleDataUpdate := map[string]interface{}{
"description": "My developer role",
"ocid_list": "ocid3",
"token_policies": "ocid1",
"token_ttl": 1000,
}
roleReqUpdate := &logical.Request{
Operation: logical.UpdateOperation,
Storage: config.StorageView,
Data: roleDataUpdate,
}
for i := 1; i <= numRoles; i++ {
roleReqUpdate.Path = baseRolePath + strconv.Itoa(i)
resp, err = b.HandleRequest(context.Background(), roleReqUpdate)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("Role update failed. resp:%#v\n err:%v", resp, err)
}
}
// now read the roles again
for i := 1; i <= numRoles; i++ {
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: baseRolePath + strconv.Itoa(i),
Storage: config.StorageView,
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("Read roles failed. resp:%#v\n err:%v", resp, err)
}
}
// now list the roles
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "role/",
Storage: config.StorageView,
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("Listing roles failed. resp:%#v\n err:%v", resp, err)
}
if len(resp.Data["keys"].([]string)) != numRoles {
t.Fatalf("Failed to list all the roles")
}
// now delete half the roles
for i := 1; i <= 5; i++ {
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.DeleteOperation,
Path: baseRolePath + strconv.Itoa(i),
Storage: config.StorageView,
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("Read roles failed. resp:%#v\n err:%v", resp, err)
}
}
// now list the roles again
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "role/",
Storage: config.StorageView,
})
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("Listing roles failed. resp:%#v\n err:%v", resp, err)
}
roleCount := len(resp.Data["keys"].([]string))
if roleCount != 5 {
t.Fatalf("Failed to list the expected number of roles")
}
}