mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(logs): preserve and manage destination tags (#8030)
This commit is contained in:
1 parent
a6da82af8a
commit
a609eb2824
15 files changed
+402
-17
No files matched your search
@@ -3,6 +3,7 @@ package io.floci.aws.core.common;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.services.cloudwatch.logs.CloudWatchLogsCrossAccountService;
|
||||
import io.floci.aws.services.cloudwatch.logs.CloudWatchLogsDeliveryService;
|
||||
import io.floci.aws.services.cloudwatch.logs.CloudWatchLogsService;
|
||||
import io.floci.aws.services.cloudwatch.logs.LogGroupTagTarget;
|
||||
@@ -99,6 +100,7 @@ public class IamConditionContextResolver {
|
||||
private final Instance<Ec2Service> ec2Service;
|
||||
private final Instance<S3Service> s3Service;
|
||||
private final Instance<CloudWatchLogsService> logsService;
|
||||
private final Instance<CloudWatchLogsCrossAccountService> crossAccountService;
|
||||
private final Instance<IamService> iamService;
|
||||
private final Instance<LambdaWebService> lambdaWebService;
|
||||
private final RequestContext requestContext;
|
||||
@@ -116,13 +118,15 @@ public class IamConditionContextResolver {
|
||||
Instance<Ec2Service> ec2Service,
|
||||
Instance<S3Service> s3Service,
|
||||
Instance<CloudWatchLogsService> logsService,
|
||||
Instance<CloudWatchLogsCrossAccountService> crossAccountService,
|
||||
Instance<IamService> iamService,
|
||||
Instance<LambdaWebService> lambdaWebService,
|
||||
RequestContext requestContext,
|
||||
EmulatorConfig config,
|
||||
CurrentVertxRequest currentVertxRequest,
|
||||
ResolvedServiceCatalog serviceCatalog) {
|
||||
this(dynamoDbFacade, ec2Service, s3Service, logsService, iamService, lambdaWebService, requestContext,
|
||||
this(dynamoDbFacade, ec2Service, s3Service, logsService, crossAccountService, iamService,
|
||||
lambdaWebService, requestContext,
|
||||
config, currentVertxRequest, serviceCatalog, Clock.systemUTC());
|
||||
}
|
||||
|
||||
@@ -130,6 +134,7 @@ public class IamConditionContextResolver {
|
||||
Instance<Ec2Service> ec2Service,
|
||||
Instance<S3Service> s3Service,
|
||||
Instance<CloudWatchLogsService> logsService,
|
||||
Instance<CloudWatchLogsCrossAccountService> crossAccountService,
|
||||
Instance<IamService> iamService,
|
||||
Instance<LambdaWebService> lambdaWebService,
|
||||
RequestContext requestContext,
|
||||
@@ -140,6 +145,7 @@ public class IamConditionContextResolver {
|
||||
this.ec2Service = ec2Service;
|
||||
this.s3Service = s3Service;
|
||||
this.logsService = logsService;
|
||||
this.crossAccountService = crossAccountService;
|
||||
this.iamService = iamService;
|
||||
this.lambdaWebService = lambdaWebService;
|
||||
this.requestContext = requestContext;
|
||||
@@ -164,7 +170,7 @@ public class IamConditionContextResolver {
|
||||
};
|
||||
}
|
||||
|
||||
/** Group mutations and stream access use the handler's group and request scope. */
|
||||
/** Logs tag operations and group mutations use the handler's resource and request scope. */
|
||||
private Map<String, List<String>> logsConditionContext(String action, ContainerRequestContext ctx) {
|
||||
if (!("logs:CreateLogStream".equals(action) || "logs:DeleteLogStream".equals(action)
|
||||
|| LogGroupTagTarget.supports(action) || "logs:CreateLogGroup".equals(action) || "logs:DeleteLogGroup".equals(action)
|
||||
@@ -189,12 +195,14 @@ public class IamConditionContextResolver {
|
||||
conditions.put("aws:TagKeys", keys);
|
||||
return conditions;
|
||||
}
|
||||
if (!logsService.isResolvable()) {
|
||||
boolean direct = LogGroupTagTarget.supports(action);
|
||||
boolean destination = direct && CloudWatchLogsCrossAccountService.isCrossAccountDestinationArn(
|
||||
body.path("resourceArn").asText());
|
||||
if (destination ? !crossAccountService.isResolvable() : !logsService.isResolvable()) {
|
||||
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 -> {
|
||||
@@ -215,16 +223,20 @@ public class IamConditionContextResolver {
|
||||
// A delivery source, destination or delivery: its request tags apply, its own tags are not read.
|
||||
return conditions.isEmpty() ? null : conditions;
|
||||
}
|
||||
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(group, region);
|
||||
if (destination) {
|
||||
tags = crossAccountService.get().listDestinationTags(body.path("resourceArn").asText(), region);
|
||||
} else {
|
||||
String group = direct ? logsService.get().resolveTagTarget(body.path("resourceArn").asText(), region).name()
|
||||
: body.path("logGroupName").asText();
|
||||
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.
|
||||
// Leave the handler to report a missing resource; unexpected lookup errors must not hide a deny.
|
||||
return conditions.isEmpty() ? null : conditions;
|
||||
}
|
||||
if (tags == null || tags.isEmpty()) {
|
||||
|
||||
+46
-1
@@ -15,6 +15,7 @@ import jakarta.inject.Inject;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -62,7 +63,7 @@ public class CloudWatchLogsCrossAccountService {
|
||||
}
|
||||
|
||||
public synchronized LogDestination putDestination(String destinationName, String targetArn,
|
||||
String roleArn, String region) {
|
||||
String roleArn, Map<String, String> tags, String region) {
|
||||
validateDestinationName(destinationName);
|
||||
requireArn(targetArn, "targetArn");
|
||||
requireArn(roleArn, "roleArn");
|
||||
@@ -74,11 +75,55 @@ public class CloudWatchLogsCrossAccountService {
|
||||
destination.setArn(AwsArnUtils.Arn.of("logs", region, regionResolver.getAccountId(), "destination:" + destinationName).toString());
|
||||
if (destination.getCreationTime() == 0) {
|
||||
destination.setCreationTime(System.currentTimeMillis());
|
||||
destination.setTags(tags);
|
||||
}
|
||||
destinations.put(key, destination);
|
||||
return destination;
|
||||
}
|
||||
|
||||
public static boolean isCrossAccountDestinationArn(String arn) {
|
||||
if (!AwsArnUtils.isArn(arn)) {
|
||||
return false;
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
return "logs".equals(parsed.service()) && parsed.resource().startsWith("destination:");
|
||||
}
|
||||
|
||||
public synchronized Map<String, String> listDestinationTags(String arn, String region) {
|
||||
return new LinkedHashMap<>(destinationByArn(arn, region).getTags());
|
||||
}
|
||||
|
||||
public synchronized void tagDestination(String arn, Map<String, String> tags, String region) {
|
||||
LogDestination destination = destinationByArn(arn, region);
|
||||
destination.getTags().putAll(tags);
|
||||
destinations.put(destinationKey(region, destination.getDestinationName()), destination);
|
||||
}
|
||||
|
||||
public synchronized void untagDestination(String arn, List<String> tagKeys, String region) {
|
||||
LogDestination destination = destinationByArn(arn, region);
|
||||
tagKeys.forEach(destination.getTags()::remove);
|
||||
destinations.put(destinationKey(region, destination.getDestinationName()), destination);
|
||||
}
|
||||
|
||||
private LogDestination destinationByArn(String arn, String region) {
|
||||
if (!isCrossAccountDestinationArn(arn)) {
|
||||
throw destinationNotFound();
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
if (!region.equals(parsed.region()) || !regionResolver.getAccountId().equals(parsed.accountId())
|
||||
|| !regionResolver.partitionForRegion(region).equals(parsed.partition())) {
|
||||
throw destinationNotFound();
|
||||
}
|
||||
String name = parsed.resource().substring("destination:".length());
|
||||
return destinations.get(destinationKey(region, name))
|
||||
.filter(destination -> arn.equals(destination.getArn()))
|
||||
.orElseThrow(CloudWatchLogsCrossAccountService::destinationNotFound);
|
||||
}
|
||||
|
||||
private static AwsException destinationNotFound() {
|
||||
return new AwsException("ResourceNotFoundException", "The specified destination does not exist.", 400);
|
||||
}
|
||||
|
||||
public synchronized void putDestinationPolicy(String destinationName, String accessPolicy, String region) {
|
||||
validateDestinationName(destinationName);
|
||||
requireJsonObject(accessPolicy, "accessPolicy", DESTINATION_POLICY_MAX_BYTES);
|
||||
|
||||
@@ -254,6 +254,7 @@ public class CloudWatchLogsHandler {
|
||||
request.path("destinationName").asText(null),
|
||||
request.path("targetArn").asText(null),
|
||||
request.path("roleArn").asText(null),
|
||||
extractTags(request.path("tags")),
|
||||
region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("destination", buildDestination(destination));
|
||||
@@ -646,6 +647,11 @@ public class CloudWatchLogsHandler {
|
||||
response.set("tags", objectMapper.valueToTree(deliveryService.listTags(resourceArn, region)));
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
if (CloudWatchLogsCrossAccountService.isCrossAccountDestinationArn(resourceArn)) {
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("tags", objectMapper.valueToTree(crossAccountService.listDestinationTags(resourceArn, region)));
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
String groupName = logsService.resolveTagTarget(resourceArn, region).name();
|
||||
Map<String, String> tags = logsService.listTagsLogGroup(groupName, region);
|
||||
|
||||
@@ -663,6 +669,10 @@ public class CloudWatchLogsHandler {
|
||||
deliveryService.tag(resourceArn, tags, region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
if (CloudWatchLogsCrossAccountService.isCrossAccountDestinationArn(resourceArn)) {
|
||||
crossAccountService.tagDestination(resourceArn, tags, region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
String groupName = logsService.resolveTagTarget(resourceArn, region).name();
|
||||
logsService.tagLogGroup(groupName, tags, region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
@@ -676,6 +686,10 @@ public class CloudWatchLogsHandler {
|
||||
deliveryService.untag(resourceArn, tagKeys, region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
if (CloudWatchLogsCrossAccountService.isCrossAccountDestinationArn(resourceArn)) {
|
||||
crossAccountService.untagDestination(resourceArn, tagKeys, region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
String groupName = logsService.resolveTagTarget(resourceArn, region).name();
|
||||
logsService.untagLogGroup(groupName, tagKeys, region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
|
||||
@@ -3,6 +3,9 @@ package io.floci.aws.services.cloudwatch.logs.model;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class LogDestination {
|
||||
@@ -12,6 +15,7 @@ public class LogDestination {
|
||||
private String accessPolicy;
|
||||
private String arn;
|
||||
private long creationTime;
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
|
||||
public String getDestinationName() { return destinationName; }
|
||||
public void setDestinationName(String destinationName) { this.destinationName = destinationName; }
|
||||
@@ -25,4 +29,8 @@ public class LogDestination {
|
||||
public void setArn(String arn) { this.arn = arn; }
|
||||
public long getCreationTime() { return creationTime; }
|
||||
public void setCreationTime(long creationTime) { this.creationTime = creationTime; }
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags);
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import io.floci.aws.core.common.ServicePrincipals;
|
||||
import io.floci.aws.core.common.SigV4CredentialScope;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.common.auth.SigV4Canonicalization;
|
||||
import io.floci.aws.services.cloudwatch.logs.CloudWatchLogsCrossAccountService;
|
||||
import io.floci.aws.services.cloudwatch.logs.CloudWatchLogsDeliveryService;
|
||||
import io.floci.aws.services.cloudwatch.logs.LogGroupTagTarget;
|
||||
import io.floci.aws.services.eks.EksController;
|
||||
@@ -304,7 +305,7 @@ public class ResourceArnBuilder {
|
||||
/** Uses the serving operation and the same raw names as the handler. */
|
||||
public List<String> buildLogResources(String action, ContainerRequestContext ctx,
|
||||
String region, String accountId, String partition) {
|
||||
// Operations the vendored table reads (the vended-log delivery family) take their resources from it.
|
||||
// Operations the vendored table reads take their resources from it.
|
||||
if (RequestResources.reads(action)) {
|
||||
return namedResources("logs", action, ctx, region, accountId, partition);
|
||||
}
|
||||
@@ -327,6 +328,9 @@ public class ResourceArnBuilder {
|
||||
if (CloudWatchLogsDeliveryService.isDeliveryResourceArn(resourceArn)) {
|
||||
return List.of(resourceArn);
|
||||
}
|
||||
if (CloudWatchLogsCrossAccountService.isCrossAccountDestinationArn(resourceArn)) {
|
||||
return List.of(resourceArn);
|
||||
}
|
||||
return List.of(LogGroupTagTarget.resolve(resourceArn, region, accountId, partition).arn());
|
||||
}
|
||||
String name = body.path("logGroupName").asText();
|
||||
|
||||
Reference in new issue
Block a user