Skip to content

Commit 926d28f

Browse files
fix: IAM auth in CN RDS (#579) (#582)
1 parent 5264adb commit 926d28f

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

src/main/protocol-impl/java/com/mysql/cj/protocol/a/authentication/AwsIamAuthenticationTokenHelper.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
package com.mysql.cj.protocol.a.authentication;
3333

34+
import com.mysql.cj.jdbc.ha.util.RdsUtils;
3435
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
3536
import software.amazon.awssdk.regions.Region;
3637
import software.amazon.awssdk.services.rds.RdsUtilities;
@@ -50,7 +51,7 @@ public class AwsIamAuthenticationTokenHelper {
5051
private final String hostname;
5152
private final int port;
5253
private final Log log;
53-
private static final int REGION_MATCHER_GROUP = 3;
54+
private static final String REGION_MATCHER_GROUP = "region";
5455

5556
public AwsIamAuthenticationTokenHelper(final String hostname, final int port, final String logger) {
5657
this.log = LogFactory.getLogger(logger, Log.LOGGER_INSTANCE_NAME);
@@ -83,23 +84,25 @@ private String generateAuthenticationToken(final String user) {
8384

8485
private Region getRdsRegion() {
8586
// Check Hostname
86-
final Pattern auroraDnsPattern =
87-
Pattern.compile(
88-
"(.+)\\.(proxy-|cluster-|cluster-ro-|cluster-custom-)?[a-zA-Z0-9]+\\.([a-zA-Z0-9\\-]+)\\.rds\\.amazonaws\\.com",
89-
Pattern.CASE_INSENSITIVE);
90-
final Matcher matcher = auroraDnsPattern.matcher(hostname);
87+
Matcher matcher = RdsUtils.AURORA_DNS_PATTERN.matcher(hostname);
9188
if (!matcher.find()) {
92-
// Does not match Amazon's Hostname, throw exception
93-
final String exceptionMessage = Messages.getString(
94-
"AuthenticationAwsIamPlugin.UnsupportedHostname",
95-
new String[]{hostname});
96-
97-
log.logTrace(exceptionMessage);
98-
throw ExceptionFactory.createException(exceptionMessage);
89+
final Matcher chinaMatcher = RdsUtils.AURORA_CHINA_DNS_PATTERN.matcher(hostname);
90+
if (!chinaMatcher.find()) {
91+
// Does not match Amazon's Hostname, throw exception
92+
final String exceptionMessage = Messages.getString(
93+
"AuthenticationAwsIamPlugin.UnsupportedHostname",
94+
new String[]{hostname});
95+
96+
log.logTrace(exceptionMessage);
97+
throw ExceptionFactory.createException(exceptionMessage);
98+
}
99+
matcher = chinaMatcher;
99100
}
100101

101102
// Get Region
102-
final String rdsRegion = matcher.group(REGION_MATCHER_GROUP);
103+
final String rdsRegion = matcher.group(REGION_MATCHER_GROUP) == null
104+
? null
105+
: matcher.group(REGION_MATCHER_GROUP).replaceAll("rds", "").replaceAll("\\.", "");
103106

104107
// Check Region
105108
Optional<Region> regionOptional = Region.regions().stream()

src/main/resources/com/mysql/cj/LocalizedErrorMessages.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ AuthenticationProvider.BadDisabledAuthenticationPlugin=Can''t disable the defaul
6969
AuthenticationProvider.AuthenticationPluginRequiresSSL=SSL connection required for plugin "{0}". Check if ''sslMode'' is enabled.
7070
AuthenticationProvider.UnexpectedAuthenticationApproval=Unexpected authentication approval. Authentication plugin "{0}" did not report "done" state but server has approved the connection.
7171

72-
AuthenticationAwsIamPlugin.UnsupportedHostname=Unsupported AWS hostname ''{0}''. Amazon domain name in format *.AWS-Region.rds.amazonaws.com is expected
72+
AuthenticationAwsIamPlugin.UnsupportedHostname=Unsupported AWS hostname ''{0}''. Amazon domain name in format *.AWS-Region.rds.amazonaws.com or *.rds.AWS-Region.amazonaws.com.cn is expected
7373
AuthenticationAwsIamPlugin.UnsupportedRegion=Unsupported AWS region ''{0}''. For supported regions, please read https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html
7474
AuthenticationAwsIamPlugin.MissingSDK=Unable to connect using AWS IAM authentication due to missing AWS Java SDK For Amazon RDS. Add dependency to classpath.
7575

src/main/user-impl/java/com/mysql/cj/jdbc/ha/util/RdsUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class RdsUtils {
7777
// Instance Endpoint: <instance-name>.<xyz>.rds.<aws-region>.amazonaws.com.cn
7878
// Example: test-postgres-instance-1.123456789012.rds.cn-northwest-1.amazonaws.com.cn
7979

80-
private static final Pattern AURORA_DNS_PATTERN =
80+
public static final Pattern AURORA_DNS_PATTERN =
8181
Pattern.compile(
8282
"(?<instance>.+)\\."
8383
+ "(?<dns>proxy-|cluster-|cluster-ro-|cluster-custom-)?"
@@ -104,7 +104,7 @@ public class RdsUtils {
104104
+ "(?<domain>[a-zA-Z0-9]+\\.(?<region>[a-zA-Z0-9\\-]+)\\.rds\\.amazonaws\\.com(\\.cn)?)",
105105
Pattern.CASE_INSENSITIVE);
106106

107-
private static final Pattern AURORA_CHINA_DNS_PATTERN =
107+
public static final Pattern AURORA_CHINA_DNS_PATTERN =
108108
Pattern.compile(
109109
"(?<instance>.+)\\."
110110
+ "(?<dns>proxy-|cluster-|cluster-ro-|cluster-custom-)?"

src/test/java/testsuite/simple/AwsIamAuthenticationHelperTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ public void test_1_ValidHostAndRegion() {
5151
PORT,
5252
StandardLogger.class.getName()
5353
));
54+
55+
Assertions.assertNotNull(new AwsIamAuthenticationTokenHelper(
56+
"MyDBInstanceName.SomeServerName.us-east-1.rds.amazonaws.com.cn",
57+
PORT,
58+
StandardLogger.class.getName()
59+
));
60+
61+
Assertions.assertNotNull(new AwsIamAuthenticationTokenHelper(
62+
"test-postgres.cluster-123456789012.rds.cn-northwest-1.amazonaws.com.cn",
63+
PORT,
64+
StandardLogger.class.getName()
65+
));
5466
}
5567

5668

0 commit comments

Comments
 (0)