mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -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));
|
||||
|
||||
|
||||
Reference in new issue
Block a user