fix(iam): authorize Secrets Manager requests against the secret's ARN (#5781)

* fix(iam): authorize Secrets Manager requests against the secret's ARN

IAM enforcement evaluated CreateSecret against secret:* because the
builder read only SecretId, and a secret named by its name was evaluated
as secret:<name>, without the random suffix every secret ARN carries.
A secret:<name>-?????? policy, the pattern AWS documents for granting
access to a secret that may not exist yet, matched neither.

CreateSecret is now evaluated against secret:<Name>-??????, standing in
for the suffix that is assigned at creation, and a name-based SecretId
resolves to the stored secret's full ARN in the target account.

Closes #5364

* fix(iam): pick the Secrets Manager target by the serving operation

CreateSecret now reads only Name and other actions only SecretId, so a
field the handler ignores cannot choose the resource. Minted ARNs take the
request's partition, and a name lookup also reads a legacy unprefixed
secret for the default account. Adds an SDK compatibility test.

* fix(iam): resolve a partial-ARN SecretId to the stored secret's ARN

A SecretId given as an ARN without the random suffix is now authorized
against the stored secret's full ARN, resolved in the caller's account in
the order the operations use: an exact ARN first, then the ARN read as a
partial one. The enforcement tests share EnforcementFixtures' auth, a new
assumeRole helper, and a new createRole helper that removes the role after
the test. The Secrets Manager enforcement test also deletes the secrets it
creates.
This commit is contained in:
Suja Shaj authored and GitHub committed 2026-10-16 23:24:38 -04:00
1 parent 1cb8b0f747
commit a611e9ea6f
13 files changed
+787 -107

No files matched your search

