Code: Select all
package software.amazon;
import software.amazon.jdbc.PropertyDefinition;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class AwsIamAuthenticationPostgresqlExample {
public static final String POSTGRESQL_CONNECTION_STRING =
"jdbc:aws-wrapper:postgresql://db-identifier.XYZ.us-east-2.rds.amazonaws.com:5432/employees";
private static final String USERNAME = "john_smith";
public static void main(String[] args) throws SQLException {
final Properties properties = new Properties();
// Enable AWS IAM database authentication and configure driver property values
properties.setProperty(PropertyDefinition.PLUGINS.name, "iam");
properties.setProperty(PropertyDefinition.USER.name, USERNAME);
// Attempt a connection
try (Connection conn = DriverManager.getConnection(POSTGRESQL_CONNECTION_STRING, properties);
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery("select aurora_db_instance_identifier()")) {
System.out.println(Util.getResult(result));
}
}
}
Code: Select all
org.postgresql.util.PSQLExeption: FATAL: PAM authentication failed for user john_smith ...
Ich habe versucht, eine Verbindung über psql herzustellen< /code> CLI und es hat funktioniert:
Code: Select all
psql "host=$RDSHOST port=$RDSPORT sslmode=verify-ca sslrootcert=global-bundle.pem user=$RDSUSER dbname=$RDSDB"
Code: Select all
properties.setProperty("sslrootcert", "");
properties.setProperty("ssl", true);
properties.setProperty("sslmode", "verify-ca");
Dann habe ich mein global-bundle.pem in meine CA-Zertifikate importiert mit:
Code: Select all
keytool -importcert -alias aws-certs -trustcacerts -file /path/to/global-bundle.pem -storepass changeit -cacerts -noprompt
Code: Select all
org.postgresql.util.PSQLExeption: Could not open SSL root certificate file /home//.postgresql/root.crt