chore(iam): share regional resource policy ARN checks (#7745)

This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-08 18:53:33 -04:00
1 parent 6e3bdaf9ef
commit ed45a35274
9 files changed
+101 -18

No files matched your search

@@ -26,7 +26,7 @@ public class AccountResolver {
this.defaultAccountId = defaultAccountId;
}
static boolean isAccountId(String value) {
public static boolean isAccountId(String value) {
if (value == null || value.length() != 12) {
return false;
}
@@ -6,7 +6,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.List;
import java.util.regex.Pattern;
import java.util.Optional;
/**
* Supplies event bus policies to IAM policy evaluation, together with the account that owns the bus:
@@ -16,7 +16,6 @@ import java.util.regex.Pattern;
@ApplicationScoped
public class EventBridgeResourcePolicyProvider implements ResourcePolicyProvider {
private static final Pattern ACCOUNT_ID = Pattern.compile("\\d{12}");
private static final String BUS_PREFIX = "event-bus/";
private final EventBridgeService eventBridgeService;
@@ -28,13 +27,13 @@ public class EventBridgeResourcePolicyProvider implements ResourcePolicyProvider
@Override
public List<ResourcePolicy> getResourcePolicies(String credentialScope, String resourceArn) {
if (!AwsArnUtils.isArnFor(resourceArn, "events")) {
Optional<AwsArnUtils.Arn> parsed = ResourcePolicyProvider.regionalArn(resourceArn, "events");
if (parsed.isEmpty()) {
return List.of();
}
AwsArnUtils.Arn arn = AwsArnUtils.parse(resourceArn);
AwsArnUtils.Arn arn = parsed.get();
String name = arn.resource().startsWith(BUS_PREFIX) ? arn.resource().substring(BUS_PREFIX.length()) : "";
if (!ACCOUNT_ID.matcher(arn.accountId()).matches() || arn.region().isBlank()
|| name.isBlank() || name.contains("*") || name.contains("/")) {
if (name.isBlank() || name.contains("*") || name.contains("/")) {
return List.of();
}
return List.of(new ResourcePolicy(eventBridgeService.findBusPolicy(resourceArn).orElse(null),
@@ -1,6 +1,10 @@
package io.floci.aws.services.iam;
import io.floci.aws.core.common.AccountResolver;
import io.floci.aws.core.common.AwsArnUtils;
import java.util.List;
import java.util.Optional;
/**
* Supplies the resource-based policy documents that apply to a given (credentialScope, resourceArn).
@@ -13,6 +17,21 @@ public interface ResourcePolicyProvider {
record ResourcePolicy(String policyDocument, String ownerAccountId) {}
/**
* Parses a regional, account-owned ARN for the service. Resource-name constraints remain
* with the provider, since a queue, topic and event bus have different name shapes.
*/
static Optional<AwsArnUtils.Arn> regionalArn(String resourceArn, String service) {
if (!AwsArnUtils.isArnFor(resourceArn, service)) {
return Optional.empty();
}
AwsArnUtils.Arn arn = AwsArnUtils.parse(resourceArn);
if (arn.region().isBlank() || !AccountResolver.isAccountId(arn.accountId())) {
return Optional.empty();
}
return Optional.of(arn);
}
/**
* Returns the resource-based policies that apply to the specified resource,
* or an empty list if none apply.
@@ -6,6 +6,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
/**
@@ -20,7 +21,6 @@ import java.util.regex.Pattern;
@ApplicationScoped
public class SnsResourcePolicyProvider implements ResourcePolicyProvider {
private static final Pattern ACCOUNT_ID = Pattern.compile("\\d{12}");
/** A topic name, as CreateTopic accepts it; subscriptions, endpoints and applications are not topics. */
private static final Pattern TOPIC_NAME = Pattern.compile("[A-Za-z0-9_-]{1,256}(\\.fifo)?");
@@ -33,12 +33,12 @@ public class SnsResourcePolicyProvider implements ResourcePolicyProvider {
@Override
public List<ResourcePolicy> getResourcePolicies(String credentialScope, String resourceArn) {
if (!AwsArnUtils.isArnFor(resourceArn, "sns")) {
Optional<AwsArnUtils.Arn> parsed = ResourcePolicyProvider.regionalArn(resourceArn, "sns");
if (parsed.isEmpty()) {
return List.of();
}
AwsArnUtils.Arn arn = AwsArnUtils.parse(resourceArn);
if (!ACCOUNT_ID.matcher(arn.accountId()).matches() || arn.region().isBlank()
|| !TOPIC_NAME.matcher(arn.resource()).matches()) {
AwsArnUtils.Arn arn = parsed.get();
if (!TOPIC_NAME.matcher(arn.resource()).matches()) {
return List.of();
}
String policy = snsService.findTopicPolicy(resourceArn).orElse(null);
@@ -6,7 +6,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.List;
import java.util.regex.Pattern;
import java.util.Optional;
/**
* Supplies SQS queue policies to IAM policy evaluation, together with the account that owns the
@@ -16,7 +16,6 @@ import java.util.regex.Pattern;
@ApplicationScoped
public class SqsResourcePolicyProvider implements ResourcePolicyProvider {
private static final Pattern ACCOUNT_ID = Pattern.compile("\\d{12}");
private final SqsService sqsService;
@@ -27,13 +26,13 @@ public class SqsResourcePolicyProvider implements ResourcePolicyProvider {
@Override
public List<ResourcePolicy> getResourcePolicies(String credentialScope, String resourceArn) {
if (!AwsArnUtils.isArnFor(resourceArn, "sqs")) {
Optional<AwsArnUtils.Arn> parsed = ResourcePolicyProvider.regionalArn(resourceArn, "sqs");
if (parsed.isEmpty()) {
return List.of();
}
AwsArnUtils.Arn arn = AwsArnUtils.parse(resourceArn);
AwsArnUtils.Arn arn = parsed.get();
// ListQueues and a request with no QueueUrl name every queue, which is not one queue's call.
if (!ACCOUNT_ID.matcher(arn.accountId()).matches() || arn.region().isBlank()
|| arn.resource().isBlank() || arn.resource().contains("*")) {
if (arn.resource().isBlank() || arn.resource().contains("*")) {
return List.of();
}
return List.of(new ResourcePolicy(sqsService.findQueuePolicy(resourceArn).orElse(null), arn.accountId()));