@@ -416,9 +416,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
}
List<String> resources = resolveResourceArns(credentialScope,
"logs".equals(credentialScope)
? arnBuilder.buildLogResources(action, ctx, region, accountId, requestPartition())
: arnBuilder.buildResources(credentialScope, ctx, region, accountId));
targetResources(credentialScope, action, ctx, region, accountId));
Optional<PresignedScope> presignedScope = iamService.presignedScope(akid);
if (presignedScope.isPresent()) {
@@ -1183,6 +1181,18 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
};
}
/** Logs and Secrets Manager targets depend on the serving operation and the request's partition. */
private List<String> targetResources(String credentialScope, String action, ContainerRequestContext ctx,
String region, String accountId) {
if ("logs".equals(credentialScope)) {
return arnBuilder.buildLogResources(action, ctx, region, accountId, requestPartition());
}
if ("secretsmanager".equals(credentialScope)) {
return arnBuilder.buildSecretsManagerResources(action, ctx, region, accountId, requestPartition());
}
return arnBuilder.buildResources(credentialScope, ctx, region, accountId);
}
/**
* The partition the request belongs to, for the principals this filter synthesizes. Set by
* {@link AccountContextFilter}; the deployment partition covers a call that reaches here first.
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.core.common.SigV4CredentialScope;
import io.github.hectorvent.floci.services.cloudwatch.logs.LogGroupTagTarget;
import io.github.hectorvent.floci.services.lambda.LambdaArnUtils;
import io.github.hectorvent.floci.services.lambda.durable.DurableTokens;
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
@@ -50,25 +51,39 @@ public class ResourceArnBuilder {
/** Reads the CBOR bodies the RPC v2 and {@code x-amz-cbor-1.1} paths carry. */
private static final ObjectMapper CBOR_MAPPER = new ObjectMapper(new CBORFactory());
/**
* 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 = "-??????";
private final ObjectMapper objectMapper;
/**
* Looked up lazily through a provider: this builder is constructed by the enforcement filter,
* 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;
@Inject
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService) {
public ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService,
Instance<SecretsManagerService> secretsManagerService) {
this.objectMapper = objectMapper;
this.iamService = iamService;
this.secretsManagerService = secretsManagerService;
}
ResourceArnBuilder(ObjectMapper objectMapper, Instance<IamService> iamService) {
this(objectMapper, iamService, null);
}
public ResourceArnBuilder() {
this(new ObjectMapper(), null);
this(new ObjectMapper(), null, null);
}
public ResourceArnBuilder(ObjectMapper objectMapper) {
this(objectMapper, null);
this(objectMapper, null, null);
}
public String build(String credentialScope, ContainerRequestContext ctx,
@@ -90,7 +105,6 @@ public class ResourceArnBuilder {
case "sns" -> List.of(buildSnsArn(ctx, region, accountId));
case "dynamodb" -> buildDynamoDbArns(ctx, region, accountId);
case "kinesis" -> List.of(buildKinesisArn(ctx, region, accountId));
case "secretsmanager" -> List.of(buildSecretsManagerArn(ctx, region, accountId));
case "ssm" -> List.of(buildSsmArn(ctx, region, accountId));
case "kms" -> List.of(buildKmsArn(path, region, accountId));
case "iam" -> buildIamArns(ctx, region, accountId);
@@ -945,20 +959,57 @@ public class ResourceArnBuilder {
}
// ── Secrets Manager ──────────────────────────────────────────────────────────
private String buildSecretsManagerArn(ContainerRequestContext ctx, String region, String accountId) {
/**
* 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 (json.hasNonNull("SecretId")) {
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 secretId;
return stored.orElse(secretId);
}
return AwsArnUtils.Arn.of("secretsmanager", region, accountId, "secret:" + secretId).toString();
return stored.orElseGet(() -> secretArn(secretId, region, accountId, partition));
}
}
}
return AwsArnUtils.Arn.of("secretsmanager", region, accountId, "secret:*").toString();
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();
}
// ── SSM ──────────────────────────────────────────────────────────────────────
@@ -37,6 +37,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@@ -416,6 +417,39 @@ public class SecretsManagerService implements ResourceProvider {
return secret;
}
/**
* 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,
* matched exactly before it is read as a partial ARN without the random suffix. An ARN that
* matches neither way in this account is empty, and the caller authorizes the ARN as given, which
* is the secret the operations then look for in the account the ARN names. IAM enforcement reads
* it before the request runs: AWS authorizes a request against the secret's ARN, random suffix
* included, however the request names the secret.
*/
public Optional<String> findArn(String accountId, String secretId, String region) {
if (!secretId.startsWith("arn:")) {
return findArnByName(accountId, secretId, region);
}
AwsArnUtils.Arn arn = parseOrNull(secretId);
String nameFromArn = secretNameOrNull(arn);
if (nameFromArn == null) {
return Optional.empty();
}
if (findByArnInAccount(accountId, secretId, arn) != null) {
return Optional.of(secretId);
}
return findArnByName(accountId, nameFromArn, arn.region());
}
/** The full ARN of the secret with this name in the given account and region, or empty when there is none. */
Optional<String> findArnByName(String accountId, String name, String region) {
String key = regionKey(region, name);
Optional<Secret> secret = store instanceof AccountAwareStorageBackend<Secret> aware
? aware.getForAccount(accountId, key).or(() -> aware.getLegacyForAccount(accountId, key))
: store.get(key);
return secret.map(Secret::getArn);
}
/**
* Marks the secret with this ARN as owned by an AWS service, so that it rotates the way a
* service-managed secret does. Ownership is normally set when the owning service creates the
@@ -457,9 +491,14 @@ public class SecretsManagerService implements ResourceProvider {
/** Finds a secret by full ARN within the account and region that ARN names. */
private Secret findByArnInAccount(String secretArn, AwsArnUtils.Arn arn) {
return findByArnInAccount(arn.accountId(), secretArn, arn);
}
/** Finds a secret by full ARN within the given account and the region that ARN names. */
private Secret findByArnInAccount(String accountId, String secretArn, AwsArnUtils.Arn arn) {
Predicate<String> inRegion = key -> key.startsWith(arn.region() + "::");
List<Secret> candidates = store instanceof AccountAwareStorageBackend<Secret> aware
? aware.scanForAccount(arn.accountId(), inRegion)
? aware.scanForAccount(accountId, inRegion)
: store.scan(inRegion);
return candidates.stream()
.filter(s -> secretArn.equals(s.getArn()))
@@ -1773,16 +1812,11 @@ public class SecretsManagerService implements ResourceProvider {
// ARN format: arn:<partition>:secretsmanager:<region>:<account>:secret:<name>
// Parsed rather than matched against a literal prefix, because the ARN carries the
// region's own partition and a pinned "arn:aws:" refused a GovCloud or China
// secret its own ARN. The name comes off the parsed resource without a second
// split: a secret name may itself contain colons.
// secret its own ARN.
AwsArnUtils.Arn parsedArn = parseOrNull(secretId);
String secretResourcePrefix = "secret:";
if (parsedArn != null
&& "secretsmanager".equals(parsedArn.service())
&& parsedArn.resource().startsWith(secretResourcePrefix)) {
String arnRegion = parsedArn.region();
String nameFromArn = parsedArn.resource().substring(secretResourcePrefix.length());
Secret byName = store.get(regionKey(arnRegion, nameFromArn)).orElse(null);
String nameFromArn = secretNameOrNull(parsedArn);
if (nameFromArn != null) {
Secret byName = store.get(regionKey(parsedArn.region(), nameFromArn)).orElse(null);
if (byName != null) {
return byName;
}
@@ -1834,6 +1868,19 @@ public class SecretsManagerService implements ResourceProvider {
}
}
/**
* The secret name a Secrets Manager secret ARN carries, taken off the parsed resource without a
* second split because a name may itself contain colons, or {@code null} for any other ARN.
*/
private static String secretNameOrNull(AwsArnUtils.Arn arn) {
String secretResourcePrefix = "secret:";
if (arn == null || !"secretsmanager".equals(arn.service())
|| !arn.resource().startsWith(secretResourcePrefix)) {
return null;
}
return arn.resource().substring(secretResourcePrefix.length());
}
private static String regionKey(String region, String name) {
return region + "::" + name;
}