fix(secretsmanager): check Secrets Manager requests from the generated request resources (#8365)

Secrets Manager was built by hand in ResourceArnBuilder. It now moves out of
HAND_BUILT into NAMESPACES, so every operation's resources come from
iam-request-resources.json, and the hand builder and the enforcement filter's
routing to it are gone.

The six characters Secrets Manager mints after a secret's name come from
botocore, which documents CreateSecret's Name: "Secrets Manager automatically
adds a hyphen and six random characters after the secret name at the end of
the ARN". A new rule records them as mintedSuffixes ("-??????") and has the
create name the secret it makes by Name. The reader appends them open to a
secret Secrets Manager does not hold. An ARN given for a secret, a partial
one included, is checked on the ARN of the secret the operations read it as,
alone: SecretsManagerService resolves it through resolveStoredArn to the
caller's secret in the ARN's Region, else to the one the ARN's account holds
under that full ARN, reading only.

ARNs the table renders, and the resource a create makes that its tags are
checked on, take the request's partition for every namespace, as the
Secrets Manager builder did, so a Region Floci does not know keeps the
partition of the request.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-14 19:50:46 -04:00
1 parent 4373e83af1
commit 1b14d10b1f
15 files changed
+457 -193

No files matched your search

@@ -1176,7 +1176,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
String region, String accountId, String akid) {
// A create tags the resource it makes, not the parent it makes it in (see RequestResources.created).
List<String> created = RequestResources.created(operation, tagging.action(), ctx,
AwsRegions.partitionFor(region), region, accountId, IamActionRegistry.pathLabels(credentialScope, ctx));
requestPartition(), region, accountId, IamActionRegistry.pathLabels(credentialScope, ctx));
List<String> made = created.isEmpty() ? resources : created;
for (TagOnCreate.TagSet set : tagging.checked()) {
Map<String, List<String>> context = TagOnCreate.conditionContext(tagging, set, operation);
@@ -1847,9 +1847,6 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
if ("logs".equals(credentialScope)) {
return arnBuilder.buildLogResources(action, ctx, region, accountId, requestPartition());
}
if ("secretsmanager".equals(credentialScope)) {
return arnBuilder.buildSecretsManagerResources(action, ctx, region, accountId, requestPartition());
}
if ("kms".equals(credentialScope)) {
return arnBuilder.buildKmsResources(action, ctx, region, accountId, requestPartition());
}
@@ -1860,8 +1857,8 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|| "apigateway".equals(credentialScope)) {
return arnBuilder.buildResources(credentialScope, ctx, region, accountId, requestPartition());
}
String partition = "route53".equals(credentialScope) ? requestPartition() : AwsRegions.partitionFor(region);
return arnBuilder.buildResources(credentialScope, operation, ctx, region, accountId, partition);
// ARNs the table renders take the request's partition, which a Region Floci does not know cannot be mapped to.
return arnBuilder.buildResources(credentialScope, operation, ctx, region, accountId, requestPartition());
}
/**
@@ -48,6 +48,8 @@ final class RequestResourceMembers {
"^arn:\\$\\{Partition}:[^:]*:[^:]*:[^:]*:([A-Za-z][A-Za-z0-9-]*)(.*)$");
/** A format's resource that is a literal and one variable. */
private static final Pattern ONE_VARIABLE = Pattern.compile("[^$]*\\$\\{\\w+}");
/** A separator and the characters a service mints after a name, each {@code ?}: {@code -??????}. */
private static final Pattern MINTED_SUFFIX = Pattern.compile("[-_.]\\?+");
/** A member a REST request carries outside its body, by its name, and where: a path label or a query parameter. */
private static final Pattern LOCATED_MEMBER = Pattern.compile("[A-Za-z][A-Za-z0-9_]*");
private static final Pattern LOCATION = Pattern.compile("\\{[A-Za-z0-9_]+}|\\?[A-Za-z0-9_.-]+");
@@ -96,12 +98,15 @@ final class RequestResourceMembers {
* checks an ARN of it given with a qualifier, one more {@code :<name>} past the format, without that qualifier
* (Step Functions checks a version or an alias on its state machine's ARN); {@code minted} when its service
* follows the one variable the format ends in with an id it mints, {@code /<id>}, which the format leaves out
* (EventBridge's {@code connection/<name>/<id>}).
* (EventBridge's {@code connection/<name>/<id>}); {@code mintedSuffix} the characters its service follows the
* name that variable takes with, as IAM matches them, one {@code ?} each after the separator (a Secrets Manager
* secret's {@code -??????}), or null.
*/
record ArnFormat(String format, Pattern pattern, boolean unqualified, boolean minted, Pattern integers) {
record ArnFormat(String format, Pattern pattern, boolean unqualified, boolean minted, Pattern integers,
String mintedSuffix) {
ArnFormat(String format, Pattern pattern, boolean unqualified, boolean minted) {
this(format, pattern, unqualified, minted, null);
this(format, pattern, unqualified, minted, null, null);
}
ArnFormat(String format, Pattern pattern, boolean unqualified) {
@@ -327,6 +332,7 @@ final class RequestResourceMembers {
document.path("arnFormats").properties().forEach(namespace -> {
Set<String> unqualified = names(document.path("unqualified").path(namespace.getKey()));
Set<String> minted = names(document.path("mintedIds").path(namespace.getKey()));
JsonNode suffixes = document.path("mintedSuffixes").path(namespace.getKey());
Map<String, ArnFormat> types = new LinkedHashMap<>();
namespace.getValue().properties().forEach(type -> {
String format = type.getValue().asText("");
@@ -336,7 +342,8 @@ final class RequestResourceMembers {
}
Set<String> integers = names(document.path("integerVariables").path(namespace.getKey()).path(type.getKey()));
types.put(type.getKey(), new ArnFormat(format, pattern(format), unqualified.contains(type.getKey()),
minted.contains(type.getKey()), integers.isEmpty() ? null : pattern(format, integers)));
minted.contains(type.getKey()), integers.isEmpty() ? null : pattern(format, integers),
suffixes.path(type.getKey()).asText(null)));
});
formats.put(namespace.getKey(), Map.copyOf(types));
});
@@ -355,6 +362,18 @@ final class RequestResourceMembers {
+ namespace.getValue() + " a minted id, no types whose format ends in its one variable");
}
});
document.path("mintedSuffixes").properties().forEach(namespace -> {
Map<String, ArnFormat> types = formats.getOrDefault(namespace.getKey(), Map.of());
namespace.getValue().properties().forEach(type -> {
if (!types.containsKey(type.getKey())
|| !ONE_VARIABLE.matcher(types.get(type.getKey()).format().split(":", 6)[5]).matches()
|| !type.getValue().isTextual() || !MINTED_SUFFIX.matcher(type.getValue().asText()).matches()) {
throw new IllegalStateException(RESOURCE_NAME + " gives " + namespace.getKey() + "'s "
+ type.getKey() + " the minted suffix " + type.getValue()
+ ", no type whose format ends in its one variable or no separator and characters");
}
});
});
Map<String, Map<String, List<String>>> resourceTagKeys = new LinkedHashMap<>();
if (!document.path("resourceTagKeys").isMissingNode() && !document.path("resourceTagKeys").isObject()) {
throw new IllegalStateException(RESOURCE_NAME + " has malformed resource tag keys");
@@ -824,7 +824,8 @@ public final class RequestResources {
* <p>The service is asked for every type of the namespace, not only for the ones whose ARN carries an id it
* mints, so a service may answer for a type whose ARN it does not mint: a key an alias names is the key the
* alias points at. A type whose service follows its name with an id it mints has that id open when the service
* stores none, and any other type's is as rendered.
* stores none, one whose service follows it with characters it mints those characters open (a secret's
* {@code -??????}, as a create names the secret it makes), and any other type's is as rendered.
*/
private static String withId(String arn, RequestResourceMembers.ArnFormat format, String type,
Map<String, String> filling, StoredResourceArns stored, String region) {
@@ -834,7 +835,10 @@ public final class RequestResources {
return found.get();
}
}
return format.minted() ? arn + "/*" : arn;
if (format.minted()) {
return arn + "/*";
}
return format.mintedSuffix() != null ? arn + format.mintedSuffix() : arn;
}
/**
@@ -884,7 +888,8 @@ public final class RequestResources {
* and one of a type AWS owns ({@code awsOwned}) its account, {@code aws}, which is no caller's. One naming another
* account whose resource {@code stored} says the service acts on there (an OAM sink a link is made to, a RAM
* invitation sent to the caller) is that resource alone. When the service resolves the complete ARN to a stored
* resource in its own coordinates, that stored ARN alone is checked.
* resource, that stored ARN alone is checked: a layer version in its own coordinates, or the secret a full or
* partial ARN names in the ARN's Region, as its handler reads it.
*/
private static void addNamed(Set<String> arns, String arn, boolean awsOwned, String partition, String region,
String accountId, StoredResourceArns stored) {
@@ -23,7 +23,8 @@ import java.util.Optional;
* Redshift Serverless workgroups it runs statements on and the targets of its sessions; Payment Cryptography, for
* the key an alias names; EMR, for the clusters of its instance groups; IAM Identity Center, for the accounts its
* permission sets are provisioned to; Resource Explorer, for its default view; OAM and RAM, for the sinks,
* invitations and shares of other accounts they act on); looked up lazily by {@code ResourceArnBuilder} through {@code Instance<StoredResourceArns>}
* invitations and shares of other accounts they act on; Secrets Manager, for the secret a name or an ARN names,
* random suffix included); looked up lazily by {@code ResourceArnBuilder} through {@code Instance<StoredResourceArns>}
* for the namespace a request is of, so IAM never depends on that service directly.</p>
*
* <p>Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find},
@@ -45,7 +46,9 @@ public interface StoredResourceArns {
/**
* The resource resolved by an ARN in its own account and Region, without substituting the caller's
* coordinates. Empty when the service has no such resource or does not resolve ARN selectors this way.
* coordinates (a layer version), or as the service's handler reads the ARN where it reads it first as the
* caller's (a secret a full or partial ARN names, in the ARN's Region, before one another account holds under
* that full ARN). Empty when the service has no such resource or does not resolve ARN selectors this way.
* Used only for operations read by the generated request-resource table.
*/
default Optional<String> resolveStoredArn(String arn) {
@@ -24,7 +24,6 @@ import io.floci.aws.services.lambdamicrovms.LambdaMicrovmsService;
import io.floci.aws.services.s3.S3ControlArnUtils;
import io.floci.aws.services.s3.S3ObjectKeys;
import io.floci.aws.services.s3.S3ResourceArns;
import io.floci.aws.services.secretsmanager.SecretsManagerService;
import io.floci.aws.services.sqs.SqsService;
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
import io.vertx.ext.web.RoutingContext;
@@ -71,12 +70,6 @@ public class ResourceArnBuilder {
/** What a service-linked role deletion task identifier starts with; see its resolver. */
private static final String DELETION_TASK_PREFIX = "task" + ServiceLinkedRoles.PATH;
/**
* Stands in for the random suffix of a secret that {@code CreateSecret} has not made yet. Each
* {@code ?} matches the one character a {@code secret:<name>-??????} policy expects there.
*/
private static final String UNASSIGNED_SECRET_SUFFIX = "-??????";
/** Where an S3 Control tagging route carries the ARN of the resource it acts on. */
private static final String S3_CONTROL_TAGS_PREFIX = "/v20180820/tags/";
@@ -95,8 +88,6 @@ public class ResourceArnBuilder {
* which IamService itself does not depend on, and a direct injection would close that loop.
*/
private final Instance<IamService> iamService;
/** Looked up lazily for the same reason, to resolve a secret named by its name or a partial ARN. */
private final Instance<SecretsManagerService> secretsManagerService;
/** Looked up lazily for the same reason, to read the image a MicroVM runs. */
private final Instance<LambdaMicrovmsService> microvmsService;
/** Looked up lazily for the same reason, to read the ARN an EKS cluster or what it holds is stored under. */
@@ -117,7 +108,6 @@ public class ResourceArnBuilder {
@Inject
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
Instance<SecretsManagerService> secretsManagerService,
Instance<LambdaMicrovmsService> microvmsService,
Instance<EksService> eksService,
Instance<KmsService> kmsService,
@@ -125,7 +115,6 @@ public class ResourceArnBuilder {
CurrentVertxRequest currentVertxRequest) {
this.objectMapper = objectMapper;
this.iamService = iamService;
this.secretsManagerService = secretsManagerService;
this.microvmsService = microvmsService;
this.eksService = eksService;
this.kmsService = kmsService;
@@ -134,39 +123,30 @@ public class ResourceArnBuilder {
}
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
Instance<SecretsManagerService> secretsManagerService,
Instance<LambdaMicrovmsService> microvmsService,
Instance<EksService> eksService,
Instance<KmsService> kmsService,
Instance<StoredResourceArns> storedResourceArns) {
this(objectMapper, iamService, secretsManagerService, microvmsService, eksService, kmsService,
this(objectMapper, iamService, microvmsService, eksService, kmsService,
storedResourceArns, null);
}
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
Instance<SecretsManagerService> secretsManagerService,
Instance<LambdaMicrovmsService> microvmsService,
Instance<EksService> eksService,
Instance<KmsService> kmsService) {
this(objectMapper, iamService, secretsManagerService, microvmsService, eksService, kmsService, null);
this(objectMapper, iamService, microvmsService, eksService, kmsService, null);
}
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
Instance<SecretsManagerService> secretsManagerService,
Instance<LambdaMicrovmsService> microvmsService,
Instance<EksService> eksService) {
this(objectMapper, iamService, secretsManagerService, microvmsService, eksService, null);
this(objectMapper, iamService, microvmsService, eksService, null);
}
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
Instance<SecretsManagerService> secretsManagerService,
Instance<LambdaMicrovmsService> microvmsService) {
this(objectMapper, iamService, secretsManagerService, microvmsService, null);
}
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
Instance<SecretsManagerService> secretsManagerService) {
this(objectMapper, iamService, secretsManagerService, null);
this(objectMapper, iamService, microvmsService, null);
}
ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService) {
@@ -182,7 +162,7 @@ public class ResourceArnBuilder {
}
ResourceArnBuilder(ObjectMapper objectMapper, CurrentVertxRequest currentVertxRequest) {
this(objectMapper, null, null, null, null, null, null, currentVertxRequest);
this(objectMapper, null, null, null, null, null, currentVertxRequest);
}
public String build(String credentialScope, ContainerRequestContext ctx,
@@ -1844,60 +1824,6 @@ public class ResourceArnBuilder {
return AwsArnUtils.Arn.of("kinesis", region, accountId, "stream/*").toString();
}
// ── Secrets Manager ──────────────────────────────────────────────────────────
/**
* Uses the serving operation, as {@link #buildLogResources} does: {@code CreateSecret}
* names the secret in {@code Name} and every other operation in {@code SecretId}, so a field the
* handler does not read cannot choose the resource. A minted ARN takes the request's partition,
* the one the secret is created in, which a region Floci does not know cannot be mapped to.
*/
public List<String> buildSecretsManagerResources(String action, ContainerRequestContext ctx,
String region, String accountId, String partition) {
return List.of(buildSecretsManagerArn(action, ctx, region, accountId, partition));
}
/**
* A secret named by its name or a partial ARN is authorized against its full ARN, random suffix
* included, so a {@code secret:<name>-??????} policy matches it as it does on AWS. The suffix of
* the secret {@code CreateSecret} is making is not assigned until it is created.
*/
private String buildSecretsManagerArn(String action, ContainerRequestContext ctx, String region,
String accountId, String partition) {
JsonNode json = readJsonBody(ctx);
if (json != null && json.isObject()) {
if ("secretsmanager:CreateSecret".equals(action)) {
if (json.hasNonNull("Name")) {
String name = json.get("Name").asText().trim();
if (!name.isEmpty()) {
return secretArn(name + UNASSIGNED_SECRET_SUFFIX, region, accountId, partition);
}
}
} else if (json.hasNonNull("SecretId")) {
String secretId = json.get("SecretId").asText().trim();
if (!secretId.isEmpty()) {
Optional<String> stored = storedSecretArn(secretId, region, accountId);
if (AwsArnUtils.isArnFor(secretId, "secretsmanager")) {
return stored.orElse(secretId);
}
return stored.orElseGet(() -> secretArn(secretId, region, accountId, partition));
}
}
}
return secretArn("*", region, accountId, partition);
}
private Optional<String> storedSecretArn(String secretId, String region, String accountId) {
if (secretsManagerService == null) {
return Optional.empty();
}
return secretsManagerService.get().findArn(accountId, secretId, region);
}
private static String secretArn(String name, String region, String accountId, String partition) {
return new AwsArnUtils.Arn(partition, "secretsmanager", region, accountId, "secret:" + name).toString();
}
// ── KMS ──────────────────────────────────────────────────────────────────────
/**
@@ -8,6 +8,7 @@ import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.ServicePrincipals;
import io.floci.aws.core.common.StoredResourceArns;
import io.floci.aws.core.resource.ExplorerResource;
import io.floci.aws.core.resource.ResourceProvider;
import io.floci.aws.core.resource.SupportedResourceType;
@@ -54,7 +55,7 @@ import java.util.function.Supplier;
import java.util.regex.Pattern;
@ApplicationScoped
public class SecretsManagerService implements ResourceProvider {
public class SecretsManagerService implements ResourceProvider, StoredResourceArns {
private static final Logger LOG = Logger.getLogger(SecretsManagerService.class);
@@ -494,6 +495,36 @@ public class SecretsManagerService implements ResourceProvider {
return secret;
}
@Override
public String namespace() {
return "secretsmanager";
}
/**
* What IAM asks Secrets Manager of a request's resources: the ARN, random suffix included, of the caller's secret
* a name or an ARN names, a partial one included ({@link #findArn}). Reads only.
*/
@Override
public Optional<String> find(String type, List<String> names, String region) {
return "Secret".equals(type) && names.size() == 1
? findArn(regionResolver.getAccountId(), names.getFirst(), region) : Optional.empty();
}
/**
* The secret an ARN names, a partial one included, as the operations read it ({@link #resolveSecret}): the
* caller's, in the Region the ARN names ({@link #findArn}), else the one the account the ARN names holds under
* that full ARN. Reads only.
*/
@Override
public Optional<String> resolveStoredArn(String arn) {
AwsArnUtils.Arn parsed = parseOrNull(arn);
if (parsed == null || secretNameOrNull(parsed) == null) {
return Optional.empty();
}
return findArn(regionResolver.getAccountId(), arn, parsed.region())
.or(() -> Optional.ofNullable(findByArnInAccount(arn, parsed)).map(Secret::getArn));
}
/**
* The full ARN of the secret {@code secretId} names in the given account, or empty when there is
* none, resolved in the order the operations resolve it: a name in {@code region}, or an ARN,