This repository has been archived by the owner on Jan 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbackend_test.go
256 lines (211 loc) · 5.21 KB
/
backend_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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gcppca
import (
"context"
"reflect"
"strings"
"testing"
"time"
"github.com/hashicorp/vault/sdk/helper/useragent"
"github.com/hashicorp/vault/sdk/logical"
"google.golang.org/api/option"
"google.golang.org/grpc/connectivity"
pcaapi "cloud.google.com/go/security/privateca/apiv1"
hclog "github.com/hashicorp/go-hclog"
)
// testBackend creates a new isolated instance of the backend for testing.
func testBackend(tb testing.TB) (*backend, logical.Storage) {
tb.Helper()
config := logical.TestBackendConfig()
config.StorageView = new(logical.InmemStorage)
config.Logger = hclog.NewNullLogger()
b, err := Factory(context.Background(), config)
if err != nil {
tb.Fatal(err)
}
return b.(*backend), config.StorageView
}
// testFieldValidation verifies the given path has field validation.
func testFieldValidation(tb testing.TB, op logical.Operation, pth string) {
tb.Helper()
b, storage := testBackend(tb)
_, err := b.HandleRequest(context.Background(), &logical.Request{
Storage: storage,
Operation: op,
Path: pth,
Data: map[string]interface{}{
"literally-never-a-key": true,
},
})
if err == nil {
tb.Error("expected error")
}
if !strings.Contains(err.Error(), "unknown field") {
tb.Error(err)
}
}
// testPCAClient creates a new Certificate Service client with the default scopes and user
// agent.
func testPCAClient(tb testing.TB) *pcaapi.CertificateAuthorityClient {
tb.Helper()
ctx := context.Background()
kmsClient, err := pcaapi.NewCertificateAuthorityClient(ctx,
option.WithScopes(defaultScope),
option.WithUserAgent(useragent.String()),
)
if err != nil {
tb.Fatalf("failed to create kms client: %s", err)
}
return kmsClient
}
func TestBackend_PCAClient(t *testing.T) {
t.Parallel()
t.Run("allows_concurrent_reads", func(t *testing.T) {
t.Parallel()
b, storage := testBackend(t)
_, closer1, err := b.PCAClient(storage)
if err != nil {
t.Fatal(err)
}
defer closer1()
doneCh := make(chan struct{})
go func() {
_, closer2, err := b.PCAClient(storage)
if err != nil {
t.Fatal(err)
}
defer closer2()
close(doneCh)
}()
select {
case <-doneCh:
case <-time.After(1 * time.Second):
t.Errorf("client was not available")
}
})
t.Run("caches", func(t *testing.T) {
t.Parallel()
b, storage := testBackend(t)
client1, closer1, err := b.PCAClient(storage)
if err != nil {
t.Fatal(err)
}
defer closer1()
client2, closer2, err := b.PCAClient(storage)
if err != nil {
t.Fatal(err)
}
defer closer2()
// Note: not a bug; literally checking object equality
if client1 != client2 {
t.Errorf("expected %#v to be %#v", client1, client2)
}
})
t.Run("expires", func(t *testing.T) {
t.Parallel()
b, storage := testBackend(t)
b.pcaClientLifetime = 50 * time.Millisecond
client1, closer1, err := b.PCAClient(storage)
if err != nil {
t.Fatal(err)
}
closer1()
time.Sleep(100 * time.Millisecond)
client2, closer2, err := b.PCAClient(storage)
if err != nil {
t.Fatal(err)
}
closer2()
if client1 == client2 {
t.Errorf("expected %#v to not be %#v", client1, client2)
}
})
}
func TestBackend_ResetClient(t *testing.T) {
t.Parallel()
t.Run("closes_client", func(t *testing.T) {
t.Parallel()
b, storage := testBackend(t)
client, closer, err := b.PCAClient(storage)
if err != nil {
t.Fatal(err)
}
// Verify the client is "open"
if client.Connection().GetState() == connectivity.Shutdown {
t.Fatalf("connection is already stopped")
}
// Stop read lock
closer()
// Reset the clients
b.ResetClient()
// Verify the client closed
if state := client.Connection().GetState(); state != connectivity.Shutdown {
t.Errorf("expected client to be closed, was: %v", state)
}
})
}
func TestBackend_Config(t *testing.T) {
t.Parallel()
cases := []struct {
name string
c []byte
e *Config
err bool
}{
{
"default",
nil,
DefaultConfig(),
false,
},
{
"saved",
[]byte(`{"credentials":"foo", "scopes":["bar"]}`),
&Config{
Credentials: "foo",
Scopes: []string{"bar"},
},
false,
},
{
"invalid",
[]byte(`{x`),
nil,
true,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
b, storage := testBackend(t)
if tc.c != nil {
if err := storage.Put(context.Background(), &logical.StorageEntry{
Key: "config",
Value: tc.c,
}); err != nil {
t.Fatal(err)
}
}
c, err := b.Config(context.Background(), storage)
if (err != nil) != tc.err {
t.Fatal(err)
}
if !reflect.DeepEqual(c, tc.e) {
t.Errorf("expected %#v to be %#v", c, tc.e)
}
})
}
}