diff --git a/README.md b/README.md index afaf3b9..9ab6cb1 100644 --- a/README.md +++ b/README.md @@ -91,3 +91,5 @@ The secret being used should be in the JSON format we use for our rotation lambd ... } ``` + +Alternatively, you can pass the secret ID as the jdbc uri and omit user. The JDBC connection details such as host, port, dbname will be obtained from your secrets manager secret. diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriver.java index 5e482f0..0bf4c3d 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriver.java @@ -376,9 +376,21 @@ public Connection connect(String url, Properties info) throws SQLException { return null; } - String unwrappedUrl = ""; if (url.startsWith(SCHEME)) { // If this is a URL in the correct scheme, unwrap it - unwrappedUrl = unwrapUrl(url); + String unwrappedUrl = unwrapUrl(url); + + if (info != null && info.getProperty("user") != null) { + String credentialsSecretId = info.getProperty("user"); + try { + return connectWithSecret(unwrappedUrl, info, credentialsSecretId); + } catch (InterruptedException e) { + // User driven exception. Throw a runtime exception. + throw new RuntimeException(e); + } + } else { + return getWrappedDriver().connect(unwrappedUrl, info); + } + } else { // Else, assume this is a secret ID and try to retrieve it String secretString = secretCache.getSecretString(url); if (StringUtils.isNullOrEmpty(secretString)) { @@ -386,6 +398,7 @@ public Connection connect(String url, Properties info) throws SQLException { SCHEME + " or a valid retrievable secret ID "); } + String unwrappedUrl = ""; try { JsonNode jsonObject = mapper.readTree(secretString); String endpoint = jsonObject.get("host").asText(); @@ -398,18 +411,14 @@ public Connection connect(String url, Properties info) throws SQLException { // Most likely to occur in the event that the data is not JSON. This is more of a user error. throw new RuntimeException(INVALID_SECRET_STRING_JSON); } - } - if (info != null && info.getProperty("user") != null) { - String credentialsSecretId = info.getProperty("user"); try { - return connectWithSecret(unwrappedUrl, info, credentialsSecretId); + return connectWithSecret(unwrappedUrl, info, url); } catch (InterruptedException e) { // User driven exception. Throw a runtime exception. throw new RuntimeException(e); } - } else { - return getWrappedDriver().connect(unwrappedUrl, info); + } } diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriverTest.java index 27af579..8c5b58f 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriverTest.java @@ -229,9 +229,8 @@ public void test_connect_jdbc_returnsNull() throws SQLException { } @Test - public void test_connect_works_secretId() { + public void test_connect_works_secretId_in_url() { Properties props = new Properties(); - props.setProperty("user", "user"); assertNotThrows(() -> sut.connect("someSecretId", props)); assertEquals(1, DummyDriver.connectCallCount); }