mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 14:30:28 -05:00
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:
1 parent
27def6079d
commit
24a8c8440b
9 files changed
+631
-25
No files matched your search
@@ -2147,6 +2147,28 @@ public interface EmulatorConfig {
|
||||
*/
|
||||
Optional<List<String>> extraHosts();
|
||||
|
||||
/**
|
||||
* Accept a {@code Layers} ARN naming another account, recording it on the function
|
||||
* without mounting its content. Off by default, because it broadens what CreateFunction
|
||||
* and UpdateFunctionConfiguration accept beyond what AWS does.
|
||||
*
|
||||
* <p>On the live service a foreign-account layer resolves through its resource policy:
|
||||
* an AWS-managed public layer succeeds, and everything else is AccessDeniedException.
|
||||
* Floci implements no layer permissions, so it cannot tell those apart. The default
|
||||
* answers both the way AWS answers the second, which is the faithful choice for a
|
||||
* compatibility layer. Turn this on to attach public layers such as Powertools, the
|
||||
* AppConfig extension or a vendor-published layer, at the cost of also accepting an
|
||||
* ARN AWS would refuse. The content is never fetched either way, so a function whose
|
||||
* behaviour depends on the layer will not run correctly here.
|
||||
*
|
||||
* <p>A layer ARN outside the {@code aws} partition is rejected regardless: partitions
|
||||
* are isolated, so no resource policy can make one readable.
|
||||
*
|
||||
* Env var: FLOCI_SERVICES_LAMBDA_ACCEPT_EXTERNAL_LAYER_ARNS
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean acceptExternalLayerArns();
|
||||
|
||||
/**
|
||||
* Concurrent executions ceiling applied per region. AWS Lambda's
|
||||
* "account-level" concurrency is in fact a per-region quota (default 1000);
|
||||
|
||||
@@ -129,6 +129,36 @@ public final class AwsArnUtils {
|
||||
return isArn(value) && value.split(":", 6)[2].equals(service);
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the ARN names a partition other than {@code aws}, the only one Floci emulates.
|
||||
* An empty partition field is not foreign: callers that omit it are naming a local
|
||||
* resource.
|
||||
*
|
||||
* <p>Deliberately a literal {@code aws} rather than {@link #PARTITION_REGEX}. That constant
|
||||
* exists to recognise a legal ARN in any partition; this asks the opposite question, whether
|
||||
* the ARN names a partition this emulator can serve, and the answer is only ever the
|
||||
* commercial one.
|
||||
*/
|
||||
public static boolean isForeignPartition(Arn arn) {
|
||||
if (arn == null) {
|
||||
return false;
|
||||
}
|
||||
String partition = arn.partition();
|
||||
return partition != null && !partition.isEmpty() && !"aws".equals(partition);
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the ARN names an account other than {@code localAccountId}. An empty account
|
||||
* field is not foreign: several AWS ARN forms omit it for a resource the caller owns.
|
||||
*/
|
||||
public static boolean isForeignAccount(Arn arn, String localAccountId) {
|
||||
if (arn == null) {
|
||||
return false;
|
||||
}
|
||||
String account = arn.accountId();
|
||||
return account != null && !account.isEmpty() && !account.equals(localAccountId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an SQS ARN to a queue URL using the given base URL.
|
||||
* Example: arn:aws:sqs:us-east-1:000000000000:my-queue → http://localhost:4566/000000000000/my-queue
|
||||
|
||||
@@ -454,24 +454,15 @@ public class LambdaLayerService {
|
||||
+ " constraint: Member must satisfy regular expression pattern: "
|
||||
+ LAYER_VERSION_ARN_PATTERN, 400);
|
||||
}
|
||||
// resolveLayerByArn keys on region/name/version within the caller's own partition, so an
|
||||
// ARN naming another account would otherwise resolve to the caller's same-named layer.
|
||||
// No layer here can be shared cross-account (layer permissions are unimplemented), so a
|
||||
// foreign account is always a miss; this is where sharing would hook in if that changes.
|
||||
// resolveLayerByArn also drops the partition, so an ARN naming another one would
|
||||
// otherwise resolve to the local layer under a foreign-partition ARN. Floci emulates
|
||||
// the aws partition; the live service rejects the others outright.
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(layerVersionArn);
|
||||
if (parsed.partition() != null && !parsed.partition().isEmpty()
|
||||
&& !"aws".equals(parsed.partition())) {
|
||||
// Floci emulates the aws partition; the live service rejects the others outright, and
|
||||
// with a different error than an unresolvable ARN, so this precedes the lookup.
|
||||
if (AwsArnUtils.isForeignPartition(AwsArnUtils.parse(layerVersionArn))) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Invalid layer version " + layerVersionArn, 400);
|
||||
}
|
||||
String requestedAccount = AwsArnUtils.accountOrDefault(layerVersionArn, null);
|
||||
if (requestedAccount != null && !requestedAccount.equals(regionResolver.getAccountId())) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"The resource you requested does not exist.", 404);
|
||||
}
|
||||
// A foreign account is a miss rather than a substitution: resolveLayerByArn scopes the
|
||||
// lookup to the caller's own account. This is where cross-account sharing would hook in
|
||||
// if layer permissions were ever implemented.
|
||||
LambdaLayerVersion lv = resolveLayerByArn(layerVersionArn);
|
||||
if (lv == null) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
@@ -496,6 +487,9 @@ public class LambdaLayerService {
|
||||
if (resourceParts.length < 3 || !"layer".equals(resourceParts[0])) {
|
||||
return null;
|
||||
}
|
||||
if (isForeignLayerArn(arn)) {
|
||||
return null;
|
||||
}
|
||||
String region = arn.region();
|
||||
String layerName = resourceParts[1];
|
||||
long version;
|
||||
@@ -507,6 +501,52 @@ public class LambdaLayerService {
|
||||
return layerStore.get(region, layerName, version).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the ARN is a well-formed layer version ARN naming an account or partition other
|
||||
* than the caller's, so Floci cannot decide whether it exists. On the live service such a
|
||||
* layer resolves through its resource policy, measured against ap-southeast-1: an ARN in
|
||||
* another account whose name and version match a layer of the caller's own is
|
||||
* {@code AccessDeniedException}, never a substitution. Floci has no layer permission model
|
||||
* (see the Not Implemented list), so it treats every foreign ARN as unknowable rather than
|
||||
* resolving it inside the caller's own partition.
|
||||
*/
|
||||
public boolean isForeignLayerArn(String layerVersionArn) {
|
||||
AwsArnUtils.Arn arn = parseLayerVersionArn(layerVersionArn);
|
||||
if (arn == null) {
|
||||
return false;
|
||||
}
|
||||
return isForeignLayerArn(arn);
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the ARN is a well-formed layer version ARN outside the {@code aws} partition.
|
||||
* Partitions are isolated, so such a layer is unreachable rather than merely unreadable, and
|
||||
* {@link #getLayerVersionByArn} already rejects it outright. Attaching one to a function has
|
||||
* to reject too, or Floci would persist an ARN its own lookup path calls invalid.
|
||||
*/
|
||||
public boolean isForeignPartitionLayerArn(String layerVersionArn) {
|
||||
return AwsArnUtils.isForeignPartition(parseLayerVersionArn(layerVersionArn));
|
||||
}
|
||||
|
||||
private AwsArnUtils.Arn parseLayerVersionArn(String layerVersionArn) {
|
||||
AwsArnUtils.Arn arn;
|
||||
try {
|
||||
arn = AwsArnUtils.parse(layerVersionArn);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return null;
|
||||
}
|
||||
String[] resourceParts = arn.resource().split(":");
|
||||
if (resourceParts.length < 3 || !"layer".equals(resourceParts[0])) {
|
||||
return null;
|
||||
}
|
||||
return arn;
|
||||
}
|
||||
|
||||
private boolean isForeignLayerArn(AwsArnUtils.Arn arn) {
|
||||
return AwsArnUtils.isForeignPartition(arn)
|
||||
|| AwsArnUtils.isForeignAccount(arn, regionResolver.getAccountId());
|
||||
}
|
||||
|
||||
private byte[] resolveLayerContent(Map<String, Object> content) {
|
||||
String zipFileBase64 = (String) content.get("ZipFile");
|
||||
if (zipFileBase64 != null) {
|
||||
|
||||
@@ -234,13 +234,42 @@ public class LambdaService implements ResourceProvider {
|
||||
// content mounted, which is correct AWS-parity behavior for a layer deleted *after* being
|
||||
// attached (AWS doesn't re-validate on every invoke either), but was previously the only
|
||||
// signal at all for a bad ARN, even a typo caught at attach time on real AWS.
|
||||
//
|
||||
// An ARN naming another account or another partition is answered on the live service by the
|
||||
// layer's resource policy: a 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 rather than inventing a distinction the API does not make.
|
||||
//
|
||||
// Floci implements no layer permissions, so it cannot tell a public layer from a private one
|
||||
// and cannot fetch either one's content. Refusing is the faithful default: it is the answer
|
||||
// AWS gives to every foreign ARN except a public layer. floci.services.lambda
|
||||
// .accept-external-layer-arns records a same-partition foreign ARN unresolved instead, which
|
||||
// is what a stack attaching Powertools or the AppConfig extension needs. Cross-partition
|
||||
// stays refused even then, because partitions are isolated and no policy can reach across
|
||||
// one, and because GetLayerVersionByArn already calls such an ARN invalid.
|
||||
private void validateLayersResolvable(List<String> layerArns) {
|
||||
if (layerArns == null || layerService == null) return;
|
||||
for (String arn : layerArns) {
|
||||
if (layerService.resolveLayerByArn(arn) == null) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Layer version " + arn + " does not exist.", 400);
|
||||
if (layerService.resolveLayerByArn(arn) != null) {
|
||||
continue;
|
||||
}
|
||||
if (layerService.isForeignLayerArn(arn)) {
|
||||
boolean acceptable = !layerService.isForeignPartitionLayerArn(arn)
|
||||
&& config != null
|
||||
&& config.services().lambda().acceptExternalLayerArns();
|
||||
if (!acceptable) {
|
||||
throw new AwsException("AccessDeniedException",
|
||||
"User is not authorized to perform: lambda:GetLayerVersion on resource: "
|
||||
+ arn + " because no resource-based policy allows the"
|
||||
+ " lambda:GetLayerVersion action", 403);
|
||||
}
|
||||
LOG.warnv("Layer {0} belongs to another account; recorded on the function but its"
|
||||
+ " content will not be mounted at /opt", arn);
|
||||
continue;
|
||||
}
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Layer version " + arn + " does not exist.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user