mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(lambdaweb): set the WebFunctionAuthType condition keys on endpoint routes (#6893)
This commit is contained in:
1 parent
d7204696d4
commit
385ffcfdd0
14 files changed
+604
-12
No files matched your search
+45
-2
@@ -16,6 +16,7 @@ import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iam.ResourceArnBuilder;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamRole;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamUser;
|
||||
import io.github.hectorvent.floci.services.lambdaweb.LambdaWebService;
|
||||
import io.github.hectorvent.floci.services.s3.S3ControlTagConditionKeys;
|
||||
import io.github.hectorvent.floci.services.s3.S3ObjectActions;
|
||||
import io.github.hectorvent.floci.services.s3.S3ObjectKeys;
|
||||
@@ -86,12 +87,15 @@ public class IamConditionContextResolver {
|
||||
public static final String OBJECT_LOCK_RETAIN_UNTIL_DATE = "s3:object-lock-retain-until-date";
|
||||
public static final String OBJECT_LOCK_REMAINING_RETENTION_DAYS = "s3:object-lock-remaining-retention-days";
|
||||
public static final String OBJECT_LOCK_LEGAL_HOLD = "s3:object-lock-legal-hold";
|
||||
static final String REQUEST_AUTH_TYPE = "lambda:Request/WebFunctionAuthType";
|
||||
static final String RESOURCE_AUTH_TYPE = "lambda:Resource/WebFunctionAuthType";
|
||||
|
||||
private final Instance<DynamoDbFacade> dynamoDbFacade;
|
||||
private final Instance<Ec2Service> ec2Service;
|
||||
private final Instance<S3Service> s3Service;
|
||||
private final Instance<CloudWatchLogsService> logsService;
|
||||
private final Instance<IamService> iamService;
|
||||
private final Instance<LambdaWebService> lambdaWebService;
|
||||
private final RequestContext requestContext;
|
||||
private final EmulatorConfig config;
|
||||
private final CurrentVertxRequest currentVertxRequest;
|
||||
@@ -108,12 +112,13 @@ public class IamConditionContextResolver {
|
||||
Instance<S3Service> s3Service,
|
||||
Instance<CloudWatchLogsService> logsService,
|
||||
Instance<IamService> iamService,
|
||||
Instance<LambdaWebService> lambdaWebService,
|
||||
RequestContext requestContext,
|
||||
EmulatorConfig config,
|
||||
CurrentVertxRequest currentVertxRequest,
|
||||
ResolvedServiceCatalog serviceCatalog) {
|
||||
this(dynamoDbFacade, ec2Service, s3Service, logsService, iamService, requestContext, config, currentVertxRequest,
|
||||
serviceCatalog, Clock.systemUTC());
|
||||
this(dynamoDbFacade, ec2Service, s3Service, logsService, iamService, lambdaWebService, requestContext,
|
||||
config, currentVertxRequest, serviceCatalog, Clock.systemUTC());
|
||||
}
|
||||
|
||||
IamConditionContextResolver(Instance<DynamoDbFacade> dynamoDbFacade,
|
||||
@@ -121,6 +126,7 @@ public class IamConditionContextResolver {
|
||||
Instance<S3Service> s3Service,
|
||||
Instance<CloudWatchLogsService> logsService,
|
||||
Instance<IamService> iamService,
|
||||
Instance<LambdaWebService> lambdaWebService,
|
||||
RequestContext requestContext,
|
||||
EmulatorConfig config,
|
||||
CurrentVertxRequest currentVertxRequest,
|
||||
@@ -130,6 +136,7 @@ public class IamConditionContextResolver {
|
||||
this.s3Service = s3Service;
|
||||
this.logsService = logsService;
|
||||
this.iamService = iamService;
|
||||
this.lambdaWebService = lambdaWebService;
|
||||
this.requestContext = requestContext;
|
||||
this.config = config;
|
||||
this.currentVertxRequest = currentVertxRequest;
|
||||
@@ -145,6 +152,7 @@ public class IamConditionContextResolver {
|
||||
case "dynamodb" -> dynamoDbConditionContext(action, ctx);
|
||||
case "iam" -> iamConditionContext(action, ctx);
|
||||
case "logs" -> logsConditionContext(action, ctx);
|
||||
case "lambda" -> lambdaConditionContext(action, ctx);
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
@@ -321,6 +329,41 @@ public class IamConditionContextResolver {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Lambda ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/** The authType a web function endpoint request names, and the one the endpoint has before it. */
|
||||
private Map<String, List<String>> lambdaConditionContext(String action, ContainerRequestContext ctx) {
|
||||
Map<String, List<String>> conditions = new LinkedHashMap<>();
|
||||
for (String member : LambdaWebConditionKeys.members(action, REQUEST_AUTH_TYPE)) {
|
||||
PassedRoles.values(PassedRoles.jsonBody(ctx), member.split("\\."))
|
||||
.forEach(value -> putIfPresent(conditions, REQUEST_AUTH_TYPE, value));
|
||||
}
|
||||
if (LambdaWebConditionKeys.takes(action, RESOURCE_AUTH_TYPE)) {
|
||||
putIfPresent(conditions, RESOURCE_AUTH_TYPE, storedAuthType(ctx));
|
||||
}
|
||||
return conditions.isEmpty() ? null : conditions;
|
||||
}
|
||||
|
||||
private String storedAuthType(ContainerRequestContext ctx) {
|
||||
if (!lambdaWebService.isResolvable()) {
|
||||
return null;
|
||||
}
|
||||
String region = requestContext.getRegion() == null ? config.defaultRegion() : requestContext.getRegion();
|
||||
String endpointArn = ResourceArnBuilder.webFunctionArn(ctx.getUriInfo().getPath(), region,
|
||||
requestContext.getAccountId());
|
||||
int child = endpointArn.lastIndexOf("/endpoint/");
|
||||
if (child < 0) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return lambdaWebService.get().getEndpoint(region, endpointArn.substring(0, child),
|
||||
endpointArn.substring(child + "/endpoint/".length())).authType;
|
||||
} catch (AwsException expected) {
|
||||
// The handler refuses the same names, so the key is only left out, as for a missing endpoint on AWS.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ── S3 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
private Map<String, List<String>> s3ConditionContext(String action, ContainerRequestContext ctx) {
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The Lambda Web Functions authType condition keys each action takes, and the request members that
|
||||
* carry the request key, generated from the AWS Service Reference and pinned botocore by
|
||||
* {@code tools/aws/regen_lambda_web_condition_keys.py}.
|
||||
*/
|
||||
final class LambdaWebConditionKeys {
|
||||
|
||||
private static final JsonNode TABLE = VendoredResources.read("aws/lambda-web-condition-keys.json",
|
||||
"Lambda Web condition keys", input -> {
|
||||
JsonNode document = new ObjectMapper().readTree(input);
|
||||
if (!document.path("actions").isObject() || document.path("actions").isEmpty()
|
||||
|| !document.path("members").isObject() || document.path("members").isEmpty()) {
|
||||
throw new IllegalStateException("Malformed Lambda Web condition key table");
|
||||
}
|
||||
return document;
|
||||
});
|
||||
|
||||
private LambdaWebConditionKeys() {}
|
||||
|
||||
static boolean takes(String action, String key) {
|
||||
for (JsonNode taken : TABLE.path("actions").path(action)) {
|
||||
if (key.equals(taken.asText())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** The dotted paths of the request members that carry {@code key} for {@code action}; empty when none. */
|
||||
static List<String> members(String action, String key) {
|
||||
List<String> members = new ArrayList<>();
|
||||
TABLE.path("members").path(action).path(key).forEach(member -> members.add(member.asText()));
|
||||
return members;
|
||||
}
|
||||
}
|
||||
@@ -820,7 +820,7 @@ final class PassedRoles {
|
||||
}
|
||||
|
||||
/** The strings at {@code path} in {@code node}, where a {@code []} step takes each element of an array. */
|
||||
private static List<String> values(JsonNode node, String... path) {
|
||||
static List<String> values(JsonNode node, String... path) {
|
||||
List<String> found = new ArrayList<>();
|
||||
collect(node, path, 0, found);
|
||||
return found;
|
||||
|
||||
@@ -1348,7 +1348,7 @@ public class ResourceArnBuilder {
|
||||
* "Lambda Web Function permissions"). ListWebFunctions, GetWebAccountSettings and CreateWebFunction
|
||||
* name no resource. Null for every other Lambda route.
|
||||
*/
|
||||
private static String webFunctionArn(String path, String region, String accountId) {
|
||||
public static String webFunctionArn(String path, String region, String accountId) {
|
||||
if (!path.startsWith("/2025-03-07/")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user