feat(ses): implement identity (sending authorization) policies for v1 and v2 (#1750)

* feat(ses): implement identity (sending authorization) policies for v1 and v2

Add the SES identity-policy APIs across both protocols, backed by one shared
StorageFactory store (key policy::<region>::<identity>::<policyName>):

- v1 Query: PutIdentityPolicy (upsert), GetIdentityPolicies, ListIdentityPolicies,
  DeleteIdentityPolicy (idempotent).
- v2 REST JSON: CreateEmailIdentityPolicy (fails on duplicate),
  GetEmailIdentityPolicies, UpdateEmailIdentityPolicy (fails when missing),
  DeleteEmailIdentityPolicy.

Behaviour and error shapes were verified against real AWS: the per-identity
limit of 20, the v1/v2 create/update/delete strictness asymmetry, PolicyName
constraints, and the shared v1<->v2 store. Floci stores and returns policies but
does not enforce the authorization (Principal-account existence, Resource-ARN
match) or gate sending on them.

* test(ses): inject policyStore into the test SesService constructor

Thread the identity-policy store through the package-private constructors as a
parameter instead of hardcoding a new InMemoryStorage, matching every other
store and keeping the test constructor aligned with the production wiring.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-07-27 22:36:47 -05:00
1 parent 4c9f61dfe8
commit 881c45b393
13 files changed
+827 -3

No files matched your search

@@ -0,0 +1,137 @@
package io.github.hectorvent.floci.services.ses;
import io.quarkus.test.junit.QuarkusTest;
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.containsString;
import static org.hamcrest.Matchers.not;
/**
* Integration tests for the SES V1 Query-protocol identity-policy APIs
* (Put/Get/List/DeleteIdentityPolicy). Verified against real AWS: v1 Put is an
* upsert, v1 Delete is idempotent, v1 List/Get do not require the identity, and
* policies are shared with the V2 store (a v1 policy is visible via V2 Get).
*/
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class SesIdentityPolicyV1IntegrationTest {
private static final String AUTH =
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request";
private static final String V2_AUTH =
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request";
private static final String IDENTITY = "policy-v1.floci.test";
private static final String DOC_A = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"AAA\"}]}";
private static final String DOC_B = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"BBB\"}]}";
private static io.restassured.specification.RequestSpecification query(String action) {
return given().contentType("application/x-www-form-urlencoded").header("Authorization", AUTH)
.formParam("Action", action);
}
@Test
@Order(0)
void setup_createIdentity() {
query("VerifyDomainIdentity").formParam("Domain", IDENTITY).when().post("/").then().statusCode(200);
}
@Test
@Order(1)
void putIdentityPolicy_thenUpsert() {
query("PutIdentityPolicy").formParam("Identity", IDENTITY)
.formParam("PolicyName", "pA").formParam("Policy", DOC_A)
.when().post("/").then().statusCode(200)
.body(containsString("PutIdentityPolicyResponse"));
// Same name again with a different document -> upsert, no error.
query("PutIdentityPolicy").formParam("Identity", IDENTITY)
.formParam("PolicyName", "pA").formParam("Policy", DOC_B)
.when().post("/").then().statusCode(200);
}
@Test
@Order(2)
void getIdentityPolicies_returnsUpdatedDoc() {
query("GetIdentityPolicies").formParam("Identity", IDENTITY)
.formParam("PolicyNames.member.1", "pA")
.when().post("/").then().statusCode(200)
.body(containsString("<key>pA</key>"))
.body(containsString("BBB"))
.body(not(containsString("AAA")));
}
@Test
@Order(3)
void getIdentityPolicies_missingNameOmitted() {
query("GetIdentityPolicies").formParam("Identity", IDENTITY)
.formParam("PolicyNames.member.1", "pA")
.formParam("PolicyNames.member.2", "nope")
.when().post("/").then().statusCode(200)
.body(containsString("<key>pA</key>"))
.body(not(containsString("nope")));
}
@Test
@Order(4)
void listIdentityPolicies() {
query("ListIdentityPolicies").formParam("Identity", IDENTITY)
.when().post("/").then().statusCode(200)
.body(containsString("<member>pA</member>"));
}
@Test
@Order(5)
void deleteIdentityPolicy_isIdempotent() {
query("DeleteIdentityPolicy").formParam("Identity", IDENTITY).formParam("PolicyName", "pA")
.when().post("/").then().statusCode(200);
// Deleting a missing policy again succeeds (no error) on v1.
query("DeleteIdentityPolicy").formParam("Identity", IDENTITY).formParam("PolicyName", "pA")
.when().post("/").then().statusCode(200)
.body(containsString("DeleteIdentityPolicyResponse"));
}
@Test
@Order(6)
void listAndGetOnMissingIdentity_returnEmpty() {
query("ListIdentityPolicies").formParam("Identity", "never-verified.floci.test")
.when().post("/").then().statusCode(200)
.body(containsString("<PolicyNames"));
query("GetIdentityPolicies").formParam("Identity", "never-verified.floci.test")
.formParam("PolicyNames.member.1", "x")
.when().post("/").then().statusCode(200);
}
@Test
@Order(8)
void invalidPolicyName_v1ErrorCodes() {
// Verified against real AWS: v1 uses InvalidParameterValue for bad chars and ValidationError
// for length (the v2 controller remaps both to BadRequestException).
query("PutIdentityPolicy").formParam("Identity", IDENTITY)
.formParam("PolicyName", "bad name!").formParam("Policy", DOC_A)
.when().post("/").then().statusCode(400)
.body(containsString("<Code>InvalidParameterValue</Code>"))
.body(containsString("Policy names must only include"));
query("PutIdentityPolicy").formParam("Identity", IDENTITY)
.formParam("PolicyName", "a".repeat(65)).formParam("Policy", DOC_A)
.when().post("/").then().statusCode(400)
.body(containsString("<Code>ValidationError</Code>"))
.body(containsString("length less than or equal to 64"));
}
@Test
@Order(7)
void v1Policy_isVisibleViaV2() {
query("PutIdentityPolicy").formParam("Identity", IDENTITY)
.formParam("PolicyName", "shared").formParam("Policy", DOC_A)
.when().post("/").then().statusCode(200);
given().header("Authorization", V2_AUTH)
.when().get("/v2/email/identities/" + IDENTITY + "/policies")
.then().statusCode(200)
.body("Policies.shared", containsString("AAA"));
}
}
@@ -0,0 +1,216 @@
package io.github.hectorvent.floci.services.ses;
import io.quarkus.test.junit.QuarkusTest;
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.notNullValue;
import static org.hamcrest.Matchers.nullValue;
/**
* Integration tests for the SES V2 REST JSON identity-policy APIs
* (Create/Get/Update/DeleteEmailIdentityPolicy). Behaviour and error shapes are
* verified against real AWS (per-identity limit, create-dup/update-missing/
* delete-missing errors, PolicyName constraints, missing-identity handling).
*/
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class SesIdentityPolicyV2IntegrationTest {
private static final String AUTH =
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request";
private static final String IDENTITY = "policy-v2.floci.test";
private static final String BASE = "/v2/email/identities/" + IDENTITY + "/policies";
private static final String DOC = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
private static String policyBody(String doc) {
return "{\"Policy\":\"" + doc.replace("\"", "\\\"") + "\"}";
}
@Test
@Order(0)
void setup_createIdentity() {
given().contentType("application/json").header("Authorization", AUTH)
.body("{\"EmailIdentity\": \"" + IDENTITY + "\"}")
.when().post("/v2/email/identities")
.then().statusCode(200);
}
@Test
@Order(1)
void createPolicy_thenGet() {
given().contentType("application/json").header("Authorization", AUTH).body(policyBody(DOC))
.when().post(BASE + "/p1")
.then().statusCode(200);
given().header("Authorization", AUTH)
.when().get(BASE)
.then().statusCode(200)
.body("Policies.p1", notNullValue());
}
@Test
@Order(2)
void createDuplicateName_throwsAlreadyExists() {
given().contentType("application/json").header("Authorization", AUTH).body(policyBody(DOC))
.when().post(BASE + "/p1")
.then().statusCode(400)
.body("__type", equalTo("AlreadyExistsException"))
.body("message", equalTo("Policy <p1> already exists"));
}
@Test
@Order(3)
void createSameContentDifferentName_ok() {
given().contentType("application/json").header("Authorization", AUTH).body(policyBody(DOC))
.when().post(BASE + "/p2")
.then().statusCode(200);
}
@Test
@Order(4)
void updatePolicy_replaces() {
String updated = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"X\"}]}";
given().contentType("application/json").header("Authorization", AUTH).body(policyBody(updated))
.when().put(BASE + "/p1")
.then().statusCode(200);
given().header("Authorization", AUTH)
.when().get(BASE)
.then().statusCode(200)
.body("Policies.p1", equalTo(updated));
}
@Test
@Order(5)
void updateMissing_throwsNotFound() {
given().contentType("application/json").header("Authorization", AUTH).body(policyBody(DOC))
.when().put(BASE + "/nope")
.then().statusCode(404)
.body("__type", equalTo("NotFoundException"))
.body("message", equalTo("Policy <nope> does not exist"));
}
@Test
@Order(6)
void deletePolicy_thenGone() {
given().header("Authorization", AUTH)
.when().delete(BASE + "/p2")
.then().statusCode(200);
given().header("Authorization", AUTH)
.when().get(BASE)
.then().statusCode(200)
.body("Policies.p2", nullValue());
}
@Test
@Order(7)
void deleteMissing_throwsNotFound() {
given().header("Authorization", AUTH)
.when().delete(BASE + "/nope")
.then().statusCode(404)
.body("__type", equalTo("NotFoundException"));
}
@Test
@Order(8)
void operationsOnMissingIdentity_throwNotFound() {
String ghost = "/v2/email/identities/ghost-v2.floci.test/policies";
given().contentType("application/json").header("Authorization", AUTH).body(policyBody(DOC))
.when().post(ghost + "/p")
.then().statusCode(404)
.body("__type", equalTo("NotFoundException"))
.body("message", equalTo("Email identity <ghost-v2.floci.test> does not exist."));
given().header("Authorization", AUTH).when().get(ghost).then().statusCode(404);
}
@Test
@Order(9)
void invalidPolicyName_chars_returns400() {
given().contentType("application/json").header("Authorization", AUTH).body(policyBody(DOC))
.when().post(BASE + "/bad%20name")
.then().statusCode(400)
.body("__type", equalTo("BadRequestException"))
.body("message", equalTo("PolicyName is invalid. Policy names must only include "
+ "alpha-numeric characters, dashes, and underscores."));
}
@Test
@Order(10)
void invalidPolicyName_tooLong_returns400() {
given().contentType("application/json").header("Authorization", AUTH).body(policyBody(DOC))
.when().post(BASE + "/" + "a".repeat(65))
.then().statusCode(400)
.body("__type", equalTo("BadRequestException"))
.body("message", equalTo("1 validation error detected: Value at 'policyName' failed "
+ "to satisfy constraint: Member must have length less than or equal to 64"));
}
@Test
@Order(11)
void perIdentityLimit_21stThrowsLimitExceeded() {
// p1 already exists (Order 4). Fill to 20, then the 21st create fails.
for (int i = 2; i <= 20; i++) {
given().contentType("application/json").header("Authorization", AUTH).body(policyBody(DOC))
.when().post(BASE + "/lp" + i)
.then().statusCode(200);
}
given().contentType("application/json").header("Authorization", AUTH).body(policyBody(DOC))
.when().post(BASE + "/overflow")
.then().statusCode(400)
.body("__type", equalTo("LimitExceededException"))
.body("message", equalTo("Number of policies for <" + IDENTITY
+ "> exceeds max allowed number of policies per resource"));
}
@Test
@Order(12)
void deleteIdentity_purgesPolicies_noResurrectOnRecreate() {
String id = "cascade.floci.test";
String base = "/v2/email/identities/" + id + "/policies";
given().contentType("application/json").header("Authorization", AUTH)
.body("{\"EmailIdentity\": \"" + id + "\"}")
.when().post("/v2/email/identities").then().statusCode(200);
given().contentType("application/json").header("Authorization", AUTH).body(policyBody(DOC))
.when().post(base + "/c1").then().statusCode(200);
given().header("Authorization", AUTH)
.when().delete("/v2/email/identities/" + id).then().statusCode(200);
// Recreate the same-named identity: the old policy must not leak back in.
given().contentType("application/json").header("Authorization", AUTH)
.body("{\"EmailIdentity\": \"" + id + "\"}")
.when().post("/v2/email/identities").then().statusCode(200);
given().header("Authorization", AUTH)
.when().get(base).then().statusCode(200)
.body("Policies.size()", equalTo(0));
}
@Test
@Order(13)
void policyWrongType_returns400() {
// Policy is a string field; a non-string must be rejected, not coerced to "123".
given().contentType("application/json").header("Authorization", AUTH).body("{\"Policy\": 123}")
.when().post(BASE + "/wrongtype")
.then().statusCode(400)
.body("__type", equalTo("SerializationException"));
}
@Test
@Order(14)
void policyMissingOrNull_returnsValidationError() {
String expected = "1 validation error detected: Value at 'policy' failed to satisfy "
+ "constraint: Member must not be null";
given().contentType("application/json").header("Authorization", AUTH).body("{}")
.when().post(BASE + "/missing")
.then().statusCode(400).body("message", equalTo(expected));
given().contentType("application/json").header("Authorization", AUTH).body("{\"Policy\": null}")
.when().post(BASE + "/nullpol")
.then().statusCode(400).body("message", equalTo(expected));
}
}
@@ -57,6 +57,7 @@ class SesServiceDkimLookupCacheTest {
new InMemoryStorage<String, DedicatedIpPool>(),
new InMemoryStorage<String, ContactList>(),
new InMemoryStorage<String, Contact>(),
new InMemoryStorage<String, String>(),
mock(SmtpRelay.class),
new ObjectMapper(),
route53Service,
@@ -46,6 +46,7 @@ class SesServiceLegacyDkimTokensTest {
new InMemoryStorage<String, DedicatedIpPool>(),
new InMemoryStorage<String, ContactList>(),
new InMemoryStorage<String, Contact>(),
new InMemoryStorage<String, String>(),
mock(SmtpRelay.class),
new ObjectMapper(),
Clock.systemUTC());
@@ -45,6 +45,7 @@ class SesServiceSmtpTest {
new InMemoryStorage<String, DedicatedIpPool>(),
new InMemoryStorage<String, ContactList>(),
new InMemoryStorage<String, Contact>(),
new InMemoryStorage<String, String>(),
smtpRelay,
new ObjectMapper(),
Clock.systemUTC());
@@ -53,6 +53,7 @@ class SesServiceSuppressionLegacyKeyTest {
new InMemoryStorage<String, DedicatedIpPool>(),
new InMemoryStorage<String, ContactList>(),
new InMemoryStorage<String, Contact>(),
new InMemoryStorage<String, String>(),
mock(SmtpRelay.class),
new ObjectMapper(),
Clock.systemUTC());
@@ -53,6 +53,7 @@ class SesServiceSuppressionReasonTest {
new InMemoryStorage<String, DedicatedIpPool>(),
new InMemoryStorage<String, ContactList>(),
new InMemoryStorage<String, Contact>(),
new InMemoryStorage<String, String>(),
mock(SmtpRelay.class),
new ObjectMapper(),
Clock.systemUTC());