mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(dynamodb): check backup ARNs as AWS does (#6291)
* fix(dynamodb): check the account of a backup ARN before the rest of it * fix(dynamodb): check the table name and partition of a backup ARN * fix(dynamodb): check the ListBackups ExclusiveStartBackupArn as AWS does
This commit is contained in:
1 parent
1f25fd88cf
commit
9790b5fe46
4 files changed
+177
-21
No files matched your search
+29
-16
@@ -14,39 +14,52 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
final class DynamoDbBackupArns {
|
||||
|
||||
private static final Pattern RESOURCE = Pattern.compile("table/([a-zA-Z0-9_.\\-]+)/backup/([^/]{3,})");
|
||||
private static final Pattern RESOURCE = Pattern.compile("table/([a-zA-Z0-9_.\\-]{3,255})/backup/([^/]{3,})");
|
||||
private static final Pattern BACKUP_ID = Pattern.compile("\\d{14}-[0-9a-fA-F]{8}");
|
||||
|
||||
private DynamoDbBackupArns() {}
|
||||
|
||||
record BackupArn(String arn, String region, String accountId, String tableName, long creationMillis) {}
|
||||
record BackupArn(String arn, String partition, String service, String region, String accountId,
|
||||
String tableName, String backupId) {
|
||||
|
||||
/** AWS answers "Invalid Request: BackupArn is not valid" when this fails. */
|
||||
boolean isValid() {
|
||||
return "dynamodb".equals(service) && AwsRegions.isRegionId(region)
|
||||
&& AwsRegions.partitionFor(region).equals(partition) && !accountId.isEmpty()
|
||||
&& BACKUP_ID.matcher(backupId).matches();
|
||||
}
|
||||
|
||||
long creationMillis() {
|
||||
return Long.parseLong(backupId.substring(0, 14));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS rejects an ARN it cannot split into a table and a backup id with "Invalid Backup ARN", and
|
||||
* one that splits but names another service, an unknown region or a malformed id with
|
||||
* "Invalid Request: BackupArn is not valid".
|
||||
* AWS answers "Invalid Backup ARN" when it cannot split the input into the ARN fields. It checks
|
||||
* the account right after this, before the rest of the ARN.
|
||||
*/
|
||||
static BackupArn parse(String input) {
|
||||
if (input == null) {
|
||||
throw invalidBackupArn();
|
||||
}
|
||||
static AwsArnUtils.Arn split(String input) {
|
||||
AwsArnUtils.Arn arn;
|
||||
try {
|
||||
arn = AwsArnUtils.parse(input);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw invalidBackupArn();
|
||||
}
|
||||
if (arn.partition().isEmpty() || arn.service().isEmpty() || arn.resource().isEmpty()) {
|
||||
throw invalidBackupArn();
|
||||
}
|
||||
return arn;
|
||||
}
|
||||
|
||||
/** AWS answers "Invalid Backup ARN" when the resource is not a table and a backup id. */
|
||||
static BackupArn parse(String input) {
|
||||
AwsArnUtils.Arn arn = split(input);
|
||||
Matcher resource = RESOURCE.matcher(arn.resource());
|
||||
if (!resource.matches()) {
|
||||
throw invalidBackupArn();
|
||||
}
|
||||
String backupId = resource.group(2);
|
||||
if (!"dynamodb".equals(arn.service()) || !AwsRegions.isRegionId(arn.region())
|
||||
|| !BACKUP_ID.matcher(backupId).matches()) {
|
||||
throw new AwsException("ValidationException", "Invalid Request: BackupArn is not valid", 400);
|
||||
}
|
||||
long creationMillis = Long.parseLong(backupId.substring(0, 14));
|
||||
return new BackupArn(input, arn.region(), arn.accountId(), resource.group(1), creationMillis);
|
||||
return new BackupArn(input, arn.partition(), arn.service(), arn.region(), arn.accountId(),
|
||||
resource.group(1), resource.group(2));
|
||||
}
|
||||
|
||||
static String mint(String tableArn, Instant creationDateTime) {
|
||||
|
||||
@@ -5130,11 +5130,18 @@ public class DynamoDbService {
|
||||
return backup;
|
||||
}
|
||||
|
||||
public TableBackup describeBackup(String backupArn, String region) {
|
||||
DynamoDbBackupArns.BackupArn arn = DynamoDbBackupArns.parse(backupArn);
|
||||
if (!arn.accountId().equals(regionResolver.getAccountId())) {
|
||||
/** AWS checks the account of a backup ARN before the rest of it. */
|
||||
DynamoDbBackupArns.BackupArn requireOwnBackupArn(String backupArn) {
|
||||
if (!DynamoDbBackupArns.split(backupArn).accountId().equals(regionResolver.getAccountId())) {
|
||||
throw new AwsException("AccessDeniedException", "Access is denied", 400);
|
||||
}
|
||||
return DynamoDbBackupArns.parse(backupArn);
|
||||
}
|
||||
|
||||
public TableBackup describeBackup(String backupArn, String region) {
|
||||
if (!requireOwnBackupArn(backupArn).isValid()) {
|
||||
throw invalidBackupArnValue();
|
||||
}
|
||||
return backupStore.get(backupArn)
|
||||
.filter(backup -> backup.getRegion().equals(region))
|
||||
.filter(this::isUnexpired)
|
||||
@@ -5161,7 +5168,7 @@ public class DynamoDbService {
|
||||
public ListBackupsResult listBackups(String tableName, Integer limit, Instant lowerBound, Instant upperBound,
|
||||
String exclusiveStartBackupArn, String backupType, String region) {
|
||||
DynamoDbBackupArns.BackupArn start = exclusiveStartBackupArn != null
|
||||
? DynamoDbBackupArns.parse(exclusiveStartBackupArn) : null;
|
||||
? exclusiveStart(exclusiveStartBackupArn, tableName, region) : null;
|
||||
Set<String> types = "ALL".equals(backupType) ? Set.of("USER", "SYSTEM") : Set.of(backupType);
|
||||
List<TableBackup> matching = backupStore.scan(key -> true).stream()
|
||||
.filter(backup -> backup.getRegion().equals(region))
|
||||
@@ -5224,6 +5231,33 @@ public class DynamoDbService {
|
||||
return backup.getExpiryDateTime() == null || clock.instant().isBefore(backup.getExpiryDateTime());
|
||||
}
|
||||
|
||||
private DynamoDbBackupArns.BackupArn exclusiveStart(String exclusiveStartBackupArn, String tableName,
|
||||
String region) {
|
||||
DynamoDbBackupArns.BackupArn start = DynamoDbBackupArns.parse(exclusiveStartBackupArn);
|
||||
if (!start.isValid()) {
|
||||
throw invalidExclusiveStart("is not a valid backup arn");
|
||||
}
|
||||
if (!start.region().equals(region)) {
|
||||
throw invalidExclusiveStart("does not match the region");
|
||||
}
|
||||
if (!start.accountId().equals(regionResolver.getAccountId())) {
|
||||
throw invalidExclusiveStart("does not match the account");
|
||||
}
|
||||
if (tableName != null && !tableName.equals(start.tableName())) {
|
||||
throw invalidExclusiveStart("does not match the table name predicate");
|
||||
}
|
||||
return start;
|
||||
}
|
||||
|
||||
private static AwsException invalidExclusiveStart(String problem) {
|
||||
return new AwsException("ValidationException",
|
||||
"Invalid Request: The supplied ExclusiveStartBackupArn " + problem, 400);
|
||||
}
|
||||
|
||||
private static AwsException invalidBackupArnValue() {
|
||||
return new AwsException("ValidationException", "Invalid Request: BackupArn is not valid", 400);
|
||||
}
|
||||
|
||||
private static boolean isAfter(TableBackup backup, DynamoDbBackupArns.BackupArn start) {
|
||||
int byTime = Long.compare(backup.getCreationDateTime().toEpochMilli(), start.creationMillis());
|
||||
return 0 < byTime || (byTime == 0 && 0 < backup.getBackupArn().compareTo(start.arn()));
|
||||
|
||||
+4
-1
@@ -1983,6 +1983,9 @@ public class NativeDynamoDbJsonHandler {
|
||||
+ "Member must satisfy enum value set: " + BACKUP_TYPE_FILTERS);
|
||||
}
|
||||
throwValidationErrors(violations);
|
||||
if (exclusiveStartBackupArn != null) {
|
||||
DynamoDbBackupArns.parse(exclusiveStartBackupArn);
|
||||
}
|
||||
Instant lowerBound = timestampOrNull(request, "TimeRangeLowerBound");
|
||||
Instant upperBound = timestampOrNull(request, "TimeRangeUpperBound");
|
||||
if (lowerBound != null && upperBound != null && upperBound.isBefore(lowerBound)) {
|
||||
@@ -2012,7 +2015,7 @@ public class NativeDynamoDbJsonHandler {
|
||||
*/
|
||||
private Response handleRestoreTableFromBackup(JsonNode request, String region) {
|
||||
String backupArn = textOrNull(request, "BackupArn");
|
||||
DynamoDbBackupArns.parse(backupArn);
|
||||
dynamoDbService.requireOwnBackupArn(backupArn);
|
||||
String targetTableName = requireTableNameParameter(request, "TargetTableName");
|
||||
throwValidationErrors(restoreViolations(request, targetTableName));
|
||||
|
||||
|
||||
+106
@@ -153,6 +153,55 @@ class DynamoDbBackupIntegrationTest {
|
||||
.body("message", equalTo("Invalid Backup ARN"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void backupArnTableNameAndPartitionAreChecked() {
|
||||
String backup = "/backup/01700000000000-abcdef12";
|
||||
for (String table : List.of("tt", "t".repeat(256))) {
|
||||
ddb("DescribeBackup", "{\"BackupArn\": \"" + TABLE_ARN_PREFIX + table + backup + "\"}")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("Invalid Backup ARN"));
|
||||
}
|
||||
for (String arn : List.of("arn:aws-cn:dynamodb:us-east-1:000000000000:table/BackupIT-none" + backup,
|
||||
"arn:aws:dynamodb:cn-north-1:000000000000:table/BackupIT-none" + backup)) {
|
||||
ddb("DescribeBackup", "{\"BackupArn\": \"" + arn + "\"}")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("Invalid Request: BackupArn is not valid"));
|
||||
}
|
||||
for (String arn : List.of(TABLE_ARN_PREFIX + "ttt" + backup, TABLE_ARN_PREFIX + "t".repeat(255) + backup,
|
||||
"arn:aws-cn:dynamodb:cn-north-1:000000000000:table/BackupIT-none" + backup)) {
|
||||
ddb("DescribeBackup", "{\"BackupArn\": \"" + arn + "\"}")
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("BackupNotFoundException"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void backupArnOfAnotherAccountIsDeniedOnceItSplitsIntoFields() {
|
||||
String id = "01700000000000-abcdef12";
|
||||
String other = "arn:aws:dynamodb:us-east-1:123456789012:";
|
||||
for (String arn : List.of(other + "table/BackupIT-none/backup/xy", other + "table/BackupIT-none",
|
||||
"arn:aws:s3:nowhere-1:123456789012:table/BackupIT-none/backup/1700000000000-abcdef12",
|
||||
"arn:aws:dynamodb:us-east-1::table/BackupIT-none/backup/" + id)) {
|
||||
for (String action : List.of("DescribeBackup", "DeleteBackup")) {
|
||||
ddb(action, "{\"BackupArn\": \"" + arn + "\"}")
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("AccessDeniedException"))
|
||||
.body("message", equalTo("Access is denied"));
|
||||
}
|
||||
ddb("RestoreTableFromBackup", "{\"BackupArn\": \"" + arn + "\", \"TargetTableName\": \"a b\"}")
|
||||
.statusCode(400)
|
||||
.body("__type", containsString("AccessDeniedException"));
|
||||
}
|
||||
for (String arn : List.of("arn:aws:dynamodb:us-east-1:123456789012", other,
|
||||
"arn::dynamodb:us-east-1:123456789012:table/BackupIT-none/backup/" + id,
|
||||
"arn:aws::us-east-1:123456789012:table/BackupIT-none/backup/" + id,
|
||||
"ARN:aws:dynamodb:us-east-1:123456789012:table/BackupIT-none/backup/" + id)) {
|
||||
ddb("DescribeBackup", "{\"BackupArn\": \"" + arn + "\"}")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("Invalid Backup ARN"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownBackupIsBackupNotFound() {
|
||||
String arn = TABLE_ARN_PREFIX + "BackupIT-none/backup/01700000000000-ABCDEF12";
|
||||
@@ -296,6 +345,46 @@ class DynamoDbBackupIntegrationTest {
|
||||
+ "Time range lower bound must be less than or equal to upper bound"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listBackupsChecksTheExclusiveStartBackupArn() {
|
||||
String table = "BackupIT-start";
|
||||
String backup = "/backup/01700000000000-abcdef12";
|
||||
String start = TABLE_ARN_PREFIX + table + backup;
|
||||
String supplied = "Invalid Request: The supplied ExclusiveStartBackupArn ";
|
||||
|
||||
listBackupsFrom(table, TABLE_ARN_PREFIX + "tt" + backup)
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("Invalid Backup ARN"));
|
||||
listBackupsFrom(table, TABLE_ARN_PREFIX + table + "/backup/1700000000000-abcdef12")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(supplied + "is not a valid backup arn"));
|
||||
listBackupsFrom(table, start.replace("000000000000", ""))
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(supplied + "is not a valid backup arn"));
|
||||
listBackupsFrom(table, start.replace("us-east-1:000000000000", "eu-west-2:123456789012"))
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(supplied + "does not match the region"));
|
||||
listBackupsFrom(table, start.replace("000000000000", "123456789012"))
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(supplied + "does not match the account"));
|
||||
listBackupsFrom(table, TABLE_ARN_PREFIX + "BackupIT-other" + backup)
|
||||
.statusCode(400)
|
||||
.body("message", equalTo(supplied + "does not match the table name predicate"));
|
||||
ddb("ListBackups", "{\"ExclusiveStartBackupArn\": \"" + TABLE_ARN_PREFIX + "BackupIT-other" + backup + "\"}")
|
||||
.statusCode(200);
|
||||
|
||||
String reversedRange = "\"TimeRangeLowerBound\": 2, \"TimeRangeUpperBound\": 1";
|
||||
ddb("ListBackups", "{\"ExclusiveStartBackupArn\": \"" + TABLE_ARN_PREFIX + "tt" + backup + "\", "
|
||||
+ reversedRange + "}")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("Invalid Backup ARN"));
|
||||
ddb("ListBackups", "{\"ExclusiveStartBackupArn\": \"" + start.replace("000000000000", "123456789012")
|
||||
+ "\", " + reversedRange + "}")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("One or more parameter values were invalid: "
|
||||
+ "Time range lower bound must be less than or equal to upper bound"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listBackupsReportsEveryViolation() {
|
||||
ddb("ListBackups", "{\"Limit\": 0, \"BackupType\": \"BAD\"}")
|
||||
@@ -463,6 +552,17 @@ class DynamoDbBackupIntegrationTest {
|
||||
+ "constraint: Member must satisfy regular expression pattern: [a-zA-Z0-9_.-]+"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreTableFromBackupChecksTheBackupIdAfterTheRequest() {
|
||||
String arn = TABLE_ARN_PREFIX + "BackupIT-none/backup/1700000000000-abcdef12";
|
||||
ddb("RestoreTableFromBackup", "{\"BackupArn\": \"" + arn + "\"}")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("The parameter 'TableName' is required but was not present in the request"));
|
||||
ddb("RestoreTableFromBackup", "{\"BackupArn\": \"" + arn + "\", \"TargetTableName\": \"BackupIT-never\"}")
|
||||
.statusCode(400)
|
||||
.body("message", equalTo("Invalid Request: BackupArn is not valid"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreReportsEveryViolationBeforeTheBackupLookup() {
|
||||
String arn = TABLE_ARN_PREFIX + "BackupIT-none/backup/01700000000000-abcdef12";
|
||||
@@ -804,4 +904,10 @@ class DynamoDbBackupIntegrationTest {
|
||||
{"TimeRangeLowerBound": %s, "TimeRangeUpperBound": %s}
|
||||
""".formatted(lowerBound, upperBound));
|
||||
}
|
||||
|
||||
private static ValidatableResponse listBackupsFrom(String tableName, String exclusiveStartBackupArn) {
|
||||
return ddb("ListBackups", """
|
||||
{"TableName": "%s", "ExclusiveStartBackupArn": "%s"}
|
||||
""".formatted(tableName, exclusiveStartBackupArn));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user