forked from manhtukhang/vault-plugin-harbor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_test.go
214 lines (184 loc) · 5.43 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
package harbor
import (
"context"
"os"
"strings"
"testing"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/require"
)
const (
envVarRunAccTests = "VAULT_ACC"
envVarHarborUsername = "TEST_HARBOR_USERNAME"
envVarHarborPassword = "TEST_HARBOR_PASSWORD"
envVarHarborURL = "TEST_HARBOR_URL"
)
// getTestBackend will help you construct a test backend object.
// Update this function with your target backend.
func getTestBackend(tb testing.TB) (*harborBackend, logical.Storage) {
tb.Helper()
config := logical.TestBackendConfig()
config.StorageView = new(logical.InmemStorage)
config.Logger = hclog.NewNullLogger()
config.System = logical.TestSystemView()
b, err := Factory(context.Background(), config)
if err != nil {
tb.Fatal(err)
}
return b.(*harborBackend), config.StorageView
}
// runAcceptanceTests will separate unit tests from
// acceptance tests, which will make active requests
// to your target API.
var runAcceptanceTests = os.Getenv(envVarRunAccTests) == "1"
// testEnv creates an object to store and track testing environment
// resources
type testEnv struct {
Username string
Password string
URL string
Backend logical.Backend
Context context.Context
Storage logical.Storage
// SecretToken tracks the API token, for checking rotations
SecretToken string
// Tokens tracks the generated tokens, to make sure we clean up
Tokens []string
}
// AddConfig adds the configuration to the test backend.
// Make sure data includes all of the configuration
// attributes you need and the `config` path!
func (e *testEnv) AddConfig(t *testing.T) {
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "config",
Storage: e.Storage,
Data: map[string]interface{}{
"username": e.Username,
"password": e.Password,
"url": e.URL,
},
}
resp, err := e.Backend.HandleRequest(e.Context, req)
require.Nil(t, resp)
require.Nil(t, err)
}
// AddRobotAccountRole adds a role for the Harbor robot account
func (e *testEnv) AddRobotAccountRole(t *testing.T) {
req := &logical.Request{
Operation: logical.UpdateOperation,
Path: "roles/test-role",
Storage: e.Storage,
Data: map[string]interface{}{
"name": "test-role",
"ttl": "30",
"max_ttl": "60",
"permissions": `
[
{
"namespace": "public",
"access":
[
{
"action": "pull",
"resource": "repository"
}
],
"kind": "project"
}
]
`,
},
}
resp, err := e.Backend.HandleRequest(e.Context, req)
require.Nil(t, resp)
require.Nil(t, err)
}
// ReadRobotAccount retrieves the robot account
// based on a Vault role.
func (e *testEnv) ReadRobotAccount(t *testing.T) {
req := &logical.Request{
Operation: logical.ReadOperation,
Path: "creds/test-role",
Storage: e.Storage,
}
resp, err := e.Backend.HandleRequest(e.Context, req)
require.Nil(t, err)
require.NotNil(t, resp)
if t, ok := resp.Data["robot_account_name"]; ok {
e.Tokens = append(e.Tokens, t.(string))
}
require.NotEmpty(t, resp.Data["robot_account_name"])
if e.SecretToken != "" {
require.NotEqual(t, e.SecretToken, resp.Data["robot_account_name"])
}
// collect secret IDs to revoke at end of test
require.NotNil(t, resp.Secret)
if t, ok := resp.Secret.InternalData["robot_account_name"]; ok {
e.SecretToken = t.(string)
}
}
// RevokeRobotAccount revokes the robot account
// based on a Vault role.
func (e *testEnv) RevokeRobotAccount(t *testing.T) {
readReq := &logical.Request{
Operation: logical.ReadOperation,
Path: "creds/test-role",
Storage: e.Storage,
}
resp, err := e.Backend.HandleRequest(e.Context, readReq)
require.Nil(t, err)
require.NotNil(t, resp)
secret := resp.Secret
revokeReq := &logical.Request{
Operation: logical.RevokeOperation,
Secret: secret,
Storage: e.Storage,
}
resp, err = e.Backend.HandleRequest(e.Context, revokeReq)
require.NoError(t, err)
require.Nil(t, resp)
}
// RenewRobotAccount renews the robot account
// based on a Vault role.
func (e *testEnv) RenewRobotAccount(t *testing.T) {
readReq := &logical.Request{
Operation: logical.ReadOperation,
Path: "creds/test-role",
Storage: e.Storage,
}
resp, err := e.Backend.HandleRequest(e.Context, readReq)
require.Nil(t, err)
require.NotNil(t, resp)
if t, ok := resp.Data["robot_account_name"]; ok {
e.Tokens = append(e.Tokens, t.(string))
}
secret := resp.Secret
renewReq := &logical.Request{
Operation: logical.RenewOperation,
Secret: secret,
Storage: e.Storage,
}
resp, err = e.Backend.HandleRequest(e.Context, renewReq)
require.NoError(t, err)
require.NotNil(t, resp)
}
// CleanupRobotAccounts removes the robot account
// when the test completes.
func (e *testEnv) CleanupRobotAccounts(t *testing.T) {
if len(e.Tokens) == 0 {
t.Fatalf("expected 2 tokens, got: %d", len(e.Tokens))
}
for _, token := range e.Tokens {
tk := strings.Split(token, "$")[1]
b := e.Backend.(*harborBackend)
client, err := b.getClient(e.Context, e.Storage)
if err != nil {
t.Fatal("fatal getting client")
}
if err := client.DeleteRobotAccountByName(context.Background(), tk); err != nil {
t.Fatalf("unexpected error deleting user token: %s", err)
}
}
}