-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpath_config_token_autorotate_test.go
266 lines (239 loc) · 9.14 KB
/
path_config_token_autorotate_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
//go:build unit
package gitlab_test
import (
"fmt"
"testing"
"time"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
gitlab "github.com/ilijamt/vault-plugin-secrets-gitlab"
)
func TestPathConfig_AutoRotate(t *testing.T) {
t.Run("auto_rotate_token should be false if not specified", func(t *testing.T) {
ctx, url := getCtxGitlabClientWithUrl(t, "unit")
b, l, err := getBackend(ctx)
require.NoError(t, err)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.UpdateOperation,
Path: fmt.Sprintf("%s/%s", gitlab.PathConfigStorage, gitlab.DefaultConfigName), Storage: l,
Data: map[string]any{
"token": "glpat-secret-random-token",
"base_url": url,
"type": gitlab.TypeSelfManaged.String(),
},
})
require.NoError(t, err)
require.NotNil(t, resp)
assert.False(t, resp.Data["auto_rotate_token"].(bool))
})
t.Run("auto_rotate_before cannot be more than the minimal value", func(t *testing.T) {
ctx, url := getCtxGitlabClientWithUrl(t, "unit")
b, l, err := getBackend(ctx)
require.NoError(t, err)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.UpdateOperation,
Path: fmt.Sprintf("%s/%s", gitlab.PathConfigStorage, gitlab.DefaultConfigName), Storage: l,
Data: map[string]any{
"token": "glpat-secret-random-token",
"base_url": url,
"auto_rotate_before": "2h",
"type": gitlab.TypeSelfManaged.String(),
},
})
require.Error(t, err)
require.Nil(t, resp)
require.ErrorIs(t, err, gitlab.ErrInvalidValue)
})
t.Run("auto_rotate_before should be less than the maximal limit", func(t *testing.T) {
ctx, url := getCtxGitlabClientWithUrl(t, "unit")
b, l, err := getBackend(ctx)
require.NoError(t, err)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.UpdateOperation,
Path: fmt.Sprintf("%s/%s", gitlab.PathConfigStorage, gitlab.DefaultConfigName), Storage: l,
Data: map[string]any{
"token": "glpat-secret-random-token",
"base_url": url,
"auto_rotate_before": (gitlab.DefaultAutoRotateBeforeMaxTTL + time.Hour).String(),
"type": gitlab.TypeSelfManaged.String(),
},
})
require.ErrorIs(t, err, gitlab.ErrInvalidValue)
require.Nil(t, resp)
})
t.Run("auto_rotate_before should be set to correct value", func(t *testing.T) {
ctx, url := getCtxGitlabClientWithUrl(t, "unit")
b, l, err := getBackend(ctx)
require.NoError(t, err)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.UpdateOperation,
Path: fmt.Sprintf("%s/%s", gitlab.PathConfigStorage, gitlab.DefaultConfigName), Storage: l,
Data: map[string]any{
"token": "glpat-secret-random-token",
"base_url": url,
"auto_rotate_before": "48h",
"type": gitlab.TypeSelfManaged.String(),
},
})
require.NoError(t, err)
require.NotNil(t, resp)
require.EqualValues(t, "48h0m0s", resp.Data["auto_rotate_before"])
})
t.Run("auto_rotate_before should be more than the minimal limit", func(t *testing.T) {
ctx, url := getCtxGitlabClientWithUrl(t, "unit")
b, l, err := getBackend(ctx)
require.NoError(t, err)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.UpdateOperation,
Path: fmt.Sprintf("%s/%s", gitlab.PathConfigStorage, gitlab.DefaultConfigName), Storage: l,
Data: map[string]any{
"token": "glpat-secret-random-token",
"base_url": url,
"auto_rotate_before": (gitlab.DefaultAutoRotateBeforeMinTTL - time.Hour).String(),
"type": gitlab.TypeSelfManaged.String(),
},
})
require.ErrorIs(t, err, gitlab.ErrInvalidValue)
require.Nil(t, resp)
})
t.Run("auto_rotate_before should be set to min if not specified", func(t *testing.T) {
ctx, url := getCtxGitlabClientWithUrl(t, "unit")
b, l, err := getBackend(ctx)
require.NoError(t, err)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.UpdateOperation,
Path: fmt.Sprintf("%s/%s", gitlab.PathConfigStorage, gitlab.DefaultConfigName), Storage: l,
Data: map[string]any{
"token": "glpat-secret-random-token",
"base_url": url,
"type": gitlab.TypeSelfManaged.String(),
},
})
require.NoError(t, err)
require.NotNil(t, resp)
assert.NotEmpty(t, resp.Data["auto_rotate_before"])
})
t.Run("auto_rotate_before should be between the min and max value", func(t *testing.T) {
ctx, url := getCtxGitlabClientWithUrl(t, "unit")
b, l, err := getBackend(ctx)
require.NoError(t, err)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.UpdateOperation,
Path: fmt.Sprintf("%s/%s", gitlab.PathConfigStorage, gitlab.DefaultConfigName), Storage: l,
Data: map[string]any{
"token": "glpat-secret-random-token",
"base_url": url,
"auto_rotate_before": "10h",
"type": gitlab.TypeSelfManaged.String(),
},
})
require.ErrorIs(t, err, gitlab.ErrInvalidValue)
require.Nil(t, resp)
})
}
func TestPathConfig_AutoRotateToken(t *testing.T) {
t.Run("no error when auto rotate is disabled and config is not set", func(t *testing.T) {
ctx := getCtxGitlabClient(t, "unit")
b, l, err := getBackend(ctx)
require.NoError(t, err)
err = b.PeriodicFunc(ctx, &logical.Request{Storage: l})
require.NoError(t, err)
})
t.Run("no error when auto rotate is disabled and config is set", func(t *testing.T) {
var client = newInMemoryClient(true)
ctx, url := getCtxGitlabClientWithUrl(t, "unit")
ctx = gitlab.GitlabClientNewContext(ctx, client)
b, l, err := getBackendWithConfig(ctx, map[string]any{
"token": "glpat-secret-token",
"base_url": url,
"type": gitlab.TypeSelfManaged.String(),
})
require.NoError(t, err)
b.SetClient(newInMemoryClient(true), gitlab.DefaultConfigName)
err = b.PeriodicFunc(ctx, &logical.Request{Storage: l})
require.NoError(t, err)
})
t.Run("call auto rotate the main token and rotate the token", func(t *testing.T) {
var client = newInMemoryClient(true)
ctx, url := getCtxGitlabClientWithUrl(t, "unit")
ctx = gitlab.GitlabClientNewContext(ctx, newInMemoryClient(true))
b, l, events, err := getBackendWithEventsAndConfig(ctx, map[string]any{
"token": "token",
"base_url": url,
"auto_rotate_token": true,
"auto_rotate_before": "360h",
"type": gitlab.TypeSelfManaged.String(),
})
require.NoError(t, err)
client.rotateMainToken.Token = "new token"
b.SetClient(client, gitlab.DefaultConfigName)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: fmt.Sprintf("%s/%s", gitlab.PathConfigStorage, gitlab.DefaultConfigName), Storage: l,
})
require.NoError(t, err)
require.NotNil(t, resp)
require.NoError(t, resp.Error())
require.NotEmpty(t, resp.Data)
require.NotEmpty(t, resp.Data["token_expires_at"])
err = b.PeriodicFunc(ctx, &logical.Request{Storage: l})
require.NoError(t, err)
assert.Greater(t, client.calledRotateMainToken, 0)
resp, err = b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: fmt.Sprintf("%s/%s", gitlab.PathConfigStorage, gitlab.DefaultConfigName), Storage: l,
})
require.NoError(t, err)
require.NotNil(t, resp)
require.NoError(t, resp.Error())
require.NotEmpty(t, resp.Data)
require.NotEmpty(t, resp.Data["token_sha1_hash"])
require.NotEmpty(t, resp.Data["token_expires_at"])
events.expectEvents(t, []expectedEvent{
{
eventType: "gitlab/config-write",
},
{
eventType: "gitlab/config-token-rotate",
},
})
})
t.Run("call auto rotate the main token but the token is still valid", func(t *testing.T) {
var client = newInMemoryClient(true)
ctx, url := getCtxGitlabClientWithUrl(t, "unit")
ctx = gitlab.GitlabClientNewContext(ctx, newInMemoryClient(true))
b, l, err := getBackendWithConfig(ctx, map[string]any{
"token": "token",
"base_url": url,
"auto_rotate_token": true,
"auto_rotate_before": "24h",
"type": gitlab.TypeSelfManaged.String(),
})
require.NoError(t, err)
var expiresAt = time.Now().Add(100 * 24 * time.Hour)
client.mainTokenInfo.ExpiresAt = &expiresAt
b.SetClient(client, gitlab.DefaultConfigName)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: fmt.Sprintf("%s/%s", gitlab.PathConfigStorage, gitlab.DefaultConfigName), Storage: l,
})
require.NoError(t, err)
require.NotNil(t, resp)
require.NoError(t, resp.Error())
require.NotEmpty(t, resp.Data)
require.NotEmpty(t, resp.Data["token_expires_at"])
err = b.PeriodicFunc(ctx, &logical.Request{Storage: l})
require.NoError(t, err)
assert.EqualValues(t, 1, client.calledRotateMainToken)
resp, err = b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: fmt.Sprintf("%s/%s", gitlab.PathConfigStorage, gitlab.DefaultConfigName), Storage: l,
})
require.NoError(t, err)
require.NotNil(t, resp)
require.NoError(t, resp.Error())
require.NotEmpty(t, resp.Data)
require.NotEmpty(t, resp.Data["token_expires_at"])
})
}