Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiyvamz committed Feb 1, 2024
1 parent 3577c46 commit de0707b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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(), "<anything>");

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");
Expand Down

0 comments on commit de0707b

Please sign in to comment.