mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
refactor: read and check ARNs through AwsArnUtils in DataSync, DynamoDB, Glue, Lake Formation, Redshift and S3 Tables (#6903)
Ten startsWith("arn:") probes on name-or-ARN parameters become AwsArnUtils.looksLikeArn. DataSync's private arnRegion and arnAccountId copies are deleted in favour of AwsArnUtils.regionOrDefault and accountOrDefault, and its resource, Glue's resource and Redshift's integration source type are read with AwsArnUtils.parse instead of a split.
Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
d68876edcc
commit
d7204696d4
10 files changed
+20
-45
No files matched your search
@@ -363,8 +363,8 @@ public class DataSyncService {
|
||||
String svmArn = configuration.path("StorageVirtualMachineArn").asText("");
|
||||
String fileSystemId = fsxFileSystemId(svmArn);
|
||||
if (!fileSystemId.isEmpty()) {
|
||||
configuration.put("FsxFilesystemArn", AwsArnUtils.Arn.of("fsx", arnRegion(svmArn, region),
|
||||
arnAccountId(svmArn), "file-system/" + fileSystemId).toString());
|
||||
configuration.put("FsxFilesystemArn", AwsArnUtils.Arn.of("fsx", AwsArnUtils.regionOrDefault(svmArn, region),
|
||||
AwsArnUtils.accountOrDefault(svmArn, regionResolver.getAccountId()), "file-system/" + fileSystemId).toString());
|
||||
}
|
||||
}
|
||||
case HDFS -> {
|
||||
@@ -431,7 +431,7 @@ public class DataSyncService {
|
||||
fileSystemUri(type, configuration.path("FsxFilesystemArn").asText(""), region, configuration);
|
||||
case FSX_ONTAP -> {
|
||||
String svmArn = configuration.path("StorageVirtualMachineArn").asText("");
|
||||
yield "fsxn://" + arnRegion(svmArn, region) + "." + fsxFileSystemId(svmArn) + "."
|
||||
yield "fsxn://" + AwsArnUtils.regionOrDefault(svmArn, region) + "." + fsxFileSystemId(svmArn) + "."
|
||||
+ fsxStorageVirtualMachineId(svmArn) + subdirectory(configuration);
|
||||
}
|
||||
case HDFS -> {
|
||||
@@ -459,7 +459,7 @@ public class DataSyncService {
|
||||
if (slash >= 0) {
|
||||
fileSystemId = fileSystemId.substring(slash + 1);
|
||||
}
|
||||
return type.uriScheme() + "://" + arnRegion(fileSystemArn, region) + "." + fileSystemId
|
||||
return type.uriScheme() + "://" + AwsArnUtils.regionOrDefault(fileSystemArn, region) + "." + fileSystemId
|
||||
+ subdirectory(configuration);
|
||||
}
|
||||
|
||||
@@ -497,19 +497,8 @@ public class DataSyncService {
|
||||
return segments.length > 2 ? segments[2] : "";
|
||||
}
|
||||
|
||||
private static String arnRegion(String arn, String fallback) {
|
||||
String[] segments = arn.split(":", 6);
|
||||
return segments.length > 3 && !segments[3].isEmpty() ? segments[3] : fallback;
|
||||
}
|
||||
|
||||
private String arnAccountId(String arn) {
|
||||
String[] segments = arn.split(":", 6);
|
||||
return segments.length > 4 && !segments[4].isEmpty() ? segments[4] : regionResolver.getAccountId();
|
||||
}
|
||||
|
||||
private static String arnResource(String arn) {
|
||||
String[] segments = arn.split(":", 6);
|
||||
return segments.length > 5 ? segments[5] : "";
|
||||
return AwsArnUtils.isArn(arn) ? AwsArnUtils.parse(arn).resource() : "";
|
||||
}
|
||||
|
||||
// ---- Filters ----
|
||||
|
||||
@@ -3013,7 +3013,7 @@ public class DynamoDbService {
|
||||
*/
|
||||
public DynamoDbTableNames.ResolvedTableRef pointInTimeSourceArn(String sourceTableArn, String region,
|
||||
boolean sseOverride) {
|
||||
if (!sourceTableArn.startsWith("arn:")) {
|
||||
if (!AwsArnUtils.looksLikeArn(sourceTableArn)) {
|
||||
throw invalidParameter("ARNs must start with 'arn:': " + sourceTableArn);
|
||||
}
|
||||
AwsArnUtils.Arn arn;
|
||||
|
||||
@@ -36,7 +36,7 @@ public final class DynamoDbTableNames {
|
||||
throw SmithyValidationErrors.validationException(
|
||||
SmithyValidationErrors.lengthOutOfRange("tableName", input, 3, 255));
|
||||
}
|
||||
if (input.startsWith("arn:")) {
|
||||
if (AwsArnUtils.looksLikeArn(input)) {
|
||||
throw invalid("TableName must be a short name, not an ARN: " + input);
|
||||
}
|
||||
validateTableName(input);
|
||||
@@ -60,7 +60,7 @@ public final class DynamoDbTableNames {
|
||||
throw SmithyValidationErrors.validationException(
|
||||
SmithyValidationErrors.lengthOutOfRange("tableName", input, 1, 255));
|
||||
}
|
||||
if (input.startsWith("arn:")) {
|
||||
if (AwsArnUtils.looksLikeArn(input)) {
|
||||
return parseArn(input);
|
||||
}
|
||||
validateTableName(input);
|
||||
|
||||
+2
-2
@@ -239,7 +239,7 @@ public class DynamoDbLocalBackend implements DynamoDbBackend {
|
||||
}
|
||||
|
||||
static String tableName(Scope scope, String value) {
|
||||
if (!value.startsWith("arn:")) {
|
||||
if (!AwsArnUtils.looksLikeArn(value)) {
|
||||
return value;
|
||||
}
|
||||
DynamoDbTableNames.resolveWithRegion(value, scope.region());
|
||||
@@ -528,7 +528,7 @@ public class DynamoDbLocalBackend implements DynamoDbBackend {
|
||||
}
|
||||
|
||||
private String existingTableKey(Scope scope, String resourceArn) {
|
||||
if (resourceArn == null || !resourceArn.startsWith("arn:")) {
|
||||
if (!AwsArnUtils.looksLikeArn(resourceArn)) {
|
||||
throw new AwsException("ValidationException", "Invalid TableArn", 400);
|
||||
}
|
||||
String name = ownTableName(scope, resourceArn);
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.Base64Url;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
@@ -2967,16 +2968,15 @@ public class GlueService {
|
||||
}
|
||||
|
||||
private void validateArn(String arn) {
|
||||
if (arn == null || !arn.startsWith("arn:")) {
|
||||
if (!AwsArnUtils.looksLikeArn(arn)) {
|
||||
throw new AwsException("InvalidInputException", "Invalid ARN", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateResourceExists(String arn) {
|
||||
// At most six parts: a job, table or other name may itself contain ':'.
|
||||
String[] parts = arn.split(":", 6);
|
||||
if (parts.length >= 6) {
|
||||
String resource = parts[5];
|
||||
if (AwsArnUtils.isArn(arn)) {
|
||||
String resource = AwsArnUtils.parse(arn).resource();
|
||||
if (resource.startsWith("job/")) {
|
||||
getJob(resource.substring(4));
|
||||
return;
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ public class LakeFormationService {
|
||||
if (request.getResourceArn() == null) {
|
||||
throw new AwsException("InvalidInputException", "ResourceArn is required", 400);
|
||||
}
|
||||
if (!request.getResourceArn().startsWith("arn:")) {
|
||||
if (!AwsArnUtils.looksLikeArn(request.getResourceArn())) {
|
||||
throw new AwsException("InvalidInputException", "ResourceArn must be a valid ARN", 400);
|
||||
}
|
||||
if (storage.describeResource(region, request.getResourceArn()).isPresent()) {
|
||||
|
||||
@@ -784,11 +784,7 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
}
|
||||
|
||||
private static String sourceType(String sourceArn) {
|
||||
if (sourceArn == null) {
|
||||
return null;
|
||||
}
|
||||
String[] parts = sourceArn.split(":");
|
||||
return parts.length > 2 ? parts[2] : null;
|
||||
return AwsArnUtils.isArn(sourceArn) ? AwsArnUtils.parse(sourceArn).service() : null;
|
||||
}
|
||||
|
||||
private static int resolveMaxRecords(Integer maxRecords) {
|
||||
@@ -2746,7 +2742,7 @@ public class RedshiftService implements ManagedSecretRotator {
|
||||
if (resourceName == null || resourceName.isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "ResourceName is required.", 400);
|
||||
}
|
||||
if (!resourceName.startsWith("arn:")) {
|
||||
if (!AwsArnUtils.looksLikeArn(resourceName)) {
|
||||
throw new AwsException("InvalidParameterValue", "ResourceName must be a Redshift ARN: " + resourceName, 400);
|
||||
}
|
||||
AwsArnUtils.Arn arn;
|
||||
|
||||
+2
-1
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.redshiftserverless;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
@@ -1029,7 +1030,7 @@ public class RedshiftServerlessService implements Resettable {
|
||||
if (nameOrArn == null || nameOrArn.isBlank()) {
|
||||
throw ServerlessSupport.validation("workgroupName is required.");
|
||||
}
|
||||
if (!nameOrArn.startsWith("arn:")) {
|
||||
if (!AwsArnUtils.looksLikeArn(nameOrArn)) {
|
||||
return getWorkgroup(nameOrArn, region);
|
||||
}
|
||||
return workgroupByArn(nameOrArn, region).orElseThrow(() -> workgroupNotFound(nameOrArn));
|
||||
|
||||
@@ -312,7 +312,7 @@ public class S3TablesService {
|
||||
// Guard and cut must use the same needle. Checking for ":" + prefix but cutting on prefix
|
||||
// alone means a name that itself contains the prefix is cut at the wrong place.
|
||||
String needle = ":" + prefix;
|
||||
if (arn == null || !arn.startsWith("arn:") || !arn.contains(needle)) {
|
||||
if (!AwsArnUtils.looksLikeArn(arn) || !arn.contains(needle)) {
|
||||
throw notFound("The table bucket does not exist.");
|
||||
}
|
||||
return arn.substring(arn.indexOf(needle) + needle.length());
|
||||
|
||||
@@ -22,18 +22,10 @@ arn-probe src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFron
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailService.java 1
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsCrossAccountService.java 1
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/connect/ConnectService.java 1
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java 1
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbTableNames.java 2
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/dynamodb/backend/DynamoDbLocalBackend.java 2
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2TargetResolver.java 1
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java 1
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java 8
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/iot/IotService.java 1
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/lakeformation/LakeFormationService.java 1
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java 1
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/redshiftserverless/RedshiftServerlessService.java 1
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/resourcegroups/ResourceGroupsService.java 2
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/s3tables/S3TablesService.java 1
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java 3
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/ses/SesTemplateService.java 1
|
||||
arn-probe src/main/java/io/github/hectorvent/floci/services/ssm/SsmService.java 1
|
||||
@@ -45,13 +37,10 @@ arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecont
|
||||
arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreMemoryService.java 1
|
||||
arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreTagHandler.java 1
|
||||
arn-split src/main/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsService.java 1
|
||||
arn-split src/main/java/io/github/hectorvent/floci/services/datasync/DataSyncService.java 3
|
||||
arn-split src/main/java/io/github/hectorvent/floci/services/ec2/Ec2InstanceCredentials.java 3
|
||||
arn-split src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java 1
|
||||
arn-split src/main/java/io/github/hectorvent/floci/services/iam/PublicAccessConditions.java 1
|
||||
arn-split src/main/java/io/github/hectorvent/floci/services/kms/KmsJsonHandler.java 1
|
||||
arn-split src/main/java/io/github/hectorvent/floci/services/ram/RamService.java 2
|
||||
arn-split src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java 1
|
||||
arn-split src/main/java/io/github/hectorvent/floci/services/resourcegroupstagging/ResourceGroupsTaggingService.java 1
|
||||
arn-split src/main/java/io/github/hectorvent/floci/services/sagemaker/SageMakerService.java 1
|
||||
arn-split src/main/java/io/github/hectorvent/floci/services/servicecatalog/ServiceCatalogService.java 1
|
||||
|
||||
|
Reference in new issue
Block a user