mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ses): support tag operations on tenant ARNs (#2832)
* feat(ses): support tag operations on tenant ARNs * fix(ses): shield the stored tenant record from tag-callback mutation * test(ses): cover the cross-region guard for tenant TagResource/UntagResource * docs(ses): drop em dashes from the tenant tag additions
This commit is contained in:
1 parent
eb0472fccc
commit
812911fd03
7 files changed
+362
-2
No files matched your search
@@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Clock;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -139,6 +140,65 @@ class SesTenantServiceTest {
|
||||
assertThrows(AwsException.class, () -> service.deleteTenant(null, REGION)).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByTenantId_matchesIdOnly_perRegion() {
|
||||
Tenant tenant = service.createTenant("acme", List.of(), ACCOUNT, REGION);
|
||||
assertEquals("acme", service.findByTenantId(tenant.tenantId(), REGION).orElseThrow().tenantName());
|
||||
assertTrue(service.findByTenantId("tn-000000000000000000000000000000", REGION).isEmpty());
|
||||
assertTrue(service.findByTenantId(tenant.tenantId(), "eu-west-1").isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tenantForTagArn_resolvesByIdAlone_notFoundMessagesMatchAws() {
|
||||
Tenant tenant = service.createTenant("acme", List.of(), ACCOUNT, REGION);
|
||||
// The name segment is never matched; only the id resolves.
|
||||
assertEquals("acme",
|
||||
service.tenantForTagArn("wrong-name/" + tenant.tenantId(), REGION).tenantName());
|
||||
// The missing space before "with" is AWS's own wording.
|
||||
assertEquals("No Tenant present with name: acmewith tenantId: tn-0000",
|
||||
assertThrows(AwsException.class,
|
||||
() -> service.tenantForTagArn("acme/tn-0000", REGION)).getMessage());
|
||||
// A remainder without a slash parses as a null name with the segment as the id.
|
||||
assertEquals("No Tenant present with name: nullwith tenantId: acme",
|
||||
assertThrows(AwsException.class,
|
||||
() -> service.tenantForTagArn("acme", REGION)).getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mutateTags_appliesToCurrentRecordUnderLock() {
|
||||
Tenant tenant = service.createTenant("acme", List.of(new Tag("team", "floci")), ACCOUNT, REGION);
|
||||
service.mutateTags("acme/" + tenant.tenantId(), REGION, tags -> List.of(new Tag("env", "dev")));
|
||||
List<Tag> tags = service.getTenant("acme", REGION).tags();
|
||||
assertEquals(1, tags.size());
|
||||
assertEquals("env", tags.get(0).key());
|
||||
|
||||
// A stale TenantId (delete/recreate happened since the caller's lookup) must not resurrect
|
||||
// the old record: the mutation re-resolves by id under the lock and 404s.
|
||||
service.deleteTenant("acme", REGION);
|
||||
service.createTenant("acme", List.of(), ACCOUNT, REGION);
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.mutateTags(
|
||||
"acme/" + tenant.tenantId(), REGION, current -> current));
|
||||
assertEquals("No Tenant present with name: acmewith tenantId: " + tenant.tenantId(),
|
||||
e.getMessage());
|
||||
assertEquals(0, service.getTenant("acme", REGION).tags().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mutateTags_shieldsTheStoredRecordFromCallbackMutation() {
|
||||
Tenant tenant = service.createTenant("acme",
|
||||
new ArrayList<>(List.of(new Tag("team", "floci"))), ACCOUNT, REGION);
|
||||
// The callback gets an immutable copy: an in-place mutation fails fast instead of
|
||||
// silently aliasing the stored record's list.
|
||||
assertThrows(UnsupportedOperationException.class, () -> service.mutateTags(
|
||||
"acme/" + tenant.tenantId(), REGION, tags -> {
|
||||
tags.add(new Tag("env", "dev"));
|
||||
return tags;
|
||||
}));
|
||||
List<Tag> tags = service.getTenant("acme", REGION).tags();
|
||||
assertEquals(1, tags.size());
|
||||
assertEquals("team", tags.get(0).key());
|
||||
}
|
||||
|
||||
// ──────────────────────── Resource associations (Phase 2) ────────────────────────
|
||||
|
||||
private static SesTenantService.AssociationResource identityRef(String name) {
|
||||
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.json.JsonPath;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
|
||||
/**
|
||||
* Integration tests for the tag operations on TENANT ARNs (the follow-up gap noted on the tenant
|
||||
* tracking issue). Probe-confirmed quirks: the ARN is resolved by the TenantId segment ALONE (the
|
||||
* name segment is never matched), tags are TenantId-scoped (a recreated same-name tenant's old ARN
|
||||
* 404s), the not-found message carries AWS's own missing space ("name: <name>with tenantId:"), the
|
||||
* tag list renders ordered by key, and the generic cross-account/cross-region tag semantics apply.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class SesTenantTagV2IntegrationTest {
|
||||
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request";
|
||||
private static final String TENANT = "floci-tag-tenant";
|
||||
private static final String ARN_PREFIX = "arn:aws:ses:us-east-1:000000000000:";
|
||||
|
||||
private static String tenantArn;
|
||||
private static String tenantId;
|
||||
|
||||
private static io.restassured.specification.RequestSpecification v2() {
|
||||
return given().contentType("application/json").header("Authorization", AUTH);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTenantWithTags_visibleThroughListTagsForResource() {
|
||||
JsonPath created = v2()
|
||||
.body("{\"TenantName\":\"" + TENANT + "\",\"Tags\":[{\"Key\":\"team\",\"Value\":\"floci\"}]}")
|
||||
.when().post("/v2/email/tenants").then().statusCode(200)
|
||||
.extract().jsonPath();
|
||||
tenantArn = created.getString("TenantArn");
|
||||
tenantId = created.getString("TenantId");
|
||||
|
||||
v2().when().get("/v2/email/tags?ResourceArn=" + tenantArn).then().statusCode(200)
|
||||
.body("Tags", hasSize(1))
|
||||
.body("Tags[0].Key", equalTo("team"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void tagUntag_lifecycle_sortedByKey_reflectsIntoGetTenant() {
|
||||
v2().body("{\"ResourceArn\":\"" + tenantArn + "\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"dev\"}]}")
|
||||
.when().post("/v2/email/tags").then().statusCode(200);
|
||||
v2().when().get("/v2/email/tags?ResourceArn=" + tenantArn).then().statusCode(200)
|
||||
.body("Tags", hasSize(2))
|
||||
// Ordered by key: env before team (probe-confirmed).
|
||||
.body("Tags[0].Key", equalTo("env"))
|
||||
.body("Tags[1].Key", equalTo("team"));
|
||||
|
||||
// Re-tagging an existing key replaces its value.
|
||||
v2().body("{\"ResourceArn\":\"" + tenantArn + "\",\"Tags\":[{\"Key\":\"env\",\"Value\":\"prod\"}]}")
|
||||
.when().post("/v2/email/tags").then().statusCode(200);
|
||||
v2().when().get("/v2/email/tags?ResourceArn=" + tenantArn).then().statusCode(200)
|
||||
.body("Tags[0].Value", equalTo("prod"));
|
||||
|
||||
// Untag removes the key; a nonexistent key is a silent success.
|
||||
v2().when().delete("/v2/email/tags?ResourceArn=" + tenantArn + "&TagKeys=env")
|
||||
.then().statusCode(200);
|
||||
v2().when().delete("/v2/email/tags?ResourceArn=" + tenantArn + "&TagKeys=ghost-key")
|
||||
.then().statusCode(200);
|
||||
|
||||
v2().body("{\"TenantName\":\"" + TENANT + "\"}")
|
||||
.when().post("/v2/email/tenants/get").then().statusCode(200)
|
||||
.body("Tenant.Tags", hasSize(1))
|
||||
.body("Tenant.Tags[0].Key", equalTo("team"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void arnResolution_isByTenantIdAlone() {
|
||||
// A wrong name with a valid id resolves that id's tenant.
|
||||
v2().when().get("/v2/email/tags?ResourceArn="
|
||||
+ ARN_PREFIX + "tenant/wrong-name/" + tenantId)
|
||||
.then().statusCode(200)
|
||||
.body("Tags", hasSize(1))
|
||||
.body("Tags[0].Key", equalTo("team"));
|
||||
|
||||
// A valid name with a wrong id is a 404, with AWS's own missing space in the message.
|
||||
v2().when().get("/v2/email/tags?ResourceArn="
|
||||
+ ARN_PREFIX + "tenant/" + TENANT + "/tn-000000000000000000000000000000")
|
||||
.then().statusCode(404)
|
||||
.body("message", equalTo("No Tenant present with name: " + TENANT
|
||||
+ "with tenantId: tn-000000000000000000000000000000"));
|
||||
|
||||
// An ARN without the id segment parses as a null name and the segment as the id.
|
||||
v2().when().get("/v2/email/tags?ResourceArn=" + ARN_PREFIX + "tenant/" + TENANT)
|
||||
.then().statusCode(404)
|
||||
.body("message", equalTo("No Tenant present with name: nullwith tenantId: " + TENANT));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void crossAccountAndCrossRegion_matchTheGenericTagSemantics() {
|
||||
v2().when().get("/v2/email/tags?ResourceArn="
|
||||
+ tenantArn.replace(":000000000000:", ":111111111111:"))
|
||||
.then().statusCode(400)
|
||||
.body("message", equalTo(
|
||||
"Operations on a resource created in a different account is not allowed"));
|
||||
|
||||
// Matches the observable behavior of the other taggable types: existence is checked against
|
||||
// the signing region, then a mismatched-region ARN yields an empty tag set (for tenants the
|
||||
// tags live on the tenant record itself; the empty result comes from the dispatch's
|
||||
// explicit region comparison).
|
||||
v2().when().get("/v2/email/tags?ResourceArn="
|
||||
+ tenantArn.replace(":us-east-1:", ":eu-west-1:"))
|
||||
.then().statusCode(200)
|
||||
.body("Tags", hasSize(0));
|
||||
|
||||
// Tag and Untag take the generic pre-dispatch region guard instead: 400 with the same
|
||||
// wording as the other six taggable types, probe-confirmed on a tenant ARN directly
|
||||
// (the guard fires before any tenant resolution, so nothing is mutated).
|
||||
String crossRegionArn = tenantArn.replace(":us-east-1:", ":eu-west-1:");
|
||||
v2().body("{\"ResourceArn\":\"" + crossRegionArn
|
||||
+ "\",\"Tags\":[{\"Key\":\"x\",\"Value\":\"y\"}]}")
|
||||
.when().post("/v2/email/tags").then().statusCode(400)
|
||||
.body("message", equalTo("Failed to tag resource"));
|
||||
v2().when().delete("/v2/email/tags?ResourceArn=" + crossRegionArn + "&TagKeys=team")
|
||||
.then().statusCode(400)
|
||||
.body("message", equalTo("Failed to untag resource"));
|
||||
|
||||
// The guarded calls left the tenant's tags untouched.
|
||||
v2().when().get("/v2/email/tags?ResourceArn=" + tenantArn).then().statusCode(200)
|
||||
.body("Tags", hasSize(1))
|
||||
.body("Tags[0].Key", equalTo("team"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void tags_areTenantIdScoped_acrossRecreation() {
|
||||
v2().body("{\"TenantName\":\"" + TENANT + "\"}")
|
||||
.when().post("/v2/email/tenants/delete").then().statusCode(200);
|
||||
v2().when().get("/v2/email/tags?ResourceArn=" + tenantArn).then().statusCode(404)
|
||||
.body("message", equalTo("No Tenant present with name: " + TENANT
|
||||
+ "with tenantId: " + tenantId));
|
||||
|
||||
JsonPath recreated = v2().body("{\"TenantName\":\"" + TENANT + "\"}")
|
||||
.when().post("/v2/email/tenants").then().statusCode(200)
|
||||
.extract().jsonPath();
|
||||
v2().when().get("/v2/email/tags?ResourceArn=" + recreated.getString("TenantArn"))
|
||||
.then().statusCode(200)
|
||||
.body("Tags", hasSize(0));
|
||||
v2().when().get("/v2/email/tags?ResourceArn=" + tenantArn).then().statusCode(404);
|
||||
|
||||
v2().body("{\"TenantName\":\"" + TENANT + "\"}")
|
||||
.when().post("/v2/email/tenants/delete").then().statusCode(200);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user