mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
refactor(secretsmanager): build the rotation function ARN with Lambda's own helper (#4709)
Follow-ups from the review of #4697. rotationFunctionArn built the partial-ARN case by string concatenation while the bare-name case went through AwsArnUtils.Arn. Turning a FunctionName reference into a full function ARN is Lambda's knowledge, so it now lives in LambdaArnUtils.functionArn, next to eventSourceMappingArn: the reference is read with LambdaArnUtils.resolve, the parser Lambda itself uses, and every case is minted through AwsArnUtils.Arn from the parts. Secrets Manager calls it with the secret's partition and account, and keeps no Lambda layout of its own. ResolvedFunctionRef also carries the account a full or partial ARN names (null for a bare name), which the helper needs. A reference Lambda cannot read, such as a Region-prefixed partial ARN, is now refused with Lambda's InvalidParameterValueException before the permission check, instead of being minted with the Region twice and failing later at the function lookup. The missing-function message names its service principal through ServicePrincipals.of, as AGENTS.md now asks, in place of the literal marked partition-invariant in #4697. The comment on the caller == null branch of IamEnforcementFilter.authorizeAdditionalResource said the path only served a presigned POST whose signature was already verified. It now also serves CopyObject, RotateSecret and SSM GetParameter, and even for the presigned POST the signature check runs after it, and only when S3 auth enforcement is on. The comment now says what actually makes an unknown key safe on each path.
This commit is contained in:
1 parent
e69ee61d40
commit
f752946acf
5 files changed
+110
-52
No files matched your search
@@ -704,9 +704,13 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
boolean accountRootPrincipal = false;
|
||||
CallerContext caller = iamService.resolveCallerContext(akid);
|
||||
if (caller == null) {
|
||||
// No unknown-key rejection here, unlike filter(): this path runs only for a
|
||||
// presigned POST, whose form signature S3PostPolicySigner has already verified
|
||||
// against the key's secret, so an unknown key never reaches it.
|
||||
// No unknown-key rejection here, unlike filter(), and none is needed. For a second
|
||||
// resource of a request filter() has seen (CopyObject's source, RotateSecret's
|
||||
// rotation function, the secrets behind SSM GetParameter) the credential is the one
|
||||
// filter() evaluated, and filter() refuses an unknown key before the handler runs. A
|
||||
// presigned POST carries its credential only in the form body, which filter() never
|
||||
// sees: an unknown key there is refused by S3's presigned-POST signature check when
|
||||
// S3 auth enforcement is on, and otherwise uploads as an unsigned request would.
|
||||
if (scpLevels == null || !akid.equals(accountId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -47,6 +47,25 @@ public final class LambdaArnUtils {
|
||||
return arn.resource().substring(EVENT_SOURCE_MAPPING_PREFIX.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* The full function ARN a {@code FunctionName} reference names, the way an IAM policy's
|
||||
* {@code Resource} names it. The reference is read as {@link #resolve} reads it, so a malformed
|
||||
* one is refused here. A full ARN comes back as given; a name or partial ARN is completed with
|
||||
* {@code partition} and {@code region}, and with {@code defaultAccount} unless it names its own
|
||||
* account. A qualifier is kept, since Lambda authorizes a qualified call against the qualified ARN.
|
||||
*/
|
||||
public static String functionArn(String reference, String partition, String region, String defaultAccount) {
|
||||
ResolvedFunctionRef ref = resolve(reference);
|
||||
if (reference.startsWith("arn:")) {
|
||||
return reference;
|
||||
}
|
||||
String resource = ref.qualifier() == null
|
||||
? "function:" + ref.name()
|
||||
: "function:" + ref.name() + ":" + ref.qualifier();
|
||||
String account = ref.account() != null ? ref.account() : defaultAccount;
|
||||
return new AwsArnUtils.Arn(partition, "lambda", region, account, resource).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolved components of a Lambda function reference.
|
||||
*
|
||||
@@ -54,8 +73,9 @@ public final class LambdaArnUtils {
|
||||
* @param qualifier version or alias, or null if absent
|
||||
* @param region region extracted from a full ARN, or null for bare
|
||||
* name / partial ARN inputs
|
||||
* @param account account named by a full or partial ARN, or null for a bare name
|
||||
*/
|
||||
public record ResolvedFunctionRef(String name, String qualifier, String region) {}
|
||||
public record ResolvedFunctionRef(String name, String qualifier, String region, String account) {}
|
||||
|
||||
/**
|
||||
* Parses a {@code FunctionName} path parameter. Throws {@link AwsException}, HTTP 400,
|
||||
@@ -98,7 +118,7 @@ public final class LambdaArnUtils {
|
||||
if (effective != null && !QUALIFIER_PATTERN.matcher(effective).matches()) {
|
||||
throw invalid("Invalid qualifier: " + effective);
|
||||
}
|
||||
return new ResolvedFunctionRef(ref.name(), effective, ref.region());
|
||||
return new ResolvedFunctionRef(ref.name(), effective, ref.region(), ref.account());
|
||||
}
|
||||
|
||||
private static ResolvedFunctionRef parseFullArn(String input) {
|
||||
@@ -133,7 +153,7 @@ public final class LambdaArnUtils {
|
||||
if (qualifier != null) {
|
||||
validateQualifier(qualifier);
|
||||
}
|
||||
return new ResolvedFunctionRef(name, qualifier, base.region());
|
||||
return new ResolvedFunctionRef(name, qualifier, base.region(), base.accountId());
|
||||
}
|
||||
|
||||
private static ResolvedFunctionRef parsePartialArn(String input) {
|
||||
@@ -155,7 +175,7 @@ public final class LambdaArnUtils {
|
||||
if (qualifier != null) {
|
||||
validateQualifier(qualifier);
|
||||
}
|
||||
return new ResolvedFunctionRef(name, qualifier, null);
|
||||
return new ResolvedFunctionRef(name, qualifier, null, account);
|
||||
}
|
||||
|
||||
private static ResolvedFunctionRef parseNameWithOptionalQualifier(String input) {
|
||||
@@ -170,7 +190,7 @@ public final class LambdaArnUtils {
|
||||
if (qualifier != null) {
|
||||
validateQualifier(qualifier);
|
||||
}
|
||||
return new ResolvedFunctionRef(name, qualifier, null);
|
||||
return new ResolvedFunctionRef(name, qualifier, null, null);
|
||||
}
|
||||
|
||||
private static void validateNamePattern(String name) {
|
||||
|
||||
+20
-31
@@ -1,23 +1,28 @@
|
||||
package io.github.hectorvent.floci.services.secretsmanager;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.ServicePrincipals;
|
||||
import io.github.hectorvent.floci.core.resource.ExplorerResource;
|
||||
import io.github.hectorvent.floci.core.resource.ResourceProvider;
|
||||
import io.github.hectorvent.floci.core.resource.SupportedResourceType;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.kms.KmsService;
|
||||
import io.github.hectorvent.floci.services.kms.model.KmsKey;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaArnUtils;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import io.github.hectorvent.floci.services.scheduler.SchedulerExpressionParser;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -28,22 +33,19 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Predicate;
|
||||
import io.github.hectorvent.floci.core.resource.ExplorerResource;
|
||||
import io.github.hectorvent.floci.core.resource.ResourceProvider;
|
||||
import io.github.hectorvent.floci.core.resource.SupportedResourceType;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ApplicationScoped
|
||||
@@ -1137,7 +1139,10 @@ public class SecretsManagerService implements ResourceProvider {
|
||||
// One full ARN for the permission check, the lookup and the invoke, so all three reach the same
|
||||
// function: a name or partial ARN would otherwise be invoked in whatever account the rotation
|
||||
// thread resolves, which is not necessarily the one that was checked.
|
||||
String functionArn = serviceManaged ? null : rotationFunctionArn(finalLambdaArn, secret, region);
|
||||
AwsArnUtils.Arn secretArn = AwsArnUtils.parse(secret.getArn());
|
||||
String functionArn = serviceManaged
|
||||
? null
|
||||
: LambdaArnUtils.functionArn(finalLambdaArn, secretArn.partition(), region, secretArn.accountId());
|
||||
if (!serviceManaged) {
|
||||
authorizeInvoke.accept(functionArn);
|
||||
}
|
||||
@@ -1151,7 +1156,8 @@ public class SecretsManagerService implements ResourceProvider {
|
||||
if (e.getHttpStatus() == 404) {
|
||||
throw new AwsException("AccessDeniedException",
|
||||
"Secrets Manager cannot invoke the specified Lambda function. Ensure that the function "
|
||||
+ "policy grants access to the principal secretsmanager.amazonaws.com.", 400); // partition-literal: AWS's error text verbatim; names the service principal, which the Secrets Manager guide gives with no partition variant
|
||||
+ "policy grants access to the principal " + ServicePrincipals.of("secretsmanager")
|
||||
+ ".", 400);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
@@ -1209,23 +1215,6 @@ public class SecretsManagerService implements ResourceProvider {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The rotation function as the full ARN an IAM policy's {@code Resource} names. A function name
|
||||
* or partial ARN resolves in the secret's own account and the request's Region, as Lambda
|
||||
* resolves one.
|
||||
*/
|
||||
static String rotationFunctionArn(String functionRef, Secret secret, String region) {
|
||||
if (functionRef.startsWith("arn:")) {
|
||||
return functionRef;
|
||||
}
|
||||
AwsArnUtils.Arn secretArn = AwsArnUtils.parse(secret.getArn());
|
||||
if (functionRef.contains(":function:")) {
|
||||
return "arn:" + secretArn.partition() + ":lambda:" + region + ":" + functionRef;
|
||||
}
|
||||
return new AwsArnUtils.Arn(secretArn.partition(), "lambda", region, secretArn.accountId(),
|
||||
"function:" + functionRef).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* The outcome of {@link #cancelRotateSecret}: the secret with rotation turned off, plus the id
|
||||
* of the version a cancelled rotation left behind, or {@code null} when no rotation was in
|
||||
|
||||
Reference in new issue
Block a user