- Newest
- Most votes
- Most comments
Hello.
I think the following documents will be helpful.
Change the application to obtain authentication information and cache it.
After that, if the cache expires or is rotated and an authentication error occurs, you can create an application to retrieve authentication information from Secrets Manager again.
https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/rotate-database-credentials-without-restarting-containers.html
Referencing Secrets Manager in the ECS task definition sets environment variables which do not refresh unless the container is restarted and is not a secure method to provide credentials
Riku's answer is the best way to go
Thank you for your reply. I have checked the contents of your website.
I am using a Java application, so I understand that the following can be used.
https://github.com/aws/aws-secretsmanager-jdbc
I tried the following to test the connection.
- Add the following to your Java application
public class DatabaseConnection {
public static void main(String[] args) {
try {
/// Load the JDBC driver
Class.forName("com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver").newInstance();
// Get connection information using secret ID
String secretId = "{my_secret_id(ARN)}";
Properties info = new Properties();
info.put("user", secretId);
// Establish database connection
String url = "jdbc-secretsmanager:postgresql://{my_db_host}:{my_db_port}/{my_db_name}";
Connection conn = DriverManager.getConnection(url, info);
// Connection success message
System.out.println("Connection established successfully!");
// Close the connection
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- Download "aws-secretsmanager-jdbc-2.0.2.jar" and place it in any directory.
https://mvnrepository.com/artifact/com.amazonaws.secretsmanager/aws-secretsmanager-jdbc/2.0.2
- Compile Java program
javac -cp .:{path}/aws-secretsmanager-jdbc-2.0.2.jar DatabaseConnection.java
- Run
java -cp .:{path}/aws-secretsmanager-jdbc-2.0.2.jar DatabaseConnection
As a result, the following error will appear.
Exception in thread "main" java.lang.NoClassDefFoundError: software/amazon/awssdk/services/secretsmanager/SecretsManagerClient at com.amazonaws.secretsmanager.util.JDBCSecretCacheBuilderProvider.build(JDBCSecretCacheBuilderProvider.java:58) at com.amazonaws.secretsmanager.sql.AWSSecretsManagerDriver.<init>(AWSSecretsManagerDriver.java:126) at com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver.<init>(AWSSecretsManagerPostgreSQLDriver.java:63) at com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver.<clinit>(AWSSecretsManagerPostgreSQLDriver.java:55) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:421) at java.base/java.lang.Class.forName(Class.java:412) at DatabaseConnection.main(DatabaseConnection.java:9) Caused by: java.lang.ClassNotFoundException: software.amazon.awssdk.services.secretsmanager.SecretsManagerClient at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526) ... 8 more
Can you tell me how to deal with this?
answered 2 years ago
Relevant content
- AWS OFFICIALUpdated 7 months ago
