diff --git a/AGENTS.md b/AGENTS.md index 6222e57513..95572ece20 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -123,12 +123,17 @@ Floci serves every AWS partition, not only the commercial one, so a literal that prints correctly in `us-east-1`. `make partition-check` (CI: Partition Literals) inventories `src/main/java` for `arn:aws:` prefixes, `amazonaws.com` hosts, Route 53 hosted-zone ids, hand-rolled `arn:aws[a-z-]*:` regexes, `Arn.of(...)` calls with a blank region and comparisons -with the literal `"aws"` against `tools/partition/baseline.tsv`, per file. The baseline is empty, so any new one fails. +with the literal `"aws"` against `tools/partition/baseline.tsv`, per file. It also inventories +ARNs handled by hand: `"arn:" + ...` concatenation (`arn-concat`, except a pattern built from +`PARTITION_REGEX`), `startsWith("arn:")` checks (`arn-probe`) and `split(":")` on a value named +`*arn*`/`*Arn*` (`arn-split`). Those three still have baselined sites, which only shrink; every +other category's baseline is empty, so any new one fails. A literal in a new file fails, growth in an existing file fails, and a drop fails until you run `make partition-baseline` and commit the smaller baseline. - Mint ARNs through `AwsArnUtils.Arn.of(...)` / `RegionResolver.buildArn(...)`, recognise - them with `AwsArnUtils.isArn` / `PARTITION_REGEX`, and derive hosts from the region's + them with `AwsArnUtils.isArn` / `PARTITION_REGEX`, read their fields with + `AwsArnUtils.parse(...)`, and derive hosts from the region's DNS suffix (`AwsRegions.dnsSuffixFor`). - A regionless ARN (IAM, STS, CloudFront, Organizations, an S3 bucket) never goes through `Arn.of(service, "", ...)`, which silently means `aws`: use `RegionResolver.buildGlobalArn` @@ -309,7 +314,8 @@ When adding functionality: value, while strict SDKs (Go, Java) reject the whole response. `javap -c` on the SDK model class shows the traits on each `SdkField` 9. Add `ServiceTest` and `IntegrationTest` -10. If the service mints ARNs or AWS hosts (`buildArn`, `Arn.of`, `new AwsArnUtils.Arn`, `dnsSuffixFor`, `AwsEndpoints`), +10. If the service mints ARNs or AWS hosts (`buildArn`, `Arn.of`, `new AwsArnUtils.Arn`, `dnsSuffixFor`, `AwsEndpoints`, + or a `"arn:" + ...` concatenation), add a case to `PartitionCrossServiceSmokeIntegrationTest` and its `COVERED_PACKAGES`, or a row with a reason to `src/test/resources/partition/smoke-exemptions.tsv`; `PartitionSmokeInventoryTest` fails until one of the two exists diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsArnUtils.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsArnUtils.java index cce9e23e21..121f59e3af 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/AwsArnUtils.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsArnUtils.java @@ -51,7 +51,7 @@ public final class AwsArnUtils { @Override public String toString() { - return "arn:" + partition + ":" + service + ":" + region + ":" + accountId + ":" + resource; + return "arn:" + partition + ":" + service + ":" + region + ":" + accountId + ":" + resource; // partition-literal: the one ARN formatter } } @@ -65,7 +65,7 @@ public final class AwsArnUtils { if (arn == null || arn.isBlank()) { throw new IllegalArgumentException("ARN must not be null or blank"); } - String[] parts = arn.split(":", 6); + String[] parts = arn.split(":", 6); // partition-literal: the one ARN parser if (parts.length < 6 || !"arn".equals(parts[0])) { throw new IllegalArgumentException("Invalid ARN: " + arn); } diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PartitionSmokeInventoryTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PartitionSmokeInventoryTest.java index 45411864bb..f134af747e 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/PartitionSmokeInventoryTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/PartitionSmokeInventoryTest.java @@ -31,7 +31,9 @@ class PartitionSmokeInventoryTest { private static final Path EXEMPTIONS = Path.of("src/test/resources/partition/smoke-exemptions.tsv"); private static final Pattern MINTS = Pattern.compile( "buildArn\\(|buildGlobalArn\\(|\\bArn\\.of\\(|\\bArn\\.global\\(|\\bnew (AwsArnUtils\\.)?Arn\\(" - + "|dnsSuffixFor\\(|AwsEndpoints\\.\\w+\\("); + + "|dnsSuffixFor\\(|AwsEndpoints\\.\\w+\\(" + // An ARN concatenated by hand mints as surely as Arn.of; a PARTITION_REGEX pattern only matches one. + + "|\"arn:\"\\s*\\+(?!\\s*(?:AwsArnUtils\\s*\\.\\s*)?PARTITION_REGEX\\b)"); @Test void everyMintingPackageIsCoveredOrExempted() { diff --git a/tools/partition/baseline.tsv b/tools/partition/baseline.tsv index 30d3f492e4..61b0b75334 100644 --- a/tools/partition/baseline.tsv +++ b/tools/partition/baseline.tsv @@ -1,2 +1,88 @@ # category path count # Generated by tools/partition/partition_literals.py --write-baseline; do not hand-edit. +arn-concat src/main/java/io/github/hectorvent/floci/services/appsync/graphql/auth/IamAuthValidator.java 1 +arn-concat src/main/java/io/github/hectorvent/floci/services/controlcatalog/ControlCatalogService.java 1 +arn-concat src/main/java/io/github/hectorvent/floci/services/dsql/DsqlClusterPolicies.java 1 +arn-concat src/main/java/io/github/hectorvent/floci/services/ec2/Ec2InstanceCredentials.java 1 +arn-concat src/main/java/io/github/hectorvent/floci/services/eks/EksAccessEntryService.java 2 +arn-concat src/main/java/io/github/hectorvent/floci/services/eks/EksAddonService.java 2 +arn-concat src/main/java/io/github/hectorvent/floci/services/iam/AwsManagedPolicies.java 2 +arn-concat src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java 2 +arn-concat src/main/java/io/github/hectorvent/floci/services/ses/SesService.java 1 +arn-concat src/main/java/io/github/hectorvent/floci/services/stepfunctions/StatesIntegration.java 1 +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 +arn-probe src/main/java/io/github/hectorvent/floci/services/appsync/graphql/resolver/AppSyncResolverExecutor.java 1 +arn-probe src/main/java/io/github/hectorvent/floci/services/batch/BatchService.java 4 +arn-probe src/main/java/io/github/hectorvent/floci/services/bedrock/BedrockService.java 3 +arn-probe src/main/java/io/github/hectorvent/floci/services/budgets/BudgetsService.java 1 +arn-probe src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationService.java 7 +arn-probe src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BatchCfnProvisioner.java 1 +arn-probe src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/BedrockAgentCoreCfnProvisioner.java 2 +arn-probe src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java 1 +arn-probe src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/EcsCfnProvisioner.java 1 +arn-probe src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamPolicyCfnProvisioner.java 1 +arn-probe src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java 1 +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/docdb/DocDbService.java 3 +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/ecs/EcsService.java 9 +arn-probe src/main/java/io/github/hectorvent/floci/services/ecs/container/EcsContainerManager.java 1 +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/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/neptune/NeptuneService.java 3 +arn-probe src/main/java/io/github/hectorvent/floci/services/rds/RdsService.java 13 +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 +arn-probe src/main/java/io/github/hectorvent/floci/services/ssooidc/SsoOidcController.java 2 +arn-split src/main/java/io/github/hectorvent/floci/core/common/IamConditionContextResolver.java 1 +arn-split src/main/java/io/github/hectorvent/floci/services/appsync/graphql/resolver/AppSyncResolverExecutor.java 1 +arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreControlService.java 3 +arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecontrol/BedrockAgentCoreGatewayService.java 1 +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/eks/EksAccessEntryService.java 1 +arn-split src/main/java/io/github/hectorvent/floci/services/eks/EksAddonService.java 3 +arn-split src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java 4 +arn-split src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityAssociationService.java 2 +arn-split src/main/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgramming.java 1 +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/neptune/NeptuneQueryHandler.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/rds/RdsQueryHandler.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 +arn-split src/main/java/io/github/hectorvent/floci/services/ses/SesTenantService.java 2 +arn-split src/main/java/io/github/hectorvent/floci/services/ssoadmin/SsoAdminService.java 1 +arn-split src/main/java/io/github/hectorvent/floci/services/swf/SwfService.java 1 +arn-split src/main/java/io/github/hectorvent/floci/services/verifiedpermissions/VerifiedPermissionsService.java 1 diff --git a/tools/partition/partition_literals.py b/tools/partition/partition_literals.py index 848e547060..8158924e62 100644 --- a/tools/partition/partition_literals.py +++ b/tools/partition/partition_literals.py @@ -10,6 +10,11 @@ not others. Each one is a silent bug in a non-commercial deployment, and each on trivial to add by accident, because the literal is exactly what an `aws` deployment prints. +It also counts ARNs handled by hand, which name no partition but bypass the helpers that +get every partition right: an ARN concatenated from `"arn:"` (`arn-concat`), a +`startsWith("arn:")` probe (`arn-probe`) and a `split(":")` of an ARN-named value +(`arn-split`). Those categories started with existing sites in the baseline, which only shrink. + This script inventories those literals in `src/main/java` by category and gates them against a checked-in per-file baseline (`baseline.tsv`): @@ -127,6 +132,32 @@ CATEGORIES: tuple[Category, ...] = ( "partition, or escape a non-partition use (an account, a command name)", "code", ), + Category( + "arn-concat", + # An ARN assembled by concatenation. A regex built from PARTITION_REGEX is the sanctioned + # way to recognise one, so that form does not count. + re.compile(r'"arn:"\s*\+(?!\s*(?:AwsArnUtils\s*\.\s*)?PARTITION_REGEX\b)'), + True, + "an ARN or ARN prefix built by concatenating \"arn:\"; mint through AwsArnUtils.Arn.of / Arn.global " + "or RegionResolver.buildArn, and recognise one with AwsArnUtils.PARTITION_REGEX", + "code", + ), + Category( + "arn-probe", + re.compile(r'\.\s*startsWith\(\s*"arn:"\s*\)'), + True, + "a hand-rolled ARN check; use AwsArnUtils.isArn, isArnFor or parse", + "code", + ), + Category( + "arn-split", + # A split on ":" of a value whose name says it is an ARN (arn, roleArn, getArn(), arnParts...). + re.compile(r'\b(?:arn|\w*Arn)\w*(?:\(\s*\))?\s*\.\s*split\(\s*":"'), + True, + "an ARN cut into fields by hand; use AwsArnUtils.parse and its partition(), region(), accountId() " + "and resource()", + "code", + ), ) CATEGORY_BY_NAME = {category.name: category for category in CATEGORIES} diff --git a/tools/partition/test_partition_literals.py b/tools/partition/test_partition_literals.py index f30cd4c5ab..be9d6dc2b0 100644 --- a/tools/partition/test_partition_literals.py +++ b/tools/partition/test_partition_literals.py @@ -238,6 +238,70 @@ def test_blank_region_arn_is_a_code_category_and_never_matches_literals(tmp_path assert counted == Counter({("blank-region-arn", "a/A.java"): 1}) +@pytest.mark.parametrize("code, expected", [ + ('return "arn:" + partition + ":eks:" + region;', True), + ('String prefix = "arn:"+AwsRegions.partitionFor(region) + ":ses:";', True), + ('Pattern.compile("arn:" + AwsArnUtils.PARTITION_REGEX + ":iam::\\\\d{12}:root")', False), + ('Pattern.compile("arn:" + PARTITION_REGEX + ":sso:::instance/.+")', False), + ('Pattern.compile("arn:"\n + AwsArnUtils.PARTITION_REGEX + ":logs:")', False), + ('String partitionRegexName = "arn:" + PARTITION_REGEX_NAME;', True), + ('String s = "arn:aws:" + name;', False), + ('String s = prefix + "arn:";', False), +]) +def test_arn_concat_matches_a_concatenated_arn_but_not_a_partition_regex(code, expected): + category = p.CATEGORY_BY_NAME["arn-concat"] + assert (category.regex.search(code) is not None) is expected + + +@pytest.mark.parametrize("code, expected", [ + ('if (value.startsWith("arn:")) {', True), + ('id != null && id . startsWith( "arn:" )', True), + ('value.startsWith("arn:aws:")', False), + ('value.startsWith("arn")', False), + ('AwsArnUtils.isArn(value)', False), +]) +def test_arn_probe_matches_a_bare_arn_prefix_check(code, expected): + category = p.CATEGORY_BY_NAME["arn-probe"] + assert (category.regex.search(code) is not None) is expected + + +@pytest.mark.parametrize("code, expected", [ + ('String[] parts = arn.split(":");', True), + ('String[] parts = roleArn.split(":", 6);', True), + ('String account = role.getArn().split(":", 6)[4];', True), + ('String[] p = arnParts . split( ":" , -1);', True), + ('String[] parts = fn.getFunctionArn().split(":");', True), + ('String[] parts = arn.split("/");', False), + ('String[] parts = warning.split(":");', False), + ('String[] parts = learnerId.split(":");', False), + ('String[] parts = value.split(":");', False), +]) +def test_arn_split_matches_a_colon_split_of_an_arn_named_value(code, expected): + category = p.CATEGORY_BY_NAME["arn-split"] + assert (category.regex.search(code) is not None) is expected + + +def test_arn_shape_categories_count_code_only_and_honour_an_escape(tmp_path): + write_java(tmp_path, "a/A.java", ( + 'class A {\n' + ' String arn = "arn:" + partition + ":sqs:" + region;\n' + ' boolean probe = value.startsWith("arn:");\n' + ' String[] parts = arn.split(":", 6);\n' + ' // "arn:" + partition, value.startsWith("arn:"), arn.split(":") in a comment never count\n' + ' String doc = "arn.split(\\":\\")";\n' + ' String[] kept = arn.split(":", 6); // partition-literal: the one ARN parser\n' + '}\n' + )) + findings = p.collect_findings(tmp_path, []) + assert p.count(findings) == Counter({ + ("arn-concat", "a/A.java"): 1, + ("arn-probe", "a/A.java"): 1, + ("arn-split", "a/A.java"): 1, + }) + assert [(f.category, f.line, f.counted) for f in findings if f.category == "arn-split"] == [ + ("arn-split", 4, True), ("arn-split", 7, False)] + + def test_every_category_is_gated(): assert {c.name for c in p.CATEGORIES if c.gated} == {c.name for c in p.CATEGORIES}