fix(logs): bind ARN tag operations to their owning group (#5810)

* fix(logs): bind ARN tag operations to their owning group

* fix(logs): preserve literal names in tag requests

* test(logs): share authorization fixtures
This commit is contained in:
David Phillips authored and GitHub committed 2026-10-16 18:23:59 -04:00
1 parent 1f6261fc1c
commit 49a3ff9a10
16 files changed
+720 -169

No files matched your search

@@ -4,6 +4,7 @@ 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.cloudwatch.logs.LogGroupTagTarget;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbConditionKeys;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbFacade;
import io.github.hectorvent.floci.services.dynamodb.DynamoDbTableNames;
@@ -106,7 +107,8 @@ public class IamConditionContextResolver {
/** 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))) {
if (!("logs:CreateLogStream".equals(action) || "logs:DeleteLogStream".equals(action)
|| LogGroupTagTarget.supports(action))) {
return null;
}
JsonNode body = bufferedJsonBody(ctx);
@@ -114,20 +116,39 @@ public class IamConditionContextResolver {
return null;
}
String region = requestContext.getRegion() == null ? config.defaultRegion() : requestContext.getRegion();
Map<String, List<String>> conditions = new LinkedHashMap<>();
boolean direct = LogGroupTagTarget.supports(action);
if ("logs:TagResource".equals(action) && body.path("tags").isObject()) {
List<String> keys = new ArrayList<>();
body.path("tags").fields().forEachRemaining(entry -> {
keys.add(entry.getKey());
conditions.put(REQUEST_TAG_PREFIX + entry.getKey(), List.of(entry.getValue().asText()));
});
if (!keys.isEmpty()) {
conditions.put("aws:TagKeys", keys);
}
} else if ("logs:UntagResource".equals(action)) {
List<String> keys = new ArrayList<>();
body.path("tagKeys").forEach(key -> keys.add(key.asText()));
if (!keys.isEmpty()) {
conditions.put("aws:TagKeys", keys);
}
}
String group = direct ? logsService.get().resolveTagTarget(body.path("resourceArn").asText(), region).name()
: body.path("logGroupName").asText();
Map<String, String> tags;
try {
tags = logsService.get().listTagsLogGroup(body.path("logGroupName").asText(), region);
tags = logsService.get().listTagsLogGroup(group, 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;
return conditions.isEmpty() ? null : conditions;
}
if (tags == null || tags.isEmpty()) {
return null;
return conditions.isEmpty() ? null : conditions;
}
Map<String, List<String>> conditions = new LinkedHashMap<>();
tags.forEach((key, value) -> conditions.put(RESOURCE_TAG_PREFIX + key,
List.of(value == null ? "" : value)));
return conditions;
@@ -549,7 +549,7 @@ public class CloudWatchLogsHandler {
private Response handleListTagsForResource(JsonNode request, String region) {
String resourceArn = request.path("resourceArn").asText();
String groupName = extractLogGroupNameFromArn(resourceArn);
String groupName = logsService.resolveTagTarget(resourceArn, region).name();
Map<String, String> tags = logsService.listTagsLogGroup(groupName, region);
ObjectNode response = objectMapper.createObjectNode();
@@ -561,7 +561,7 @@ public class CloudWatchLogsHandler {
private Response handleTagResource(JsonNode request, String region) {
String resourceArn = request.path("resourceArn").asText();
String groupName = extractLogGroupNameFromArn(resourceArn);
String groupName = logsService.resolveTagTarget(resourceArn, region).name();
Map<String, String> tags = extractTags(request.path("tags"));
logsService.tagLogGroup(groupName, tags, region);
return Response.ok(objectMapper.createObjectNode()).build();
@@ -569,7 +569,7 @@ public class CloudWatchLogsHandler {
private Response handleUntagResource(JsonNode request, String region) {
String resourceArn = request.path("resourceArn").asText();
String groupName = extractLogGroupNameFromArn(resourceArn);
String groupName = logsService.resolveTagTarget(resourceArn, region).name();
List<String> tagKeys = new ArrayList<>();
request.path("tagKeys").forEach(k -> tagKeys.add(k.asText()));
logsService.untagLogGroup(groupName, tagKeys, region);
@@ -373,9 +373,14 @@ public class CloudWatchLogsService implements ResourceProvider {
groupStore.put(key, group);
}
public LogGroupTagTarget resolveTagTarget(String resourceArn, String region) {
return LogGroupTagTarget.resolve(resourceArn, region, regionResolver.getAccountId(), regionResolver.getPartition());
}
public void tagLogGroup(String groupName, Map<String, String> tags, 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));
group.getTags().putAll(tags);
@@ -385,6 +390,7 @@ public class CloudWatchLogsService implements ResourceProvider {
public void untagLogGroup(String groupName, List<String> tagKeys, 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));
tagKeys.forEach(group.getTags()::remove);
@@ -0,0 +1,27 @@
package io.github.hectorvent.floci.services.cloudwatch.logs;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
/** The same locally owned group identity for direct tag authorization and execution. */
public record LogGroupTagTarget(String name, String arn) {
public static boolean supports(String action) {
return "logs:TagResource".equals(action) || "logs:UntagResource".equals(action)
|| "logs:ListTagsForResource".equals(action);
}
public static LogGroupTagTarget resolve(String input, String region, String account, String partition) {
String prefix = new AwsArnUtils.Arn(partition, "logs", region, account, "log-group:").toString();
if (AwsArnUtils.isArn(input) && !input.startsWith(prefix)) {
throw new AwsException("ResourceNotFoundException", "The specified log group does not exist.", 400);
}
// Plain names remain literal; a marker inside a name must not select another group.
String name = input.startsWith(prefix) ? input.substring(prefix.length()) : input;
if (input.startsWith(prefix) && name.endsWith(":*")) {
name = name.substring(0, name.length() - 2);
}
return new LogGroupTagTarget(name,
new AwsArnUtils.Arn(partition, "logs", region, account, "log-group:" + name).toString());
}
}
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.AwsQueryServiceResolver;
import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.ServicePrincipals;
import io.github.hectorvent.floci.core.common.SigV4CredentialScope;
import io.github.hectorvent.floci.services.cloudwatch.logs.LogGroupTagTarget;
import io.github.hectorvent.floci.services.lambda.LambdaArnUtils;
import io.github.hectorvent.floci.services.lambda.durable.DurableTokens;
import jakarta.enterprise.context.ApplicationScoped;
@@ -100,13 +101,17 @@ 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))) {
if (!("logs:CreateLogStream".equals(action) || "logs:DeleteLogStream".equals(action)
|| LogGroupTagTarget.supports(action))) {
return List.of("*");
}
JsonNode body = readJsonBody(ctx);
if (body == null) {
return List.of("*");
}
if (LogGroupTagTarget.supports(action)) {
return List.of(LogGroupTagTarget.resolve(body.path("resourceArn").asText(), region, accountId, partition).arn());
}
// 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();