mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(lambda): keep every AddPermission condition and store account principals as root ARNs (#5123)
AddPermission kept only one condition and stored an account principal as the bare ID. It now writes every condition member in one statement, laid out the way AWS writes it: SourceArn under ArnLike, SourceAccount, FunctionUrlAuthType, PrincipalOrgID and EventSourceToken under StringEquals, and InvokedViaFunctionUrl under Bool as the string "true". A 12-digit account principal is stored as that account's root ARN in the function's partition, and an ARN is recognised before the dot test, so an IAM role, user or session name with a dot is no longer taken for a service. The condition members are checked against the model's enum and patterns before the function is looked up, and AWS::Lambda::Permission passes all of them through from a template. Fixes #4627
This commit is contained in:
1 parent
8deac0a424
commit
e28c58cc8a
4 files changed
+286
-18
No files matched your search
+14
-4
@@ -42,6 +42,10 @@ public class LambdaAddressingCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final String PERMISSION = "AWS::Lambda::Permission";
|
||||
private static final String URL = "AWS::Lambda::Url";
|
||||
// The optional AWS::Lambda::Permission string properties, which share their names with the
|
||||
// AddPermission members that become the statement's conditions.
|
||||
private static final List<String> PERMISSION_CONDITION_PROPERTIES = List.of(
|
||||
"SourceArn", "SourceAccount", "PrincipalOrgID", "FunctionUrlAuthType", "EventSourceToken");
|
||||
|
||||
private final LambdaService lambdaService;
|
||||
|
||||
@@ -113,10 +117,16 @@ public class LambdaAddressingCfnProvisioner implements CfnResourceProvisioner {
|
||||
request.put("StatementId", statementId);
|
||||
request.put("Action", ctx.resolveOptional(props, "Action"));
|
||||
request.put("Principal", ctx.resolveOptional(props, "Principal"));
|
||||
String sourceArn = ctx.resolveOptional(props, "SourceArn");
|
||||
if (sourceArn != null && !sourceArn.isBlank()) request.put("SourceArn", sourceArn);
|
||||
String sourceAccount = ctx.resolveOptional(props, "SourceAccount");
|
||||
if (sourceAccount != null && !sourceAccount.isBlank()) request.put("SourceAccount", sourceAccount);
|
||||
for (String property : PERMISSION_CONDITION_PROPERTIES) {
|
||||
String value = ctx.resolveOptional(props, property);
|
||||
if (value != null && !value.isBlank()) {
|
||||
request.put(property, value);
|
||||
}
|
||||
}
|
||||
String invokedViaFunctionUrl = ctx.resolveOptional(props, "InvokedViaFunctionUrl");
|
||||
if (invokedViaFunctionUrl != null && !invokedViaFunctionUrl.isBlank()) {
|
||||
request.put("InvokedViaFunctionUrl", Boolean.parseBoolean(invokedViaFunctionUrl));
|
||||
}
|
||||
try {
|
||||
lambdaService.addPermission(ctx.region(), functionName, null, request);
|
||||
} catch (RuntimeException failure) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.core.common.CustomResourceLiveness;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
@@ -54,6 +55,7 @@ import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@@ -128,6 +130,11 @@ public class LambdaService implements ResourceProvider {
|
||||
private static final Pattern LIST_ESM_FUNCTION_NAME_PATTERN = Pattern.compile(
|
||||
"^(arn:(aws[a-zA-Z-]*)?:lambda:)?((eusc-)?[a-z]{2}((-gov)|(-iso([a-z]?)))?-[a-z]+-\\d{1}:)?(\\d{12}:)?"
|
||||
+ "(function:)?([a-zA-Z0-9-_\\.]+)(:(\\$LATEST(\\.PUBLISHED)?|[a-zA-Z0-9-_]+))?$");
|
||||
// AddPermission members: botocore's PrincipalOrgID, SourceOwner and EventSourceToken shapes.
|
||||
private static final Pattern PRINCIPAL_ORG_ID_PATTERN = Pattern.compile("o-[a-z0-9]{10,32}");
|
||||
private static final Pattern ACCOUNT_ID_PATTERN = Pattern.compile("\\d{12}");
|
||||
private static final Pattern EVENT_SOURCE_TOKEN_PATTERN = Pattern.compile("[a-zA-Z0-9._\\-]+");
|
||||
private static final int MAX_EVENT_SOURCE_TOKEN_LENGTH = 256;
|
||||
|
||||
/** botocore lambda/2015-03-31 Runtime shape enum, kept in sync with service-2.json. */
|
||||
static final List<String> RUNTIME_VALUES = List.of(
|
||||
@@ -3592,6 +3599,7 @@ public class LambdaService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public Map<String, Object> addPermission(String region, String functionName, String qualifier, Map<String, Object> request) {
|
||||
validatePermissionConditions(request);
|
||||
LambdaArnUtils.ResolvedFunctionRef ref = resolveWithRegion(region, functionName, qualifier);
|
||||
LambdaFunction observed = getFunction(region, ref.name());
|
||||
synchronized (lockForPolicyMutation(observed.getFunctionArn())) {
|
||||
@@ -3612,25 +3620,16 @@ public class LambdaService implements ResourceProvider {
|
||||
|
||||
String principal = (String) request.get("Principal");
|
||||
String action = (String) request.get("Action");
|
||||
String sourceArn = (String) request.get("SourceArn");
|
||||
String sourceAccount = (String) request.get("SourceAccount");
|
||||
|
||||
Map<String, Object> statement = new java.util.LinkedHashMap<>();
|
||||
Map<String, Object> statement = new LinkedHashMap<>();
|
||||
statement.put("Sid", statementId);
|
||||
statement.put("Effect", "Allow");
|
||||
if (principal != null && principal.contains(".")) {
|
||||
statement.put("Principal", Map.of("Service", principal));
|
||||
} else if (principal != null && principal.startsWith("arn:")) {
|
||||
statement.put("Principal", Map.of("AWS", principal));
|
||||
} else {
|
||||
statement.put("Principal", principal);
|
||||
}
|
||||
statement.put("Principal", permissionPrincipal(principal, region));
|
||||
statement.put("Action", action);
|
||||
statement.put("Resource", resourceArn);
|
||||
if (sourceArn != null) {
|
||||
statement.put("Condition", Map.of("ArnLike", Map.of("AWS:SourceArn", sourceArn)));
|
||||
} else if (sourceAccount != null) {
|
||||
statement.put("Condition", Map.of("StringEquals", Map.of("AWS:SourceAccount", sourceAccount)));
|
||||
Map<String, Object> condition = permissionCondition(request);
|
||||
if (!condition.isEmpty()) {
|
||||
statement.put("Condition", condition);
|
||||
}
|
||||
|
||||
fn.getPolicies().add(statement);
|
||||
@@ -3640,6 +3639,76 @@ public class LambdaService implements ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/** The model constraints on the AddPermission members that become conditions. */
|
||||
private static void validatePermissionConditions(Map<String, Object> request) {
|
||||
validatePattern(request.get("SourceAccount"), "sourceAccount", ACCOUNT_ID_PATTERN);
|
||||
validatePattern(request.get("PrincipalOrgID"), "principalOrgID", PRINCIPAL_ORG_ID_PATTERN);
|
||||
validateEnum(request.get("FunctionUrlAuthType"), "functionUrlAuthType", FUNCTION_URL_AUTH_TYPE_VALUES);
|
||||
validateMaxLength(request.get("EventSourceToken"), "eventSourceToken", MAX_EVENT_SOURCE_TOKEN_LENGTH);
|
||||
validatePattern(request.get("EventSourceToken"), "eventSourceToken", EVENT_SOURCE_TOKEN_PATTERN);
|
||||
}
|
||||
|
||||
/**
|
||||
* The statement's Principal as AWS writes it: an ARN, or an account ID as that account's root
|
||||
* ARN, is an {@code AWS} principal, a domain-style name is a {@code Service}, and anything else
|
||||
* (such as {@code *}) is kept as given. ARNs are checked first because an IAM role, user or
|
||||
* session name may contain a dot.
|
||||
*/
|
||||
private static Object permissionPrincipal(String principal, String region) {
|
||||
if (principal == null) {
|
||||
return null;
|
||||
}
|
||||
if (principal.startsWith("arn:")) {
|
||||
return Map.of("AWS", principal);
|
||||
}
|
||||
if (ACCOUNT_ID_PATTERN.matcher(principal).matches()) {
|
||||
String partition = AwsRegions.partitionFor(region);
|
||||
return Map.of("AWS", AwsArnUtils.Arn.global(partition, "iam", principal, "root").toString());
|
||||
}
|
||||
if (principal.contains(".")) {
|
||||
return Map.of("Service", principal);
|
||||
}
|
||||
return principal;
|
||||
}
|
||||
|
||||
/**
|
||||
* The statement's Condition, in the layout AWS writes: {@code SourceArn} under {@code ArnLike},
|
||||
* the other string members under {@code StringEquals}, and {@code InvokedViaFunctionUrl} under
|
||||
* {@code Bool} as the string {@code "true"}. Empty when the request carries none of them.
|
||||
*/
|
||||
private static Map<String, Object> permissionCondition(Map<String, Object> request) {
|
||||
Map<String, Object> stringEquals = new LinkedHashMap<>();
|
||||
String sourceAccount = (String) request.get("SourceAccount");
|
||||
if (sourceAccount != null) {
|
||||
stringEquals.put("AWS:SourceAccount", sourceAccount);
|
||||
}
|
||||
String functionUrlAuthType = (String) request.get("FunctionUrlAuthType");
|
||||
if (functionUrlAuthType != null) {
|
||||
stringEquals.put("lambda:FunctionUrlAuthType", functionUrlAuthType);
|
||||
}
|
||||
String principalOrgId = (String) request.get("PrincipalOrgID");
|
||||
if (principalOrgId != null) {
|
||||
stringEquals.put("aws:PrincipalOrgID", principalOrgId);
|
||||
}
|
||||
String eventSourceToken = (String) request.get("EventSourceToken");
|
||||
if (eventSourceToken != null) {
|
||||
stringEquals.put("lambda:EventSourceToken", eventSourceToken);
|
||||
}
|
||||
|
||||
Map<String, Object> condition = new LinkedHashMap<>();
|
||||
if (!stringEquals.isEmpty()) {
|
||||
condition.put("StringEquals", stringEquals);
|
||||
}
|
||||
String sourceArn = (String) request.get("SourceArn");
|
||||
if (sourceArn != null) {
|
||||
condition.put("ArnLike", Map.of("AWS:SourceArn", sourceArn));
|
||||
}
|
||||
if (Boolean.TRUE.equals(request.get("InvokedViaFunctionUrl"))) {
|
||||
condition.put("Bool", Map.of("lambda:InvokedViaFunctionUrl", "true"));
|
||||
}
|
||||
return condition;
|
||||
}
|
||||
|
||||
public Map<String, Object> getPolicy(String region, String functionName, String qualifier) {
|
||||
LambdaArnUtils.ResolvedFunctionRef ref = resolveWithRegion(region, functionName, qualifier);
|
||||
LambdaFunction observed = getFunction(region, ref.name());
|
||||
|
||||
Reference in new issue
Block a user