forked from kula/vault-plugin-secrets-backblazeb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_config_rotate_test.go
99 lines (77 loc) · 2.33 KB
/
path_config_rotate_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
package vault_plugin_secrets_backblazeb2
import (
"context"
"fmt"
"os"
"testing"
b2client "github.com/Backblaze/blazer/b2"
"github.com/hashicorp/vault/sdk/logical"
)
func TestPathConfigRotateRoot(t *testing.T) {
if !runAcceptanceTests {
t.SkipNow()
}
skipIfMissingEnvVars(t,
envVarBackblazeB2ApplicationKeyID,
envVarBackblazeB2ApplicationKey,
)
client, err := b2client.NewClient(context.Background(), os.Getenv(envVarBackblazeB2ApplicationKeyID), os.Getenv(envVarBackblazeB2ApplicationKey))
if err != nil {
t.Fatalf("Unable to create b2 client: %s", err)
}
key, err := client.CreateKey(context.Background(), "test-rotation-key", b2client.Capabilities("listKeys", "writeKeys", "deleteKeys"))
if err != nil {
t.Fatalf("Unable to create test rotation key: %s", err)
}
defer func() {
err := key.Delete(context.Background())
if err != nil {
t.Errorf("Unable to delete test rotation key: %s", err)
}
}()
b, s := getTestBackend(t)
configData := map[string]interface{}{
"application_key_id": key.ID(),
"application_key": key.Secret(),
}
err = testConfigCreate(b, s, configData)
if err != nil {
t.Fatalf("Cannot create config: %s", err)
}
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "config/rotate-root",
Data: map[string]interface{}{},
Storage: s,
})
if err != nil {
t.Fatal(err)
}
if resp != nil && resp.IsError() {
t.Fatal(resp.Error())
}
config, err := b.getConfig(context.Background(), s)
if err != nil {
t.Fatal(err)
}
if config.ApplicationKeyId == "" {
t.Fatal(fmt.Errorf("application key id was empty after rotate root, it shouldn't be"))
}
if config.ApplicationKeyId == key.ID() {
t.Fatal("old and new application key ids are equal after rotate-root, it shouldn't be")
}
if config.ApplicationKey == "" {
t.Fatal("application key is empty, it shouldn't be")
}
if config.ApplicationKey == key.Secret() {
t.Fatal("old and new application keys are equal after rotate-root, it shouldn't be")
}
keys, _, err := client.ListKeys(context.Background(), 1, config.ApplicationKeyId)
for _, currentKey := range keys {
if currentKey.ID() == config.ApplicationKeyId {
if err = currentKey.Delete(context.Background()); err != nil {
t.Errorf("Unable to delete rotated key: %s", err)
}
}
}
}