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
This commit is contained in:
mrfelek authored and GitHub committed 2026-11-22 20:28:32 -04:00
1 parent 739a22ab32
commit cd85191a24
9 files changed
+254 -52

No files matched your search

@@ -1,14 +1,20 @@
package io.floci.aws.services.aps;
import io.floci.aws.services.iam.IamService;
import io.floci.aws.services.iam.model.AccessKey;
import io.floci.aws.testing.PartitionCleanup;
import io.floci.aws.testing.SignedIamEnforcementProfile;
import io.floci.aws.testing.SignedIamUsers;
import io.floci.aws.testing.SignedRestRequests;
import io.floci.aws.testing.TestAccounts;
import io.floci.aws.testutil.AwsRequestSigner;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.Response;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import java.net.URI;
import java.net.URLEncoder;
@@ -32,6 +38,7 @@ import static org.hamcrest.Matchers.nullValue;
@TestProfile(SignedIamEnforcementProfile.class)
class ApsIamIntegrationTest {
private static final String TEAM_METRICS = "{\"tags\":{\"team\":\"metrics\"}}";
private static final String RULES = Base64.getEncoder()
.encodeToString("groups: []\n".getBytes(StandardCharsets.UTF_8));
@@ -40,6 +47,12 @@ class ApsIamIntegrationTest {
@TestHTTPResource("/")
URI endpoint;
@Inject
IamService iam;
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@Test
void aWorkspaceReadIsRefusedWithoutThePermissionAllowedWithItAndRefusedOnceItIsRemoved() throws Exception {
String workspaceId = createWorkspace();
@@ -203,6 +216,80 @@ class ApsIamIntegrationTest {
response.then().statusCode(400).body("message", equalTo("Invalid resource ARN: " + arn));
}
/**
* An {@code aws:ResourceTag} condition reads the tags AMP holds on the workspace a request names, as tag calls
* leave them: an allow on one team's tag reads that team's workspace, not an untagged one, no longer reads it once
* {@code UntagResource} has removed the tag, and reads the other once {@code TagResource} has added it.
*/
@Test
void aResourceTagConditionReadsTheTagsOfTheWorkspaceARequestNames() throws Exception {
String account = TestAccounts.randomId();
AwsRequestSigner owner = user(account, "aps-owner", allow("\"aps:*\"", "*"));
AwsRequestSigner caller = user(account, "aps-tags", onTeamMetrics("aps:DescribeWorkspace"));
Workspace tagged = workspace(owner, TEAM_METRICS);
Workspace untagged = workspace(owner, "{}");
send(caller, "GET", "/workspaces/" + tagged.id(), null).then().statusCode(200)
.body("workspace.arn", equalTo(tagged.arn()));
denied(send(caller, "GET", "/workspaces/" + untagged.id(), null), "aps:DescribeWorkspace");
send(owner, "DELETE", "/tags/" + encoded(tagged.arn()) + "?tagKeys=team", null).then().statusCode(200);
denied(send(caller, "GET", "/workspaces/" + tagged.id(), null), "aps:DescribeWorkspace");
send(owner, "POST", "/tags/" + encoded(untagged.arn()), TEAM_METRICS).then().statusCode(200);
send(caller, "GET", "/workspaces/" + untagged.id(), null).then().statusCode(200)
.body("workspace.arn", equalTo(untagged.arn()));
}
/** As for a workspace, for the rule groups namespace a request names by its workspace and name. */
@Test
void aResourceTagConditionReadsTheTagsOfTheRuleGroupsNamespaceARequestNames() throws Exception {
String account = TestAccounts.randomId();
AwsRequestSigner owner = user(account, "aps-owner", allow("\"aps:*\"", "*"));
AwsRequestSigner caller = user(account, "aps-tags", onTeamMetrics("aps:DescribeRuleGroupsNamespace"));
String namespaces = "/workspaces/" + workspace(owner, "{}").id() + "/rulegroupsnamespaces";
String arn = send(owner, "POST", namespaces, tagged("metrics")).then().statusCode(202).extract().path("arn");
send(owner, "POST", namespaces, namespace("untagged")).then().statusCode(202);
send(caller, "GET", namespaces + "/tagged", null).then().statusCode(200)
.body("ruleGroupsNamespace.arn", equalTo(arn));
denied(send(caller, "GET", namespaces + "/untagged", null), "aps:DescribeRuleGroupsNamespace");
send(owner, "DELETE", "/tags/" + encoded(arn) + "?tagKeys=team", null).then().statusCode(200);
denied(send(caller, "GET", namespaces + "/tagged", null), "aps:DescribeRuleGroupsNamespace");
String untagged = arn.replace("/tagged", "/untagged");
send(owner, "POST", "/tags/" + encoded(untagged), TEAM_METRICS).then().statusCode(200);
send(caller, "GET", namespaces + "/untagged", null).then().statusCode(200)
.body("ruleGroupsNamespace.arn", equalTo(untagged));
}
/** A user of {@code account} with the inline policy {@code policy}, removed after the test, signing for AMP. */
private AwsRequestSigner user(String account, String name, String policy) {
AccessKey key = SignedIamUsers.createUser(iam, cleanup, account, name, "access", policy);
return AwsRequestSigner.signedAs(key.getAccessKeyId(), key.getSecretAccessKey(), "aps");
}
private record Workspace(String id, String arn) {
}
/** A workspace {@code owner} creates with {@code body}, deleted after the test. */
private Workspace workspace(AwsRequestSigner owner, String body) throws Exception {
Response created = send(owner, "POST", "/workspaces", body);
created.then().statusCode(202);
Workspace workspace = new Workspace(created.path("workspaceId"), created.path("arn"));
cleanup.register(() -> {
try {
send(owner, "DELETE", "/workspaces/" + workspace.id(), null).then().statusCode(202);
} catch (Exception e) {
throw new IllegalStateException(e);
}
});
return workspace;
}
/** An allow of {@code action} on any resource tagged {@code team} {@code metrics}. */
private static String onTeamMetrics(String action) {
return "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"" + action
+ "\",\"Resource\":\"*\",\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"metrics\"}}}]}";
}
private static String encoded(String arn) {
return URLEncoder.encode(arn, StandardCharsets.UTF_8);
}
@@ -391,4 +391,27 @@ class ApsServiceTest {
service.untagResource(US_EAST_1, arn, List.of("env"));
assertEquals(Map.of("team", "devops"), service.listTags(US_EAST_1, arn));
}
/**
* IAM's tag lookup reads the tags of the workspace or the rule groups namespace whose ARN it is given, as tag calls
* leave them; nothing for an ARN of another account or Region, of no such resource, or of another service.
*/
@Test
void storedTagsAreTheTagsOfTheWorkspaceOrTheNamespaceTheArnNames() {
PrometheusWorkspace workspace = service.createWorkspace(US_EAST_1, "tagged", Map.of("env", "test"), null);
String namespace = service.createRuleGroupsNamespace(US_EAST_1, workspace.getWorkspaceId(), "alerts", RULES,
Map.of("env", "test")).getArn();
for (String arn : List.of(workspace.getArn(), namespace)) {
service.tagResource(US_EAST_1, arn, Map.of("team", "metrics"));
service.untagResource(US_EAST_1, arn, List.of("env"));
assertEquals("aps", service.namespace());
assertEquals(Map.of("team", "metrics"), service.storedTags(arn));
assertEquals(Map.of(), service.storedTags(arn.replace(":000000000000:", ":111122223333:")));
assertEquals(Map.of(), service.storedTags(arn.replace(":" + US_EAST_1 + ":", ":" + EU_WEST_1 + ":")));
assertEquals(Map.of(), service.storedTags(arn + "0"));
assertEquals(Map.of(), service.storedTags(arn.replace(":aps:", ":ecs:")));
}
assertEquals(Map.of(), service.storedTags("arn:aws:aps:us-east-1:000000000000:scraper/s-1"));
}
}
@@ -1,7 +1,6 @@
package io.floci.aws.services.stepfunctions;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.RequestScopes;
import io.floci.aws.services.iam.IamService;
import io.floci.aws.services.iam.model.AccessKey;
import io.floci.aws.services.sqs.SqsService;
@@ -241,14 +240,7 @@ class StepFunctionsCallbackIamIntegrationTest {
}
private AccessKey foreignUser(String name, String policy) {
String account = "111111111111";
RequestScopes.runAs(account, () -> iam.createUser(name, "/"));
cleanup.register(() -> RequestScopes.runAs(account, () -> iam.deleteUser(name)));
RequestScopes.runAs(account, () -> iam.putUserPolicy(name, "callback", policy));
cleanup.register(() -> RequestScopes.runAs(account, () -> iam.deleteUserPolicy(name, "callback")));
AccessKey key = RequestScopes.callAs(account, () -> iam.createAccessKey(name));
cleanup.register(() -> RequestScopes.runAs(account, () -> iam.deleteAccessKey(name, key.getAccessKeyId())));
return key;
return SignedIamUsers.createUser(iam, cleanup, "111111111111", name, "callback", policy);
}
private SfnClient client(String access, String secret) {
@@ -1,5 +1,8 @@
package io.floci.aws.testing;
import io.floci.aws.core.common.RequestScopes;
import io.floci.aws.services.iam.IamService;
import io.floci.aws.services.iam.model.AccessKey;
import io.floci.aws.testutil.AwsRequestSigner;
import io.restassured.response.Response;
@@ -19,7 +22,8 @@ import static io.restassured.RestAssured.given;
*
* <p>{@link #createUser} makes an IAM user with an access key and an optional inline policy; closing the
* returned {@link User} deletes the key, the policy and the user, so a test can hold it in a
* try-with-resources block.
* try-with-resources block. {@link #createUser(IamService, PartitionCleanup, String, String, String, String)} makes
* one in an account of the test's own, which the root {@code test} key, signing for the default account, cannot.
*/
public final class SignedIamUsers {
@@ -78,7 +82,26 @@ public final class SignedIamUsers {
}
}
/** An IAM user made by {@link #createUser}; {@link #close} removes it with its key and policy. */
/**
* Creates the user {@code name} of {@code account} through {@code iam}, with one access key and the inline policy
* {@code policyName} holding {@code policyDocument}; {@code cleanup} removes the key, the policy and the user
* after the test.
*/
public static AccessKey createUser(IamService iam, PartitionCleanup cleanup, String account, String name,
String policyName, String policyDocument) {
RequestScopes.runAs(account, () -> iam.createUser(name, "/"));
cleanup.register(() -> RequestScopes.runAs(account, () -> iam.deleteUser(name)));
RequestScopes.runAs(account, () -> iam.putUserPolicy(name, policyName, policyDocument));
cleanup.register(() -> RequestScopes.runAs(account, () -> iam.deleteUserPolicy(name, policyName)));
AccessKey key = RequestScopes.callAs(account, () -> iam.createAccessKey(name));
cleanup.register(() -> RequestScopes.runAs(account, () -> iam.deleteAccessKey(name, key.getAccessKeyId())));
return key;
}
/**
* An IAM user made by {@link #createUser(URI, String, String, String)}; {@link #close} removes it with its key
* and policy.
*/
public record User(URI endpoint, String name, String policyName, String accessKeyId, String secretAccessKey)
implements AutoCloseable {