Skip to content

Commit 8f3f4d5

Browse files
authored
Update Example Usage (#260)
* Update Example Usage * Update Usage Example
1 parent 4aa9883 commit 8f3f4d5

File tree

1 file changed

+37
-39
lines changed

1 file changed

+37
-39
lines changed

README.md

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -49,56 +49,54 @@ To use the latest build (pre-release), don't forget to enable the download of sn
4949
```
5050

5151
### Usage Example
52-
We provide database drivers that intercept calls to real database drivers and swap out secret IDs for actual login credentials.
53-
This prevents hard-coding database credentials into your application code. This can be integrated into your app through a few
54-
configuration file changes. Here is an example for making this work with your c3p0 config:
55-
56-
```properties
57-
# c3p0.properties
58-
59-
# MySQL example
60-
c3p0.user=secretId
61-
c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerMySQLDriver
62-
c3p0.jdbcUrl=jdbc-secretsmanager:mysql://example.com:3306
63-
64-
# PostgreSQL example
65-
# c3p0.user=secretId
66-
# c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver
67-
# c3p0.jdbcUrl=jdbc-secretsmanager:postgresql://example.com:5432/database
68-
69-
# Oracle example
70-
# c3p0.user=secretId
71-
# c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerOracleDriver
72-
# c3p0.jdbcUrl=jdbc-secretsmanager:oracle:thin:@example.com:1521/ORCL
73-
74-
# MSSQLServer example
75-
# c3p0.user=secretId
76-
# c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerMSSQLServerDriver
77-
# c3p0.jdbcUrl=jdbc-secretsmanager:sqlserver://example.com:1433
78-
79-
# Redshift example
80-
# c3p0.user=secretId
81-
# c3p0.driverClass=com.amazonaws.secretsmanager.sql.AWSSecretsManagerRedshiftDriver
82-
# c3p0.jdbcUrl=jdbc-secretsmanager:redshift://example.com:5439
52+
We provide database drivers that intercept calls to real database drivers and replace secret IDs with actual login credentials.
53+
This prevents hard-coding database credentials into your application code.
8354

55+
56+
The following is an example which uses the secret to resolve both the endpoint and the login credentials.
57+
58+
59+
```
60+
// Load the JDBC driver
61+
Class.forName( "com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver" ).newInstance();
62+
63+
// Retrieve the connection info from the secret using the secret ARN
64+
String URL = "secretId";
65+
66+
// Populate the user property with the secret ARN to retrieve user and password from the secret
67+
Properties info = new Properties( );
68+
info.put( "user", "secretId" );
69+
70+
// Establish the connection
71+
conn = DriverManager.getConnection(URL, info);
8472
```
8573

86-
The only changes that need to happen in the c3p0 config are to:
74+
To specify a custom endpoint and port instead of resolving from the secret, use the jdbc-secretsmanager prefix with your database information.
75+
76+
```
77+
// Options to resolve the connection information
8778
88-
* change the jdbc url to one that our driver will intercept (starting with jdbc-secretsmanager),
89-
* change the c3p0 user to be the secret ID of the secret in secrets manager that has the username and password,
90-
* and change the `driverClass` to be our driver wrapper.
79+
// Set url to secret arn to resolve endpoint and port from secret
80+
String URL = "secretId";
9181
92-
The secret being used should be in the JSON format we use for our rotation lambdas for RDS databases. E.g:
82+
// Use jdbc-secretsmanager prefix to specify endpoint and port instead of resolving from secret
83+
String URL = "jdbc-secretsmanager:postgresql://example.com:5432/database";
84+
```
9385

86+
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:
9487
```json
9588
{
96-
"username": "user",
97-
"password": "pass",
98-
...
89+
"host": "<host name>",
90+
"username": "<username>",
91+
"password": "<password>",
92+
"dbname": "<database name>",
93+
"port": "<port number>",
9994
}
10095
```
10196

97+
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).
98+
99+
102100
## Credentials
103101

104102
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:

0 commit comments

Comments
 (0)