-
Notifications
You must be signed in to change notification settings - Fork 16
/
path_rotate_root_test.go
194 lines (163 loc) · 4.52 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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package azureauth
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/hashicorp/vault/sdk/logical"
)
// This test performs a rotate root operation and
// swaps the root credentials for the application
func TestRotateRootSuccess(t *testing.T) {
b, s := getTestBackend(t)
tenantID, clientID, clientSecret := getAzureEnvironmentSettings()
if tenantID == "" || clientID == "" || clientSecret == "" {
t.Skip("environment variables not set, skipping test")
}
configData := map[string]interface{}{
"tenant_id": tenantID,
"resource": "https://management.azure.com/",
"client_id": clientID,
"environment": "azurepublicCloud",
"client_secret": clientSecret,
}
if _, err := testConfigCreate(t, b, s, configData); err != nil {
t.Fatalf("err: %v", err)
}
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.config(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.config(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"))
}
}
// This test verifies that the periodicFunc does not remove
// stale credentials until the value for NewClientSecretCreated
// is greater than 1 minute
func TestRotateRootPeriodicFunctionBeforeMinute(t *testing.T) {
b, s := getTestBackend(t)
tenantID, clientID, clientSecret := getAzureEnvironmentSettings()
if tenantID == "" ||
clientID == "" || clientSecret == "" {
t.Skip("environment variables not set, skipping test")
}
configData := map[string]interface{}{
"tenant_id": tenantID,
"resource": "https://management.azure.com/",
"client_id": clientID,
"client_secret": clientSecret,
}
if _, err := testConfigCreate(t, b, s, configData); err != nil {
t.Fatalf("err: %v", err)
}
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.config(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.config(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 getAzureEnvironmentSettings() (string, string, string) {
tenantID := os.Getenv("TENANT_ID")
clientID := os.Getenv("CLIENT_ID")
clientSecret := os.Getenv("CLIENT_SECRET")
return tenantID, clientID, clientSecret
}