forked from aws/aws-secretsmanager-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAWSSecretsManagerDriverTest.java
More file actions
359 lines (304 loc) · 14.7 KB
/
Copy pathAWSSecretsManagerDriverTest.java
File metadata and controls
359 lines (304 loc) · 14.7 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 com.amazonaws.secretsmanager.sql;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import com.amazonaws.secretsmanager.caching.SecretCache;
import com.amazonaws.secretsmanager.caching.SecretCacheConfiguration;
import com.amazonaws.secretsmanager.util.TestClass;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder;
/**
* Tests for AWSSecretsManagerDriver. Uses a config file in the resources folder just to make sure it can read from
* the file.
*/
public class AWSSecretsManagerDriverTest extends TestClass {
private AWSSecretsManagerDummyDriver sut;
private static final String VALID_USER = "VALID_USER";
private static final String INVALID_USER = "INVALID_USER";
private static final String BAD_FORMAT_SECRET = "BAD_FORMAT_SECRET";
private static final String NEEDS_REFRESH_SECRET = "NEEDS_REFRESH_SECRET";
private static final String BAD_REFRESH_SECRET = "BAD_REFRESH_SECRET";
private static final String INVALID_AFTER_REFRESH = "INVALID_AFTER_REFRESH";
@Mock
private SecretCache cache;
boolean hasRefreshed;
@BeforeEach
public void setup() throws InterruptedException {
System.clearProperty("drivers.dummy.realDriverClass");
// Instantiate mocks
hasRefreshed = false;
MockitoAnnotations.openMocks(this);
Mockito.when(cache.getSecretString(Mockito.any(String.class))).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
if (arguments != null && arguments.length > 0 && arguments[0] != null){
String secretId = (String) arguments[0];
String returnUser = secretId;
if (INVALID_USER.equals(secretId)) {
return null;
} else if (BAD_FORMAT_SECRET.equals(secretId)) {
return "NotJSONFormat";
} else if (NEEDS_REFRESH_SECRET.equals(secretId) || BAD_REFRESH_SECRET.equals(secretId)) {
returnUser = hasRefreshed ? VALID_USER : DummyDriver.SQL_ERROR_USERNAME;
} else if (INVALID_AFTER_REFRESH.equals(secretId)) {
returnUser = DummyDriver.SQL_ERROR_USERNAME;
}
return String.format("{\"username\": \"%s\",\n\"password\": \"%s\",\n\"host\": \"%s\"}", returnUser, secretId, secretId);
}
return null;
}
});
Mockito.when(cache.refreshNow(Mockito.any(String.class))).thenAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
if (arguments != null && arguments.length > 0 && arguments[0] != null){
String secretId = (String) arguments[0];
if (BAD_REFRESH_SECRET.equals(secretId)) {
return false;
}
hasRefreshed = true;
return true;
}
return false;
}
});
// Instantiate the driver
sut = new AWSSecretsManagerDummyDriver(cache);
DummyDriver.reset();
try {
DriverManager.registerDriver(sut);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/*******************************************************************************************************************
* init Tests
******************************************************************************************************************/
@Test
public void test_init_constructor_null_params() {
try {
new AWSSecretsManagerDummyDriver((SecretsManagerClientBuilder) null);
} catch (Exception e) {}
try {
new AWSSecretsManagerDummyDriver((SecretCacheConfiguration) null);
} catch (Exception e) {}
try {
new AWSSecretsManagerDummyDriver((SecretsManagerClient) null);
} catch (Exception e) {}
}
@Test
public void test_init_works_realDriverFromConfig() {
System.setProperty("drivers.dummy.realDriverClass", "some.other.class");
AWSSecretsManagerDummyDriver sut2 = new AWSSecretsManagerDummyDriver(cache);
assertEquals(getFieldFrom(sut2, "realDriverClass"), "some.other.class");
}
/*******************************************************************************************************************
* getWrappedDriver Tests
******************************************************************************************************************/
@Test
public void test_getWrappedDriver_works_goodDriver() {
assertEquals(DummyDriver.instance, sut.getWrappedDriver());
}
@Test
public void test_getWrappedDriver_throws_badDriver() {
setFieldFrom(sut, "realDriverClass", "some.bad.class");
assertThrows(IllegalStateException.class, () -> sut.getWrappedDriver());
}
/*******************************************************************************************************************
* acceptsURL Tests
******************************************************************************************************************/
@Test
public void test_acceptsURL_throws_nullURL() {
assertThrows(SQLException.class, () -> sut.acceptsURL(null));
}
@Test
public void test_acceptsURL_returnsFalse_wrongURL() {
assertNotThrows(() -> assertFalse(sut.acceptsURL("jdbc-secretsmanager:wrongUrl")));
assertEquals(1, DummyDriver.acceptsURLCallCount);
}
@Test
public void test_acceptsURL_returnsTrue_correctURL() {
assertNotThrows(() -> assertTrue(sut.acceptsURL("jdbc-secretsmanager:expectedUrl")));
assertEquals(1, DummyDriver.acceptsURLCallCount);
}
@Test
public void test_acceptsURL_returnsFalse_JdbcUrl() {
assertNotThrows(() -> assertFalse(sut.acceptsURL("jdbc:expectedUrl")));
assertEquals(0, DummyDriver.acceptsURLCallCount);
}
@Test
public void test_acceptsURL_returnsTrue_secretId() {
assertNotThrows(() -> assertTrue(sut.acceptsURL("someSecretId")));
assertEquals(0, DummyDriver.acceptsURLCallCount);
}
/*******************************************************************************************************************
* connect Tests
******************************************************************************************************************/
@Test
public void test_connect_throws_nullURL() {
assertThrows(SQLException.class, () -> sut.connect(null, null));
}
@Test
public void test_connect_works_nullInfo() {
assertNotThrows(() -> sut.connect("jdbc-secretsmanager:expectedUrl", null));
assertEquals(1, DummyDriver.connectCallCount);
}
@Test
public void test_connect_works_nullUser() {
Properties props = new Properties();
assertNotThrows(() -> sut.connect("jdbc-secretsmanager:expectedUrl", props));
assertEquals(1, DummyDriver.connectCallCount);
}
@Test
public void test_connect_works_valid_url() {
Properties props = new Properties();
props.setProperty("user", "user");
assertNotThrows(() -> sut.connect("jdbc-secretsmanager:expectedUrl", props));
assertEquals(1, DummyDriver.connectCallCount);
}
@Test
public void test_connect_jdbc_returnsNull() throws SQLException {
Connection conn = sut.connect("jdbc:expectedUrl", null);
assertEquals(conn, null);
}
@Test
public void test_connect_works_secretId() {
Properties props = new Properties();
props.setProperty("user", "user");
assertNotThrows(() -> sut.connect("someSecretId", props));
assertEquals(1, DummyDriver.connectCallCount);
}
@Test
public void test_connect_works_withSecretRefresh() {
Properties props = new Properties();
props.setProperty("user", NEEDS_REFRESH_SECRET);
sut.exceptionIsDueToAuth = true;
assertNotThrows(() -> sut.connect("jdbc-secretsmanager:expectedUrl", props));
assertEquals(2, DummyDriver.connectCallCount);
}
@Test
public void test_connect_throws_afterRetryMax() {
Properties props = new Properties();
props.setProperty("user", INVALID_AFTER_REFRESH);
sut.exceptionIsDueToAuth = true;
assertThrows(SQLException.class, () -> sut.connect("jdbc-secretsmanager:expectedUrl", props));
assertEquals(AWSSecretsManagerDriver.MAX_RETRY + 1, DummyDriver.connectCallCount);
}
@Test
public void test_connect_throws_withBadRefresh() {
Properties props = new Properties();
props.setProperty("user", BAD_REFRESH_SECRET);
sut.exceptionIsDueToAuth = true;
assertThrows(SQLException.class, () -> sut.connect("jdbc-secretsmanager:expectedUrl", props));
assertEquals(1, DummyDriver.connectCallCount);
}
@Test
public void test_connect_rethrowsSQLException_onFailure() {
Properties props = new Properties();
props.setProperty("user", DummyDriver.SQL_ERROR_USERNAME);
sut.exceptionIsDueToAuth = false;
assertThrows(SQLException.class, () -> sut.connect("jdbc-secretsmanager:expectedUrl", props));
assertEquals(1, DummyDriver.connectCallCount);
}
@Test
public void test_connect_rethrowsRuntimeException_onFailure() {
Properties props = new Properties();
props.setProperty("user", DummyDriver.RUNTIME_ERROR_USERNAME);
sut.exceptionIsDueToAuth = false;
assertThrows(RuntimeException.class, () -> sut.connect("jdbc-secretsmanager:expectedUrl", props));
assertEquals(1, DummyDriver.connectCallCount);
}
@Test
public void test_connect_throws_badSecretId() {
Properties props = new Properties();
props.setProperty("user", "user");
assertThrows(IllegalArgumentException.class, () -> sut.connect(INVALID_USER, props));
assertEquals(0, DummyDriver.connectCallCount);
}
@Test
public void test_connect_throws_badlyFormattedSecretId() {
Properties props = new Properties();
props.setProperty("user", "user");
assertThrows(RuntimeException.class, () -> sut.connect(BAD_FORMAT_SECRET, props));
assertEquals(0, DummyDriver.connectCallCount);
}
@Test
public void test_connect_throws_userBadlyFormattedSecretId() {
Properties props = new Properties();
props.setProperty("user", BAD_FORMAT_SECRET);
assertThrows(RuntimeException.class, () -> sut.connect("jdbc-secretsmanager:expectedUrl", props));
assertEquals(0, DummyDriver.connectCallCount);
}
/*******************************************************************************************************************
* getMajorVersion Tests
******************************************************************************************************************/
@Test
public void test_getMajorVersion_propagatesToRealDriver() {
assertEquals(DummyDriver.GET_MAJOR_VERSION_RETURN_VALUE, sut.getMajorVersion());
assertEquals(1, DummyDriver.getMajorVersionCallCount);
}
/*******************************************************************************************************************
* getMinorVersion Tests
******************************************************************************************************************/
@Test
public void test_getMinorVersion_propagatesToRealDriver() {
assertEquals(DummyDriver.GET_MINOR_VERSION_RETURN_VALUE, sut.getMinorVersion());
assertEquals(1, DummyDriver.getMinorVersionCallCount);
}
/*******************************************************************************************************************
* getParentLogger Tests
******************************************************************************************************************/
@Test
public void test_getParentLogger_propagatesToRealDriver() {
assertNotThrows(() -> assertEquals(null, sut.getParentLogger()));
assertEquals(1, DummyDriver.getParentLoggerCallCount);
}
/*******************************************************************************************************************
* getPropertyInfo Tests
******************************************************************************************************************/
@Test
public void test_getPropertyInfo_propagatesToRealDriver() {
String param1 = "jdbc-secretsmanager:expectedUrl";
Properties param2 = new Properties();
assertNotThrows(() -> Assertions.assertNull(sut.getPropertyInfo(param1, param2)));
assertEquals(1, DummyDriver.getPropertyInfoCallCount);
String param1Expected = "jdbc:expectedUrl";
assertEquals(param1Expected, DummyDriver.getPropertyInfoParam1);
assertSame(param2, DummyDriver.getPropertyInfoParam2);
}
/*******************************************************************************************************************
* jdbcCompliant Tests
******************************************************************************************************************/
@Test
public void test_jdbcCompliant_propagatesToRealDriver() {
assertEquals(true, sut.jdbcCompliant());
assertEquals(1, DummyDriver.jdbcCompliantCallCount);
}
}