fix(rds): accept the master usernames each engine really allows (#3730)

The 16 character cap was applied to every engine, but RDS accepts 63 on
PostgreSQL and 32 on MySQL, so an instance whose master username is longer
could not be created at all. The bound now comes from DatabaseEngine, using
the engine createDbInstance already resolves; the character rule and the
error message are unchanged.

Closes #3729
This commit is contained in:
Michal Schreiber authored and GitHub committed 2026-09-16 09:39:37 -04:00
1 parent f99e507916
commit f48bc2aa40
4 files changed
+74 -2

No files matched your search

@@ -404,3 +404,31 @@ teardown() {
assert_failure
assert_output --partial 'InvalidParameterCombination'
}
@test "rds: a master username longer than 16 characters is accepted on postgres, as on AWS" {
run aws_cmd rds create-db-instance \
--db-instance-identifier "$DB_ID" \
--engine postgres \
--db-instance-class db.t3.micro \
--allocated-storage 10 \
--master-username application_db_admin_user \
--master-user-password "secret99password"
assert_success
run aws_cmd rds describe-db-instances --db-instance-identifier "$DB_ID" \
--query 'DBInstances[0].MasterUsername'
assert_success
assert_output --partial 'application_db_admin_user'
# 64 characters is one past what PostgreSQL accepts on a live account
long_username="a$(printf 'b%.0s' $(seq 63))"
run aws_cmd rds create-db-instance \
--db-instance-identifier "$DB_ID_2" \
--engine postgres \
--db-instance-class db.t3.micro \
--allocated-storage 10 \
--master-username "$long_username" \
--master-user-password "secret99password"
assert_failure
assert_output --partial 'InvalidParameterValue'
}
@@ -611,7 +611,8 @@ public class RdsService implements Resettable, ResourceProvider {
int proxyPort = allocateProxyPort();
if (masterUsername == null || masterUsername.isBlank()) {
masterUsername = "root";
} else if (masterUsername.length() > 16 || !masterUsername.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) {
} else if (masterUsername.length() > engine.maxMasterUsernameLength()
|| !masterUsername.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) {
throw new AwsException("InvalidParameterValue",
"MasterUsername must begin with a letter and contain only alphanumeric characters or underscores.", 400);
}
@@ -12,4 +12,14 @@ public enum DatabaseEngine {
case MYSQL, MARIADB -> 3306;
};
}
// The API reference documents 16 characters for every engine, but RDS accepts longer
// master usernames on PostgreSQL and MySQL. These are the limits the service enforces.
public int maxMasterUsernameLength() {
return switch (this) {
case POSTGRES -> 63;
case MYSQL -> 32;
case MARIADB -> 16;
};
}
}
@@ -2760,11 +2760,44 @@ class RdsServiceTest {
AwsException ex4 = assertThrows(AwsException.class, () ->
rdsService.createDbInstance("mydb4", "postgres", "13",
"a1234567890123456", "password", "dbname", "db.t3.micro",
"a" + "b".repeat(63), "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false));
assertEquals("InvalidParameterValue", ex4.getErrorCode());
}
@Test
void createDbInstanceAcceptsTheLongestMasterUsernameEachEngineAllows() {
DbInstance postgres = rdsService.createDbInstance("pg63", "postgres", "13",
"a" + "b".repeat(62), "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false);
assertEquals(63, postgres.getMasterUsername().length());
DbInstance mysql = rdsService.createDbInstance("my32", "mysql", "8.0",
"a" + "b".repeat(31), "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false);
assertEquals(32, mysql.getMasterUsername().length());
DbInstance mariadb = rdsService.createDbInstance("mar16", "mariadb", "11.4",
"a" + "b".repeat(15), "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false);
assertEquals(16, mariadb.getMasterUsername().length());
}
@Test
void createDbInstanceRejectsMasterUsernameLongerThanTheEngineAllows() {
AwsException mysql = assertThrows(AwsException.class, () ->
rdsService.createDbInstance("my33", "mysql", "8.0",
"a" + "b".repeat(32), "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false));
assertEquals("InvalidParameterValue", mysql.getErrorCode());
AwsException mariadb = assertThrows(AwsException.class, () ->
rdsService.createDbInstance("mar17", "mariadb", "11.4",
"a" + "b".repeat(16), "password", "dbname", "db.t3.micro",
20, false, null, null, null, null, false));
assertEquals("InvalidParameterValue", mariadb.getErrorCode());
}
@Test
void createDbSnapshotThrowsForNonPostgres() {
rdsService.createDbInstance("mydb", "mysql", "8.0",