public abstract class AWSSecretsManagerDriver extends Object implements Driver
Provides support for accessing SQL databases using credentials stored within AWS Secrets Manager. If this functionality is desired, then a subclass of this class should be specified as the JDBC driver for an application.
The driver to propagate connect requests to should also be specified in the configuration. Doing this
will cause the real driver to be registered once an instance of this driver is made (which will be when this driver
is registered).
This base class registers itself with the java.sql.DriverManager when its constructor is called. That
means a subclass only needs to make a new instance of itself in its static block to register.
This does not support including the user (secret ID) and password in the jdbc url, as JDBC url formats are database specific. If this functionality is desired, it must be implemented in a subclass.
Ignores the password field, drawing a secret ID from the user field. The secret referred to by this field is expected to be in the standard JSON format used by the rotation lambdas provided by Secrets Manager:
{
"username": "xxxx",
"password": "xxxx",
...
}
Here is a list of the configuration properties. The subprefix is an implementation specific String used to keep
the properties for different drivers separate. For example, the MySQL driver wrapper might use mysql as its
subprefix, making the full property name for the realDriverClass for the MySQL driver wrapper
drivers.mysql.realDriverClass (all Driver properties will be prefixed with "drivers."). This String is defined by
the method getPropertySubprefix.
| Modifier and Type | Field and Description |
|---|---|
static String |
INVALID_SECRET_STRING_JSON
Message to return on the RuntimeException when secret string is invalid json
|
static int |
MAX_RETRY
Maximum number of times to retry connecting to DB on auth failures
|
static String |
PROPERTY_PREFIX
"drivers", so all configuration properties start with "drivers.".
|
static String |
SCHEME
"jdbc-secretsmanager", so the JDBC URL should start with "jdbc-secretsmanager" instead of just "jdbc".
|
| Modifier | Constructor and Description |
|---|---|
protected |
AWSSecretsManagerDriver()
Constructs the driver setting the properties from the properties file using system properties as defaults.
|
protected |
AWSSecretsManagerDriver(com.amazonaws.services.secretsmanager.AWSSecretsManager client)
Constructs the driver setting the properties from the properties file using system properties as defaults.
|
protected |
AWSSecretsManagerDriver(com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder builder)
Constructs the driver setting the properties from the properties file using system properties as defaults.
|
protected |
AWSSecretsManagerDriver(com.amazonaws.secretsmanager.caching.SecretCache cache)
Constructs the driver setting the properties from the properties file using system properties as defaults.
|
protected |
AWSSecretsManagerDriver(com.amazonaws.secretsmanager.caching.SecretCacheConfiguration cacheConfig)
Constructs the driver setting the properties from the properties file using system properties as defaults.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
acceptsURL(String url) |
Connection |
connect(String url,
Properties info) |
abstract String |
constructUrlFromEndpointPortDatabase(String endpoint,
String port,
String dbname)
Construct a database URL from the endpoint, port and database name.
|
abstract String |
getDefaultDriverClass()
Get the default real driver class name for this driver.
|
int |
getMajorVersion() |
int |
getMinorVersion() |
Logger |
getParentLogger() |
DriverPropertyInfo[] |
getPropertyInfo(String url,
Properties info) |
abstract String |
getPropertySubprefix()
Gets the "subprefix" used for configuration properties for this driver.
|
Driver |
getWrappedDriver()
Returns an instance of the real
java.sql.Driver that this should propagate calls to. |
abstract boolean |
isExceptionDueToAuthenticationError(Exception exception)
Determines whether or not an
Exception is due to an authentication failure with the remote
database. |
boolean |
jdbcCompliant() |
protected static void |
register(AWSSecretsManagerDriver driver)
Registers a driver along with the
DriverAction implementation. |
public static final String SCHEME
public static final int MAX_RETRY
public static final String PROPERTY_PREFIX
public static final String INVALID_SECRET_STRING_JSON
protected AWSSecretsManagerDriver()
protected AWSSecretsManagerDriver(com.amazonaws.secretsmanager.caching.SecretCache cache)
cache - Secret cache to use to retrieve secretsprotected AWSSecretsManagerDriver(com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder builder)
builder - Builder used to instantiate cacheprotected AWSSecretsManagerDriver(com.amazonaws.services.secretsmanager.AWSSecretsManager client)
client - AWS Secrets Manager client to instantiate cacheprotected AWSSecretsManagerDriver(com.amazonaws.secretsmanager.caching.SecretCacheConfiguration cacheConfig)
cacheConfig - Cache configuration to instantiate cacheprotected static void register(AWSSecretsManagerDriver driver)
DriverAction implementation.driver - The driver to register.RuntimeException - If the driver could not be registered.public abstract String getPropertySubprefix()
public Driver getWrappedDriver()
java.sql.Driver that this should propagate calls to. The real
driver is specified by the realDriverClass property.Driver that calls should be
propagated to.IllegalStateException - When there is no driver with the name
realDriverClasspublic boolean acceptsURL(String url) throws SQLException
acceptsURL in interface DriverSQLExceptionpublic abstract boolean isExceptionDueToAuthenticationError(Exception exception)
Exception is due to an authentication failure with the remote
database. This method is called during connect to decide if authentication needs to be attempted
again with refreshed credentials. A good way to implement this is to look up the error codes that
java.sqlSQLExceptions will have when an authentication failure occurs. These are database
specific.exception - The Exception to test.Exception indicates that
the credentials used for authentication are stale.public abstract String constructUrlFromEndpointPortDatabase(String endpoint, String port, String dbname)
connect method is called with a secret ID instead of a URL.endpoint - The endpoint retrieved from the secret cacheport - The port retrieved from the secret cachedbname - The database name retrieved from the secret cachepublic abstract String getDefaultDriverClass()
public Connection connect(String url, Properties info) throws SQLException
connect in interface DriverSQLExceptionpublic int getMajorVersion()
getMajorVersion in interface Driverpublic int getMinorVersion()
getMinorVersion in interface Driverpublic Logger getParentLogger() throws SQLFeatureNotSupportedException
getParentLogger in interface DriverSQLFeatureNotSupportedExceptionpublic DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException
getPropertyInfo in interface DriverSQLExceptionpublic boolean jdbcCompliant()
jdbcCompliant in interface DriverCopyright © 2023. All rights reserved.