mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(lambdaweb): fail a revision whose code is not a zip (#6141)
* feat(lambdaweb): fail a revision whose code is not a zip
A revision whose code is not a zip is Failed, as on AWS, and cannot be
deployed. The endpoint CreateWebFunction makes on such a revision is
Failed too and answers 409 Platform.EndpointNotActive. An empty object
is a 500 InternalServerException with an empty body, as on AWS.
* fix(lambdaweb): remove a code copy that cannot be read and name the {} body
* test(lambdaweb): upload a real zip in the compatibility tests
This commit is contained in:
1 parent
7c202a8966
commit
62972c91bf
21 files changed
+466
-33
No files matched your search
+8
@@ -169,6 +169,14 @@ public class LambdaWebCfnProvisioner implements CfnResourceProvisioner {
|
||||
revision = service.createRevision(ctx.region(), functionName, request);
|
||||
}
|
||||
r.setPhysicalId(revision.revisionArn);
|
||||
if (revision.failureReason != null) {
|
||||
// The AWS handler waits for Active, so a Failed revision fails the resource as NotStabilized.
|
||||
AwsException failure = new AwsException("NotStabilized", revision.failureReason, 400);
|
||||
CfnRollback.guarded(r, ctx, true, () -> delete(REVISION, r.getPhysicalId(), ctx.region()), () -> { },
|
||||
() -> {
|
||||
throw failure;
|
||||
});
|
||||
}
|
||||
ReplacementCleanup.record(r, ctx, attributesBefore);
|
||||
r.getAttributes().put("FunctionArn", revision.functionArn);
|
||||
r.getAttributes().put("RevisionArn", revision.revisionArn);
|
||||
|
||||
@@ -21,6 +21,8 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.ZipException;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
/**
|
||||
* The code package each revision copied from S3 when it was created. AWS prepares its own copy at
|
||||
@@ -45,17 +47,29 @@ public class LambdaWebCode {
|
||||
this.baseDir = baseDir;
|
||||
}
|
||||
|
||||
void store(String accountId, String region, String functionName, String revisionId, InputStream zip) {
|
||||
/** Returns the number of bytes kept. */
|
||||
long store(String accountId, String region, String functionName, String revisionId, InputStream zip) {
|
||||
Path file = file(accountId, region, functionName, revisionId);
|
||||
try {
|
||||
Files.createDirectories(file.getParent());
|
||||
Files.copy(zip, file, StandardCopyOption.REPLACE_EXISTING);
|
||||
return Files.copy(zip, file, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException e) {
|
||||
deleteRevision(accountId, region, functionName, revisionId);
|
||||
throw new UncheckedIOException("Could not keep the code of " + functionName + " revision " + revisionId, e);
|
||||
}
|
||||
}
|
||||
|
||||
boolean opensAsZip(String accountId, String region, String functionName, String revisionId) {
|
||||
try (ZipFile ignored = new ZipFile(file(accountId, region, functionName, revisionId).toFile())) {
|
||||
return true;
|
||||
} catch (ZipException e) {
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
deleteRevision(accountId, region, functionName, revisionId);
|
||||
throw new UncheckedIOException("Could not read the code of " + functionName + " revision " + revisionId, e);
|
||||
}
|
||||
}
|
||||
|
||||
void deleteRevision(String accountId, String region, String functionName, String revisionId) {
|
||||
try {
|
||||
Files.deleteIfExists(file(accountId, region, functionName, revisionId));
|
||||
|
||||
@@ -171,6 +171,11 @@ public class LambdaWebDataPlane {
|
||||
return;
|
||||
}
|
||||
WebFunctionTarget target = located.get();
|
||||
if (target.endpoint().failed) {
|
||||
metrics.answered(target, LambdaWebErrorPages.ENDPOINT_NOT_ACTIVE.status());
|
||||
page(request, LambdaWebErrorPages.ENDPOINT_NOT_ACTIVE, requestId, false);
|
||||
return;
|
||||
}
|
||||
Integer rateLimit = target.endpoint().throttleConfig == null ? null : target.endpoint().throttleConfig.rateLimit;
|
||||
if (Integer.valueOf(0).equals(rateLimit)) {
|
||||
metrics.answered(target, LambdaWebErrorPages.ENDPOINT_DISABLED.status());
|
||||
|
||||
@@ -19,6 +19,7 @@ final class LambdaWebErrorPages {
|
||||
static final Page FORBIDDEN = new Page(403, "Forbidden", null, "You are not authorized to access this endpoint.");
|
||||
static final Page INVALID_ENDPOINT = new Page(404, "Not Found", "Platform.InvalidEndpoint",
|
||||
"The requested endpoint could not be found.");
|
||||
static final Page ENDPOINT_NOT_ACTIVE = new Page(409, "Conflict", "Platform.EndpointNotActive", APPLICATION_ERROR);
|
||||
static final Page ENDPOINT_DISABLED = new Page(503, "Service Unavailable", "Platform.EndpointDisabled",
|
||||
"The service is temporarily unable to handle your request. Please try again later.");
|
||||
static final Page REQUEST_TIMEOUT = new Page(504, "Gateway Timeout", "Runtime.RequestTimeout",
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package io.github.hectorvent.floci.services.lambdaweb;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
|
||||
/** An internal error. AWS answers it with the type in the header and the body {@code {}}. */
|
||||
public final class LambdaWebInternalServerException extends AwsException {
|
||||
|
||||
LambdaWebInternalServerException() {
|
||||
super("InternalServerException", null, 500);
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package io.github.hectorvent.floci.services.lambdaweb;
|
||||
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.ext.ExceptionMapper;
|
||||
import jakarta.ws.rs.ext.Provider;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Provider
|
||||
public final class LambdaWebInternalServerExceptionMapper implements ExceptionMapper<LambdaWebInternalServerException> {
|
||||
|
||||
@Override
|
||||
public Response toResponse(LambdaWebInternalServerException exception) {
|
||||
return Response.status(exception.getHttpStatus())
|
||||
.type(MediaType.APPLICATION_JSON)
|
||||
.header("X-Amzn-Errortype", exception.getErrorCode())
|
||||
.entity(Map.of())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+14
-2
@@ -52,7 +52,7 @@ final class LambdaWebResponses {
|
||||
out.put("revisionArn", revision.revisionArn);
|
||||
out.put("revisionId", revision.revisionId);
|
||||
putIfPresent(out, "description", revision.description);
|
||||
putRevisionState(out, view);
|
||||
putRevisionState(out, revision, view);
|
||||
out.put("createdAt", timestamp(revision.createdAt));
|
||||
return out;
|
||||
}
|
||||
@@ -87,10 +87,13 @@ final class LambdaWebResponses {
|
||||
return out;
|
||||
}
|
||||
|
||||
private static void putRevisionState(Map<String, Object> out, View view) {
|
||||
private static void putRevisionState(Map<String, Object> out, WebFunctionRevision revision, View view) {
|
||||
if (view == View.CREATED) {
|
||||
out.put("state", "Pending");
|
||||
out.put("stateReason", "The revision is being activated.");
|
||||
} else if (revision.failureReason != null) {
|
||||
out.put("state", "Failed");
|
||||
out.put("stateReason", revision.failureReason);
|
||||
} else {
|
||||
out.put("state", "Active");
|
||||
out.put("stateReason", "The revision is active.");
|
||||
@@ -159,6 +162,15 @@ final class LambdaWebResponses {
|
||||
: "The endpoint is being activated and is not yet invokable in any region.");
|
||||
return;
|
||||
}
|
||||
if (endpoint.failed) {
|
||||
out.put("state", "Failed");
|
||||
out.put("stateReason", regional
|
||||
? "The endpoint activation failed because of an internal error."
|
||||
: "The endpoint activation failed in " + endpoint.regions + ". The endpoint is not invokable in"
|
||||
+ " any region. For details, see the stateReason for those regions in regionalEndpoints."
|
||||
+ " To retry, delete the endpoint and create it again.");
|
||||
return;
|
||||
}
|
||||
out.put("state", "Active");
|
||||
out.put("stateReason", regional ? "The endpoint is active in the region." : "The endpoint is active and invokable.");
|
||||
if (view == View.UPDATED) {
|
||||
|
||||
@@ -53,10 +53,11 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Control plane of Lambda Web Functions (apiVersion 2025-03-07). A revision and an endpoint are
|
||||
* stored Active and an endpoint update is stored Successful. The create and update responses show
|
||||
* the transitional state AWS reports. Invocation through an endpoint domain is not part of this
|
||||
* service.
|
||||
* Control plane of Lambda Web Functions (apiVersion 2025-03-07). A revision is stored Active, or
|
||||
* Failed when its code is not a zip. An endpoint is stored Active, or Failed when CreateWebFunction
|
||||
* gives it a Failed revision, and an endpoint update is stored Successful. The create and update
|
||||
* responses show the transitional state AWS reports. Invocation through an endpoint domain is not
|
||||
* part of this service.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class LambdaWebService implements Resettable {
|
||||
@@ -95,6 +96,7 @@ public class LambdaWebService implements Resettable {
|
||||
private static final Set<String> PRINCIPAL_KEYS = Set.of("AWS", "Service", "Federated", "CanonicalUser");
|
||||
private static final String POLICY_SYNTAX = "Policy has syntax errors.";
|
||||
private static final String INVALID_PRINCIPAL = "The provided principal was invalid. Please check the principal and try again.";
|
||||
private static final String NOT_A_ZIP = "Failed to read zip file: zip: not a valid zip file";
|
||||
private static final String INVALID_TAGS = "One or more of your tags are invalid. Please ensure none of your tag keys"
|
||||
+ " start with \"aws:\", none are empty, and there are no duplicate tag keys.";
|
||||
|
||||
@@ -305,6 +307,7 @@ public class LambdaWebService implements Resettable {
|
||||
WebFunctionRevision revision = buildRevision(fn, revisionConfig);
|
||||
keepCode(region, fn, revision);
|
||||
attachEndpoint(region, fn, endpoint, revision);
|
||||
endpoint.failed = revision.failureReason != null;
|
||||
fn.revisions.put(revision.revisionId, revision);
|
||||
fn.endpoints.put(endpoint.endpointName, endpoint);
|
||||
indexDomains(fn, endpoint);
|
||||
@@ -405,7 +408,7 @@ public class LambdaWebService implements Resettable {
|
||||
keepCode(region, fn, revision);
|
||||
fn.revisions.put(revision.revisionId, revision);
|
||||
for (WebFunctionEndpoint endpoint : fn.endpoints.values()) {
|
||||
if ("LatestRevision".equals(endpoint.autoDeploymentMode)) {
|
||||
if ("LatestRevision".equals(endpoint.autoDeploymentMode) && revision.failureReason == null) {
|
||||
endpoint.revisionWeights = List.of(weight(revision.revisionId, 100));
|
||||
endpoint.updatedAt = now();
|
||||
}
|
||||
@@ -431,9 +434,10 @@ public class LambdaWebService implements Resettable {
|
||||
synchronized (lockFor(region, functionName)) {
|
||||
WebFunction fn = function(region, functionName);
|
||||
List<WebFunctionRevision> snapshots = fn.revisions.values().stream()
|
||||
.filter(r -> list.matchesState(r.failureReason == null ? "Active" : "Failed"))
|
||||
.map(r -> copy(r, WebFunctionRevision.class))
|
||||
.toList();
|
||||
return page(list.matchesState("Active") ? snapshots : List.of(), list,
|
||||
return page(snapshots, list,
|
||||
scope("ListWebFunctionRevisions", region, functionName, list), LambdaWebService::newestFirst);
|
||||
}
|
||||
}
|
||||
@@ -587,10 +591,11 @@ public class LambdaWebService implements Resettable {
|
||||
if (s3Service == null) {
|
||||
return;
|
||||
}
|
||||
String accountId = AwsArnUtils.parse(fn.functionArn).accountId();
|
||||
WebFunctionRevision.BuildConfig build = revision.buildConfig;
|
||||
long size;
|
||||
try (S3Service.ObjectRead object = s3Service.openObject(build.bucket, build.key, build.versionId)) {
|
||||
code.store(AwsArnUtils.parse(fn.functionArn).accountId(), region, fn.functionName, revision.revisionId,
|
||||
object.body());
|
||||
size = code.store(accountId, region, fn.functionName, revision.revisionId, object.body());
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException("Could not close the code object s3://" + build.bucket + "/" + build.key, e);
|
||||
} catch (AwsException e) {
|
||||
@@ -609,6 +614,15 @@ public class LambdaWebService implements Resettable {
|
||||
throw new AwsException("ValidationException", "The S3 object " + object + " (versionId=" + build.versionId
|
||||
+ ") is inaccessible. Verify the request parameters and permissions are correct.", 400);
|
||||
}
|
||||
if (size == 0) {
|
||||
// An empty object is an internal error on AWS.
|
||||
code.deleteRevision(accountId, region, fn.functionName, revision.revisionId);
|
||||
throw new LambdaWebInternalServerException();
|
||||
}
|
||||
if (!code.opensAsZip(accountId, region, fn.functionName, revision.revisionId)) {
|
||||
code.deleteRevision(accountId, region, fn.functionName, revision.revisionId);
|
||||
revision.failureReason = NOT_A_ZIP;
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateRevisionShape(LambdaWebValidation v, JsonNode request, String prefix) {
|
||||
@@ -712,9 +726,10 @@ public class LambdaWebService implements Resettable {
|
||||
synchronized (lockFor(region, functionName)) {
|
||||
WebFunction fn = function(region, functionName);
|
||||
List<WebFunctionEndpoint> snapshots = fn.endpoints.values().stream()
|
||||
.filter(e -> list.matchesState(e.failed ? "Failed" : "Active"))
|
||||
.map(e -> copy(e, WebFunctionEndpoint.class))
|
||||
.toList();
|
||||
return page(list.matchesState("Active") ? snapshots : List.of(), list,
|
||||
return page(snapshots, list,
|
||||
scope("ListWebFunctionEndpoints", region, functionName, list), e -> e.endpointName);
|
||||
}
|
||||
}
|
||||
@@ -735,6 +750,10 @@ public class LambdaWebService implements Resettable {
|
||||
synchronized (lockFor(region, functionName)) {
|
||||
WebFunction fn = function(region, functionName);
|
||||
WebFunctionEndpoint endpoint = endpoint(fn, endpointName);
|
||||
if (endpoint.failed) {
|
||||
throw new AwsException("ConflictException", "The endpoint " + endpoint.endpointName
|
||||
+ " is in Failed state. The operation is allowed only when the endpoint is in Active state.", 409);
|
||||
}
|
||||
WebFunctionEndpoint before = copy(endpoint, WebFunctionEndpoint.class);
|
||||
|
||||
String effectiveMode = mode != null ? mode : endpoint.autoDeploymentMode;
|
||||
@@ -938,18 +957,29 @@ public class LambdaWebService implements Resettable {
|
||||
private static List<WebFunctionEndpoint.RevisionWeight> resolveWeights(WebFunction fn,
|
||||
List<WebFunctionEndpoint.RevisionWeight> weights) {
|
||||
for (WebFunctionEndpoint.RevisionWeight weight : weights) {
|
||||
if (!fn.revisions.containsKey(weight.revisionId)) {
|
||||
WebFunctionRevision revision = fn.revisions.get(weight.revisionId);
|
||||
if (revision == null) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"The revision " + weight.revisionId + " does not exist.", 404);
|
||||
}
|
||||
if (revision.failureReason != null) {
|
||||
throw new AwsException("ConflictException", "The revision " + weight.revisionId
|
||||
+ " is in Failed state and cannot be deployed. Only revisions in Active state can be deployed.", 409);
|
||||
}
|
||||
}
|
||||
return List.copyOf(weights);
|
||||
}
|
||||
|
||||
/** The newest revision, which LatestRevision mode deploys. */
|
||||
private static Optional<String> latestRevision(WebFunction fn) {
|
||||
return fn.revisions.values().stream()
|
||||
.max(Comparator.comparing((WebFunctionRevision r) -> r.createdAt))
|
||||
.map(r -> r.revisionId);
|
||||
Optional<WebFunctionRevision> latest = fn.revisions.values().stream()
|
||||
.max(Comparator.comparing((WebFunctionRevision r) -> r.createdAt));
|
||||
if (latest.isPresent() && latest.get().failureReason != null) {
|
||||
throw new AwsException("ConflictException", "The latest revision " + latest.get().revisionId
|
||||
+ " is in Failed state and cannot be deployed. Only revisions in Active state can be deployed."
|
||||
+ " Create a new revision.", 409);
|
||||
}
|
||||
return latest.map(r -> r.revisionId);
|
||||
}
|
||||
|
||||
private static WebFunctionEndpoint.RevisionWeight weight(String revisionId, int weight) {
|
||||
|
||||
+2
@@ -26,6 +26,8 @@ public final class WebFunctionEndpoint {
|
||||
public ScalingConfig scalingConfig;
|
||||
public ThrottleConfig throttleConfig;
|
||||
public String updateStatus;
|
||||
/** Created on a revision that failed. It stays Failed until it is deleted. */
|
||||
public boolean failed;
|
||||
public Map<String, String> regionalDomainNames = new LinkedHashMap<>();
|
||||
public Instant createdAt;
|
||||
public Instant updatedAt;
|
||||
|
||||
+2
@@ -17,6 +17,8 @@ public final class WebFunctionRevision {
|
||||
public BuildConfig buildConfig = new BuildConfig();
|
||||
public ServiceConfig serviceConfig = new ServiceConfig();
|
||||
public Instant createdAt;
|
||||
/** The stateReason of a Failed revision. */
|
||||
public String failureReason;
|
||||
|
||||
@RegisterForReflection
|
||||
public static final class BuildConfig {
|
||||
|
||||
Reference in new issue
Block a user