fix(lambda): resolve layer version ARNs by account (#2948)

* fix(lambda): resolve layer version ARNs by account

Layer resolution dropped the ARN's account segment, so an ARN naming another
account resolved inside the caller's own partition. Two consequences, filed
separately as #2813 and #2809.

A foreign ARN whose layer name and version happened to match one of the
caller's own resolved to that layer: the function recorded an ARN it was not
using, and the launcher mounted different content at /opt than
GetFunctionConfiguration reported. resolveLayerByArn now scopes the lookup to
the caller's account and partition, so a foreign ARN is a miss.

A foreign ARN that matched nothing was rejected outright, which no AWS-managed
public layer can ever satisfy: Powertools, the AppConfig extension and
Datadog-published layers all live in AWS-owned accounts, and every Terraform
stack attaching one failed at CreateFunction. Validation now applies the
eager existence check only to the caller's own account and records a foreign
ARN unresolved, warning that its content is not mounted.

Measured against the live service in ap-southeast-1:

  CreateFunction, foreign ARN matching an own layer name/version
    -> AccessDeniedException, not a substitution
  CreateFunction, AWS-managed Powertools layer
    -> 201, ARN echoed verbatim on GetFunctionConfiguration
  CreateFunction, missing layer in the caller's own account
    -> InvalidParameterValueException: Layer version ... does not exist.

The own-account message and status are unchanged, matching that third case
exactly. The AccessDenied case is a known divergence: Floci implements no
layer permissions, so it cannot tell a public layer from one the caller has no
policy for, and accepts both.

Closes #2813
Closes #2809

* fix(lambda): reject foreign-partition layer ARNs when attaching them

GetLayerVersionByArn already answers a non-aws partition with
InvalidParameterValueException, but CreateFunction and
UpdateFunctionConfiguration accepted the same ARN and persisted it, so a
function could carry a layer Floci's own lookup path calls invalid.

Partitions are isolated, so no resource policy can ever make a cn or
gov layer readable from the aws partition. That is what separates it
from a foreign account, where a policy can, and where AWS-managed public
layers live. Foreign accounts stay accepted; foreign partitions now
reject with the message GetLayerVersionByArn already uses.

Measured on CreateFunction in ap-southeast-1: a cross-partition layer
ARN and a foreign-account layer ARN both fail with AccessDeniedException,
so the live service does not distinguish them at this call. The split
here is Floci's, on reachability grounds, and is documented as such.

* fix(lambda): gate foreign-account layer ARNs behind a config flag

Review asked for the accept-foreign-ARN behaviour to be opt-in rather than the
default, on both PRs' reasoning that it broadens what CreateFunction accepts
beyond AWS. It is now floci.services.lambda.accept-external-layer-arns, default
false, env FLOCI_SERVICES_LAMBDA_ACCEPT_EXTERNAL_LAYER_ARNS.

Off, a layer ARN naming another account is refused. On, it is recorded on the
function verbatim and its content is not mounted, which is what a stack
attaching Powertools or the AppConfig extension needs. A same-account ARN is
unaffected either way: a missing layer is still the eager
InvalidParameterValueException.

The refusal is AccessDeniedException, 403, not the InvalidParameterValueException
this branch previously used for the partition case. That was the second review
finding: the live probe in this PR measured CreateFunction returning
AccessDeniedException for a cross-partition ARN and the identical
AccessDeniedException for a same-partition foreign account, so reporting a
different error class than what was measured was an observable divergence
presented as parity. Both now report what AWS reported.

Cross-partition stays refused with the flag on. Partitions are isolated, so no
resource policy can make an aws-cn layer readable from aws, and
GetLayerVersionByArn already calls such an ARN invalid.

Negative control, with the default flipped to true and the tests kept:

  LambdaLayerArnAccountIntegrationTest
    foreignAccountArnIsDeniedByDefaultAndTheFunctionIsNotCreated
    updateFunctionConfigurationIsDeniedAndLeavesTheExistingLayerInPlace
  Tests run: 7, Failures: 2

Also moves the two generic ARN ownership checks this branch was adding into
AwsArnUtils as isForeignPartition and isForeignAccount, so the layer code keeps
only the layer-shaped part. Review noted RdsService.targetGroupBelongsTo already
carries its own copy and asked not to land a further one. The existing RDS, EKS
and EC2 copies are deliberately left alone; this only avoids adding another.
This commit is contained in:
Brian Taylor authored and GitHub committed 2026-09-13 00:19:37 -04:00
1 parent 27def6079d
commit 24a8c8440b
9 files changed
+631 -25

No files matched your search

+28 -7
View File
@@ -200,13 +200,34 @@ architecture. `Marker` is opaque and signed with a key generated at startup, so
edited or previous-run token is rejected with `InvalidParameterValueException` rather than
applied as a cursor. One divergence: a parameter sent with an empty value
(`?CompatibleRuntime=`) is treated as absent rather than rejected, because RESTEasy binds an
empty query value as null. `CreateFunction`/`UpdateFunctionConfiguration`
validate each `Layers` ARN eagerly against that storage, matching real AWS - an unresolvable ARN
is rejected with `InvalidParameterValueException`, not silently accepted. Only resolves layers
published into this same local Floci instance; a real AWS-owned layer ARN (e.g. the AWS AppConfig
Extension or a Datadog-published layer) can never resolve here, since there's no mechanism in
Floci for fetching real AWS content - publish your own copy of the layer's content locally under
a name you control and reference that ARN instead.
empty query value as null. Resolution honours the ARN's
account and partition: an ARN naming another account resolves to nothing rather than to a
same-named layer of the caller's own, matching the live service, which answers that case with
`AccessDeniedException` and never substitutes. `CreateFunction`/`UpdateFunctionConfiguration`
validate each `Layers` ARN in the caller's own account eagerly against that storage, matching
real AWS - an unresolvable one is rejected with `InvalidParameterValueException: Layer version
... does not exist.`, not silently accepted.
An ARN naming another account or another partition is answered on the live service by the layer's
resource policy: an AWS-managed public layer resolves, and everything else is
`AccessDeniedException`. Measured on `CreateFunction` in ap-southeast-1, a foreign-account ARN and
a cross-partition ARN return the same `AccessDeniedException`, so Floci returns that for both.
Floci implements no layer permissions and cannot fetch real AWS content, so it cannot tell a
public layer from a private one; refusing is the faithful default, being the answer AWS gives to
every foreign ARN except a public one.
Set `floci.services.lambda.accept-external-layer-arns: true`
(`FLOCI_SERVICES_LAMBDA_ACCEPT_EXTERNAL_LAYER_ARNS`) to record a same-partition foreign ARN on the
function instead of refusing it, which is what a stack attaching Powertools, the AppConfig
extension or a vendor-published layer needs. The trade is explicit: with it on, Floci also accepts
an ARN AWS would refuse with `AccessDeniedException`, so a typo or a private third-party layer
passes here and fails on deploy. The content is never mounted at `/opt` either way, and a warning
is logged at attach time and again at invoke; publish your own copy of the content locally under a
name you control if the handler needs it at runtime.
A layer ARN outside the `aws` partition is refused whatever that setting says. Partitions are
isolated, so no resource policy can ever make such a layer readable, and `GetLayerVersionByArn`
rejects one outright with `InvalidParameterValueException: Invalid layer version ...`.
## Not Implemented