forked from hashicorp/vault-plugin-secrets-azure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_rotate_root_test.go
196 lines (164 loc) · 4.15 KB
/
path_rotate_root_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package azuresecrets
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/hashicorp/vault/sdk/logical"
)
func TestRotateRootSuccess(t *testing.T) {
b, s := getTestBackend(t)
skipIfMissingEnvVars(t,
"AZURE_CLIENT_ID",
"AZURE_CLIENT_SECRET",
"AZURE_TENANT_ID",
)
configData := map[string]interface{}{
"tenant_id": os.Getenv("AZURE_TENANT_ID"),
"client_id": os.Getenv("AZURE_CLIENT_ID"),
"client_secret": os.Getenv("AZURE_CLIENT_SECRET"),
}
testConfigCreate(t, b, s, configData, false)
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "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.ClientSecret == "" {
t.Fatal(fmt.Errorf("root password was empty after rotate root, it shouldn't be"))
}
if config.NewClientSecret == config.ClientSecret {
t.Fatal("old and new password equal after rotate-root, it shouldn't be")
}
if config.NewClientSecret == "" {
t.Fatal("new password is empty, it shouldn't be")
}
if config.NewClientSecretKeyID == "" {
t.Fatal("new password key id is empty, it shouldn't be")
}
if !b.updatePassword {
t.Fatal("update password is false, it shouldn't be")
}
config.NewClientSecretCreated = config.NewClientSecretCreated.Add(-(time.Minute * 1))
err = b.saveConfig(context.Background(), config, s)
if err != nil {
t.Fatal(err)
}
err = b.periodicFunc(context.Background(), &logical.Request{
Storage: s,
})
if err != nil {
t.Fatal(err)
}
newConfig, err := b.getConfig(context.Background(), s)
if err != nil {
t.Fatal(err)
}
if newConfig.ClientSecret != config.NewClientSecret {
t.Fatal(fmt.Errorf("old and new password aren't equal after periodic function, they should be"))
}
}
func TestRotateRootPeriodicFunctionBeforeMinute(t *testing.T) {
b, s := getTestBackend(t)
skipIfMissingEnvVars(t,
"AZURE_CLIENT_ID",
"AZURE_CLIENT_SECRET",
"AZURE_TENANT_ID",
)
configData := map[string]interface{}{
"tenant_id": os.Getenv("AZURE_TENANT_ID"),
"client_id": os.Getenv("AZURE_CLIENT_ID"),
"client_secret": os.Getenv("AZURE_CLIENT_SECRET"),
}
testConfigCreate(t, b, s, configData, false)
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "rotate-root",
Data: map[string]interface{}{},
Storage: s,
})
if err != nil {
t.Fatal(err)
}
if resp != nil && resp.IsError() {
t.Fatal(resp.Error())
}
tests := []struct {
Name string
Created time.Duration
}{
{
Name: "1 second test:",
Created: time.Second * 1,
},
{
Name: "5 seconds test:",
Created: time.Second * 5,
},
{
Name: "30 seconds test:",
Created: time.Second * 30,
},
{
Name: "50 seconds test:",
Created: time.Second * 50,
},
}
for _, test := range tests {
t.Log(test.Name)
config, err := b.getConfig(context.Background(), s)
if err != nil {
t.Fatal(err)
}
config.NewClientSecretCreated = time.Now().Add(-(test.Created))
err = b.saveConfig(context.Background(), config, s)
if err != nil {
t.Fatal(test.Name, err)
}
err = b.periodicFunc(context.Background(), &logical.Request{
Storage: s,
})
if err != nil {
t.Fatal(test.Name, err)
}
newConfig, err := b.getConfig(context.Background(), s)
if err != nil {
t.Fatal(test.Name, err)
}
if newConfig.ClientSecret == config.NewClientSecret {
t.Fatal(test.Name, fmt.Errorf("old and new password are equal after periodic function, they shouldn't be"))
}
}
}
func assertNotNil(t *testing.T, val interface{}) {
t.Helper()
if val == nil {
t.Fatalf("expected not nil, but was nil")
}
}
func assertNotEmptyString(t *testing.T, str string) {
t.Helper()
if str == "" {
t.Fatalf("string is empty")
}
}
func assertStrSliceIsNotEmpty(t *testing.T, strs []string) {
t.Helper()
if strs == nil || len(strs) == 0 {
t.Fatalf("string slice is empty")
}
}