Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(cloudfront): sign requests to a Lambda Web Function origin under OAC (#6037)

TATSUNO “Taz” Yasuhiro8bff1ba0f337
diff --git a/docs/services/cloudfront.md b/docs/services/cloudfront.md
index 6b84ea7ce8..f0ac4f9e56 100644
--- a/docs/services/cloudfront.md
+++ b/docs/services/cloudfront.md
@@ -251,8 +251,22 @@ PUT, PATCH and DELETE).
served by Floci itself, at the address the viewer reached. The origin names the endpoint's
`domainName` with or without its port, and its protocol and ports do not matter, so neither DNS nor
the private address check applies. A forwarded viewer `Host` is a 502, as on AWS, where the endpoint's
- certificate does not name it. Origin access control does not sign these requests yet, so an
- `IamAuth` endpoint answers them with its 403 page.
+ certificate does not name it. Under an `always-amz-auth` origin access control, Floci signs these
+ requests for the distribution. An `IamAuth` endpoint then lets them through, or with IAM enforcement on
+ evaluates the function's resource policy for `cloudfront.amazonaws.com`, with the distribution as
+ `aws:SourceArn` and its account as `aws:SourceAccount`. Without one, an `IamAuth` endpoint answers
+ with its 403 page. CloudFront forwards the viewer's `Authorization` on GET and HEAD only when the
+ cache policy includes it. A forwarded `Authorization` reaches the function unless it is an AWS
+ signature.
+- Origin access controls and distributions follow the AWS checks for Lambda origins. CloudFront tells
+ them apart by a hostname under `on.aws` with its fixed labels in lowercase, and Floci also by one
+ under its own host. A Web Function endpoint needs a `lambda` origin access control with
+ `always-amz-auth`, signed with `sigv4a` for a `global` endpoint and `sigv4` otherwise, and
+ `always-amz-auth` is for Web Function origins only. A Lambda@Edge origin trigger is refused in
+ front of an origin signed with `always-amz-auth` or `sigv4a`. The `execute-api` origin type is
+ refused with `AccessDenied`, as AWS refuses it. A distribution has to name an origin access control
+ that exists, and a control in use keeps its origin type, its signing protocol and whether it is
+ `always-amz-auth`.
- Response headers policies validate the AWS configuration shape and are applied after the origin
response, including CORS preflight fields, origin override behavior, custom headers, security
headers, allowed header removals, and sampled `Server-Timing` metrics. `Pragma: server-timing`
diff --git a/docs/services/lambda-web-functions.md b/docs/services/lambda-web-functions.md
index 71b6500930..ffa07bf8ac 100644
--- a/docs/services/lambda-web-functions.md
+++ b/docs/services/lambda-web-functions.md
@@ -41,7 +41,7 @@ The `Lambda Web` service model (apiVersion 2025-03-07, `aws lambda-web` in the A
- **ARNs** given as identifiers must name the request's region and the caller's account, as on AWS. A revision or endpoint ARN resolves by its id or name only.
- **Page tokens** are base64 cursors that carry the request and the position of the page, not the UUIDs AWS issues. A token reused with other parameters gets the AWS "does not match the request" error, and any other token the "invalid or has expired" error.
- **Domain names** take the form `<id>.001.lambda-web.<region>.<floci host>` and `<id>.lambda-web.global.<floci host>`, with the host and port of `floci.base-url`. A `PerRegion` endpoint gets one domain per region in `regionalEndpoints`.
-- **Not implemented yet:** SigV4a on `MultiRegion` endpoints, the check that a policy's principal exists, the automatic deletion of revisions past 50, the metadata API, CloudFront origin access control, and serving a `MultiRegion` or `PerRegion` endpoint in its other regions.
+- **Not implemented yet:** SigV4a on `MultiRegion` endpoints, the check that a policy's principal exists, the automatic deletion of revisions past 50, the metadata API, and serving a `MultiRegion` or `PerRegion` endpoint in its other regions.
### `aws lambda-web deploy`
@@ -64,7 +64,7 @@ curl -H "Host: <domainName>" http://localhost:4566/
The `Host` must match the domain exactly, as on AWS: another case or another port is `404` with `lambda-web-error-type: Platform.InvalidEndpoint`.
-A CloudFront distribution of this Floci can use the endpoint as a custom origin, named by its `domainName` without the port as on AWS. See [CloudFront](cloudfront.md).
+A CloudFront distribution of this Floci can use the endpoint as a custom origin, named by its `domainName` without the port as on AWS. Under an `always-amz-auth` origin access control, CloudFront signs the requests to an `IamAuth` endpoint, and the function's resource policy has to allow `cloudfront.amazonaws.com` when IAM enforcement is on. See [CloudFront](cloudfront.md).
An execution environment is a `public.ecr.aws/lambda/nodejs:24` container that a request starts when every environment of the endpoint is full. The code is the revision's S3 object unpacked into `/var/task`. Node starts `/var/runtime/index.js` with the flags AWS uses, as on AWS, and that runtime imports `index.js` or the file `AWS_LAMBDA_NODEJS_ENTRYPOINT` names. So `require.main` is not the app's module, as on AWS. The app sees `AWS_LAMBDA_HTTP_ENDPOINT=0.0.0.0:3000`, `AWS_LAMBDA_INITIALIZATION_TYPE=native-http`, `AWS_EXECUTION_ENV`, `AWS_LAMBDA_FUNCTION_MEMORY_SIZE=2048`, `AWS_LAMBDA_LOG_LEVEL`, `AWS_ACCOUNT_ID`, `AWS_REGION`, credentials and `AWS_ENDPOINT_URL` for Floci, `NODE_PATH`, and its own variables. The server has 9 seconds to listen. An environment that sees no request for `floci.services.lambda.container-idle-timeout-seconds` is stopped with `SIGTERM` and one second, as on scale to zero. Node runs under Docker's init, so a server without a `SIGTERM` handler stops at once, as on AWS. A process that exits between requests is replaced by a fresh environment on the next request.
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java
index 7619a6bd18..d36699e6c1 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.config.LocalStackParity;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.CloudFrontEdgeDomain;
import io.github.hectorvent.floci.core.common.RegionResolver;
+import io.github.hectorvent.floci.core.common.SmithyValidationErrors;
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
@@ -34,6 +35,7 @@ import io.github.hectorvent.floci.services.iam.ServerCertificateReferenceProvide
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
+import java.net.URI;
import java.security.SecureRandom;
import java.time.Instant;
import java.util.ArrayList;
@@ -51,6 +53,7 @@ import java.util.Set;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ApplicationScoped
@@ -60,10 +63,12 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
private static final int MAX_CUSTOM_RESPONSE_HEADERS_POLICIES = 20;
private static final int MAX_DISTRIBUTIONS_PER_RESPONSE_HEADERS_POLICY = 100;
private static final SecureRandom RANDOM = new SecureRandom();
- private static final Set<String> OAC_ORIGIN_TYPES =
- Set.of("s3", "mediastore", "mediapackagev2", "lambda");
- private static final Set<String> OAC_SIGNING_BEHAVIORS =
- Set.of("always", "never", "no-override");
+ /** The enum values in the order AWS lists them. */
+ private static final List<String> OAC_ORIGIN_TYPES =
+ List.of("s3", "execute-api", "lambda", "mediastore", "mediapackagev2");
+ private static final List<String> OAC_SIGNING_PROTOCOLS = List.of("sigv4", "sigv4a");
+ private static final List<String> OAC_SIGNING_BEHAVIORS =
+ List.of("always-amz-auth", "never", "always", "no-override");
private static final Set<String> FUNCTION_RUNTIMES = Set.of("cloudfront-js-1.0", "cloudfront-js-2.0");
private static final int MAX_ORIGIN_CUSTOM_HEADERS = 30;
private static final int MAX_CUSTOM_HEADER_NAME_LENGTH = 256;
@@ -139,6 +144,7 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
* certificate, so a signed URL for either can be downloaded over HTTPS.
*/
private final List<String> localDeliverySuffixes;
+ private final Pattern lambdaOriginHost;
public CloudFrontService(StorageFactory factory, EmulatorConfig config, RegionResolver regionResolver) {
this(factory, config, regionResolver, null);
@@ -186,6 +192,7 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
this.regionResolver = regionResolver;
this.configuredDomainSuffix = config.services().cloudfront().domainSuffix().orElse(null);
this.localDeliverySuffixes = localDeliverySuffixes(config);
+ this.lambdaOriginHost = lambdaOriginHost(config);
}
// ── Distributions ─────────────────────────────────────────────────────────
@@ -195,6 +202,7 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
validateOriginCustomHeaders(dist.getConfig());
validateResponseHeadersPolicyReferences(dist.getConfig(), null);
validateTrustedKeyGroups(dist.getConfig());
+ validateOriginAccessControls(dist.getConfig());
return whileCertificatesHeld(dist.getConfig(), () -> {
validateViewerCertificate(dist.getConfig());
String id = generateDistributionId();
@@ -228,6 +236,7 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
validateOriginCustomHeaders(updated.getConfig());
validateResponseHeadersPolicyReferences(updated.getConfig(), id);
validateTrustedKeyGroups(updated.getConfig());
+ validateOriginAccessControls(updated.getConfig());
return whileCertificatesHeld(updated.getConfig(), () -> {
validateViewerCertificate(updated.getConfig());
updated.setId(id);
@@ -1132,6 +1141,9 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
"The If-Match version is missing or not valid for the resource.", 400);
}
validateOriginAccessControl(updated);
+ if (changesHowItSigns(existing, updated) && originAccessControlInUse(id)) {
+ throw new AwsException("IllegalUpdate", "Cannot update origin type when associated to origins.", 400);
+ }
updated.setId(id);
updated.setEtag(UUID.randomUUID().toString());
updated.setLastModifiedTime(Instant.now());
@@ -1222,29 +1234,166 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
throw new AwsException("InvalidArgument",
"The origin access control configuration is required.", 400);
}
- if (oac.getName() == null || oac.getName().isBlank()) {
- throw new AwsException("InvalidArgument", "The parameter Name is required.", 400);
+ List<String> violations = new ArrayList<>();
+ addEnumViolation(violations, "originAccessControlOriginType", oac.getOriginAccessControlOriginType(),
+ OAC_ORIGIN_TYPES);
+ addEnumViolation(violations, "signingProtocol", oac.getSigningProtocol(), OAC_SIGNING_PROTOCOLS);
+ addEnumViolation(violations, "signingBehavior", oac.getSigningBehavior(), OAC_SIGNING_BEHAVIORS);
+ if (oac.getName() == null) {
+ violations.add(nullViolation("name"));
+ }
+ if (!violations.isEmpty()) {
+ throw SmithyValidationErrors.exception("MalformedXML", violations);
+ }
+ if (oac.getName().isEmpty()) {
+ throw new AwsException("InvalidArgument", "The parameter Name is too small or empty.", 400);
}
if (oac.getName().length() > 64) {
- throw new AwsException("InvalidArgument",
- "The parameter Name must be 64 characters or fewer.", 400);
+ throw new AwsException("InvalidArgument", "The parameter Name is too big.", 400);
}
- if (!"sigv4".equals(oac.getSigningProtocol())) {
+ String originType = oac.getOriginAccessControlOriginType();
+ if ("always-amz-auth".equals(oac.getSigningBehavior()) && !"lambda".equals(originType)) {
throw new AwsException("InvalidArgument",
- "The parameter SigningProtocol must be sigv4.", 400);
+ "The parameter SigningBehavior always-amz-auth is only supported with lambda origin type.", 400);
}
- if (oac.getSigningBehavior() == null
- || !OAC_SIGNING_BEHAVIORS.contains(oac.getSigningBehavior())) {
+ if ("sigv4a".equals(oac.getSigningProtocol()) && !"s3".equals(originType) && !"lambda".equals(originType)) {
throw new AwsException("InvalidArgument",
- "The parameter SigningBehavior must be one of always, never, or no-override.",
- 400);
+ "The parameter OriginType sigv4a is only supported with s3 or lambda origin types.", 400);
}
- if (oac.getOriginAccessControlOriginType() == null
- || !OAC_ORIGIN_TYPES.contains(oac.getOriginAccessControlOriginType())) {
- throw new AwsException("InvalidArgument",
- "The parameter OriginAccessControlOriginType must be one of s3, mediastore, "
- + "mediapackagev2, or lambda.",
- 400);
+ if ("execute-api".equals(originType)) {
+ // AWS lists this origin type but refuses it.
+ throw new AwsException("AccessDenied", "Access Denied.", 403);
+ }
+ }
+
+ /** What an origin access control may not change while an origin uses it. */
+ private static boolean changesHowItSigns(OriginAccessControl existing, OriginAccessControl updated) {
+ return !Objects.equals(existing.getOriginAccessControlOriginType(), updated.getOriginAccessControlOriginType())
+ || !Objects.equals(existing.getSigningProtocol(), updated.getSigningProtocol())
+ || "always-amz-auth".equals(existing.getSigningBehavior())
+ != "always-amz-auth".equals(updated.getSigningBehavior());
+ }
+
+ private static void addEnumViolation(List<String> violations, String member, String value, List<String> values) {
+ if (value == null) {
+ violations.add(nullViolation(member));
+ } else if (!values.contains(value)) {
+ violations.add("Value '" + value + "' at 'originAccessControlConfig." + member
+ + "' failed to satisfy constraint: Member must satisfy enum value set: ["
+ + String.join(", ", values) + "]");
+ }
+ }
+
+ private static String nullViolation(String member) {
+ return "Value null at 'originAccessControlConfig." + member
+ + "' failed to satisfy constraint: Member must not be null";
+ }
+
+ /** The kinds of Lambda origin CloudFront tells apart. */
+ private enum LambdaOrigin { NONE, FUNCTION_URL, WEB_FUNCTION, GLOBAL_WEB_FUNCTION }
+
+ /**
+ * CloudFront knows a Lambda origin by its hostname alone, case sensitively. The endpoints of this
+ * Floci end in its own host instead of {@code on.aws}.
+ */
+ private static Pattern lambdaOriginHost(EmulatorConfig config) {
+ String authority = URI.create(config.effectiveBaseUrl()).getAuthority();
+ String flociHost = Pattern.quote(authority.replaceFirst(":\\d+$", "")) + "(?::\\d+)?";
+ return Pattern.compile(".+\\.(lambda-web|lambda-url)\\.(global|[a-z]{2,4}-[a-z-]+-\\d+)\\.(?:on\\.aws|"
+ + flociHost + ")");
+ }
+
+ private LambdaOrigin lambdaOrigin(String domainName) {
+ Matcher matcher = lambdaOriginHost.matcher(domainName == null ? "" : domainName);
+ if (!matcher.matches()) {
+ return LambdaOrigin.NONE;
+ }
+ boolean global = "global".equals(matcher.group(2));
+ if ("lambda-url".equals(matcher.group(1))) {
+ return global ? LambdaOrigin.NONE : LambdaOrigin.FUNCTION_URL;
+ }
+ return global ? LambdaOrigin.GLOBAL_WEB_FUNCTION : LambdaOrigin.WEB_FUNCTION;
+ }
+
+ /**
+ * The origin access control an origin names has to exist, and suit a Lambda origin by the rules
+ * CloudFront checks in this order. Then come the Lambda@Edge origin triggers it refuses in front
+ * of the origins those controls sign.
+ */
+ private void validateOriginAccessControls(DistributionConfig config) {
+ if (config == null || config.getOrigins() == null) {
+ return;
+ }
+ Set<String> amzAuthOrigins = new HashSet<>();
+ for (Origin origin : config.getOrigins()) {
+ String oacId = origin == null ? null : origin.getOriginAccessControlId();
+ if (oacId == null || oacId.isBlank()) {
+ continue;
+ }
+ Optional<OriginAccessControl> oac = oacStore.get(oacId);
+ if (oac.isEmpty()) {
+ throw new AwsException("InvalidOriginAccessControl",
+ "The specified origin access control does not exist or is not valid.", 400);
+ }
+ validateOriginAccessControlFor(lambdaOrigin(origin.getDomainName()), oac.get());
+ if ("always-amz-auth".equals(oac.get().getSigningBehavior())
+ || "sigv4a".equals(oac.get().getSigningProtocol())) {
+ amzAuthOrigins.add(origin.getId());
+ }
+ }
+ if (config.getDefaultCacheBehavior() != null) {
+ rejectOriginTriggers(amzAuthOrigins, config.getDefaultCacheBehavior().getTargetOriginId(),
+ config.getDefaultCacheBehavior().getLambdaFunctionAssociations());
+ }
+ if (config.getCacheBehaviors() != null) {
+ for (CacheBehavior behavior : config.getCacheBehaviors()) {
+ rejectOriginTriggers(amzAuthOrigins, behavior.getTargetOriginId(),
+ behavior.getLambdaFunctionAssociations());
+ }
+ }
+ }
+
+ private static void validateOriginAccessControlFor(LambdaOrigin origin, OriginAccessControl oac) {
+ boolean lambda = "lambda".equals(oac.getOriginAccessControlOriginType());
+ boolean webFunction = origin == LambdaOrigin.WEB_FUNCTION || origin == LambdaOrigin.GLOBAL_WEB_FUNCTION;
+ if (webFunction ? !lambda : (lambda && origin == LambdaOrigin.NONE)) {
+ throw illegalOriginAccessConfiguration("The origin type and OAC origin type differ");
+ }
+ if (!lambda) {
+ return;
+ }
+ boolean sigv4a = "sigv4a".equals(oac.getSigningProtocol());
+ if (origin == LambdaOrigin.GLOBAL_WEB_FUNCTION && !sigv4a) {
+ throw illegalOriginAccessConfiguration("This origin domain requires SigV4a signing protocol.");
+ }
+ if (origin != LambdaOrigin.GLOBAL_WEB_FUNCTION && sigv4a) {
+ throw illegalOriginAccessConfiguration("SigV4a signing protocol is not compatible with this origin domain.");
+ }
+ boolean amzAuth = "always-amz-auth".equals(oac.getSigningBehavior());
+ if (webFunction && !amzAuth) {
+ throw illegalOriginAccessConfiguration("Lambda-Web origins require always-amz-auth signing behavior.");
+ }
+ if (!webFunction && amzAuth) {
+ throw illegalOriginAccessConfiguration("always-amz-auth signing behavior requires a Lambda-Web origin.");
+ }
+ }
+
+ private static AwsException illegalOriginAccessConfiguration(String reason) {
+ return new AwsException("IllegalOriginAccessConfiguration", "Illegal configuration: " + reason, 400);
+ }
+
+ private static void rejectOriginTriggers(Set<String> amzAuthOrigins, String targetOriginId,
+ List<Map<String, Object>> associations) {
+ if (!amzAuthOrigins.contains(targetOriginId) || associations == null) {
+ return;
+ }
+ for (Map<String, Object> association : associations) {
+ Object eventType = association.get("EventType");
+ if ("origin-request".equals(eventType) || "origin-response".equals(eventType)) {
+ throw new AwsException("InvalidLambdaFunctionAssociation",
+ "Lambda@Edge origin triggers are incompatible with SigV4a and always-amz-auth origins."
+ + " Use a different origin or disassociate the function.", 400);
+ }
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontServingController.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontServingController.java
index b799d4e642..aeef65a860 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontServingController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontServingController.java
@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.services.cloudfront.model.Distribution;
import io.github.hectorvent.floci.services.cloudfront.model.DistributionConfig;
import io.github.hectorvent.floci.services.cloudfront.model.Origin;
import io.github.hectorvent.floci.services.cloudfront.model.OriginAccessControl;
+import io.github.hectorvent.floci.services.lambdaweb.LambdaWebIamAuth;
import io.github.hectorvent.floci.services.lambdaweb.LambdaWebService;
import io.github.hectorvent.floci.services.s3.ByteRange;
import io.github.hectorvent.floci.services.s3.S3Service;
@@ -109,6 +110,7 @@ public class CloudFrontServingController {
private final CloudFrontService service;
private final S3Service s3Service;
private final LambdaWebService lambdaWebService;
+ private final LambdaWebIamAuth lambdaWebIamAuth;
private final CurrentVertxRequest currentVertxRequest;
private final HttpServer httpServer;
private final CloudFrontOriginHttpClient httpClient;
@@ -116,11 +118,13 @@ public class CloudFrontServingController {
@Inject
public CloudFrontServingController(CloudFrontService service, S3Service s3Service,
- LambdaWebService lambdaWebService, EmulatorConfig emulatorConfig,
- CurrentVertxRequest currentVertxRequest, HttpServer httpServer) {
+ LambdaWebService lambdaWebService, LambdaWebIamAuth lambdaWebIamAuth,
+ EmulatorConfig emulatorConfig, CurrentVertxRequest currentVertxRequest,
+ HttpServer httpServer) {
this.service = service;
this.s3Service = s3Service;
this.lambdaWebService = lambdaWebService;
+ this.lambdaWebIamAuth = lambdaWebIamAuth;
this.currentVertxRequest = currentVertxRequest;
this.httpServer = httpServer;
this.httpClient = new CloudFrontOriginHttpClient(
@@ -496,7 +500,7 @@ public class CloudFrontServingController {
CloudFrontOriginRequestBuilder.build(viewerRequest(method, viewerScheme),
forwarding(config, normalized), distribution.getDomainName());
return fetchFromCustomOrigin(
- origin, forwardUri, originRequest, viewerScheme, method, viewerBody);
+ distribution, origin, forwardUri, originRequest, viewerScheme, method, viewerBody);
}
/** The live viewer request, with CloudFront's signing parameters removed from its query. */
@@ -763,7 +767,7 @@ public class CloudFrontServingController {
* Fetches from a custom (non-S3) origin. {@code forwardUri} already includes the origin path;
* {@code originRequest} carries the forwarded query and headers.
*/
- private OriginResponse fetchFromCustomOrigin(Origin origin, String forwardUri,
+ private OriginResponse fetchFromCustomOrigin(Distribution distribution, Origin origin, String forwardUri,
CloudFrontOriginRequestBuilder.OriginRequest originRequest,
String viewerScheme, String method, ViewerBody viewerBody) {
boolean includeBody = !"HEAD".equals(method);
@@ -797,6 +801,9 @@ public class CloudFrontServingController {
}
if (webEndpoint != null) {
originHeaders.put("Host", webEndpoint);
+ if (alwaysAmzAuth(origin)) {
+ originHeaders.putAll(lambdaWebIamAuth.cloudFrontSignature(distribution.getArn()));
+ }
}
rb.method(method, HttpRequest.BodyPublishers.noBody());
// Both bodies are streamed, so neither has to fit in memory: the viewer's goes out with its
@@ -829,6 +836,13 @@ public class CloudFrontServingController {
}
}
+ /** Whether the origin's access control is always-amz-auth, the one a Web Function origin is signed under. */
+ private boolean alwaysAmzAuth(Origin origin) {
+ String oacId = origin.getOriginAccessControlId();
+ return oacId != null && !oacId.isBlank()
+ && "always-amz-auth".equals(service.getOriginAccessControl(oacId).getSigningBehavior());
+ }
+
private String webFunctionEndpoint(Origin origin) {
try {
return lambdaWebService.endpointDomain(normalizeOriginHost(origin.getDomainName())).orElse(null);
@@ -1056,7 +1070,7 @@ public class CloudFrontServingController {
} else {
String forwardUri = CloudFrontRequestRouter.resolveForwardUri(errOrigin.getOriginPath(), errNormalized, null);
page = fetchFromCustomOrigin(
- errOrigin, forwardUri, CloudFrontOriginRequestBuilder.OriginRequest.empty(),
+ distribution, errOrigin, forwardUri, CloudFrontOriginRequestBuilder.OriginRequest.empty(),
viewerScheme, includeBody ? "GET" : "HEAD", null);
}
if (page.status() >= 400) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIamAuth.java b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIamAuth.java
index 155a6a1ac8..04e7bdd8a4 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIamAuth.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIamAuth.java
@@ -1,8 +1,10 @@
package io.github.hectorvent.floci.services.lambdaweb;
import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.IamConditionContextResolver;
import io.github.hectorvent.floci.core.common.RequestScopes;
+import io.github.hectorvent.floci.core.common.ServicePrincipals;
import io.github.hectorvent.floci.core.common.auth.CredentialScope;
import io.github.hectorvent.floci.core.common.auth.SessionTokenCheck;
import io.github.hectorvent.floci.core.common.auth.SigV4Canonicalization;
@@ -27,9 +29,13 @@ import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.security.SecureRandom;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -39,6 +45,10 @@ import java.util.Optional;
* signature for the service {@code lambda} in the endpoint's region, then
* {@code lambda:InvokeWebFunctionEndpoint} on the endpoint ARN. The signature is always checked.
* The policies are evaluated when IAM enforcement is on, as everywhere else in Floci.
+ *
+ * <p>CloudFront signs for a distribution under an {@code always-amz-auth} origin access control.
+ * Floci's CloudFront stands in for that signature with the {@code lambda-web-cloudfront-*} headers,
+ * which carry the distribution and a key only this process knows. The function never sees them.
*/
@ApplicationScoped
public class LambdaWebIamAuth {
@@ -48,11 +58,14 @@ public class LambdaWebIamAuth {
private static final String SERVICE = "lambda";
private static final String SECURITY_TOKEN = "X-Amz-Security-Token";
private static final String TEST_ACCESS_KEY = "test";
+ private static final String CLOUDFRONT_SOURCE_ARN = "lambda-web-cloudfront-source-arn";
+ private static final String CLOUDFRONT_KEY = "lambda-web-cloudfront-key";
private final EmulatorConfig config;
private final IamService iamService;
private final IamPolicyEvaluator evaluator;
private final Instance<ScpProvider> scpProvider;
+ private final String cloudFrontKey;
@Inject
public LambdaWebIamAuth(EmulatorConfig config, IamService iamService, IamPolicyEvaluator evaluator,
@@ -61,6 +74,14 @@ public class LambdaWebIamAuth {
this.iamService = iamService;
this.evaluator = evaluator;
this.scpProvider = scpProvider;
+ byte[] key = new byte[32];
+ new SecureRandom().nextBytes(key);
+ this.cloudFrontKey = HexFormat.of().formatHex(key);
+ }
+
+ /** The headers CloudFront signs an origin request with for {@code distributionArn}. */
+ public Map<String, String> cloudFrontSignature(String distributionArn) {
+ return Map.of(CLOUDFRONT_SOURCE_ARN, distributionArn, CLOUDFRONT_KEY, cloudFrontKey);
}
/** What the checks read from a request, copied on the event loop so a worker can check it. */
@@ -87,10 +108,45 @@ public class LambdaWebIamAuth {
* storage, is thrown rather than taken for a refusal. Blocks on IAM storage.
*/
public boolean allows(Inbound request, WebFunctionTarget target) {
+ String distributionArn = cloudFrontSource(request);
+ if (distributionArn != null) {
+ return authorizeCloudFront(distributionArn, target);
+ }
String accessKeyId = authenticate(request, target.region());
return accessKeyId != null && authorize(accessKeyId, target);
}
+ /** The distribution CloudFront signed the request for, or null when it did not sign it. */
+ private String cloudFrontSource(Inbound request) {
+ String key = request.headers().get(CLOUDFRONT_KEY);
+ String distributionArn = request.headers().get(CLOUDFRONT_SOURCE_ARN);
+ if (key == null || !AwsArnUtils.isArn(distributionArn) || !MessageDigest.isEqual(
+ key.getBytes(StandardCharsets.UTF_8), cloudFrontKey.getBytes(StandardCharsets.UTF_8))) {
+ return null;
+ }
+ return distributionArn;
+ }
+
+ /** CloudFront calls as its service principal, so the function's resource policy decides alone. */
+ private boolean authorizeCloudFront(String distributionArn, WebFunctionTarget target) {
+ if (!config.services().iam().enforcementEnabled()) {
+ return true;
+ }
+ String principal = ServicePrincipals.of("cloudfront");
+ String endpointArn = target.endpoint().endpointArn;
+ Map<String, List<String>> context = resourceContext(target);
+ context.putAll(IamConditionContextResolver.serviceCallContext(principal, endpointArn, target.accountId(),
+ distributionArn, AwsArnUtils.parse(distributionArn).accountId()));
+ String policy = target.function().policy;
+ boolean allowed = evaluator.evaluateServicePrincipal(isBlank(policy) ? null : List.of(policy), principal,
+ ACTION, endpointArn, context) == Decision.ALLOW;
+ if (!allowed) {
+ LOG.infov("IAM enforcement DENY: service={0} action={1} resource={2} source={3}",
+ principal, ACTION, endpointArn, distributionArn);
+ }
+ return allowed;
+ }
+
/** The access key that signed the request, or null when the signature does not hold. */
private String authenticate(Inbound request, String region) {
Signed found = signed(request);
@@ -219,9 +275,7 @@ public class LambdaWebIamAuth {
if (scpLevels != null) {
caller = caller.withScpLevels(scpLevels);
}
- Map<String, List<String>> context = new HashMap<>();
- context.put("lambda:Resource/WebFunctionAuthType", List.of(target.endpoint().authType));
- target.function().tags.forEach((key, value) -> context.put("aws:ResourceTag/" + key, List.of(value)));
+ Map<String, List<String>> context = resourceContext(target);
Optional<CallerArns> callerArns = iamService.resolveCallerArns(accessKeyId);
if (callerArns.isPresent()) {
caller = caller.withPrincipalArn(callerArns.get().callerArn());
@@ -240,6 +294,13 @@ public class LambdaWebIamAuth {
!= Decision.DENY;
}
+ private static Map<String, List<String>> resourceContext(WebFunctionTarget target) {
+ Map<String, List<String>> context = new HashMap<>();
+ context.put("lambda:Resource/WebFunctionAuthType", List.of(target.endpoint().authType));
+ target.function().tags.forEach((key, value) -> context.put("aws:ResourceTag/" + key, List.of(value)));
+ return context;
+ }
+
private static boolean isBlank(String value) {
return value == null || value.isBlank();
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontManagementProtocolTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontManagementProtocolTest.java
index 425589ea26..88d108a41d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontManagementProtocolTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontManagementProtocolTest.java
@@ -240,7 +240,15 @@ class CloudFrontManagementProtocolTest {
.statusCode(400)
.body(hasXPath(
"//*[local-name()='Code']/text()",
- equalTo("InvalidArgument")));
+ equalTo("MalformedXML")))
+ .body(hasXPath(
+ "//*[local-name()='Message']/text()",
+ equalTo("3 validation errors detected: Value null at"
+ + " 'originAccessControlConfig.originAccessControlOriginType' failed to satisfy"
+ + " constraint: Member must not be null; Value null at"
+ + " 'originAccessControlConfig.signingProtocol' failed to satisfy constraint: Member"
+ + " must not be null; Value null at 'originAccessControlConfig.signingBehavior' failed"
+ + " to satisfy constraint: Member must not be null")));
}
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontServiceTest.java
index 8c591ca616..cfbf0ee168 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontServiceTest.java
@@ -129,6 +129,10 @@ class CloudFrontServiceTest {
/** A null {@code domainSuffix} is {@code floci.services.cloudfront.domain-suffix} left unset. */
private CloudFrontService serviceWithDomainSuffix(String domainSuffix, String region) {
+ return serviceWithDomainSuffix(domainSuffix, region, "http://localhost:4566");
+ }
+
+ private CloudFrontService serviceWithDomainSuffix(String domainSuffix, String region, String baseUrl) {
StorageFactory storageFactory = Mockito.mock(StorageFactory.class);
when(storageFactory.create(Mockito.anyString(), Mockito.anyString(), Mockito.any()))
.thenAnswer(invocation -> AccountAwareStorageBackend.inMemory("000000000000"));
@@ -140,6 +144,7 @@ class CloudFrontServiceTest {
when(config.defaultAccountId()).thenReturn(ACCOUNT);
when(config.hostname()).thenReturn(Optional.empty());
+ when(config.effectiveBaseUrl()).thenReturn(baseUrl);
when(config.dns()).thenReturn(dnsConfig);
when(dnsConfig.extraSuffixes()).thenReturn(Optional.empty());
when(config.services()).thenReturn(servicesConfig);
@@ -531,23 +536,247 @@ class CloudFrontServiceTest {
OriginAccessControl missingName = originAccessControl();
missingName.setName(null);
- assertInvalidOriginAccessControl(service, missingName);
+ assertInvalidOriginAccessControl(service, missingName, "MalformedXML", "1 validation error detected: Value null"
+ + " at 'originAccessControlConfig.name' failed to satisfy constraint: Member must not be null");
+
+ OriginAccessControl emptyName = originAccessControl();
+ emptyName.setName("");
+ assertInvalidOriginAccessControl(service, emptyName, "InvalidArgument", "The parameter Name is too small or empty.");
OriginAccessControl longName = originAccessControl();
longName.setName("x".repeat(65));
- assertInvalidOriginAccessControl(service, longName);
+ assertInvalidOriginAccessControl(service, longName, "InvalidArgument", "The parameter Name is too big.");
OriginAccessControl invalidProtocol = originAccessControl();
invalidProtocol.setSigningProtocol("sigv2");
- assertInvalidOriginAccessControl(service, invalidProtocol);
+ assertInvalidOriginAccessControl(service, invalidProtocol, "MalformedXML", "1 validation error detected: Value"
+ + " 'sigv2' at 'originAccessControlConfig.signingProtocol' failed to satisfy constraint: Member must"
+ + " satisfy enum value set: [sigv4, sigv4a]");
OriginAccessControl invalidBehavior = originAccessControl();
invalidBehavior.setSigningBehavior("sometimes");
- assertInvalidOriginAccessControl(service, invalidBehavior);
+ assertInvalidOriginAccessControl(service, invalidBehavior, "MalformedXML", "1 validation error detected: Value"
+ + " 'sometimes' at 'originAccessControlConfig.signingBehavior' failed to satisfy constraint: Member"
+ + " must satisfy enum value set: [always-amz-auth, never, always, no-override]");
OriginAccessControl invalidOriginType = originAccessControl();
invalidOriginType.setOriginAccessControlOriginType("custom");
- assertInvalidOriginAccessControl(service, invalidOriginType);
+ assertInvalidOriginAccessControl(service, invalidOriginType, "MalformedXML", "1 validation error detected:"
+ + " Value 'custom' at 'originAccessControlConfig.originAccessControlOriginType' failed to satisfy"
+ + " constraint: Member must satisfy enum value set: [s3, execute-api, lambda, mediastore,"
+ + " mediapackagev2]");
+ }
+
+ @Test
+ void originAccessControlEnumsAndNameAreReportedTogetherBeforeTheOtherRules() {
+ CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
+
+ assertInvalidOriginAccessControl(service, new OriginAccessControl(), "MalformedXML", "4 validation errors"
+ + " detected: Value null at 'originAccessControlConfig.originAccessControlOriginType' failed to satisfy"
+ + " constraint: Member must not be null; Value null at 'originAccessControlConfig.signingProtocol'"
+ + " failed to satisfy constraint: Member must not be null; Value null at"
+ + " 'originAccessControlConfig.signingBehavior' failed to satisfy constraint: Member must not be null;"
+ + " Value null at 'originAccessControlConfig.name' failed to satisfy constraint: Member must not be null");
+
+ OriginAccessControl longNameAndBehavior = originAccessControl();
+ longNameAndBehavior.setName("x".repeat(65));
+ longNameAndBehavior.setSigningBehavior("always-amz-auth");
+ assertInvalidOriginAccessControl(service, longNameAndBehavior, "InvalidArgument", "The parameter Name is too big.");
+ }
+
+ @Test
+ void alwaysAmzAuthIsForLambdaAndSigV4aForS3OrLambda() {
+ CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
+
+ assertInvalidOriginAccessControl(service, originAccessControl("sigv4", "always-amz-auth", "s3"),
+ "InvalidArgument", "The parameter SigningBehavior always-amz-auth is only supported with lambda origin type.");
+ assertInvalidOriginAccessControl(service, originAccessControl("sigv4a", "always-amz-auth", "mediastore"),
+ "InvalidArgument", "The parameter SigningBehavior always-amz-auth is only supported with lambda origin type.");
+ assertInvalidOriginAccessControl(service, originAccessControl("sigv4a", "always", "mediapackagev2"),
+ "InvalidArgument", "The parameter OriginType sigv4a is only supported with s3 or lambda origin types.");
+
+ assertDoesNotThrow(() -> service.createOriginAccessControl(originAccessControl("sigv4a", "always-amz-auth", "lambda")));
+ assertDoesNotThrow(() -> service.createOriginAccessControl(originAccessControl("sigv4a", "never", "lambda")));
+ assertDoesNotThrow(() -> service.createOriginAccessControl(originAccessControl("sigv4a", "always", "s3")));
+ }
+
+ @Test
+ void theExecuteApiOriginTypeIsRefused() {
+ AwsException error = assertAws("AccessDenied", () -> serviceWithDomainSuffix("cloudfront.net")
+ .createOriginAccessControl(originAccessControl("sigv4", "always", "execute-api")));
+
+ assertEquals("Access Denied.", error.getMessage());
+ assertEquals(403, error.getHttpStatus());
+ }
+
+ @Test
+ void aLambdaOriginNeedsTheOriginAccessControlItsHostnameCallsFor() {
+ CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
+ String regional = "abcdefghijklmnopqrstuvwxyz.001.lambda-web.us-east-1.on.aws";
+ String global = "abcdefghijklmnopqrstuvwxyz.lambda-web.global.on.aws";
+ String functionUrl = "abcdefghijklmnopqrstuvwxyz0123.lambda-url.us-east-1.on.aws";
+ String differ = "The origin type and OAC origin type differ";
+
+ assertOriginAccessControlRefused(service, regional, "sigv4", "never", "lambda",
+ "Lambda-Web origins require always-amz-auth signing behavior.");
+ assertOriginAccessControlRefused(service, "abcdefghijklmnop.001.lambda-web.us-east-1.localhost:4566", "sigv4",
+ "always", "lambda", "Lambda-Web origins require always-amz-auth signing behavior.");
+ assertOriginAccessControlRefused(service, regional, "sigv4", "always", "s3", differ);
+ assertOriginAccessControlRefused(service, global, "sigv4", "always", "s3", differ);
+ assertOriginAccessControlRefused(service, "example.com", "sigv4", "always-amz-auth", "lambda", differ);
+ assertOriginAccessControlRefused(service, "bucket.s3.us-east-1.amazonaws.com", "sigv4", "always", "lambda", differ);
+ assertOriginAccessControlRefused(service, global, "sigv4", "always-amz-auth", "lambda",
+ "This origin domain requires SigV4a signing protocol.");
+ assertOriginAccessControlRefused(service, global, "sigv4", "never", "lambda",
+ "This origin domain requires SigV4a signing protocol.");
+ assertOriginAccessControlRefused(service, regional, "sigv4a", "always", "lambda",
+ "SigV4a signing protocol is not compatible with this origin domain.");
+ assertOriginAccessControlRefused(service, "abcdefghijklmnopqrstuvwxyz.001.lambda-web.us-west-2.on.aws", "sigv4a",
+ "always-amz-auth", "lambda", "SigV4a signing protocol is not compatible with this origin domain.");
+ assertOriginAccessControlRefused(service, functionUrl, "sigv4a", "always-amz-auth", "lambda",
+ "SigV4a signing protocol is not compatible with this origin domain.");
+ assertOriginAccessControlRefused(service, functionUrl, "sigv4", "always-amz-auth", "lambda",
+ "always-amz-auth signing behavior requires a Lambda-Web origin.");
+
+ assertDoesNotThrow(() -> service.createDistribution(
+ distributionWithOrigin(regional, oac(service, "sigv4", "always-amz-auth", "lambda"), null), Map.of()));
+ assertDoesNotThrow(() -> service.createDistribution(
+ distributionWithOrigin(global, oac(service, "sigv4a", "always-amz-auth", "lambda"), null), Map.of()));
+ assertDoesNotThrow(() -> service.createDistribution(
+ distributionWithOrigin(functionUrl, oac(service, "sigv4", "never", "lambda"), null), Map.of()));
+ }
+
+ @Test
+ void onlyAHostnameUnderOnAwsOrThisFlociIsALambdaOrigin() {
+ CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
+ String differ = "The origin type and OAC origin type differ";
+
+ for (String notLambda : List.of("api.lambda-web.us-east-1.example.com",
+ "abcdefghijklmnopqrstuvwxyz.001.lambda-web.us-east-1.on.aws.example.com",
+ "abcdefghijklmnopqrstuvwxyz0123.lambda-url.us-east-1.example.com",
+ "abcdefghijklmnopqrstuvwxyz.001.lambda-web.us-east-1.amazonaws.com",
+ "abcdefghijklmnopqrstuvwxyz.001.lambda-web.us-east-1.floci.example.com",
+ "abcdefghijklmnopqrstuvwxyz.001.LAMBDA-WEB.US-EAST-1.ON.AWS",
+ "abc.lambda-web.Global.on.aws",
+ "lambda-web.us-east-1.on.aws",
+ "abc.lambda-web.foo.on.aws")) {
+ assertOriginAccessControlRefused(service, notLambda, "sigv4a", "always-amz-auth", "lambda", differ);
+ }
+ for (String webFunction : List.of("ABC.lambda-web.us-east-1.on.aws", "a.b.c.lambda-web.us-east-1.on.aws",
+ "abc.lambda-web.xx-fake-9.on.aws", "abcdefghijklmnop.001.lambda-web.us-east-1.localhost")) {
+ assertOriginAccessControlRefused(service, webFunction, "sigv4a", "always-amz-auth", "lambda",
+ "SigV4a signing protocol is not compatible with this origin domain.");
+ }
+ }
+
+ @Test
+ void aFlociHostWithAnUnderscoreNamesLambdaOriginsToo() {
+ CloudFrontService service = serviceWithDomainSuffix("cloudfront.net", "us-east-1", "http://floci_host:4566");
+
+ for (String webFunction : List.of("abcdefghijklmnop.001.lambda-web.us-east-1.floci_host:4566",
+ "abcdefghijklmnop.001.lambda-web.us-east-1.floci_host")) {
+ assertOriginAccessControlRefused(service, webFunction, "sigv4a", "always-amz-auth", "lambda",
+ "SigV4a signing protocol is not compatible with this origin domain.");
+ }
+ }
+
+ @Test
+ void anUpdateChecksTheOriginAccessControlOfALambdaOrigin() {
+ CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
+ String never = oac(service, "sigv4", "never", "lambda");
+ Distribution created = service.createDistribution(
+ distributionWithOrigin("abcdefghijklmnopqrstuvwxyz0123.lambda-url.us-east-1.on.aws", never, null), Map.of());
+
+ AwsException error = assertAws("IllegalOriginAccessConfiguration", () -> service.updateDistribution(
+ created.getId(), created.getEtag(), distributionWithOrigin(
+ "abcdefghijklmnopqrstuvwxyz.001.lambda-web.us-east-1.on.aws", never, null)));
+
+ assertEquals("Illegal configuration: Lambda-Web origins require always-amz-auth signing behavior.",
+ error.getMessage());
+ }
+
+ @Test
+ void aDistributionNamingAnUnknownOriginAccessControlIsRefused() {
+ CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
+ AwsException created = assertAws("InvalidOriginAccessControl", () -> service.createDistribution(
+ distributionWithOrigin("example.com", "E0000000000000", null), Map.of()));
+ assertEquals("The specified origin access control does not exist or is not valid.", created.getMessage());
+ assertEquals(400, created.getHttpStatus());
+
+ Distribution existing = service.createDistribution(distributionWithOrigin("example.com", null, null), Map.of());
+ assertAws("InvalidOriginAccessControl", () -> service.updateDistribution(existing.getId(),
+ existing.getEtag(), distributionWithOrigin("example.com", "E0000000000000", null)));
+ assertNull(service.getDistribution(existing.getId()).getConfig().getOrigins().getFirst()
+ .getOriginAccessControlId());
+ }
+
+ @Test
+ void anOriginAccessControlInUseKeepsHowItSigns() {
+ CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
+ String s3 = oac(service, "sigv4", "always", "s3");
+ service.createDistribution(distributionWithOrigin("bucket.s3.us-east-1.amazonaws.com", s3, null), Map.of());
+ String functionUrl = oac(service, "sigv4", "always", "lambda");
+ service.createDistribution(distributionWithOrigin(
+ "abcdefghijklmnopqrstuvwxyz0123.lambda-url.us-east-1.on.aws", functionUrl, null), Map.of());
+
+ assertDoesNotThrow(() -> updateOriginAccessControl(service, s3, "sigv4", "never", "s3"));
+ assertDoesNotThrow(() -> updateOriginAccessControl(service, s3, "sigv4", "no-override", "s3"));
+ assertDoesNotThrow(() -> updateOriginAccessControl(service, functionUrl, "sigv4", "never", "lambda"));
+ assertIllegalUpdate(service, s3, "sigv4a", "no-override", "s3");
+ assertIllegalUpdate(service, s3, "sigv4", "no-override", "mediastore");
+ assertIllegalUpdate(service, functionUrl, "sigv4", "always-amz-auth", "lambda");
+ assertIllegalUpdate(service, functionUrl, "sigv4a", "never", "lambda");
+
+ String unused = oac(service, "sigv4", "always", "s3");
+ assertDoesNotThrow(() -> updateOriginAccessControl(service, unused, "sigv4a", "always", "lambda"));
+ }
+
+ private static void updateOriginAccessControl(CloudFrontService service, String id, String protocol,
+ String behavior, String originType) {
+ service.updateOriginAccessControl(id, service.getOriginAccessControl(id).getEtag(),
+ originAccessControl(protocol, behavior, originType));
+ }
+
+ private static void assertIllegalUpdate(CloudFrontService service, String id, String protocol, String behavior,
+ String originType) {
+ AwsException error = assertAws("IllegalUpdate",
+ () -> updateOriginAccessControl(service, id, protocol, behavior, originType));
+ assertEquals("Cannot update origin type when associated to origins.", error.getMessage());
+ assertEquals(400, error.getHttpStatus());
+ }
+
+ @Test
+ void lambdaEdgeOriginTriggersAreRefusedInFrontOfAnAlwaysAmzAuthOrSigV4aOrigin() {
+ CloudFrontService service = serviceWithDomainSuffix("cloudfront.net");
+ String regional = "abcdefghijklmnopqrstuvwxyz.001.lambda-web.us-east-1.on.aws";
+ String amzAuth = oac(service, "sigv4", "always-amz-auth", "lambda");
+ String message = "Lambda@Edge origin triggers are incompatible with SigV4a and always-amz-auth origins."
+ + " Use a different origin or disassociate the function.";
+
+ for (String eventType : List.of("origin-request", "origin-response")) {
+ AwsException error = assertAws("InvalidLambdaFunctionAssociation", () -> service.createDistribution(
+ distributionWithOrigin(regional, amzAuth, eventType), Map.of()));
+ assertEquals(message, error.getMessage());
+ assertEquals(400, error.getHttpStatus());
+ }
+ Distribution onABehavior = distributionWithOrigin(regional, amzAuth, null);
+ CacheBehavior behavior = new CacheBehavior();
+ behavior.setPathPattern("/api/*");
+ behavior.setTargetOriginId("o");
+ behavior.setLambdaFunctionAssociations(List.of(Map.<String, Object>of("LambdaFunctionARN",
+ "arn:aws:lambda:us-east-1:000000000000:function:edge:1", "EventType", "origin-request")));
+ onABehavior.getConfig().setCacheBehaviors(List.of(behavior));
+ assertEquals(message, assertAws("InvalidLambdaFunctionAssociation",
+ () -> service.createDistribution(onABehavior, Map.of())).getMessage());
+ Distribution sigv4aS3 = distributionWithOrigin(
+ "bucket.s3.us-east-1.amazonaws.com", oac(service, "sigv4a", "always", "s3"), "origin-request");
+ assertEquals(message, assertAws("InvalidLambdaFunctionAssociation",
+ () -> service.createDistribution(sigv4aS3, Map.of())).getMessage());
+
+ assertDoesNotThrow(() -> service.createDistribution(
+ distributionWithOrigin(regional, amzAuth, "viewer-request"), Map.of()));
+ assertDoesNotThrow(() -> service.createDistribution(distributionWithOrigin(
+ "bucket.s3.us-east-1.amazonaws.com", oac(service, "sigv4", "always", "s3"), "origin-request"), Map.of()));
}
@Test
@@ -562,7 +791,7 @@ class CloudFrontServiceTest {
() -> service.updateOriginAccessControl(
existing.getId(), existing.getEtag(), invalid));
- assertEquals("InvalidArgument", error.getErrorCode());
+ assertEquals("MalformedXML", error.getErrorCode());
assertEquals("test-oac", service.getOriginAccessControl(existing.getId()).getName());
}
@@ -1232,14 +1461,49 @@ class CloudFrontServiceTest {
}
private static OriginAccessControl originAccessControl() {
+ return originAccessControl("sigv4", "always", "s3");
+ }
+
+ private static OriginAccessControl originAccessControl(String protocol, String behavior, String originType) {
OriginAccessControl oac = new OriginAccessControl();
oac.setName("test-oac");
- oac.setSigningProtocol("sigv4");
- oac.setSigningBehavior("always");
- oac.setOriginAccessControlOriginType("s3");
+ oac.setSigningProtocol(protocol);
+ oac.setSigningBehavior(behavior);
+ oac.setOriginAccessControlOriginType(originType);
return oac;
}
+ private static String oac(CloudFrontService service, String protocol, String behavior, String originType) {
+ return service.createOriginAccessControl(originAccessControl(protocol, behavior, originType)).getId();
+ }
+
+ /** A distribution whose only origin {@code o} is {@code domainName}, with an optional Lambda@Edge trigger. */
+ private static Distribution distributionWithOrigin(String domainName, String oacId, String edgeEventType) {
+ Origin origin = new Origin();
+ origin.setId("o");
+ origin.setDomainName(domainName);
+ origin.setOriginAccessControlId(oacId);
+ DefaultCacheBehavior behavior = new DefaultCacheBehavior();
+ behavior.setTargetOriginId("o");
+ if (edgeEventType != null) {
+ behavior.setLambdaFunctionAssociations(List.of(Map.<String, Object>of("LambdaFunctionARN",
+ "arn:aws:lambda:us-east-1:000000000000:function:edge:1", "EventType", edgeEventType)));
+ }
+ Distribution distribution = distribution(false, List.of());
+ distribution.getConfig().setOrigins(List.of(origin));
+ distribution.getConfig().setDefaultCacheBehavior(behavior);
+ return distribution;
+ }
+
+ private static void assertOriginAccessControlRefused(CloudFrontService service, String domainName, String protocol,
+ String behavior, String originType, String reason) {
+ Distribution distribution = distributionWithOrigin(domainName, oac(service, protocol, behavior, originType), null);
+ AwsException error = assertAws("IllegalOriginAccessConfiguration",
+ () -> service.createDistribution(distribution, Map.of()));
+ assertEquals("Illegal configuration: " + reason, error.getMessage(), domainName);
+ assertEquals(400, error.getHttpStatus());
+ }
+
private static PublicKey validPublicKey(String name) throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(2048);
@@ -1287,10 +1551,9 @@ class CloudFrontServiceTest {
}
private static void assertInvalidOriginAccessControl(
- CloudFrontService service, OriginAccessControl oac) {
- AwsException error = assertThrows(AwsException.class,
- () -> service.createOriginAccessControl(oac));
- assertEquals("InvalidArgument", error.getErrorCode());
+ CloudFrontService service, OriginAccessControl oac, String errorCode, String message) {
+ AwsException error = assertAws(errorCode, () -> service.createOriginAccessControl(oac));
+ assertEquals(message, error.getMessage());
assertEquals(400, error.getHttpStatus());
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java
index 2f1ff38ba2..829d93d1b0 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java
@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.services.cloudfront.model.DefaultCacheBehavior
import io.github.hectorvent.floci.services.cloudfront.model.Distribution;
import io.github.hectorvent.floci.services.cloudfront.model.DistributionConfig;
import io.github.hectorvent.floci.services.cloudfront.model.Origin;
+import io.github.hectorvent.floci.services.cloudfront.model.OriginAccessControl;
import io.github.hectorvent.floci.services.lambdaweb.LambdaWebEnvironmentManager.Environment;
import io.github.hectorvent.floci.services.s3.S3Service;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
@@ -47,6 +48,7 @@ import java.util.zip.ZipOutputStream;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
@@ -222,9 +224,55 @@ class LambdaWebDataPlaneDockerIntegrationTest {
@Timeout(180)
void aCloudFrontDistributionReachesTheEndpointAsItsOrigin() throws Exception {
String domain = createFunction(30);
+ Distribution created = distributionInFrontOf(domain, null);
+ try {
+ given().header("Host", created.getDomainName()).header("x-custom", "1").when().get("/a/b?x=1")
+ .then().statusCode(200)
+ .header("lambda-web-request-id", matchesPattern(REQUEST_ID))
+ .header("lambda-web-logstream", notNullValue())
+ .body("url", equalTo("/a/b?x=1"))
+ .body("headers.host", equalTo(domain))
+ .body("headers.x-custom", equalTo("1"))
+ .body("headers.via", endsWith(" (CloudFront)"))
+ .body("headers.x-amz-cf-id", notNullValue())
+ .body("headers.lambda-web-endpoint-name", equalTo("dev"));
+ } finally {
+ cloudFrontService.removeDistribution(created.getId());
+ }
+ }
+
+ @Test
+ @Timeout(180)
+ void anAlwaysAmzAuthOriginAccessControlSignsForTheViewerInFrontOfAnIamAuthEndpoint() throws Exception {
+ String domain = createFunction(30, 64, "INFO", "serve", "IamAuth");
+ OriginAccessControl oac = new OriginAccessControl();
+ oac.setName(functionName);
+ oac.setSigningProtocol("sigv4");
+ oac.setSigningBehavior("always-amz-auth");
+ oac.setOriginAccessControlOriginType("lambda");
+ OriginAccessControl createdOac = cloudFrontService.createOriginAccessControl(oac);
+ Distribution created = distributionInFrontOf(domain, createdOac.getId());
+ try {
+ given().header("Host", domain).when().get("/").then().statusCode(403);
+ given().header("Host", created.getDomainName()).header("Authorization", "Bearer app-token")
+ .body("{}").when().post("/a")
+ .then().statusCode(200)
+ .body("method", equalTo("POST"))
+ .body("headers.authorization", equalTo("Bearer app-token"))
+ .body("headers", not(hasKey("lambda-web-cloudfront-key")))
+ .body("headers", not(hasKey("lambda-web-cloudfront-source-arn")));
+ } finally {
+ cloudFrontService.removeDistribution(created.getId());
+ cloudFrontService.deleteOriginAccessControl(createdOac.getId(), createdOac.getEtag());
+ }
+ }
+
+ /** The endpoint behind a distribution, named as on AWS: its host without a port, over HTTPS on 443. */
+ private Distribution distributionInFrontOf(String domain, String originAccessControlId) {
Origin origin = new Origin();
origin.setId("web");
origin.setDomainName(domain.substring(0, domain.lastIndexOf(':')));
+ origin.setOriginAccessControlId(originAccessControlId);
Map<String, Object> customOriginConfig = new LinkedHashMap<>();
customOriginConfig.put("HTTPPort", "80");
customOriginConfig.put("HTTPSPort", "443");
@@ -233,6 +281,7 @@ class LambdaWebDataPlaneDockerIntegrationTest {
DefaultCacheBehavior behavior = new DefaultCacheBehavior();
behavior.setTargetOriginId("web");
behavior.setViewerProtocolPolicy("allow-all");
+ behavior.setAllowedMethods(List.of("GET", "HEAD", "OPTIONS", "PUT", "PATCH", "POST", "DELETE"));
behavior.setCachePolicyId("4135ea2d-6df8-44a3-9df3-4b5a84be39ad");
behavior.setOriginRequestPolicyId("b689b0a8-53d0-40ab-baf2-68738e2966ac");
DistributionConfig config = new DistributionConfig();
@@ -241,21 +290,7 @@ class LambdaWebDataPlaneDockerIntegrationTest {
config.setDefaultCacheBehavior(behavior);
Distribution distribution = new Distribution();
distribution.setConfig(config);
- Distribution created = cloudFrontService.createDistribution(distribution, Map.of());
- try {
- given().header("Host", created.getDomainName()).header("x-custom", "1").when().get("/a/b?x=1")
- .then().statusCode(200)
- .header("lambda-web-request-id", matchesPattern(REQUEST_ID))
- .header("lambda-web-logstream", notNullValue())
- .body("url", equalTo("/a/b?x=1"))
- .body("headers.host", equalTo(domain))
- .body("headers.x-custom", equalTo("1"))
- .body("headers.via", endsWith(" (CloudFront)"))
- .body("headers.x-amz-cf-id", notNullValue())
- .body("headers.lambda-web-endpoint-name", equalTo("dev"));
- } finally {
- cloudFrontService.removeDistribution(created.getId());
- }
+ return cloudFrontService.createDistribution(distribution, Map.of());
}
@Test
@@ -474,6 +509,11 @@ class LambdaWebDataPlaneDockerIntegrationTest {
private String createFunction(int timeoutSeconds, int maxConcurrencyPerEnvironment, String systemLogLevel,
String mode) throws IOException {
+ return createFunction(timeoutSeconds, maxConcurrencyPerEnvironment, systemLogLevel, mode, "ApplicationManaged");
+ }
+
+ private String createFunction(int timeoutSeconds, int maxConcurrencyPerEnvironment, String systemLogLevel,
+ String mode, String authType) throws IOException {
functionName = "dp-docker-" + Long.toString(System.nanoTime(), 36);
s3Service.createBucket(BUCKET, "us-east-1");
s3Service.putObject(BUCKET, functionName + ".zip", zip(APP), "application/zip", Map.of());
@@ -488,9 +528,9 @@ class LambdaWebDataPlaneDockerIntegrationTest {
"environmentVariables": {"CUSTOM": "yes", "MODE": "%s"},
"telemetryConfig": {"loggingConfig": {"systemLogLevel": "%s"}}}},
"endpointConfig": {"endpointName": "dev", "endpointType": "HomeRegion",
- "authType": "ApplicationManaged", "autoDeploymentMode": "LatestRevision"}}
+ "authType": "%s", "autoDeploymentMode": "LatestRevision"}}
""".formatted(functionName, BUCKET, functionName, ROLE, timeoutSeconds,
- maxConcurrencyPerEnvironment, mode, systemLogLevel))
+ maxConcurrencyPerEnvironment, mode, systemLogLevel, authType))
.when().put(BASE + "/web-functions")
.then().statusCode(202)
.extract().path("endpoint.domainName");
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIamAuthTest.java b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIamAuthTest.java
index ee2ced5b83..8913d0bee8 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIamAuthTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebIamAuthTest.java
@@ -20,7 +20,9 @@ import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.time.Instant;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -43,6 +45,7 @@ class LambdaWebIamAuthTest {
private static final String USER_KEY = "AKIAUSERKEY000000001";
private static final String USER_SECRET = "user-secret";
private static final String SESSION_KEY = "ASIASESSIONKEY000001";
+ private static final String DISTRIBUTION_ARN = "arn:aws:cloudfront::" + ACCOUNT + ":distribution/E2EXAMPLE";
private final EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
private final IamService iamService = mock(IamService.class);
@@ -222,6 +225,59 @@ class LambdaWebIamAuthTest {
assertTrue(allows(signer().sign("GET", HOST, "/")));
}
+ @Test
+ void cloudFrontSignsWithTheKeyOfThisProcess() {
+ assertTrue(allowsCloudFront(iamAuth.cloudFrontSignature(DISTRIBUTION_ARN)));
+
+ Map<String, String> forged = new HashMap<>(iamAuth.cloudFrontSignature(DISTRIBUTION_ARN));
+ forged.put("lambda-web-cloudfront-key", "0".repeat(64));
+ assertFalse(allowsCloudFront(forged));
+ LambdaWebIamAuth another = new LambdaWebIamAuth(config, iamService, new IamPolicyEvaluator(new ObjectMapper()),
+ scpProvider);
+ assertFalse(allowsCloudFront(another.cloudFrontSignature(DISTRIBUTION_ARN)));
+ assertFalse(allowsCloudFront(iamAuth.cloudFrontSignature("E2EXAMPLE")));
+ }
+
+ @Test
+ void underEnforcementTheFunctionPolicyMustAllowCloudFrontForTheDistribution() {
+ when(config.services().iam().enforcementEnabled()).thenReturn(true);
+ assertFalse(allowsCloudFront(iamAuth.cloudFrontSignature(DISTRIBUTION_ARN)), "no policy denies");
+
+ function.policy = cloudFrontPolicy(cloudFrontStatement("Allow", "aws:SourceArn", DISTRIBUTION_ARN));
+ assertTrue(allowsCloudFront(iamAuth.cloudFrontSignature(DISTRIBUTION_ARN)));
+ assertFalse(allowsCloudFront(iamAuth.cloudFrontSignature(
+ "arn:aws:cloudfront::" + ACCOUNT + ":distribution/E3OTHER")));
+
+ function.policy = cloudFrontPolicy(cloudFrontStatement("Allow", "aws:SourceAccount", ACCOUNT));
+ assertTrue(allowsCloudFront(iamAuth.cloudFrontSignature(DISTRIBUTION_ARN)));
+ assertFalse(allowsCloudFront(iamAuth.cloudFrontSignature(
+ "arn:aws:cloudfront::" + OTHER_ACCOUNT + ":distribution/E2EXAMPLE")));
+
+ String allowIamAuth = cloudFrontStatement("Allow", "lambda:Resource/WebFunctionAuthType", "IamAuth");
+ function.policy = cloudFrontPolicy(allowIamAuth);
+ assertTrue(allowsCloudFront(iamAuth.cloudFrontSignature(DISTRIBUTION_ARN)));
+ function.policy = cloudFrontPolicy(allowIamAuth, cloudFrontStatement("Deny", "aws:SourceArn", DISTRIBUTION_ARN));
+ assertFalse(allowsCloudFront(iamAuth.cloudFrontSignature(DISTRIBUTION_ARN)));
+ }
+
+ private boolean allowsCloudFront(Map<String, String> signature) {
+ MultiMap headers = MultiMap.caseInsensitiveMultiMap().add("Host", HOST).addAll(signature);
+ return iamAuth.allows(new Inbound("POST", "/", null, headers, MultiMap.caseInsensitiveMultiMap()),
+ new WebFunctionTarget(ACCOUNT, "us-east-1", function, endpoint));
+ }
+
+ private static String cloudFrontPolicy(String... statements) {
+ return "{\"Version\":\"2012-10-17\",\"Statement\":[" + String.join(",", statements) + "]}";
+ }
+
+ private static String cloudFrontStatement(String effect, String conditionKey, String conditionValue) {
+ return """
+ {"Effect":"%s","Principal":{"Service":"cloudfront.amazonaws.com"},
+ "Action":"lambda:InvokeWebFunctionEndpoint","Resource":"%s",
+ "Condition":{"StringEquals":{"%s":"%s"}}}"""
+ .formatted(effect, ENDPOINT_ARN, conditionKey, conditionValue);
+ }
+
private void enforceFor(String callerAccount) {
when(config.services().iam().enforcementEnabled()).thenReturn(true);
when(iamService.findSecretKeyInAnyAccount(USER_KEY, null))