mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(logs): enforce stream resource permissions (#5694)
* fix(logs): enforce stream resource permissions * fix(logs): retain request partitions for stream permissions
This commit is contained in:
1 parent
e357353472
commit
dbe7fdb92e
15 files changed
+1142
-5
No files matched your search
@@ -314,7 +314,7 @@ public class AwsJson11Controller {
|
||||
case "elasticmapreduce" -> emrHandler.handle(action, request, region);
|
||||
case "wafv2" -> wafV2Handler.handle(action, request, region);
|
||||
case "memorydb" -> memoryDbHandler.handle(action, request, region);
|
||||
case "logs" -> cloudWatchLogsHandler.handle(action, request, region);
|
||||
case "logs" -> cloudWatchLogsHandler.handle(action, request, regionResolver.getRegion());
|
||||
case "secretsmanager" -> secretsManagerJsonHandler.handle(action, request, region,
|
||||
IamEnforcementFilter.requestAuthorization(
|
||||
httpHeaders.getHeaderString("Authorization"), uriInfo.getQueryParameters()));
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.common;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbConditionKeys;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbFacade;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbTableNames;
|
||||
@@ -70,6 +71,7 @@ public class IamConditionContextResolver {
|
||||
private final Instance<DynamoDbFacade> dynamoDbFacade;
|
||||
private final Instance<Ec2Service> ec2Service;
|
||||
private final Instance<S3Service> s3Service;
|
||||
private final Instance<CloudWatchLogsService> logsService;
|
||||
private final RequestContext requestContext;
|
||||
private final EmulatorConfig config;
|
||||
|
||||
@@ -77,11 +79,13 @@ public class IamConditionContextResolver {
|
||||
public IamConditionContextResolver(Instance<DynamoDbFacade> dynamoDbFacade,
|
||||
Instance<Ec2Service> ec2Service,
|
||||
Instance<S3Service> s3Service,
|
||||
Instance<CloudWatchLogsService> logsService,
|
||||
RequestContext requestContext,
|
||||
EmulatorConfig config) {
|
||||
this.dynamoDbFacade = dynamoDbFacade;
|
||||
this.ec2Service = ec2Service;
|
||||
this.s3Service = s3Service;
|
||||
this.logsService = logsService;
|
||||
this.requestContext = requestContext;
|
||||
this.config = config;
|
||||
}
|
||||
@@ -93,10 +97,40 @@ public class IamConditionContextResolver {
|
||||
case "ec2" -> ec2ConditionContext(action, ctx);
|
||||
case "dynamodb" -> dynamoDbConditionContext(action, ctx);
|
||||
case "iam" -> iamConditionContext(action, ctx);
|
||||
case "logs" -> logStreamConditionContext(action, ctx);
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
/** Stream access uses the owning log group's tags, in the handler's account and region. */
|
||||
private Map<String, List<String>> logStreamConditionContext(String action, ContainerRequestContext ctx) {
|
||||
if (!("logs:CreateLogStream".equals(action) || "logs:DeleteLogStream".equals(action))) {
|
||||
return null;
|
||||
}
|
||||
JsonNode body = bufferedJsonBody(ctx);
|
||||
if (body == null || !logsService.isResolvable()) {
|
||||
return null;
|
||||
}
|
||||
String region = requestContext.getRegion() == null ? config.defaultRegion() : requestContext.getRegion();
|
||||
Map<String, String> tags;
|
||||
try {
|
||||
tags = logsService.get().listTagsLogGroup(body.path("logGroupName").asText(), region);
|
||||
} catch (AwsException e) {
|
||||
if (!"ResourceNotFoundException".equals(e.getErrorCode())) {
|
||||
throw e;
|
||||
}
|
||||
// Leave the handler to report a missing group; unexpected lookup errors must not hide a deny.
|
||||
return null;
|
||||
}
|
||||
if (tags == null || tags.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Map<String, List<String>> conditions = new LinkedHashMap<>();
|
||||
tags.forEach((key, value) -> conditions.put(RESOURCE_TAG_PREFIX + key,
|
||||
List.of(value == null ? "" : value)));
|
||||
return conditions;
|
||||
}
|
||||
|
||||
private static Map<String, List<String>> iamConditionContext(String action, ContainerRequestContext ctx) {
|
||||
return switch (action) {
|
||||
case "iam:AttachRolePolicy", "iam:DetachRolePolicy", "iam:AttachUserPolicy",
|
||||
|
||||
@@ -416,7 +416,9 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
}
|
||||
|
||||
List<String> resources = resolveResourceArns(credentialScope,
|
||||
arnBuilder.buildResources(credentialScope, ctx, region, accountId));
|
||||
"logs".equals(credentialScope)
|
||||
? arnBuilder.buildLogStreamResources(action, ctx, region, accountId, requestPartition())
|
||||
: arnBuilder.buildResources(credentialScope, ctx, region, accountId));
|
||||
|
||||
Optional<PresignedScope> presignedScope = iamService.presignedScope(akid);
|
||||
if (presignedScope.isPresent()) {
|
||||
@@ -750,6 +752,10 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
for (Map<String, List<String>> targetContext : targetContexts) {
|
||||
Map<String, List<String>> effectiveContext = IamConditionContextResolver.withGlobalContext(
|
||||
targetContext, resource, region, accountId, resourceOwnerAccountId);
|
||||
if ("logs".equals(credentialScope)
|
||||
&& ("logs:CreateLogStream".equals(action) || "logs:DeleteLogStream".equals(action))) {
|
||||
effectiveContext.put("aws:ResourceAccount", List.of(accountId));
|
||||
}
|
||||
if ("dynamodb".equals(credentialScope)) {
|
||||
Map<String, List<String>> batchContext =
|
||||
conditionContextResolver.dynamoDbBatchConditionContext(action, ctx, resource);
|
||||
|
||||
+15
@@ -22,10 +22,13 @@ import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ApplicationScoped
|
||||
public class CloudWatchLogsHandler {
|
||||
|
||||
private static final Pattern LOG_GROUP_NAME = Pattern.compile("[._/#A-Za-z0-9-]+");
|
||||
|
||||
private final CloudWatchLogsService logsService;
|
||||
private final CloudWatchLogsCrossAccountService crossAccountService;
|
||||
private final CloudWatchLogsMetricFilterHandler metricFilterHandler;
|
||||
@@ -248,6 +251,7 @@ public class CloudWatchLogsHandler {
|
||||
private Response handleCreateLogStream(JsonNode request, String region) {
|
||||
String groupName = request.path("logGroupName").asText();
|
||||
String streamName = request.path("logStreamName").asText();
|
||||
requireLogStreamNames(groupName, streamName);
|
||||
logsService.createLogStream(groupName, streamName, region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
@@ -255,10 +259,21 @@ public class CloudWatchLogsHandler {
|
||||
private Response handleDeleteLogStream(JsonNode request, String region) {
|
||||
String groupName = request.path("logGroupName").asText();
|
||||
String streamName = request.path("logStreamName").asText();
|
||||
requireLogStreamNames(groupName, streamName);
|
||||
logsService.deleteLogStream(groupName, streamName, region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private static void requireLogStreamNames(String group, String stream) {
|
||||
if (group.isEmpty() || group.length() > 512 || !LOG_GROUP_NAME.matcher(group).matches()) {
|
||||
throw new AwsException("InvalidParameterException", "Invalid logGroupName.", 400);
|
||||
}
|
||||
int streamLength = stream.codePointCount(0, stream.length());
|
||||
if (streamLength < 1 || streamLength > 512 || stream.indexOf(':') >= 0 || stream.indexOf('*') >= 0) {
|
||||
throw new AwsException("InvalidParameterException", "Invalid logStreamName.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleDescribeLogStreams(JsonNode request, String region) {
|
||||
String groupName = resolveLogGroupName(request);
|
||||
String prefix = request.path("logStreamNamePrefix").asText(null);
|
||||
|
||||
+7
-1
@@ -424,6 +424,7 @@ public class CloudWatchLogsService implements ResourceProvider {
|
||||
public Map<String, String> listTagsLogGroup(String groupName, String region) {
|
||||
String key = groupKey(region, groupName);
|
||||
LogGroup group = groupStore.get(key)
|
||||
.filter(stored -> groupName.equals(stored.getLogGroupName()))
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"The specified log group does not exist: " + groupName, 400));
|
||||
return group.getTags();
|
||||
@@ -439,6 +440,7 @@ public class CloudWatchLogsService implements ResourceProvider {
|
||||
String accountId, String groupName, String streamName, String region) {
|
||||
String groupKey = groupKey(region, groupName);
|
||||
getForAccount(groupStore, accountId, groupKey)
|
||||
.filter(stored -> groupName.equals(stored.getLogGroupName()))
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"The specified log group does not exist: " + groupName, 400));
|
||||
|
||||
@@ -460,6 +462,8 @@ public class CloudWatchLogsService implements ResourceProvider {
|
||||
public void deleteLogStream(String groupName, String streamName, String region) {
|
||||
String streamKey = streamKey(region, groupName, streamName);
|
||||
streamStore.get(streamKey)
|
||||
.filter(stored -> groupName.equals(stored.getLogGroupName())
|
||||
&& streamName.equals(stored.getLogStreamName()))
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"The specified log stream does not exist: " + streamName, 400));
|
||||
|
||||
@@ -1264,8 +1268,10 @@ public class CloudWatchLogsService implements ResourceProvider {
|
||||
|
||||
private void deleteEventsForStream(String region, String groupName, String streamName) {
|
||||
String eventPrefix = eventKeyPrefix(region, groupName, streamName);
|
||||
String groupPrefix = streamKeyPrefix(region, groupName);
|
||||
List<String> keys = eventStore.keys().stream()
|
||||
.filter(k -> k.startsWith(eventPrefix))
|
||||
.filter(k -> k.startsWith(eventPrefix)
|
||||
&& streamName.equals(streamNameFromEventKey(k, groupPrefix)))
|
||||
.toList();
|
||||
keys.forEach(eventStore::delete);
|
||||
}
|
||||
|
||||
@@ -97,6 +97,23 @@ public class ResourceArnBuilder {
|
||||
};
|
||||
}
|
||||
|
||||
/** Uses the serving operation, not an independently parsed target header. */
|
||||
public List<String> buildLogStreamResources(String action, ContainerRequestContext ctx,
|
||||
String region, String accountId, String partition) {
|
||||
if (!("logs:CreateLogStream".equals(action) || "logs:DeleteLogStream".equals(action))) {
|
||||
return List.of("*");
|
||||
}
|
||||
JsonNode body = readJsonBody(ctx);
|
||||
if (body == null) {
|
||||
return List.of("*");
|
||||
}
|
||||
// Match the handler's coercion and raw names, including whitespace and numeric values.
|
||||
String group = body.path("logGroupName").asText();
|
||||
String stream = body.path("logStreamName").asText();
|
||||
return List.of(new AwsArnUtils.Arn(partition, "logs", region, accountId,
|
||||
"log-group:" + group + ":log-stream:" + stream).toString());
|
||||
}
|
||||
|
||||
// ── IAM ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
/** A stored entity's ARN and path, the two things a target ARN is built from. */
|
||||
|
||||
Reference in new issue
Block a user