fix(lambda): scope resource-policy statements to the request Qualifier (#2129)

* fix(lambda): scope resource-policy statements to the request Qualifier

AddPermission, GetPolicy and RemovePermission ignored the Qualifier query
parameter. Every statement was stored against the unqualified function ARN, so
an alias-scoped permission could never be read back under its alias: GetPolicy
returned identical payloads with and without ?Qualifier, and the statement's
Resource had no qualifier suffix.

This is what stops Terraform's aws_lambda_permission with qualifier set from
converging — it creates the permission, polls GetPolicy?Qualifier=<alias>
forever, and a later apply then hits ResourceConflictException against a
statement its read could never confirm.

AWS keeps a separate resource policy per qualifier, so the qualifier is part of
a statement's identity. Declare @QueryParam("Qualifier") on the three policy
endpoints and thread it through the service, reusing the existing
resolveWithRegion() helper so a qualifier embedded in the function name
(arn:...:function:fn:ALIAS) is reconciled with the query-string value exactly as
the function-URL endpoints already do.

The scope is derived from the statement's own Resource rather than a new stored
field, so statements persisted before this change stay readable as the
function's unqualified policy. Consequently:

- AddPermission?Qualifier=X writes Resource <functionArn>:X
- GetPolicy?Qualifier=X returns only that qualifier's statements (404 if none)
- RemovePermission?Qualifier=X removes only within that qualifier
- the same StatementId may exist on the function and on each alias, and a
  duplicate within one qualifier still conflicts

Qualifier existence is deliberately not validated — that is a separate behaviour
change, and the endpoints did not validate it before either.

Adds LambdaPermissionQualifierIntegrationTest (12 cases); 7 of them fail against
the unfixed code with the exact symptom from the report (Resource missing the
alias suffix). The existing LambdaPermissionTagLayerIntegrationTest (19) and
LambdaVersionIntegrationTest (6) stay green, covering the unqualified path.

* fix(lambda): clarify missing scoped policy error
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-08-12 14:07:20 -04:00
1 parent 2f5bc39680
commit ea74b979ae
3 files changed
+323 -16

No files matched your search

@@ -479,12 +479,13 @@ public class LambdaController {
@Path("/functions/{functionName}/policy")
public Response addPermission(@Context HttpHeaders headers,
@PathParam("functionName") String functionName,
@QueryParam("Qualifier") String qualifier,
String body) {
String region = regionResolver.resolveRegion(headers);
try {
@SuppressWarnings("unchecked")
Map<String, Object> request = objectMapper.readValue(body, Map.class);
Map<String, Object> statement = lambdaService.addPermission(region, functionName, request);
Map<String, Object> statement = lambdaService.addPermission(region, functionName, qualifier, request);
String statementJson = objectMapper.writeValueAsString(statement);
ObjectNode root = objectMapper.createObjectNode();
root.put("Statement", statementJson);
@@ -499,10 +500,11 @@ public class LambdaController {
@GET
@Path("/functions/{functionName}/policy")
public Response getPolicy(@Context HttpHeaders headers,
@PathParam("functionName") String functionName) {
@PathParam("functionName") String functionName,
@QueryParam("Qualifier") String qualifier) {
String region = regionResolver.resolveRegion(headers);
try {
Map<String, Object> data = lambdaService.getPolicy(region, functionName);
Map<String, Object> data = lambdaService.getPolicy(region, functionName, qualifier);
@SuppressWarnings("unchecked")
Map<String, Object> policy = (Map<String, Object>) data.get("policy");
String policyJson = objectMapper.writeValueAsString(policy);
@@ -521,9 +523,10 @@ public class LambdaController {
@Path("/functions/{functionName}/policy/{statementId}")
public Response removePermission(@Context HttpHeaders headers,
@PathParam("functionName") String functionName,
@PathParam("statementId") String statementId) {
@PathParam("statementId") String statementId,
@QueryParam("Qualifier") String qualifier) {
String region = regionResolver.resolveRegion(headers);
lambdaService.removePermission(region, functionName, statementId);
lambdaService.removePermission(region, functionName, qualifier, statementId);
return Response.noContent().build();
}
@@ -1394,13 +1394,34 @@ public class LambdaService {
// ──────────────────────────── Permissions (Policy) ────────────────────────────
public Map<String, Object> addPermission(String region, String functionName, Map<String, Object> request) {
LambdaFunction fn = getFunction(region, functionName);
/**
* The ARN a resource-policy statement is scoped to: the bare function ARN, or
* {@code <functionArn>:<qualifier>} for an alias/version-scoped permission.
*
* <p>AWS keeps a SEPARATE resource policy per qualifier, so the qualifier is part of a
* statement's identity — the same {@code StatementId} may exist on the function and on
* each alias. We derive that identity from the statement's own {@code Resource} rather
* than storing a parallel field, which keeps already-persisted (always unqualified)
* statements readable as the function's own policy (#2124).
*/
private static String policyResourceArn(LambdaFunction fn, String qualifier) {
return qualifier == null ? fn.getFunctionArn() : fn.getFunctionArn() + ":" + qualifier;
}
private static boolean scopedTo(Map<String, Object> statement, String resourceArn) {
return resourceArn.equals(statement.get("Resource"));
}
public Map<String, Object> addPermission(String region, String functionName, String qualifier, Map<String, Object> request) {
LambdaArnUtils.ResolvedFunctionRef ref = resolveWithRegion(region, functionName, qualifier);
LambdaFunction fn = getFunction(region, ref.name());
String resourceArn = policyResourceArn(fn, ref.qualifier());
String statementId = (String) request.get("StatementId");
if (statementId == null || statementId.isBlank()) {
throw new AwsException("InvalidParameterValueException", "StatementId is required", 400);
}
fn.getPolicies().stream()
.filter(s -> scopedTo(s, resourceArn))
.filter(s -> statementId.equals(s.get("Sid")))
.findFirst()
.ifPresent(s -> {
@@ -1424,7 +1445,7 @@ public class LambdaService {
statement.put("Principal", principal);
}
statement.put("Action", action);
statement.put("Resource", fn.getFunctionArn());
statement.put("Resource", resourceArn);
if (sourceArn != null) {
statement.put("Condition", Map.of("ArnLike", Map.of("AWS:SourceArn", sourceArn)));
} else if (sourceAccount != null) {
@@ -1437,22 +1458,30 @@ public class LambdaService {
return statement;
}
public Map<String, Object> getPolicy(String region, String functionName) {
LambdaFunction fn = getFunction(region, functionName);
if (fn.getPolicies().isEmpty()) {
public Map<String, Object> getPolicy(String region, String functionName, String qualifier) {
LambdaArnUtils.ResolvedFunctionRef ref = resolveWithRegion(region, functionName, qualifier);
LambdaFunction fn = getFunction(region, ref.name());
String resourceArn = policyResourceArn(fn, ref.qualifier());
List<Map<String, Object>> statements = fn.getPolicies().stream()
.filter(s -> scopedTo(s, resourceArn))
.toList();
if (statements.isEmpty()) {
throw new AwsException("ResourceNotFoundException",
"Function not found: " + functionName, 404);
"The resource you requested does not exist.", 404);
}
Map<String, Object> policy = new java.util.LinkedHashMap<>();
policy.put("Version", "2012-10-17");
policy.put("Id", "default");
policy.put("Statement", fn.getPolicies());
policy.put("Statement", statements);
return Map.of("policy", policy, "revisionId", fn.getRevisionId());
}
public void removePermission(String region, String functionName, String statementId) {
LambdaFunction fn = getFunction(region, functionName);
boolean removed = fn.getPolicies().removeIf(s -> statementId.equals(s.get("Sid")));
public void removePermission(String region, String functionName, String qualifier, String statementId) {
LambdaArnUtils.ResolvedFunctionRef ref = resolveWithRegion(region, functionName, qualifier);
LambdaFunction fn = getFunction(region, ref.name());
String resourceArn = policyResourceArn(fn, ref.qualifier());
boolean removed = fn.getPolicies()
.removeIf(s -> statementId.equals(s.get("Sid")) && scopedTo(s, resourceArn));
if (!removed) {
throw new AwsException("ResourceNotFoundException",
"Statement " + statementId + " not found in function " + functionName, 404);