diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java index d45c6f7359..f1fa679257 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java @@ -88,9 +88,10 @@ public class LambdaController { @Path("/functions/{functionName}") public Response getFunction(@Context HttpHeaders headers, @Context UriInfo uriInfo, - @PathParam("functionName") String functionName) { + @PathParam("functionName") String functionName, + @QueryParam("Qualifier") String qualifier) { String region = regionResolver.resolveRegion(headers); - LambdaFunction fn = lambdaService.getFunction(region, functionName); + LambdaFunction fn = lambdaService.getFunction(region, functionName, qualifier); ObjectNode root = objectMapper.createObjectNode(); root.set("Configuration", objectMapper.valueToTree(buildFunctionConfiguration(fn))); @@ -145,9 +146,10 @@ public class LambdaController { @GET @Path("/functions/{functionName}/configuration") public Response getFunctionConfiguration(@Context HttpHeaders headers, - @PathParam("functionName") String functionName) { + @PathParam("functionName") String functionName, + @QueryParam("Qualifier") String qualifier) { String region = regionResolver.resolveRegion(headers); - LambdaFunction fn = lambdaService.getFunction(region, functionName); + LambdaFunction fn = lambdaService.getFunction(region, functionName, qualifier); return Response.ok(buildFunctionConfiguration(fn)).build(); } diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java index be820bb34e..b00dfd67c2 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java @@ -475,6 +475,50 @@ public class LambdaService implements ResourceProvider { "Function not found: " + functionName, 404)); } + /** + * Reads a function, honouring a {@code Qualifier} that selects a published version or an alias. + * + *
The read paths used to ignore the qualifier entirely and answer with {@code $LATEST} for + * everything, including versions that were never published (issue #2821). That defeats the + * point of publishing: a caller pinning version 1 silently got whatever {@code $LATEST} held at + * the time of the call, and a typo or a stale alias read back as a live function instead of + * failing. Resolution is delegated to the same target resolver the invoke path uses, so a + * qualifier means the same thing whether you read it or run it. + * + *
A qualifier may also be carried on the name itself, as {@code fn:1} or a qualified ARN. An
+ * explicit {@code Qualifier} and one embedded in the name must agree; AWS rejects the
+ * combination when they do not.
+ */
+ public LambdaFunction getFunction(String region, String functionName, String qualifier) {
+ LambdaArnUtils.ResolvedFunctionRef ref = LambdaArnUtils.resolve(functionName);
+ enforceRegion(region, ref);
+ String effective = resolveQualifier(ref.qualifier(), qualifier);
+ if (effective == null) {
+ return getFunction(region, functionName);
+ }
+ if (functionName.startsWith("arn:")) {
+ AwsArnUtils.Arn arn = AwsArnUtils.parse(functionName);
+ return resolveReadTargetForAccount(arn.accountId(), region, ref.name(), effective);
+ }
+ return resolveReadTarget(region, ref.name(), effective);
+ }
+
+ /**
+ * Reconciles a qualifier carried on the function name with an explicit {@code Qualifier}
+ * parameter. Either alone wins; both must agree.
+ */
+ private static String resolveQualifier(String onName, String explicit) {
+ if (explicit == null || explicit.isBlank()) {
+ return onName;
+ }
+ if (onName != null && !onName.equals(explicit)) {
+ throw new AwsException("InvalidParameterValueException",
+ "Cannot provide both a qualified function name and a Qualifier: "
+ + onName + " and " + explicit, 400);
+ }
+ return explicit;
+ }
+
/**
* Resolves a {@code FunctionName} path parameter (bare name, partial ARN,
* or full ARN, with optional {@code :qualifier}) to its canonical short
@@ -895,6 +939,22 @@ public class LambdaService implements ResourceProvider {
}
private LambdaFunction resolveInvokeTarget(String region, String name, String qualifier) {
+ return resolveTarget(region, name, qualifier, this::pickAliasVersion);
+ }
+
+ /**
+ * Resolves a qualifier for a read. Identical to the invoke path except for aliases:
+ * an alias with {@code AdditionalVersionWeights} shifts traffic, so {@link #pickAliasVersion}
+ * chooses randomly among the weighted versions, which is right for running the function and
+ * wrong for describing it. Two reads of one alias must not disagree, so a read follows the
+ * alias's primary {@code FunctionVersion}, which is what AWS reports.
+ */
+ private LambdaFunction resolveReadTarget(String region, String name, String qualifier) {
+ return resolveTarget(region, name, qualifier, LambdaAlias::getFunctionVersion);
+ }
+
+ private LambdaFunction resolveTarget(String region, String name, String qualifier,
+ java.util.function.Function Two consequences, both measured against the live service by the reporter. A caller pinning a
+ * published version silently got whatever {@code $LATEST} held at the time, which defeats the
+ * immutability guarantee that is the point of publishing. And a read against a version that does
+ * not exist succeeded instead of returning 404, so a typo or a stale alias looked live.
+ */
+@QuarkusTest
+class LambdaQualifiedReadIntegrationTest {
+
+ private static String zipB64(String body) throws Exception {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try (ZipOutputStream zos = new ZipOutputStream(baos)) {
+ zos.putNextEntry(new ZipEntry("handler.py"));
+ zos.write(("def handler(e, c):\n return {'body': '" + body + "'}\n").getBytes("UTF-8"));
+ zos.closeEntry();
+ }
+ return Base64.getEncoder().encodeToString(baos.toByteArray());
+ }
+
+ private static String createFunction(String name, String body) throws Exception {
+ given()
+ .contentType("application/json")
+ .body("""
+ {"FunctionName": "%s", "Runtime": "python3.12",
+ "Role": "arn:aws:iam::000000000000:role/r", "Handler": "handler.handler",
+ "Code": {"ZipFile": "%s"}}
+ """.formatted(name, zipB64(body)))
+ .when()
+ .post("/2015-03-31/functions")
+ .then()
+ .statusCode(anyOf200Or201());
+ return name;
+ }
+
+ private static org.hamcrest.Matcher