diff --git a/docs/services/iam.md b/docs/services/iam.md
index 9e9221cd78..d96f10e2d3 100644
--- a/docs/services/iam.md
+++ b/docs/services/iam.md
@@ -989,6 +989,7 @@ When `FLOCI_SERVICES_IAM_ENFORCEMENT_ENABLED` is active, Floci also queries regi
- An explicit **Deny** in a resource policy overrides any allows.
- In cross-account scenarios or resource-controlled access, an explicit **Allow** in a resource policy grants access to the principal.
- For detailed S3 bucket policy behavior and configuration, see [S3 Bucket Policy Enforcement](s3.md#bucket-policy-enforcement).
+- Lambda function policies, the statements `AddPermission` adds, take part the same way. An invoke of another account's function by ARN runs it in that account, so its function policy has to allow the caller as well; see [Function policies under IAM enforcement](lambda.md#function-policies-under-iam-enforcement).
### Service control policies (SCPs)
@@ -1201,9 +1202,8 @@ being bounded by SCPs (below): both forms of root enforcement now agree. A negat
absent key cannot equal what the policy names, so the condition holds, and a `Deny` written
that way applies when the key is missing.
-**Not yet supported**: `NotPrincipal`, resource-based policies (S3 bucket policy, Lambda resource
-policy), and `dynamodb:LeadingKeys` for `Scan`, `TransactWriteItems` / `TransactGetItems` and the
-PartiQL operations.
+**Not yet supported**: `dynamodb:LeadingKeys` for `Scan`, `TransactWriteItems` / `TransactGetItems`
+and the PartiQL operations.
### Assumed roles
diff --git a/docs/services/lambda.md b/docs/services/lambda.md
index 43c088e4d5..893c55fe7d 100644
--- a/docs/services/lambda.md
+++ b/docs/services/lambda.md
@@ -73,6 +73,29 @@ The event invoke configuration is stored and returned as AWS does, and `AWS::Lam
provisions it from a stack. Asynchronous invocations apply its retry, event age, destination settings,
and dead-letter queue configurations (`DeadLetterConfig`).
+### Function policies under IAM enforcement
+
+With [IAM enforcement](iam.md#resource-based-policies) on, the statements `AddPermission` adds take
+part in authorizing calls to the function:
+
+- An invoke of a function named by a full or partial ARN runs the function in the account in the
+ ARN. An invoke of another account's function needs its function policy to allow the caller as
+ well as the caller's own identity policy.
+- `GetFunction`, `GetFunctionConfiguration` and `ListDurableExecutionsByFunction`, given a full ARN
+ with a version or alias, read the function in the account in the ARN, and need its function
+ policy to allow the caller in the same way.
+- Other operations act on the caller's own function of the name the ARN gives, whatever account
+ the ARN names, and are authorized against that function. Floci does not serve them on another
+ account's function, which AWS allows through a function policy.
+- Within one account, a statement naming the caller grants on its own.
+- An invoke is authorized against the version or alias it names, in the function name or in
+ `Qualifier`. A grant added with `Qualifier` covers that version or alias only, and a grant on the
+ function does not cover a qualified invoke. Other operations are authorized against the
+ unqualified function.
+- A statement conditioned on `InvokedViaFunctionUrl` does not admit a direct `Invoke`.
+- `aws:PrincipalOrgID` is not part of the request context, so a grant added with `PrincipalOrgID`
+ admits no caller.
+
### Reserved Concurrency
Unqualified `GetFunction` reads include `Concurrency.ReservedConcurrentExecutions` when a reservation
diff --git a/docs/services/secrets-manager.md b/docs/services/secrets-manager.md
index 42bc96724d..9382fd6d82 100644
--- a/docs/services/secrets-manager.md
+++ b/docs/services/secrets-manager.md
@@ -140,10 +140,14 @@ aws secretsmanager validate-resource-policy \
- **`RotateSecret` also needs `lambda:InvokeFunction` under IAM enforcement.** As on AWS, the caller
must be allowed to invoke the rotation function as well as to call `RotateSecret`. A caller that
is not gets `AccessDeniedException` with HTTP 400, as Secrets Manager reports it, and the secret is
- left as it was. Rotations started by the background sweep have no caller and are not checked.
+ left as it was. A function in another account is checked as any cross-account call is: its
+ function policy must allow the caller too. Rotations started by the background sweep have no
+ caller and are not checked.
- **A rotation function that does not exist** is reported as AWS reports it: `AccessDeniedException`
- (HTTP 400), "Secrets Manager cannot invoke the specified Lambda function". The secret's rotation
- settings are left as they were, including any rotation function configured before.
+ (HTTP 400), "Secrets Manager cannot invoke the specified Lambda function". The function is looked
+ up where the rotation runs it, in the account its ARN names and as the version or alias the ARN
+ names, so a missing alias is reported the same way. The secret's rotation settings are left as
+ they were, including any rotation function configured before.
- **A rotation function reference Lambda refuses**, one it cannot parse or a full ARN in another
Region, gets `InvalidParameterException` (HTTP 400), the error `RotateSecret` declares for an
invalid parameter, with Lambda's reason in the message. The secret is left as it was. AWS does not
diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java
index 1322b80a17..7e7e579af3 100644
--- a/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java
+++ b/src/main/java/io/github/hectorvent/floci/services/iam/ResourceArnBuilder.java
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.iam;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
+import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.AwsQueryServiceResolver;
import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.ServicePrincipals;
@@ -78,7 +79,7 @@ public class ResourceArnBuilder {
String path = ctx.getUriInfo().getPath();
return switch (credentialScope) {
case "s3" -> List.of(buildS3Arn(path, region));
- case "lambda" -> List.of(buildLambdaArn(path, region, accountId));
+ case "lambda" -> List.of(buildLambdaArn(path, ctx, region, accountId));
case "sqs" -> List.of(buildSqsArn(ctx, region, accountId));
case "sns" -> List.of(buildSnsArn(ctx, region, accountId));
case "dynamodb" -> buildDynamoDbArns(ctx, region, accountId);
@@ -541,7 +542,19 @@ public class ResourceArnBuilder {
}
// ── Lambda ──────────────────────────────────────────────────────────────────
- private String buildLambdaArn(String path, String region, String accountId) {
+
+ /**
+ * The function a Lambda request acts on, as an IAM policy names it. An invoke runs the function
+ * in the account and Region its full or partial ARN names, so it is evaluated against that
+ * function, and against the version or alias it names in the function name or in
+ * {@code Qualifier}, since Lambda authorizes a qualified call against the qualified ARN (Lambda
+ * Developer Guide, "Referencing functions in the Resource section of policies"). The reads
+ * {@link #readsInTheAccountItNames} names act in the account a qualified ARN names too. Every
+ * other route acts on the caller's own function of that name, whatever account an ARN names.
+ * Those are evaluated unqualified, because not every one of them acts on the qualifier it is
+ * given.
+ */
+ private String buildLambdaArn(String path, ContainerRequestContext ctx, String region, String accountId) {
String executionArn = durableExecutionArn(path);
if (executionArn == null) {
executionArn = durableCallbackExecutionArn(path);
@@ -552,10 +565,49 @@ public class ResourceArnBuilder {
// path: /2015-03-31/functions/name or similar
String name = extractSegmentAfter(path, "functions");
if (name == null) return "*";
- // strip qualifier if present
- int colon = name.indexOf(':');
- if (colon > 0) name = name.substring(0, colon);
- return AwsArnUtils.Arn.of("lambda", region, accountId, "function:" + name).toString();
+ boolean invoke = path.endsWith("/functions/" + name + "/invocations");
+ try {
+ if (!invoke) {
+ LambdaArnUtils.ResolvedFunctionRef ref = LambdaArnUtils.resolve(name);
+ boolean namedAccount = readsInTheAccountItNames(path, name, ref, ctx);
+ return AwsArnUtils.Arn.of("lambda",
+ namedAccount ? ref.region() : region,
+ namedAccount ? ref.account() : accountId,
+ "function:" + ref.name()).toString();
+ }
+ LambdaArnUtils.ResolvedFunctionRef ref = LambdaArnUtils.resolveWithQualifier(
+ name, ctx.getUriInfo().getQueryParameters().getFirst("Qualifier"));
+ String resource = ref.qualifier() != null
+ ? "function:" + ref.name() + ":" + ref.qualifier()
+ : "function:" + ref.name();
+ return AwsArnUtils.Arn.of("lambda",
+ ref.region() != null ? ref.region() : region,
+ ref.account() != null ? ref.account() : accountId,
+ resource).toString();
+ } catch (AwsException e) {
+ // A reference Lambda refuses, which its handler answers with a 400 before acting on anything.
+ return AwsArnUtils.Arn.of("lambda", region, accountId, "function:" + name).toString();
+ }
+ }
+
+ /**
+ * {@code GetFunction}, {@code GetFunctionConfiguration} and {@code ListDurableExecutionsByFunction}
+ * read a function named by a qualified full ARN in the account the ARN names, where the version or
+ * alias is resolved ({@code LambdaService.getFunction(region, name, qualifier)}). Unqualified, or
+ * by partial ARN, they read the caller's own function of that name, as every other route acts.
+ */
+ private static boolean readsInTheAccountItNames(String path, String name,
+ LambdaArnUtils.ResolvedFunctionRef ref,
+ ContainerRequestContext ctx) {
+ if (!"GET".equals(ctx.getMethod()) || !name.startsWith("arn:")) {
+ return false;
+ }
+ String route = path.substring(path.indexOf("/functions/" + name) + "/functions/".length() + name.length());
+ if (!route.isEmpty() && !route.equals("/configuration") && !route.equals("/durable-executions")) {
+ return false;
+ }
+ String qualifier = ctx.getUriInfo().getQueryParameters().getFirst("Qualifier");
+ return ref.qualifier() != null || (qualifier != null && !qualifier.isBlank());
}
/** Durable execution actions are authorized against the execution ARN, which carries the version. */
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaResourcePolicyProvider.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaResourcePolicyProvider.java
new file mode 100644
index 0000000000..911a489464
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaResourcePolicyProvider.java
@@ -0,0 +1,81 @@
+package io.github.hectorvent.floci.services.lambda;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.core.common.AwsArnUtils;
+import io.github.hectorvent.floci.services.iam.ResourcePolicyProvider;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Supplies Lambda function policies to the IAM policy evaluation pipeline, together with the account
+ * that owns the function.
+ *
+ *
The owner is the account in the function ARN, and it is returned even when the function has no
+ * policy or does not exist: a call to another account's function is then judged as cross-account,
+ * which needs the function policy to allow the caller as well as the caller's own policy (Lambda
+ * Developer Guide, function URLs: "If the principal making the request is in a different account,
+ * then the principal must have both an identity-based policy ... and permissions granted to them in
+ * a resource-based policy").
+ *
+ *
All of a function's statements are returned, its own and those of its versions and aliases. Each
+ * statement's {@code Resource} names the function, version or alias it was added to, so matching it
+ * against the ARN the call names is what scopes it: a grant on an alias covers calls to that alias
+ * only, and one on the function does not cover a qualified call (Lambda Developer Guide, "Referencing
+ * functions in the Resource section of policies").
+ *
+ *
Keyed on the ARN rather than the request's credential scope, because a function is also the
+ * second resource of calls signed for another service: {@code RotateSecret} checks
+ * {@code lambda:InvokeFunction} on its rotation function under the {@code secretsmanager} scope.
+ */
+@ApplicationScoped
+public class LambdaResourcePolicyProvider implements ResourcePolicyProvider {
+
+ private static final Pattern ACCOUNT_ID = Pattern.compile("\\d{12}");
+ /** A function, version or alias, or a durable execution under one: the name is group 1. */
+ private static final Pattern FUNCTION_RESOURCE = Pattern.compile("function:([^:/]+)(?:[:/].*)?");
+
+ private final LambdaService lambdaService;
+ private final ObjectMapper objectMapper;
+
+ @Inject
+ public LambdaResourcePolicyProvider(LambdaService lambdaService, ObjectMapper objectMapper) {
+ this.lambdaService = lambdaService;
+ this.objectMapper = objectMapper;
+ }
+
+ @Override
+ public List getResourcePolicies(String credentialScope, String resourceArn) {
+ if (!AwsArnUtils.isArnFor(resourceArn, "lambda")) {
+ return List.of();
+ }
+ AwsArnUtils.Arn arn = AwsArnUtils.parse(resourceArn);
+ Matcher function = FUNCTION_RESOURCE.matcher(arn.resource());
+ if (!function.matches() || !ACCOUNT_ID.matcher(arn.accountId()).matches()) {
+ return List.of();
+ }
+ List