Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build-logic/build-parameters/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ buildParameters {
}
val projectName = "pgjdbc"
integer("jdkBuildVersion") {
defaultValue.set(17)
defaultValue.set(21)
mandatory.set(true)
description.set("JDK version to use for building $projectName. If the value is 0, then the current Java is used. (see https://docs.gradle.org/8.4/userguide/toolchains.html#sec:consuming)")
}
Expand Down
33 changes: 33 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ private Properties loadDefaultProperties() throws IOException {
props.setProperty(propName, propValue);
}
}

// Explicitly set password with IAM generated token
String password = generateIAMToken(
PGProperty.PG_HOST.getOrDefault(props),
PGProperty.PG_PORT.getOrDefault(props),
PGProperty.USER.getOrDefault(props), );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compiler doesn't complain about a trailing comma?

PGProperty.PASSWORD.set(props, password);

// parse URL and add more properties
if ((props = parseURL(url, props)) == null) {
throw new PSQLException(
Expand Down Expand Up @@ -324,6 +332,31 @@ private Properties loadDefaultProperties() throws IOException {
}
}

// **IAM Token Generation Method**

/**
* IAM Token Generation -- generates a token using local IAM credentials
* We want this to use the operating system here to create this token locally
*/
private String generateIAMToken(String host, int port, String username) throws SQLException {
try {
ProcessBuilder processBuilder = new ProcessBuilder(
"aws", "rds", "generate-db-auth-token",
"--hostname", host,
"--port", String.valueOf(port),
"--username", username,
"--region", "us-east-2"
);

Process process = processBuilder.start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
return reader.readLine().trim();
}
} catch (Exception e) {
throw new SQLException("Failed to generate IAM token", e);
}
}

/**
* this is an empty method left here for graalvm
* we removed the ability to setup the logger from properties
Expand Down