forked from kula/vault-plugin-secrets-backblazeb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_test.go
156 lines (131 loc) · 4.05 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
package vault_plugin_secrets_backblazeb2
import (
"context"
"os"
"testing"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/require"
)
const (
envVarRunAccTests = "VAULT_ACC"
envVarBackblazeB2ApplicationKeyID = "TEST_BACKBLAZEB2_APPLICATION_KEY_ID"
envVarBackblazeB2ApplicationKey = "TEST_BACKBLAZEB2_APPLICATION_KEY"
)
func getTestBackend(tb testing.TB) (*backblazeB2Backend, logical.Storage) {
tb.Helper()
config := logical.TestBackendConfig()
config.StorageView = new(logical.InmemStorage)
config.Logger = hclog.NewNullLogger()
config.System = logical.TestSystemView()
b, err := Factory("test")(context.Background(), config)
if err != nil {
tb.Fatal(err)
}
return b.(*backblazeB2Backend), 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"
// testCloudEnv creates an object to store and track testing environment
// resources.
type testCloudEnv struct {
ApplicationKeyID string
ApplicationKey string
Backend logical.Backend
Context context.Context
Storage logical.Storage
// SecretToken tracks the API token, for checking rotations.
SecretToken string
// Tracks the generated application keys, to make sure we clean up.
ApplicationKeyIDs []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 *testCloudEnv) AddConfig(t *testing.T) {
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "config",
Storage: e.Storage,
Data: map[string]interface{}{
"application_key_id": e.ApplicationKeyID,
"application_key": e.ApplicationKey,
},
}
resp, err := e.Backend.HandleRequest(e.Context, req)
require.Nil(t, resp)
require.Nil(t, err)
}
func (e *testCloudEnv) AddRole(t *testing.T) {
req := &logical.Request{
Operation: logical.UpdateOperation,
Path: "roles/test-role",
Storage: e.Storage,
Data: map[string]interface{}{
"capabilities": []string{"listFiles", "readFiles", "writeFiles"},
},
}
resp, err := e.Backend.HandleRequest(e.Context, req)
require.Nil(t, resp)
require.Nil(t, err)
}
func (e *testCloudEnv) ReadApplicationKey(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)
require.NotEmpty(t, resp.Secret.InternalData["application_key_id"])
require.NotNil(t, resp.Secret)
require.NotEmpty(t, resp.Data["application_key_id"])
require.NotEmpty(t, resp.Data["application_key"])
if e.SecretToken != "" {
require.NotEqual(t, e.SecretToken, resp.Data["application_key"])
}
e.SecretToken = resp.Data["application_key"].(string)
e.ApplicationKeyIDs = append(e.ApplicationKeyIDs, resp.Secret.InternalData["application_key_id"].(string))
}
func (e *testCloudEnv) VerifyNumberOfIssuedCredentials(t *testing.T) {
if len(e.ApplicationKeyIDs) != 2 {
t.Fatalf("expected 2 application keys, got: %d", len(e.ApplicationKeyIDs))
}
}
func (e *testCloudEnv) CleanupCreds(t *testing.T) {
if len(e.ApplicationKeyIDs) <= 0 {
return
}
b := e.Backend.(*backblazeB2Backend)
client, err := b.getB2Client(e.Context, e.Storage)
if err != nil {
t.Fatal("error getting client")
}
for _, id := range e.ApplicationKeyIDs {
keys, _, err := client.ListKeys(context.Background(), 1, id)
if err != nil {
t.Fatalf("error listing keys: %s", err)
}
// We should only get one, but verify
for _, key := range keys {
if key.ID() == id {
err = key.Delete(context.Background())
if err != nil {
t.Fatalf("error deleting key: %s", err)
}
break
}
}
}
}
func skipIfMissingEnvVars(t *testing.T, envVars ...string) {
t.Helper()
for _, envVar := range envVars {
if os.Getenv(envVar) == "" {
t.Skipf("Missing env variable: [%s] - skipping test", envVar)
}
}
}