-
Notifications
You must be signed in to change notification settings - Fork 3
/
path_config_rotate_test.go
267 lines (217 loc) · 8.25 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package ibmcloudsecrets
import (
"context"
"fmt"
"strings"
"testing"
"time"
gomock "github.com/golang/mock/gomock"
"github.com/hashicorp/vault/sdk/logical"
)
func TestConfigRotateRootSuccess(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockHelper := NewMockiamHelper(ctrl)
// Set up the config, the iamHelper mocks, and the backend
var configData = map[string]interface{}{
apiKeyField: "adminKey",
accountIDField: "theAccountID",
}
mockHelper.EXPECT().ObtainToken("adminKey").Return("AdminToken", nil)
mockHelper.EXPECT().VerifyToken(gomock.Any(), "AdminToken").Return(&tokenInfo{Expiry: time.Now().Add(time.Hour)}, nil)
mockHelper.EXPECT().GetAPIKeyDetails("AdminToken", "adminKey").
Return(&APIKeyDetailsResponse{ID: "oldID", IAMID: "testIAMID", AccountID: "theAccountID"}, nil)
mockHelper.EXPECT().GetAPIKeyDetails("AdminToken", "adminKey").
Return(&APIKeyDetailsResponse{ID: "oldID", IAMID: "testIAMID", AccountID: "theAccountID"}, nil)
mockHelper.EXPECT().CreateAPIKey("AdminToken", "testIAMID", "theAccountID", gomock.Any(), gomock.Any()).
Return(&APIKeyV1Response{APIKey: "newKeyVal", ID: "newKeyID"}, nil)
mockHelper.EXPECT().Cleanup()
mockHelper.EXPECT().DeleteAPIKey("AdminToken", "oldID").Return(nil)
b, s := testBackend(t)
err := testConfigCreate(t, b, s, configData)
if err != nil {
t.Fatal("error configuring the backend")
}
b.iamHelper = mockHelper
// Rotate the key
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "config/rotate-root",
Storage: s,
})
if err != nil {
t.Fatalf("err: %v", err)
}
if resp != nil && resp.IsError() {
t.Fatalf("err: %v", resp.Error())
}
// verify the new API key ID was returned in the response
keyID, ok := resp.Data[apiKeyID]
if !ok {
t.Fatal("the api_key_id field was not found in the response")
}
if keyID != "newKeyID" {
t.Fatal("the new API key ID was not the expected value in the response")
}
// Verify the API key was updated in the config
config, resp := b.getConfig(context.Background(), s)
if resp != nil {
t.Fatalf("err: %v", resp.Error())
}
if config.APIKey != "newKeyVal" {
t.Fatalf("the API key was not set as expected. Received %s. Expected %s", config.APIKey, "newKeyVal")
}
}
func TestRotateCreateFails(t *testing.T) {
// Test the case when creating the new API key fails
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockHelper := NewMockiamHelper(ctrl)
// Set up the config, the iamHelper mocks, and the backend
var configData = map[string]interface{}{
apiKeyField: "adminKey",
accountIDField: "theAccountID",
}
mockHelper.EXPECT().ObtainToken("adminKey").Return("AdminToken", nil)
mockHelper.EXPECT().VerifyToken(gomock.Any(), "AdminToken").Return(&tokenInfo{Expiry: time.Now().Add(time.Hour)}, nil)
mockHelper.EXPECT().GetAPIKeyDetails("AdminToken", "adminKey").
Return(&APIKeyDetailsResponse{ID: "oldID", IAMID: "testIAMID", AccountID: "theAccountID"}, nil)
mockHelper.EXPECT().GetAPIKeyDetails("AdminToken", "adminKey").
Return(&APIKeyDetailsResponse{ID: "oldID", IAMID: "testIAMID", AccountID: "theAccountID"}, nil)
mockHelper.EXPECT().CreateAPIKey("AdminToken", "testIAMID", "theAccountID", gomock.Any(), gomock.Any()).
Return(nil, fmt.Errorf("intentional CreateAPIKey mock failure"))
b, s := testBackend(t)
err := testConfigCreate(t, b, s, configData)
if err != nil {
t.Fatal("error configuring the backend")
}
b.iamHelper = mockHelper
// Rotate the key
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "config/rotate-root",
Storage: s,
})
// Verify the expected create failure is received
if resp != nil {
t.Fatalf("error: received a response when none was expected: %v", resp)
}
if err == nil {
t.Fatalf("error: did not receive an error from the rotation as expected")
}
if err.Error() != "intentional CreateAPIKey mock failure" {
t.Fatalf("error: did not receive the expected error message. Received this instead %s", err.Error())
}
// Verify the API key was not updated in the config
config, resp := b.getConfig(context.Background(), s)
if resp != nil {
t.Fatalf("err: %v", resp.Error())
}
if config.APIKey != "adminKey" {
t.Fatalf("the API key was no set as expected. Received %s. Expected %s", config.APIKey, "adminKey")
}
}
func TestRotateDeleteKeyFails(t *testing.T) {
// Test the case where the key is rotated but the deletion of the old key fails
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockHelper := NewMockiamHelper(ctrl)
// Set up the config, the iamHelper mocks, and the backend
var configData = map[string]interface{}{
apiKeyField: "adminKey",
accountIDField: "theAccountID",
}
mockHelper.EXPECT().ObtainToken("adminKey").Return("AdminToken", nil)
mockHelper.EXPECT().VerifyToken(gomock.Any(), "AdminToken").Return(&tokenInfo{Expiry: time.Now().Add(time.Hour)}, nil)
mockHelper.EXPECT().GetAPIKeyDetails("AdminToken", "adminKey").
Return(&APIKeyDetailsResponse{ID: "oldID", IAMID: "testIAMID", AccountID: "theAccountID"}, nil)
mockHelper.EXPECT().GetAPIKeyDetails("AdminToken", "adminKey").
Return(&APIKeyDetailsResponse{ID: "oldID", IAMID: "testIAMID", AccountID: "theAccountID"}, nil)
mockHelper.EXPECT().CreateAPIKey("AdminToken", "testIAMID", "theAccountID", gomock.Any(), gomock.Any()).
Return(&APIKeyV1Response{APIKey: "newKeyVal", ID: "newKeyID"}, nil)
mockHelper.EXPECT().Cleanup()
mockHelper.EXPECT().DeleteAPIKey("AdminToken", "oldID").
Return(fmt.Errorf("intentional DeleteAPIKey mock failure"))
b, s := testBackend(t)
err := testConfigCreate(t, b, s, configData)
if err != nil {
t.Fatal("error configuring the backend")
}
b.iamHelper = mockHelper
// Rotate the key
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "config/rotate-root",
Storage: s,
})
// Verify the expected create failure is received
if resp == nil {
t.Fatalf("error: a response was expected, but none was received")
}
if err == nil {
t.Fatalf("error: did not receive an error from the rotation as expected")
}
if err.Error() != "intentional DeleteAPIKey mock failure" {
t.Fatalf("error: did not receive the expected error message. Received this instead %s", err.Error())
}
// verify the response error has both the old and new key IDs in it
if !strings.Contains(resp.Error().Error(), "oldID") {
t.Fatalf("expected %s to be in error %v", "oldID", resp.Error())
}
if !strings.Contains(resp.Error().Error(), "newKeyID") {
t.Fatalf("expected %s to be in error %v", "newKeyID", resp.Error())
}
// Verify the API key was updated in the config
config, resp := b.getConfig(context.Background(), s)
if resp != nil {
t.Fatalf("err: %v", resp.Error())
}
if config.APIKey != "newKeyVal" {
t.Fatalf("the API key was not set as expected. Received %s. Expected %s", config.APIKey, "newKeyVal")
}
}
func TestConfigRotateNoConfigSet(t *testing.T) {
// Test with no config set
b, s := testBackend(t)
// Rotate the key
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "config/rotate-root",
Storage: s,
})
if err != nil {
t.Fatal(err)
}
expectedMsg := "no API key was set in the configuration"
if resp == nil {
t.Fatalf("expected an error response on rotation but did not receive one")
} else if !strings.Contains(resp.Error().Error(), expectedMsg) {
t.Fatalf("expected message \"%s\" to be in error: %v", expectedMsg, resp.Error())
}
// Test when the key is set to the empty string
var configData = map[string]interface{}{
apiKeyField: "",
accountIDField: "",
}
err = testConfigCreate(t, b, s, configData)
if err != nil {
t.Fatal("error configuring the backend")
}
// Rotate the key
resp, err = b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "config/rotate-root",
Storage: s,
})
if err != nil {
t.Fatal(err)
}
if resp == nil {
t.Fatalf("expected an error response on rotation but did not receive one")
} else if !strings.Contains(resp.Error().Error(), expectedMsg) {
t.Fatalf("expected message \"%s\" to be in error: %v", expectedMsg, resp.Error())
}
}