diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 95f6665..866d2da 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -1,10 +1,9 @@ --- name: Bug report about: Create a report to help us improve -title: '' +title: "" labels: bug -assignees: '' - +assignees: "" --- **Describe the bug** diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index 809b7b0..60f49e9 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -1,10 +1,7 @@ --- name: Feature request about: Suggest an idea for this project -title: '' +title: "" labels: enhancement -assignees: '' - +assignees: "" --- - - diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index ab40d21..3bfff59 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,6 +1,5 @@ -*Issue #, if available:* - -*Description of changes:* +_Issue #, if available:_ +_Description of changes:_ By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 0bcbdab..f1a972d 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -2,7 +2,7 @@ name: Java Build on: push: - branches: [master, v2] + branches: [v2, master] pull_request: types: [opened, synchronize, ready_for_review] @@ -22,8 +22,18 @@ jobs: java-version: 11 distribution: adopt + - name: Prettier + uses: rutajdash/prettier-cli-action@v1.0.2 + with: + file_pattern: "**/*.{md,yml}" + + - name: Check formatting + run: mvn spotless:check + - name: Build with Maven run: mvn --batch-mode --update-snapshots package - name: Codecov uses: codecov/codecov-action@v5 + with: + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 6610bdf..107ed11 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -12,8 +12,6 @@ name: CodeQL on: - push: - branches: [master, v2] pull_request: branches: [master, v2] schedule: diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 6dc6953..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,12 +0,0 @@ -# Changelog - -### Release 1.0.2 (May 28, 2018) -* Add support for MariaDB -* For MySQL, check for com.mysql.cj.jdbc.Driver in class path and fall back to com.mysql.jdbc.Driver -* Change JSON parsing error message to be more generic - -### Release 1.0.1 (December 5, 2018) -* Fixed an issue with the way JDBC URLs are handled: - * acceptsURL() now returns false if the URL is a JDBC URL that does not begin with jdbc-secretsmanager - * connect() returns null if the URL parameter is not one we accept -* Updated jackson-databind dependency to 2.8.11.1 diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 3b64466..ec98f2b 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -1,4 +1,5 @@ ## Code of Conduct -This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). -For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact + +This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). +For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact opensource-codeofconduct@amazon.com with any additional questions or comments. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ff82785..fb54301 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,29 +1,28 @@ # Contributing Guidelines -Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional +Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional documentation, we greatly value feedback and contributions from our community. -Please read through this document before submitting any issues or pull requests to ensure we have all the necessary +Please read through this document before submitting any issues or pull requests to ensure we have all the necessary information to effectively respond to your bug report or contribution. - ## Reporting Bugs/Feature Requests We welcome you to use the GitHub issue tracker to report bugs or suggest features. -When filing an issue, please check [existing open](https://github.com/aws/aws-secretsmanager-jdbc/issues), or [recently closed](https://github.com/aws/aws-secretsmanager-jdbc/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aclosed%20), issues to make sure somebody else hasn't already +When filing an issue, please check [existing open](https://github.com/aws/aws-secretsmanager-jdbc/issues), or [recently closed](https://github.com/aws/aws-secretsmanager-jdbc/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aclosed%20), issues to make sure somebody else hasn't already reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: -* A reproducible test case or series of steps -* The version of our code being used -* Any modifications you've made relevant to the bug -* Anything unusual about your environment or deployment - +- A reproducible test case or series of steps +- The version of our code being used +- Any modifications you've made relevant to the bug +- Anything unusual about your environment or deployment ## Contributing via Pull Requests + Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: -1. You are working against the latest source on the *master* branch. +1. You are working against the latest source on the _master_ branch. 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. @@ -31,28 +30,34 @@ To send us a pull request, please: 1. Fork the repository. 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. -3. Ensure local tests pass. -4. Commit to your fork using clear commit messages. -5. Send us a pull request, answering any default questions in the pull request interface. -6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. +3. Use Spotless to ensure consistent formatting for `.java` files, and Prettier for `.md` and `.yml` files (see [Formatting instructions](#formatting-instructions)). +4. Ensure local tests pass. +5. Commit to your fork using clear commit messages. +6. Send us a pull request, answering any default questions in the pull request interface. +7. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. -GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and +GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). +# Formatting instructions + +- Install [Prettier](https://prettier.io/docs/install). + - If using VS Code, install the VS Code extension for the above. +- Run `mvn spotless:apply` and `prettier --write **/*.{md,yml}`. ## Finding contributions to work on -Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any ['help wanted'](https://github.com/aws/aws-secretsmanager-jdbc/labels/help%20wanted) issues is a great place to start. +Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any ['help wanted'](https://github.com/aws/aws-secretsmanager-jdbc/labels/help%20wanted) issues is a great place to start. ## Code of Conduct -This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). -For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact -opensource-codeofconduct@amazon.com with any additional questions or comments. +This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). +For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact +opensource-codeofconduct@amazon.com with any additional questions or comments. ## Security issue notifications -If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. +If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. ## Licensing diff --git a/README.md b/README.md index 5c63576..e399eb6 100644 --- a/README.md +++ b/README.md @@ -11,21 +11,23 @@ This library is licensed under the Apache 2.0 License. ## Features -* Provides wrappers to common JDBC drivers enabling simple database connectivity -* Provides database connection pooling support through c3p0 +- Provides wrappers to common JDBC drivers enabling simple database connectivity +- Provides database connection pooling support through c3p0 ## Building from Source -After you've downloaded the code from GitHub, you can build it using Maven. -* To disable GPG signing in the build, use this command: `mvn clean install -Dgpg.skip=true` -* To build the default (non-shaded) JAR, use this command: `mvn clean install` -* To build the shaded (uber) JAR with all dependencies included, use this command: `mvn clean install -Pshade` -The shaded JAR will be generated in the `target/` directory with the `-shaded` classifier, e.g.: `target/aws-secretsmanager-jdbc-2.0.2-shaded.jar` +After you've downloaded the code from GitHub, you can build it using Maven. + +- To disable GPG signing in the build, use this command: `mvn clean install -Dgpg.skip=true` +- To build the default (non-shaded) JAR, use this command: `mvn clean install` +- To build the shaded (uber) JAR with all dependencies included, use this command: `mvn clean install -Pshade` + The shaded JAR will be generated in the `target/` directory with the `-shaded` classifier, e.g.: `target/aws-secretsmanager-jdbc-2.0.2-shaded.jar` ## Usage -The recommended way to use the SQL Connection Library is to consume it from Maven. The latest released version can be found at: https://mvnrepository.com/artifact/com.amazonaws.secretsmanager/aws-secretsmanager-jdbc -``` xml +The recommended way to use the SQL Connection Library is to consume it from Maven. The latest released version can be found at: https://mvnrepository.com/artifact/com.amazonaws.secretsmanager/aws-secretsmanager-jdbc + +```xml com.amazonaws.secretsmanager aws-secretsmanager-jdbc @@ -35,7 +37,7 @@ The recommended way to use the SQL Connection Library is to consume it from Mave To use the latest build (pre-release), don't forget to enable the download of snapshot jars from Maven. -``` xml +```xml allow-snapshots @@ -53,13 +55,12 @@ To use the latest build (pre-release), don't forget to enable the download of sn ``` ### Usage Example + We provide database drivers that intercept calls to real database drivers and replace secret IDs with actual login credentials. This prevents hard-coding database credentials into your application code. - The following is an example which uses the secret to resolve both the endpoint and the login credentials. - ``` // Load the JDBC driver Class.forName( "com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver" ).newInstance(); @@ -88,30 +89,32 @@ String URL = "jdbc-secretsmanager:postgresql://example.com:5432/database"; ``` The secret should be in the correct JSON format. For more information, see the [AWS Secrets Manager documentation](https://docs.aws.amazon.com/secretsmanager/latest/userguide/reference_secret_json_structure). For example: + ```json { "host": "", "username": "", "password": "", "dbname": "", - "port": "", + "port": "" } ``` We support a variety of drivers. For more information, see the [AWS Secrets Manager JDBC documentation](https://docs.aws.amazon.com/secretsmanager/latest/userguide/retrieving-secrets_jdbc). - ## Credentials This library uses the [Default Credential Provider Chain](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials.html). The following options exist to override some of the defaults: -1) Set a PrivateLink DNS endpoint URL and a region in the secretsmanager.properties file: +1. Set a PrivateLink DNS endpoint URL and a region in the secretsmanager.properties file: + ```text drivers.vpcEndpointUrl= #The endpoint URL drivers.vpcEndpointRegion= #The endpoint region ``` -2) Override the primary region by setting the 'AWS_SECRET_JDBC_REGION' environment variable to the preferred region, or via the secretsmanager.properties file: +2. Override the primary region by setting the 'AWS_SECRET_JDBC_REGION' environment variable to the preferred region, or via the secretsmanager.properties file: + ```text drivers.region= #The region to use. ``` diff --git a/pom.xml b/pom.xml index 0637f63..e495b3d 100644 --- a/pom.xml +++ b/pom.xml @@ -225,6 +225,20 @@ + + com.diffplug.spotless + spotless-maven-plugin + 2.44.4 + + + + + 2.39.0 + + + + + diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriver.java index a2d8d0e..93d3a4f 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriver.java @@ -12,6 +12,13 @@ */ package com.amazonaws.secretsmanager.sql; +import com.amazonaws.secretsmanager.caching.SecretCache; +import com.amazonaws.secretsmanager.caching.SecretCacheConfiguration; +import com.amazonaws.secretsmanager.util.Config; +import com.amazonaws.secretsmanager.util.JDBCSecretCacheBuilderProvider; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.IOException; import java.sql.Connection; import java.sql.Driver; @@ -22,16 +29,6 @@ import java.util.Enumeration; import java.util.Properties; import java.util.logging.Logger; - -import com.amazonaws.secretsmanager.caching.SecretCache; -import com.amazonaws.secretsmanager.caching.SecretCacheConfiguration; -import com.amazonaws.secretsmanager.util.Config; -import com.amazonaws.secretsmanager.util.JDBCSecretCacheBuilderProvider; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder; import software.amazon.awssdk.utils.StringUtils; @@ -105,7 +102,7 @@ public abstract class AWSSecretsManagerDriver implements Driver { /** * Message to return on the RuntimeException when secret string is invalid json - */ + */ public static final String INVALID_SECRET_STRING_JSON = "Could not parse SecretString JSON"; private SecretCache secretCache; @@ -116,8 +113,6 @@ public abstract class AWSSecretsManagerDriver implements Driver { private ObjectMapper mapper = new ObjectMapper(); - - /** * Constructs the driver setting the properties from the properties file using system properties as defaults. * Instantiates the secret cache with default options. @@ -126,7 +121,6 @@ protected AWSSecretsManagerDriver() { this(new JDBCSecretCacheBuilderProvider().build()); } - /** * Constructs the driver setting the properties from the properties file using system properties as defaults. * Sets the secret cache to the cache that was passed in. @@ -196,7 +190,8 @@ private void loadRealDriver() { try { Class.forName(this.realDriverClass); } catch (ClassNotFoundException e) { - throw new IllegalStateException("Could not load real driver with name, \"" + this.realDriverClass + "\".", e); + throw new IllegalStateException( + "Could not load real driver with name, \"" + this.realDriverClass + "\".", e); } } @@ -268,8 +263,8 @@ public Driver getWrappedDriver() { } } throw new IllegalStateException("No Driver has been registered with name, " + this.realDriverClass - + ". Please check your system properties or " + Config.CONFIG_FILE_NAME - + " for typos. Also ensure that the Driver registers itself."); + + ". Please check your system properties or " + Config.CONFIG_FILE_NAME + + " for typos. Also ensure that the Driver registers itself."); } @Override @@ -284,7 +279,7 @@ public boolean acceptsURL(String url) throws SQLException { } else if (url.startsWith("jdbc:")) { // For any other JDBC URL, return false return false; - } else { + } else { // We accept a secret ID as the URL so if the config is set, and it's not a JDBC URL, return true return true; } @@ -306,7 +301,7 @@ public boolean acceptsURL(String url) throws SQLException { /** * Construct a database URL from the endpoint, port and database name. This method is called when the - * connect method is called with a secret ID instead of a URL. + * connect method is called with a secret ID instead of a URL. * * @param endpoint The endpoint retrieved from the secret cache * @param port The port retrieved from the secret cache @@ -364,11 +359,10 @@ private Connection connectWithSecret(String unwrappedUrl, Properties info, Strin if (isExceptionDueToAuthenticationError(e)) { boolean refreshSuccess = this.secretCache.refreshNow(credentialsSecretId); if (!refreshSuccess) { - throw(e); + throw (e); } - } - else { - throw(e); + } else { + throw (e); } } } @@ -391,8 +385,8 @@ public Connection connect(String url, Properties info) throws SQLException { try { String secretString = secretCache.getSecretString(url); if (StringUtils.isBlank(secretString)) { - throw new IllegalArgumentException("URL " + url + " is not a valid URL starting with scheme " + - SCHEME + " or a valid retrievable secret ID "); + throw new IllegalArgumentException("URL " + url + " is not a valid URL starting with scheme " + + SCHEME + " or a valid retrievable secret ID "); } JsonNode jsonObject = mapper.readTree(secretString); String endpoint = jsonObject.get("host").asText(); diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriver.java index 1a44728..d8bfbfb 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriver.java @@ -12,11 +12,9 @@ */ package com.amazonaws.secretsmanager.sql; -import java.sql.SQLException; - import com.amazonaws.secretsmanager.caching.SecretCache; import com.amazonaws.secretsmanager.caching.SecretCacheConfiguration; - +import java.sql.SQLException; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder; import software.amazon.awssdk.utils.StringUtils; diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriver.java index e151b86..515c71d 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriver.java @@ -15,7 +15,6 @@ import com.amazonaws.secretsmanager.caching.SecretCache; import com.amazonaws.secretsmanager.caching.SecretCacheConfiguration; import com.amazonaws.secretsmanager.util.SQLExceptionUtils; - import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder; import software.amazon.awssdk.utils.StringUtils; diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriver.java index 975f4ce..d493a8d 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriver.java @@ -15,7 +15,6 @@ import com.amazonaws.secretsmanager.caching.SecretCache; import com.amazonaws.secretsmanager.caching.SecretCacheConfiguration; import com.amazonaws.secretsmanager.util.SQLExceptionUtils; - import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder; import software.amazon.awssdk.utils.StringUtils; diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriver.java index a8f1d96..3cd8e54 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriver.java @@ -12,11 +12,9 @@ */ package com.amazonaws.secretsmanager.sql; -import java.sql.SQLException; - import com.amazonaws.secretsmanager.caching.SecretCache; import com.amazonaws.secretsmanager.caching.SecretCacheConfiguration; - +import java.sql.SQLException; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder; import software.amazon.awssdk.utils.StringUtils; @@ -125,8 +123,8 @@ public boolean isExceptionDueToAuthenticationError(Exception e) { SQLException sqle = (SQLException) e; int errorCode = sqle.getErrorCode(); return errorCode == USER_CREDENTIALS_DO_NOT_MATCH - || errorCode == INVALID_USERNAME_OR_PASSWORD - || errorCode == INCORRECT_USER_PASSWORD; + || errorCode == INVALID_USERNAME_OR_PASSWORD + || errorCode == INCORRECT_USER_PASSWORD; } return false; } diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriver.java index 1b6dacf..b37c9cd 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriver.java @@ -12,11 +12,9 @@ */ package com.amazonaws.secretsmanager.sql; -import java.sql.SQLException; - import com.amazonaws.secretsmanager.caching.SecretCache; import com.amazonaws.secretsmanager.caching.SecretCacheConfiguration; - +import java.sql.SQLException; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder; import software.amazon.awssdk.utils.StringUtils; @@ -113,7 +111,8 @@ public boolean isExceptionDueToAuthenticationError(Exception e) { if (e instanceof SQLException) { SQLException sqle = (SQLException) e; String sqlState = sqle.getSQLState(); - return sqlState.equals(ACCESS_DENIED_FOR_USER_USING_PASSWORD_TO_DATABASE) || sqlState.equals(ACCESS_DENIED_FOR_INVALID_AUTHORIZATION_SPECIFICATION); + return sqlState.equals(ACCESS_DENIED_FOR_USER_USING_PASSWORD_TO_DATABASE) + || sqlState.equals(ACCESS_DENIED_FOR_INVALID_AUTHORIZATION_SPECIFICATION); } return false; } diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriver.java index 4b84dc6..b4722bc 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriver.java @@ -12,11 +12,9 @@ */ package com.amazonaws.secretsmanager.sql; -import java.sql.SQLException; - import com.amazonaws.secretsmanager.caching.SecretCache; import com.amazonaws.secretsmanager.caching.SecretCacheConfiguration; - +import java.sql.SQLException; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder; import software.amazon.awssdk.utils.StringUtils; diff --git a/src/main/java/com/amazonaws/secretsmanager/util/Config.java b/src/main/java/com/amazonaws/secretsmanager/util/Config.java index 5ee797a..0a4db49 100644 --- a/src/main/java/com/amazonaws/secretsmanager/util/Config.java +++ b/src/main/java/com/amazonaws/secretsmanager/util/Config.java @@ -17,7 +17,6 @@ import java.util.Enumeration; import java.util.NoSuchElementException; import java.util.Properties; - import lombok.EqualsAndHashCode; /** @@ -70,7 +69,7 @@ private static Properties loadPropertiesFromConfigFile(String resourceName) { try { configFile = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName); - if(configFile != null) { + if (configFile != null) { newConfig.load(configFile); configFile.close(); } @@ -172,7 +171,7 @@ public Config getSubconfig(String subprefix) { public String fullPropertyName(String propertyName) { if (prefix != null) { return prefix + "." + propertyName; - } else { + } else { return propertyName; } } @@ -211,11 +210,13 @@ public int getIntPropertyWithDefault(String propertyName, int defaultValue) { if (propertyValue == null) { return defaultValue; } else { - try { + try { return Integer.parseInt(propertyValue); } catch (NumberFormatException e) { - throw new PropertyException(fullPropertyName(propertyName) + " must be of type int. Please check " - + Config.CONFIG_FILE_NAME + " or your system properties for typos.", e); + throw new PropertyException( + fullPropertyName(propertyName) + " must be of type int. Please check " + Config.CONFIG_FILE_NAME + + " or your system properties for typos.", + e); } } } @@ -237,11 +238,13 @@ public long getLongPropertyWithDefault(String propertyName, long defaultValue) { if (propertyValue == null) { return defaultValue; } else { - try { + try { return Long.parseLong(propertyValue); } catch (NumberFormatException e) { - throw new PropertyException(fullPropertyName(propertyName) + " must be of type long. Please check " - + Config.CONFIG_FILE_NAME + " or your system properties for typos.", e); + throw new PropertyException( + fullPropertyName(propertyName) + " must be of type long. Please check " + + Config.CONFIG_FILE_NAME + " or your system properties for typos.", + e); } } } @@ -262,12 +265,13 @@ public Class getClassPropertyWithDefault(String propertyName, Class defaul if (propertyValue == null) { return defaultValue; } else { - try { + try { return Class.forName(propertyValue); } catch (ClassNotFoundException e) { - throw new PropertyException(fullPropertyName(propertyName) + " must be a valid class name. Please check" - + " " + Config.CONFIG_FILE_NAME + " or your system properties for typos.", - e); + throw new PropertyException( + fullPropertyName(propertyName) + " must be a valid class name. Please check" + " " + + Config.CONFIG_FILE_NAME + " or your system properties for typos.", + e); } } } @@ -282,8 +286,8 @@ public Class getClassPropertyWithDefault(String propertyName, Class defaul private void throwIfPropertyIsNotSet(String propertyName) { if (config.getProperty(propertyName) == null) { throw new NoSuchElementException(fullPropertyName(propertyName) - + " property must be specified either in " + Config.CONFIG_FILE_NAME - + " or in the system properties."); + + " property must be specified either in " + Config.CONFIG_FILE_NAME + + " or in the system properties."); } } diff --git a/src/main/java/com/amazonaws/secretsmanager/util/JDBCSecretCacheBuilderProvider.java b/src/main/java/com/amazonaws/secretsmanager/util/JDBCSecretCacheBuilderProvider.java index be774a4..b49943a 100644 --- a/src/main/java/com/amazonaws/secretsmanager/util/JDBCSecretCacheBuilderProvider.java +++ b/src/main/java/com/amazonaws/secretsmanager/util/JDBCSecretCacheBuilderProvider.java @@ -1,9 +1,7 @@ package com.amazonaws.secretsmanager.util; -import java.net.URI; - import com.amazonaws.secretsmanager.sql.AWSSecretsManagerDriver; - +import java.net.URI; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder; @@ -57,12 +55,14 @@ public SecretsManagerClientBuilder build() { SecretsManagerClientBuilder builder = SecretsManagerClient.builder(); - //Retrieve data from information sources. - String vpcEndpointUrl = configFile.getStringPropertyWithDefault(AWSSecretsManagerDriver.PROPERTY_PREFIX+"."+PROPERTY_VPC_ENDPOINT_URL, null); - String vpcEndpointRegion = configFile.getStringPropertyWithDefault(AWSSecretsManagerDriver.PROPERTY_PREFIX+"."+PROPERTY_VPC_ENDPOINT_REGION, null); + // Retrieve data from information sources. + String vpcEndpointUrl = configFile.getStringPropertyWithDefault( + AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + PROPERTY_VPC_ENDPOINT_URL, null); + String vpcEndpointRegion = configFile.getStringPropertyWithDefault( + AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + PROPERTY_VPC_ENDPOINT_REGION, null); String envRegion = System.getenv(REGION_ENVIRONMENT_VARIABLE); - String configRegion = configFile.getStringPropertyWithDefault(AWSSecretsManagerDriver.PROPERTY_PREFIX+"."+PROPERTY_REGION, null); - + String configRegion = configFile.getStringPropertyWithDefault( + AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + PROPERTY_REGION, null); // Apply settings to our builder configuration. if (StringUtils.isNotBlank(vpcEndpointUrl) && StringUtils.isNotBlank(vpcEndpointRegion)) { diff --git a/src/main/java/com/amazonaws/secretsmanager/util/PropertyException.java b/src/main/java/com/amazonaws/secretsmanager/util/PropertyException.java index e91e084..58da136 100644 --- a/src/main/java/com/amazonaws/secretsmanager/util/PropertyException.java +++ b/src/main/java/com/amazonaws/secretsmanager/util/PropertyException.java @@ -38,4 +38,3 @@ public PropertyException(String message) { super(message); } } - diff --git a/src/main/java/com/amazonaws/secretsmanager/util/SQLExceptionUtils.java b/src/main/java/com/amazonaws/secretsmanager/util/SQLExceptionUtils.java index 4ddf51a..9e7d39b 100644 --- a/src/main/java/com/amazonaws/secretsmanager/util/SQLExceptionUtils.java +++ b/src/main/java/com/amazonaws/secretsmanager/util/SQLExceptionUtils.java @@ -19,7 +19,7 @@ public static boolean unwrapAndCheckForCode(Throwable t, int errorCode) { final List list = new ArrayList<>(); while (t != null && list.contains(t) == false) { list.add(t); - if ( t instanceof SQLException && ((SQLException)t).getErrorCode() == errorCode ) { + if (t instanceof SQLException && ((SQLException) t).getErrorCode() == errorCode) { return true; } t = t.getCause(); @@ -27,9 +27,8 @@ public static boolean unwrapAndCheckForCode(Throwable t, int errorCode) { return false; } - /** * Hide constructor for static class */ - private SQLExceptionUtils() { } + private SQLExceptionUtils() {} } diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDb2DriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDb2DriverTest.java index b402ddf..e6d0cd3 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDb2DriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDb2DriverTest.java @@ -12,8 +12,13 @@ */ package com.amazonaws.secretsmanager.sql; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import com.amazonaws.secretsmanager.caching.SecretCache; import com.amazonaws.secretsmanager.util.TestClass; +import java.sql.SQLException; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -23,12 +28,6 @@ import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import java.sql.SQLException; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - /** * Tests for the Db2 Driver. */ diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriverTest.java index fda47f1..3dcfaa4 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriverTest.java @@ -17,11 +17,13 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import com.amazonaws.secretsmanager.caching.SecretCache; +import com.amazonaws.secretsmanager.caching.SecretCacheConfiguration; +import com.amazonaws.secretsmanager.util.TestClass; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; - import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -34,11 +36,6 @@ import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; - -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; @@ -76,7 +73,7 @@ public void setup() throws InterruptedException { public String answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); - if (arguments != null && arguments.length > 0 && arguments[0] != null){ + if (arguments != null && arguments.length > 0 && arguments[0] != null) { String secretId = (String) arguments[0]; String returnUser = secretId; if (INVALID_USER.equals(secretId)) { @@ -89,7 +86,9 @@ public String answer(InvocationOnMock invocation) throws Throwable { returnUser = DummyDriver.SQL_ERROR_USERNAME; } - return String.format("{\"username\": \"%s\",\n\"password\": \"%s\",\n\"host\": \"%s\"}", returnUser, secretId, secretId); + return String.format( + "{\"username\": \"%s\",\n\"password\": \"%s\",\n\"host\": \"%s\"}", + returnUser, secretId, secretId); } return null; @@ -100,7 +99,7 @@ public String answer(InvocationOnMock invocation) throws Throwable { public Boolean answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); - if (arguments != null && arguments.length > 0 && arguments[0] != null){ + if (arguments != null && arguments.length > 0 && arguments[0] != null) { String secretId = (String) arguments[0]; if (BAD_REFRESH_SECRET.equals(secretId)) { return false; @@ -127,18 +126,20 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable { /******************************************************************************************************************* * init Tests ******************************************************************************************************************/ - @Test public void test_init_constructor_null_params() { try { new AWSSecretsManagerDummyDriver((SecretsManagerClientBuilder) null); - } catch (Exception e) {} + } catch (Exception e) { + } try { new AWSSecretsManagerDummyDriver((SecretCacheConfiguration) null); - } catch (Exception e) {} + } catch (Exception e) { + } try { new AWSSecretsManagerDummyDriver((SecretsManagerClient) null); - } catch (Exception e) {} + } catch (Exception e) { + } } @Test @@ -151,7 +152,6 @@ public void test_init_works_realDriverFromConfig() { /******************************************************************************************************************* * getWrappedDriver Tests ******************************************************************************************************************/ - @Test public void test_getWrappedDriver_works_goodDriver() { assertEquals(DummyDriver.instance, sut.getWrappedDriver()); @@ -166,7 +166,6 @@ public void test_getWrappedDriver_throws_badDriver() { /******************************************************************************************************************* * acceptsURL Tests ******************************************************************************************************************/ - @Test public void test_acceptsURL_throws_nullURL() { assertThrows(SQLException.class, () -> sut.acceptsURL(null)); @@ -199,7 +198,6 @@ public void test_acceptsURL_returnsTrue_secretId() { /******************************************************************************************************************* * connect Tests ******************************************************************************************************************/ - @Test public void test_connect_throws_nullURL() { assertThrows(SQLException.class, () -> sut.connect(null, null)); @@ -312,7 +310,6 @@ public void test_connect_throws_userBadlyFormattedSecretId() { /******************************************************************************************************************* * getMajorVersion Tests ******************************************************************************************************************/ - @Test public void test_getMajorVersion_propagatesToRealDriver() { assertEquals(DummyDriver.GET_MAJOR_VERSION_RETURN_VALUE, sut.getMajorVersion()); @@ -322,7 +319,6 @@ public void test_getMajorVersion_propagatesToRealDriver() { /******************************************************************************************************************* * getMinorVersion Tests ******************************************************************************************************************/ - @Test public void test_getMinorVersion_propagatesToRealDriver() { assertEquals(DummyDriver.GET_MINOR_VERSION_RETURN_VALUE, sut.getMinorVersion()); @@ -332,7 +328,6 @@ public void test_getMinorVersion_propagatesToRealDriver() { /******************************************************************************************************************* * getParentLogger Tests ******************************************************************************************************************/ - @Test public void test_getParentLogger_propagatesToRealDriver() { assertNotThrows(() -> assertEquals(null, sut.getParentLogger())); @@ -342,7 +337,6 @@ public void test_getParentLogger_propagatesToRealDriver() { /******************************************************************************************************************* * getPropertyInfo Tests ******************************************************************************************************************/ - @Test public void test_getPropertyInfo_propagatesToRealDriver() { String param1 = "jdbc-secretsmanager:expectedUrl"; @@ -357,7 +351,6 @@ public void test_getPropertyInfo_propagatesToRealDriver() { /******************************************************************************************************************* * jdbcCompliant Tests ******************************************************************************************************************/ - @Test public void test_jdbcCompliant_propagatesToRealDriver() { assertEquals(true, sut.jdbcCompliant()); diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDummyDriver.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDummyDriver.java index 170b8c4..06d46bd 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDummyDriver.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDummyDriver.java @@ -14,7 +14,6 @@ import com.amazonaws.secretsmanager.caching.SecretCache; import com.amazonaws.secretsmanager.caching.SecretCacheConfiguration; - import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClientBuilder; diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriverTest.java index e98b5c9..ddeb799 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriverTest.java @@ -16,8 +16,9 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import com.amazonaws.secretsmanager.caching.SecretCache; +import com.amazonaws.secretsmanager.util.TestClass; import java.sql.SQLException; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -27,9 +28,6 @@ import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import com.amazonaws.secretsmanager.caching.SecretCache; -import com.amazonaws.secretsmanager.util.TestClass; - /** * Tests for the MSSQL Driver. */ diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriverTest.java index fff739d..9237bbc 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriverTest.java @@ -16,8 +16,9 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import com.amazonaws.secretsmanager.caching.SecretCache; +import com.amazonaws.secretsmanager.util.TestClass; import java.sql.SQLException; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -27,9 +28,6 @@ import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import com.amazonaws.secretsmanager.caching.SecretCache; -import com.amazonaws.secretsmanager.util.TestClass; - /** * Tests for the MariaDB Driver. */ diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriverTest.java index 382a3ea..23f0b68 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriverTest.java @@ -16,8 +16,9 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import com.amazonaws.secretsmanager.caching.SecretCache; +import com.amazonaws.secretsmanager.util.TestClass; import java.sql.SQLException; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -27,9 +28,6 @@ import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import com.amazonaws.secretsmanager.caching.SecretCache; -import com.amazonaws.secretsmanager.util.TestClass; - /** * Tests for the MySQL Driver. */ diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriverTest.java index 59a6ab6..9314eb3 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriverTest.java @@ -16,8 +16,9 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import com.amazonaws.secretsmanager.caching.SecretCache; +import com.amazonaws.secretsmanager.util.TestClass; import java.sql.SQLException; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -27,9 +28,6 @@ import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import com.amazonaws.secretsmanager.caching.SecretCache; -import com.amazonaws.secretsmanager.util.TestClass; - /** * Tests for the Oracle Driver. */ diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriverTest.java index a07579e..2e5e506 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriverTest.java @@ -16,8 +16,9 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import com.amazonaws.secretsmanager.caching.SecretCache; +import com.amazonaws.secretsmanager.util.TestClass; import java.sql.SQLException; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -27,9 +28,6 @@ import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import com.amazonaws.secretsmanager.caching.SecretCache; -import com.amazonaws.secretsmanager.util.TestClass; - /** * Tests for the PostgreSQL Driver. */ diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriverTest.java index 600d8d6..9c3879d 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriverTest.java @@ -16,8 +16,9 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import com.amazonaws.secretsmanager.caching.SecretCache; +import com.amazonaws.secretsmanager.util.TestClass; import java.sql.SQLException; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -27,9 +28,6 @@ import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; -import com.amazonaws.secretsmanager.caching.SecretCache; -import com.amazonaws.secretsmanager.util.TestClass; - /** * Tests for the Redshift Driver. */ diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/DummyDriver.java b/src/test/java/com/amazonaws/secretsmanager/sql/DummyDriver.java index c987c93..fe2d8bd 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/DummyDriver.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/DummyDriver.java @@ -12,8 +12,8 @@ */ package com.amazonaws.secretsmanager.sql; -import java.sql.Driver; import java.sql.Connection; +import java.sql.Driver; import java.sql.DriverManager; import java.sql.DriverPropertyInfo; import java.sql.SQLException; @@ -38,6 +38,7 @@ public class DummyDriver implements Driver { } public static int acceptsURLCallCount; + @Override public boolean acceptsURL(String url) throws SQLException { acceptsURLCallCount++; @@ -47,6 +48,7 @@ public boolean acceptsURL(String url) throws SQLException { public static final String SQL_ERROR_USERNAME = "SQL_ERROR_USERNAME"; public static final String RUNTIME_ERROR_USERNAME = "RUNTIME_ERROR_USERNAME"; public static int connectCallCount; + @Override public Connection connect(String url, Properties info) throws SQLException { connectCallCount++; @@ -60,6 +62,7 @@ public Connection connect(String url, Properties info) throws SQLException { public static int getMajorVersionCallCount; public static final int GET_MAJOR_VERSION_RETURN_VALUE = 87; + @Override public int getMajorVersion() { getMajorVersionCallCount++; @@ -68,6 +71,7 @@ public int getMajorVersion() { public static int getMinorVersionCallCount; public static final int GET_MINOR_VERSION_RETURN_VALUE = 75; + @Override public int getMinorVersion() { getMinorVersionCallCount++; @@ -75,6 +79,7 @@ public int getMinorVersion() { } public static int getParentLoggerCallCount; + @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { getParentLoggerCallCount++; @@ -84,6 +89,7 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException { public static int getPropertyInfoCallCount; public static String getPropertyInfoParam1; public static Properties getPropertyInfoParam2; + @Override public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { getPropertyInfoCallCount++; @@ -93,6 +99,7 @@ public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws } public static int jdbcCompliantCallCount; + @Override public boolean jdbcCompliant() { jdbcCompliantCallCount++; @@ -110,4 +117,3 @@ public static void reset() { getPropertyInfoParam2 = null; } } - diff --git a/src/test/java/com/amazonaws/secretsmanager/util/ConfigTest.java b/src/test/java/com/amazonaws/secretsmanager/util/ConfigTest.java index 65262d1..b736489 100644 --- a/src/test/java/com/amazonaws/secretsmanager/util/ConfigTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/util/ConfigTest.java @@ -17,7 +17,6 @@ import java.util.NoSuchElementException; import java.util.Properties; - import org.junit.Test; /** diff --git a/src/test/java/com/amazonaws/secretsmanager/util/JDBCSecretCacheBuilderProviderTest.java b/src/test/java/com/amazonaws/secretsmanager/util/JDBCSecretCacheBuilderProviderTest.java index e05d033..d20430b 100644 --- a/src/test/java/com/amazonaws/secretsmanager/util/JDBCSecretCacheBuilderProviderTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/util/JDBCSecretCacheBuilderProviderTest.java @@ -8,12 +8,10 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import com.amazonaws.secretsmanager.sql.AWSSecretsManagerDriver; import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.EnvironmentVariables; - -import com.amazonaws.secretsmanager.sql.AWSSecretsManagerDriver; - import software.amazon.awssdk.core.exception.SdkClientException; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; @@ -29,11 +27,12 @@ public class JDBCSecretCacheBuilderProviderTest { @Test public void test_setRegion_configFileProperty() { Config configProvider = mock(Config.class); - String regionName = AWSSecretsManagerDriver.PROPERTY_PREFIX + "." - + JDBCSecretCacheBuilderProvider.PROPERTY_REGION; + String regionName = + AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + JDBCSecretCacheBuilderProvider.PROPERTY_REGION; when(configProvider.getStringPropertyWithDefault(regionName, null)).thenReturn("us-west-2"); - SecretsManagerClient client = new JDBCSecretCacheBuilderProvider(configProvider).build().build(); + SecretsManagerClient client = + new JDBCSecretCacheBuilderProvider(configProvider).build().build(); assertEquals(client.serviceClientConfiguration().region(), Region.US_WEST_2); } @@ -46,7 +45,8 @@ public void test_setRegion_environmentVariable() { environmentVariables.set(environmentRegionName, "us-east-1"); assertEquals("us-east-1", System.getenv(environmentRegionName)); - SecretsManagerClient client = new JDBCSecretCacheBuilderProvider(configProvider).build().build(); + SecretsManagerClient client = + new JDBCSecretCacheBuilderProvider(configProvider).build().build(); assertEquals(client.serviceClientConfiguration().region(), Region.US_EAST_1); } @@ -56,12 +56,16 @@ public void test_setRegion_vpcEndpoint() { String vpcEndpointUrlName = AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + PROPERTY_VPC_ENDPOINT_URL; String vpcEndpointRegion = AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + PROPERTY_VPC_ENDPOINT_REGION; String vpcEndpointUrlString = "https://asdf.us-west-2.amazonaws.com"; - when(configProvider.getStringPropertyWithDefault(vpcEndpointUrlName, null)).thenReturn(vpcEndpointUrlString); - when(configProvider.getStringPropertyWithDefault(vpcEndpointRegion, null)).thenReturn("ap-southeast-3"); + when(configProvider.getStringPropertyWithDefault(vpcEndpointUrlName, null)) + .thenReturn(vpcEndpointUrlString); + when(configProvider.getStringPropertyWithDefault(vpcEndpointRegion, null)) + .thenReturn("ap-southeast-3"); - SecretsManagerClient client = new JDBCSecretCacheBuilderProvider(configProvider).build().build(); + SecretsManagerClient client = + new JDBCSecretCacheBuilderProvider(configProvider).build().build(); - assertEquals(client.serviceClientConfiguration().endpointOverride().get().toString(), vpcEndpointUrlString); + assertEquals( + client.serviceClientConfiguration().endpointOverride().get().toString(), vpcEndpointUrlString); assertEquals(client.serviceClientConfiguration().region(), Region.AP_SOUTHEAST_3); } @@ -77,14 +81,13 @@ public void test_setRegion_defaultsToEnv() { /** * SetRegion priority tests. */ - @Test public void test_regionSelectionOrder_prefersVpcEndpointOverEverything() { Config configProvider = mock(Config.class); // Arrange so all properties return something valid. - String regionName = AWSSecretsManagerDriver.PROPERTY_PREFIX + "." - + JDBCSecretCacheBuilderProvider.PROPERTY_REGION; + String regionName = + AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + JDBCSecretCacheBuilderProvider.PROPERTY_REGION; String vpcEndpointUrlName = AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + PROPERTY_VPC_ENDPOINT_URL; String vpcEndpointRegion = AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + PROPERTY_VPC_ENDPOINT_REGION; String environmentRegionName = JDBCSecretCacheBuilderProvider.REGION_ENVIRONMENT_VARIABLE; @@ -95,31 +98,34 @@ public void test_regionSelectionOrder_prefersVpcEndpointOverEverything() { when(configProvider.getStringPropertyWithDefault(regionName, null)).thenReturn("us-east-1"); when(configProvider.getStringPropertyWithDefault(vpcEndpointUrlName, null)) .thenReturn(vpcEndpointUrlString); - when(configProvider.getStringPropertyWithDefault(vpcEndpointRegion, null)).thenReturn("us-west-2"); + when(configProvider.getStringPropertyWithDefault(vpcEndpointRegion, null)) + .thenReturn("us-west-2"); // Act: Build our client - SecretsManagerClient client = new JDBCSecretCacheBuilderProvider(configProvider).build().build(); + SecretsManagerClient client = + new JDBCSecretCacheBuilderProvider(configProvider).build().build(); // Assert: Make sure the endpoint was configured properly. assertNotEquals(client.serviceClientConfiguration().region(), Region.US_EAST_2); assertNotEquals(client.serviceClientConfiguration().region(), Region.US_EAST_1); assertEquals(client.serviceClientConfiguration().region(), Region.US_WEST_2); - assertEquals(client.serviceClientConfiguration().endpointOverride().get().toString(), - vpcEndpointUrlString); + assertEquals( + client.serviceClientConfiguration().endpointOverride().get().toString(), vpcEndpointUrlString); } @Test public void test_regionSelectionOrder_prefersEnvironmentVarOverConfig() { Config configProvider = mock(Config.class); - String regionName = AWSSecretsManagerDriver.PROPERTY_PREFIX + "." - + JDBCSecretCacheBuilderProvider.PROPERTY_REGION; + String regionName = + AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + JDBCSecretCacheBuilderProvider.PROPERTY_REGION; String environmentRegionName = JDBCSecretCacheBuilderProvider.REGION_ENVIRONMENT_VARIABLE; environmentVariables.set(environmentRegionName, "eu-west-3"); when(configProvider.getStringPropertyWithDefault(regionName, null)).thenReturn("us-east-2"); - SecretsManagerClient client = new JDBCSecretCacheBuilderProvider(configProvider).build().build(); + SecretsManagerClient client = + new JDBCSecretCacheBuilderProvider(configProvider).build().build(); assertNotEquals(client.serviceClientConfiguration().region(), Region.US_EAST_2); assertEquals(client.serviceClientConfiguration().region(), Region.EU_WEST_3); @@ -132,8 +138,8 @@ public void test_regionSelectionOrder_prefersEnvironmentVarOverConfig() { public void test_settingValidation_emptyConfigPropertyIgnored() { Config configProvider = mock(Config.class); - String regionName = AWSSecretsManagerDriver.PROPERTY_PREFIX + "." - + JDBCSecretCacheBuilderProvider.PROPERTY_REGION; + String regionName = + AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + JDBCSecretCacheBuilderProvider.PROPERTY_REGION; when(configProvider.getStringPropertyWithDefault(regionName, null)).thenReturn(""); try { @@ -147,8 +153,8 @@ public void test_settingValidation_emptyConfigPropertyIgnored() { public void test_settingValidation_nullConfigPropertyIgnored() { Config configProvider = mock(Config.class); - String regionName = AWSSecretsManagerDriver.PROPERTY_PREFIX + "." - + JDBCSecretCacheBuilderProvider.PROPERTY_REGION; + String regionName = + AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + JDBCSecretCacheBuilderProvider.PROPERTY_REGION; when(configProvider.getStringPropertyWithDefault(regionName, null)).thenReturn(""); try { @@ -194,11 +200,14 @@ public void test_settingValidation_emptyVpcIgnored() { Config configProvider = mock(Config.class); String vpcEndpointUrlName = AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + PROPERTY_VPC_ENDPOINT_URL; String vpcEndpointRegion = AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + PROPERTY_VPC_ENDPOINT_REGION; - when(configProvider.getStringPropertyWithDefault(vpcEndpointUrlName, null)).thenReturn(""); - when(configProvider.getStringPropertyWithDefault(vpcEndpointRegion, null)).thenReturn(""); + when(configProvider.getStringPropertyWithDefault(vpcEndpointUrlName, null)) + .thenReturn(""); + when(configProvider.getStringPropertyWithDefault(vpcEndpointRegion, null)) + .thenReturn(""); try { - SecretsManagerClient client = new JDBCSecretCacheBuilderProvider(configProvider).build().build(); + SecretsManagerClient client = + new JDBCSecretCacheBuilderProvider(configProvider).build().build(); assertTrue(client.serviceClientConfiguration().endpointOverride().isEmpty()); } catch (SdkClientException e) { assertTrue(e.getMessage().startsWith("Unable to load region from any of the providers in the chain")); @@ -211,15 +220,17 @@ public void test_settingValidation_nullVpcIgnored() { Config configProvider = mock(Config.class); String vpcEndpointUrlName = AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + PROPERTY_VPC_ENDPOINT_URL; String vpcEndpointRegion = AWSSecretsManagerDriver.PROPERTY_PREFIX + "." + PROPERTY_VPC_ENDPOINT_REGION; - when(configProvider.getStringPropertyWithDefault(vpcEndpointUrlName, null)).thenReturn(null); - when(configProvider.getStringPropertyWithDefault(vpcEndpointRegion, null)).thenReturn(null); + when(configProvider.getStringPropertyWithDefault(vpcEndpointUrlName, null)) + .thenReturn(null); + when(configProvider.getStringPropertyWithDefault(vpcEndpointRegion, null)) + .thenReturn(null); try { - SecretsManagerClient client = new JDBCSecretCacheBuilderProvider(configProvider).build().build(); + SecretsManagerClient client = + new JDBCSecretCacheBuilderProvider(configProvider).build().build(); assertTrue(client.serviceClientConfiguration().endpointOverride().isEmpty()); } catch (SdkClientException e) { assertTrue(e.getMessage().startsWith("Unable to load region from any of the providers in the chain")); } } - } diff --git a/src/test/java/com/amazonaws/secretsmanager/util/SQLExceptionUtilsTest.java b/src/test/java/com/amazonaws/secretsmanager/util/SQLExceptionUtilsTest.java index 0315018..18ca847 100644 --- a/src/test/java/com/amazonaws/secretsmanager/util/SQLExceptionUtilsTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/util/SQLExceptionUtilsTest.java @@ -4,7 +4,6 @@ import static org.junit.Assert.assertTrue; import java.sql.SQLException; - import org.junit.Test; public class SQLExceptionUtilsTest { @@ -43,7 +42,7 @@ public void test_unwrapAndCheckForCode_loopInWrappedExceptions_returnsFalse() { public void test_unwrapAndCheckForCode_nonSqlException_parentStillGetsFound() { SQLException e0 = new SQLException("", "", 1046); Exception e1 = new Exception("test", e0); - SQLException e2 = new SQLException("", "", 42,e1); + SQLException e2 = new SQLException("", "", 42, e1); assertTrue(SQLExceptionUtils.unwrapAndCheckForCode(e2, 1046)); } diff --git a/src/test/java/com/amazonaws/secretsmanager/util/TestClass.java b/src/test/java/com/amazonaws/secretsmanager/util/TestClass.java index 65bb4d9..5ed19c1 100644 --- a/src/test/java/com/amazonaws/secretsmanager/util/TestClass.java +++ b/src/test/java/com/amazonaws/secretsmanager/util/TestClass.java @@ -17,7 +17,6 @@ import java.lang.reflect.Method; import java.util.Arrays; import java.util.LinkedList; - import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest; /** @@ -159,8 +158,8 @@ public void assertThrows(Class exception, throwingRunnable } catch (Exception e) { if (!exception.isAssignableFrom(e.getClass())) { e.printStackTrace(); - throw new RuntimeException( - "Should have thrown a " + exception.getName() + " but threw " + e.getClass().getName()); + throw new RuntimeException("Should have thrown a " + exception.getName() + " but threw " + + e.getClass().getName()); } } } @@ -172,8 +171,8 @@ public void assertThrows(Exception exception, throwingRunnable code) { } catch (Exception e) { if (!exception.equals(e)) { e.printStackTrace(); - throw new RuntimeException( - "Should have thrown a " + exception.getMessage() + " but threw " + e.getClass().getName()); + throw new RuntimeException("Should have thrown a " + exception.getMessage() + " but threw " + + e.getClass().getName()); } } } @@ -183,7 +182,8 @@ public void assertNotThrows(throwingRunnable code) { code.run(); } catch (Exception e) { e.printStackTrace(); - throw new RuntimeException("Should not have thrown, but threw " + e.getClass().getName()); + throw new RuntimeException( + "Should not have thrown, but threw " + e.getClass().getName()); } } }