From 63aa7710d0398a68ea2ab7b122eeec7b1be00603 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Wed, 28 Oct 2026 21:17:14 -0500 Subject: [PATCH] refactor(lambda): tell names from ARNs and read regions through AwsArnUtils (#6881) The startsWith("arn:") probes in Lambda, Lambda MicroVMs and the API Gateway v2 quick-create target become AwsArnUtils.looksLikeArn. The runtime version ARN and the MicroVM image region read the region with AwsArnUtils.regionOrDefault instead of a split. LambdaTagController keeps its split, marked: Lambda reads the fourth field of any tag target, ARN or not, which IamRestRouteEnforcementIntegrationTest pins. Signed-off-by: Hector Ventura --- .../floci/services/apigatewayv2/ApiGatewayV2Service.java | 2 +- .../hectorvent/floci/services/lambda/LambdaArnUtils.java | 4 ++-- .../floci/services/lambda/LambdaController.java | 6 +----- .../hectorvent/floci/services/lambda/LambdaService.java | 8 ++++---- .../floci/services/lambda/LambdaTagController.java | 2 +- .../services/lambdamicrovms/LambdaMicrovmsController.java | 3 +-- .../services/lambdamicrovms/LambdaMicrovmsService.java | 2 +- tools/partition/baseline.tsv | 7 ------- 8 files changed, 11 insertions(+), 23 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java index e72481416e..100270dc70 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java @@ -142,7 +142,7 @@ public class ApiGatewayV2Service { Object targetValue = request.get("target"); String target = targetValue != null ? String.valueOf(targetValue) : null; if ("HTTP".equals(protocolType) && target != null && !target.isBlank()) { - boolean isLambdaTarget = target.startsWith("arn:"); + boolean isLambdaTarget = AwsArnUtils.looksLikeArn(target); Integration integration = createIntegration(region, apiId, Map.of( "integrationType", isLambdaTarget ? "AWS_PROXY" : "HTTP_PROXY", "integrationUri", target, diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaArnUtils.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaArnUtils.java index b58af74e8c..42c1854846 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaArnUtils.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaArnUtils.java @@ -70,7 +70,7 @@ public final class LambdaArnUtils { */ public static String functionArn(String reference, String partition, String region, String defaultAccount) { ResolvedFunctionRef ref = resolve(reference); - if (reference.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(reference)) { return reference; } String resource = ref.qualifier() == null @@ -106,7 +106,7 @@ public final class LambdaArnUtils { "Member must have length less than or equal to " + MAX_TOTAL_LENGTH); } - if (input.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(input)) { return parseFullArn(input); } if (input.contains(":function:")) { diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java index cf36831ec1..8a66cfdedc 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java @@ -905,11 +905,7 @@ public class LambdaController { } private String runtimeVersionArn(LambdaFunction fn) { - String region = regionResolver.getDefaultRegion(); - String[] arnParts = fn.getFunctionArn() != null ? fn.getFunctionArn().split(":") : new String[0]; - if (arnParts.length > 3 && !arnParts[3].isBlank()) { - region = arnParts[3]; - } + String region = AwsArnUtils.regionOrDefault(fn.getFunctionArn(), regionResolver.getDefaultRegion()); return AwsArnUtils.Arn.of("lambda", region, "", "runtime:" + runtimeVersionId(fn.getRuntime())).toString(); } diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java index 65410ee006..b3c5adfba4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java @@ -661,7 +661,7 @@ public class LambdaService implements ResourceProvider { if (effective == null) { return getFunction(region, functionName); } - if (functionName.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(functionName)) { AwsArnUtils.Arn arn = AwsArnUtils.parse(functionName); return targetResolver.resolveReadTargetForAccount(arn.accountId(), region, ref.name(), effective); } @@ -3742,7 +3742,7 @@ public class LambdaService implements ResourceProvider { if (principal == null) { return null; } - if (principal.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(principal)) { return Map.of("AWS", principal); } if (ACCOUNT_ID_PATTERN.matcher(principal).matches()) { @@ -3966,7 +3966,7 @@ public class LambdaService implements ResourceProvider { * than the mapping's is reported as missing, as AWS does. */ private EventSourceMapping taggedEventSourceMapping(String resourceArn) { - if (resourceArn == null || !resourceArn.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(resourceArn)) { return null; } AwsArnUtils.Arn arn; @@ -3996,7 +3996,7 @@ public class LambdaService implements ResourceProvider { if (functionArn == null || functionArn.isBlank()) { throw new AwsException("InvalidParameterValueException", "Resource ARN is required", 400); } - if (!functionArn.startsWith("arn:")) { + if (!AwsArnUtils.looksLikeArn(functionArn)) { throw new AwsException("InvalidParameterValueException", "Resource ARN must be a full Lambda function ARN: " + functionArn, 400); } diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaTagController.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaTagController.java index 1bcf577537..58976594b5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaTagController.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaTagController.java @@ -50,7 +50,7 @@ public class LambdaTagController { * the ARN's resource type. The region comes from the ARN itself. */ private static String arnRegion(String arn) { - String[] parts = arn.split(":"); + String[] parts = arn.split(":"); // partition-literal: Lambda reads the fourth field of any tag target, ARN or not return parts.length > 3 ? parts[3] : ""; } diff --git a/src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsController.java b/src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsController.java index d8c23f5b28..9b6a44f129 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsController.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsController.java @@ -464,8 +464,7 @@ public class LambdaMicrovmsController { * cannot name a different region than the resource it describes. */ private static String regionOf(String arn) { - String[] parts = arn.split(":", 5); - return parts.length > 3 ? parts[3] : ""; + return AwsArnUtils.regionOrDefault(arn, ""); } /** diff --git a/src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsService.java b/src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsService.java index 034d28ee15..3f956e1e01 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsService.java @@ -201,7 +201,7 @@ public class LambdaMicrovmsService { * ids/names through — CloudFormation physical ids stay name-based. */ private static String unwrap(String identifier) { - if (identifier != null && identifier.startsWith("arn:")) { + if (AwsArnUtils.looksLikeArn(identifier)) { return identifier.substring(identifier.lastIndexOf(':') + 1); } return identifier; diff --git a/tools/partition/baseline.tsv b/tools/partition/baseline.tsv index 1554c5d15e..1c416f3dc4 100644 --- a/tools/partition/baseline.tsv +++ b/tools/partition/baseline.tsv @@ -11,7 +11,6 @@ arn-concat src/main/java/io/github/hectorvent/floci/services/stepfunctions/State arn-concat src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsService.java 1 arn-probe src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java 1 arn-probe src/main/java/io/github/hectorvent/floci/core/common/PassedRoles.java 2 -arn-probe src/main/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2Service.java 1 arn-probe src/main/java/io/github/hectorvent/floci/services/appintegrations/AppIntegrationsService.java 1 arn-probe src/main/java/io/github/hectorvent/floci/services/appsync/graphql/datasource/AppSyncDataSourceAuthorizer.java 1 arn-probe src/main/java/io/github/hectorvent/floci/services/appsync/graphql/datasource/RelationalDataSourceInvoker.java 1 @@ -31,9 +30,6 @@ arn-probe src/main/java/io/github/hectorvent/floci/services/glue/GlueService.jav 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/lambda/LambdaArnUtils.java 2 -arn-probe src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java 4 -arn-probe src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsService.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 @@ -54,9 +50,6 @@ arn-split src/main/java/io/github/hectorvent/floci/services/ec2/Ec2InstanceCrede 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/lambda/LambdaController.java 1 -arn-split src/main/java/io/github/hectorvent/floci/services/lambda/LambdaTagController.java 1 -arn-split src/main/java/io/github/hectorvent/floci/services/lambdamicrovms/LambdaMicrovmsController.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