From cd85191a24d3d5ea420081b5b23d52c9bb43ebc0 Mon Sep 17 00:00:00 2001
From: mrfelek <17274927+mrfelek@users.noreply.github.com>
Date: Mon, 23 Nov 2026 01:28:32 +0100
Subject: [PATCH] fix(aps): give resource-tag conditions the tags of the
workspace or rule groups namespace a request names (#9230)
* refactor(test): share the signed user of an account of the test's own
* fix(aps): give resource-tag conditions the tags of the workspace or rule groups namespace a request names
---
docs/services/iam.md | 5 +-
.../aws/core/common/StoredResourceArns.java | 3 +-
.../io/floci/aws/services/aps/ApsService.java | 147 +++++++++++++-----
.../aps/model/PrometheusWorkspace.java | 2 +-
.../aps/model/RuleGroupsNamespace.java | 2 +-
.../services/aps/ApsIamIntegrationTest.java | 87 +++++++++++
.../aws/services/aps/ApsServiceTest.java | 23 +++
...epFunctionsCallbackIamIntegrationTest.java | 10 +-
.../io/floci/aws/testing/SignedIamUsers.java | 27 +++-
9 files changed, 254 insertions(+), 52 deletions(-)
diff --git a/docs/services/iam.md b/docs/services/iam.md
index c16f67cf0e..8fa04905eb 100644
--- a/docs/services/iam.md
+++ b/docs/services/iam.md
@@ -3733,8 +3733,9 @@ floci populates:
queues, SNS for its topics, Kinesis Data Streams for its streams and consumers, EventBridge Pipes for its pipes,
Aurora DSQL for its clusters, EKS for its clusters, node groups, Fargate profiles, add-ons, Pod Identity
associations and access entries, Resource Groups for its groups, CloudWatch Logs for its log groups,
- destinations, delivery sources, delivery destinations and deliveries, and Pricing Calculator for its workload
- estimates. Tag keys differing only in case are one condition key holding every value.
+ destinations, delivery sources, delivery destinations and deliveries, Pricing Calculator for its workload
+ estimates, and Amazon Managed Service for Prometheus for its workspaces and rule groups namespaces. Tag keys
+ differing only in case are one condition key holding every value.
- EC2 resource keys such as a network interface's `ec2:Vpc`, `ec2:Subnet`, `ec2:AvailabilityZone` and
`ec2:NetworkInterfaceID`: for every action on a resource EC2 holds, the members of the resource the
key names, an ARN for a key the Service Reference types as one. `resourceKeys` in
diff --git a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java
index 80d9deec9b..5c47f68c70 100644
--- a/src/main/java/io/floci/aws/core/common/StoredResourceArns.java
+++ b/src/main/java/io/floci/aws/core/common/StoredResourceArns.java
@@ -65,7 +65,8 @@ import java.util.Optional;
* for the tags of a cluster; EKS, for the tags of a cluster, a node group, a Fargate profile, an add-on, a Pod Identity
* association or an access entry; Resource Groups, for the tags of a group; CloudWatch Logs, for a delivery, the
* delivery destinations of a delivery source and the tags of a log group, a destination, a delivery source, a
- * delivery destination or a delivery; Pricing Calculator, for the tags of a workload estimate); looked up lazily by
+ * delivery destination or a delivery; Pricing Calculator, for the tags of a workload estimate; Amazon Managed Service
+ * for Prometheus, for the tags of a workspace or a rule groups namespace); looked up lazily by
* {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through {@code Instance} for
* the namespace a request or an ARN is of, so IAM never depends on that service directly.
*
diff --git a/src/main/java/io/floci/aws/services/aps/ApsService.java b/src/main/java/io/floci/aws/services/aps/ApsService.java
index cd539cca72..e619b8058f 100644
--- a/src/main/java/io/floci/aws/services/aps/ApsService.java
+++ b/src/main/java/io/floci/aws/services/aps/ApsService.java
@@ -7,7 +7,9 @@ import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.PaginatedResult;
import io.floci.aws.core.common.Pagination;
import io.floci.aws.core.common.RegionResolver;
+import io.floci.aws.core.common.StoredResourceArns;
import io.floci.aws.core.common.TagHandler;
+import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.StorageBackend;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.aps.model.PrometheusWorkspace;
@@ -17,14 +19,18 @@ import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.time.Instant;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
+import java.util.Optional;
import java.util.UUID;
+import java.util.function.Consumer;
import java.util.regex.Pattern;
@ApplicationScoped
-public class ApsService implements TagHandler {
+public class ApsService implements TagHandler, StoredResourceArns {
private static final Logger LOG = Logger.getLogger(ApsService.class);
// AMP's ListWorkspacesRequest declares maxResults with a default of 100 and a maximum of 1000.
@@ -33,6 +39,8 @@ public class ApsService implements TagHandler {
private static final int MAX_NAMESPACE_NAME_LENGTH = 128;
private static final Pattern NAMESPACE_NAME = Pattern.compile(".*[0-9A-Za-z][-.0-9A-Z_a-z]*.*");
private static final Pattern CLIENT_TOKEN = Pattern.compile("[!-~]{1,64}");
+ private static final String WORKSPACE_PREFIX = "workspace/";
+ private static final String NAMESPACE_PREFIX = "rulegroupsnamespace/";
private final StorageBackend storage;
private final StorageBackend namespaceStorage;
@@ -87,9 +95,7 @@ public class ApsService implements TagHandler {
"https://" + AwsEndpoints.host("aps-workspaces", region) + "/workspaces/" + workspaceId + "/");
workspace.setCreatedAt(Instant.now());
workspace.setKmsKeyArn(kmsKeyArn);
- if (tags != null) {
- workspace.getTags().putAll(tags);
- }
+ workspace.setTags(tags);
PrometheusWorkspace response = clientToken != null ? copyWorkspace(workspace) : workspace;
if (clientToken != null) {
@@ -136,7 +142,7 @@ public class ApsService implements TagHandler {
DEFAULT_PAGE, MAX_PAGE, "ValidationException");
}
- public void deleteWorkspace(String region, String workspaceId) {
+ public synchronized void deleteWorkspace(String region, String workspaceId) {
describeWorkspace(region, workspaceId);
String namespacePrefix = namespaceKeyPrefix(region, workspaceId);
for (RuleGroupsNamespace namespace : namespaceStorage.scan(k -> k.startsWith(namespacePrefix))) {
@@ -146,14 +152,14 @@ public class ApsService implements TagHandler {
LOG.infov("Deleted AMP workspace: {0} in {1}", workspaceId, region);
}
- public void updateWorkspaceAlias(String region, String workspaceId, String alias) {
+ public synchronized void updateWorkspaceAlias(String region, String workspaceId, String alias) {
PrometheusWorkspace workspace = describeWorkspace(region, workspaceId);
workspace.setAlias(stripAlias(alias));
storage.put(key(region, workspaceId), workspace);
}
- public RuleGroupsNamespace createRuleGroupsNamespace(String region, String workspaceId, String name,
- String encodedData, Map tags) {
+ public synchronized RuleGroupsNamespace createRuleGroupsNamespace(String region, String workspaceId, String name,
+ String encodedData, Map tags) {
describeWorkspace(region, workspaceId);
requireNamespaceName(name);
requireNamespaceData(encodedData);
@@ -172,9 +178,7 @@ public class ApsService implements TagHandler {
Instant now = Instant.now();
namespace.setCreatedAt(now);
namespace.setModifiedAt(now);
- if (tags != null) {
- namespace.getTags().putAll(tags);
- }
+ namespace.setTags(tags);
namespaceStorage.put(namespaceKey(region, workspaceId, name), namespace);
LOG.infov("Created AMP rule groups namespace: {0} in workspace {1}", name, workspaceId);
@@ -201,8 +205,8 @@ public class ApsService implements TagHandler {
DEFAULT_PAGE, MAX_PAGE, "ValidationException");
}
- public RuleGroupsNamespace putRuleGroupsNamespace(String region, String workspaceId, String name,
- String encodedData) {
+ public synchronized RuleGroupsNamespace putRuleGroupsNamespace(String region, String workspaceId, String name,
+ String encodedData) {
requireNamespaceData(encodedData);
RuleGroupsNamespace namespace = describeRuleGroupsNamespace(region, workspaceId, name);
namespace.setEncodedData(encodedData);
@@ -211,7 +215,7 @@ public class ApsService implements TagHandler {
return namespace;
}
- public void deleteRuleGroupsNamespace(String region, String workspaceId, String name) {
+ public synchronized void deleteRuleGroupsNamespace(String region, String workspaceId, String name) {
describeRuleGroupsNamespace(region, workspaceId, name);
namespaceStorage.delete(namespaceKey(region, workspaceId, name));
LOG.infov("Deleted AMP rule groups namespace: {0} in workspace {1}", name, workspaceId);
@@ -241,7 +245,7 @@ public class ApsService implements TagHandler {
}
@Override
- public void tagResource(String region, String arn, Map tags) {
+ public synchronized void tagResource(String region, String arn, Map tags) {
// Per AMP's TagResource: keys must not begin with the reserved "aws:" prefix
// (case-insensitive, matching the sibling checks in FisService and BatchService).
for (String tagKey : tags.keySet()) {
@@ -251,18 +255,25 @@ public class ApsService implements TagHandler {
}
}
Taggable taggable = taggableByArn(region, arn);
- taggable.tags().putAll(tags);
- taggable.persist().run();
+ Map tagged = new HashMap<>(taggable.tags());
+ tagged.putAll(tags);
+ taggable.store().accept(tagged);
}
@Override
- public void untagResource(String region, String arn, List tagKeys) {
+ public synchronized void untagResource(String region, String arn, List tagKeys) {
Taggable taggable = taggableByArn(region, arn);
- tagKeys.forEach(taggable.tags()::remove);
- taggable.persist().run();
+ Map untagged = new HashMap<>(taggable.tags());
+ tagKeys.forEach(untagged::remove);
+ taggable.store().accept(untagged);
}
- private record Taggable(Map tags, Runnable persist) {
+ /**
+ * A resource's tags and how to store a new set of them. A tag change stores a whole new map in the resource's
+ * volatile field, never changing a stored one, so a reader of the tags ({@link #storedTags}), which takes no lock,
+ * sees one whole set; it does so under the service's lock, so two changes cannot lose each other's tags.
+ */
+ private record Taggable(Map tags, Consumer