diff --git a/wrapper/src/main/java/software/amazon/jdbc/plugin/iam/IamAuthConnectionPlugin.java b/wrapper/src/main/java/software/amazon/jdbc/plugin/iam/IamAuthConnectionPlugin.java index 4b0e75654..c6d95bbad 100644 --- a/wrapper/src/main/java/software/amazon/jdbc/plugin/iam/IamAuthConnectionPlugin.java +++ b/wrapper/src/main/java/software/amazon/jdbc/plugin/iam/IamAuthConnectionPlugin.java @@ -258,6 +258,13 @@ String generateAuthenticationToken( try { final String user = PropertyDefinition.USER.getString(props); + if (StringUtils.isNullOrEmpty(user)) { + throw new RuntimeException( + Messages.get( + "IamAuthConnectionPlugin.missingRequiredConfigParameter", + new Object[] {PropertyDefinition.USER.name})); + } + final AwsCredentialsProvider credentialsProvider = AwsCredentialsManager.getProvider(originalHostSpec, props); return this.iamTokenUtility.generateAuthenticationToken(credentialsProvider, region, hostname, port, user); diff --git a/wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.properties b/wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.properties index ba8e4a0b3..5be0bbc04 100644 --- a/wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.properties +++ b/wrapper/src/main/resources/aws_advanced_jdbc_wrapper_messages.properties @@ -192,6 +192,7 @@ IamAuthConnectionPlugin.generatedNewIamToken=Generated new IAM token = ''{0}'' IamAuthConnectionPlugin.unhandledException=Unhandled exception: ''{0}'' IamAuthConnectionPlugin.connectException=Error occurred while opening a connection: ''{0}'' IamAuthConnectionPlugin.javaSdkNotInClasspath=Required dependency 'AWS IAM Authentication Plugin' is not on the classpath. +IamAuthConnectionPlugin.missingRequiredConfigParameter=Configuration parameter ''{0}'' is required. # Log Query Connection Plugin LogQueryConnectionPlugin.executingQuery=[{0}] Executing query: {1} diff --git a/wrapper/src/test/java/integration/container/tests/AwsIamIntegrationTest.java b/wrapper/src/test/java/integration/container/tests/AwsIamIntegrationTest.java index 0997980f9..a1975fc64 100644 --- a/wrapper/src/test/java/integration/container/tests/AwsIamIntegrationTest.java +++ b/wrapper/src/test/java/integration/container/tests/AwsIamIntegrationTest.java @@ -16,6 +16,7 @@ package integration.container.tests; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import integration.DriverHelper; @@ -40,9 +41,18 @@ import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.TestTemplate; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mockito; +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.jdbc.HostSpec; +import software.amazon.jdbc.HostSpecBuilder; import software.amazon.jdbc.PropertyDefinition; +import software.amazon.jdbc.authentication.AwsCredentialsManager; import software.amazon.jdbc.ds.AwsWrapperDataSource; +import software.amazon.jdbc.hostavailability.HostAvailabilityStrategy; import software.amazon.jdbc.plugin.iam.IamAuthConnectionPlugin; +import software.amazon.jdbc.plugin.iam.LightRdsUtility; +import software.amazon.jdbc.plugin.iam.RegularRdsUtility; @TestMethodOrder(MethodOrderer.MethodName.class) @ExtendWith(TestDriverProvider.class) @@ -236,6 +246,37 @@ void test_AwsIam_UserAndPasswordPropertiesArePreserved() throws SQLException { } } + @TestTemplate + void test_TokenGenerators() { + + final HostAvailabilityStrategy mockHostAvailabilityStrategy = Mockito.mock(HostAvailabilityStrategy.class); + + final Properties awsIamProp = + initAwsIamProps(TestEnvironment.getCurrent().getInfo().getIamUsername(), ""); + + final HostSpec hostSpec = new HostSpecBuilder(mockHostAvailabilityStrategy) + .host(TestEnvironment.getCurrent().getInfo().getDatabaseInfo().getClusterEndpoint()) + .build(); + + final AwsCredentialsProvider credentialsProvider = AwsCredentialsManager.getProvider(hostSpec, awsIamProp); + + final String regularToken = new RegularRdsUtility().generateAuthenticationToken( + credentialsProvider, + Region.of(TestEnvironment.getCurrent().getInfo().getAuroraRegion()), + TestEnvironment.getCurrent().getInfo().getDatabaseInfo().getClusterEndpoint(), + TestEnvironment.getCurrent().getInfo().getDatabaseInfo().getClusterReadOnlyEndpointPort(), + TestEnvironment.getCurrent().getInfo().getIamUsername()); + + final String lightToken = new LightRdsUtility().generateAuthenticationToken( + credentialsProvider, + Region.of(TestEnvironment.getCurrent().getInfo().getAuroraRegion()), + TestEnvironment.getCurrent().getInfo().getDatabaseInfo().getClusterEndpoint(), + TestEnvironment.getCurrent().getInfo().getDatabaseInfo().getClusterReadOnlyEndpointPort(), + TestEnvironment.getCurrent().getInfo().getIamUsername()); + + assertEquals(regularToken, lightToken); + } + protected Properties initAwsIamProps(String user, String password) { final Properties props = ConnectionStringHelper.getDefaultProperties(); props.setProperty(PropertyDefinition.PLUGINS.name, "iam");