feat(scheduler): support TagResource, UntagResource, ListTagsForResource (#700)

* feat(scheduler): support TagResource, UntagResource, ListTagsForResource

Implements the three remaining EventBridge Scheduler tag APIs by
extending the shared /tags/{arn} dispatcher. Tags apply to schedule
groups only, matching AWS behavior; ARNs pointing at individual
schedules return ValidationException.

A survey of all ~75 AWS services that share the /tags/{arn} path
shows three independent shape axes — body key (`tags` lower vs
`Tags` capital), body structure (map vs list of {Key, Value}), and
untag query parameter name (`tagKeys` vs `TagKeys`) — and most
"capital Tags" services still use lowercase tagKeys query, so these
axes vary independently. TagHandler exposes them as five orthogonal
default methods rather than a coarse-grained enum:

- tagsBodyKey()           defaults to "tags"
- tagsBodyIsList()        defaults to false (map)
- tagKeysQueryName()      defaults to "tagKeys"
- tagResourceUsesPut()    defaults to false (POST)
- strictTagValidation()   defaults to false (lenient parsing)

The defaults match the most common AWS shape (~73 services use
lowercase tags map + POST + lowercase tagKeys) so handlers like
EKS, Pipes, and AppConfig need no overrides. The dispatcher
returns 405 MethodNotAllowedException for the unused HTTP method
per handler. Strict input validation (missing Tags, wrong-shaped
Tags, missing TagKeys, entries missing Key or Value) is enforced
when strictTagValidation() is true, mirroring the AWS Scheduler
error shape; lenient handlers retain the looser parsing they had
before.

SchedulerTagHandler overrides tagsBodyKey, tagsBodyIsList,
tagKeysQueryName, and strictTagValidation. ApiGatewayTagHandler
overrides tagResourceUsesPut to true. AppConfigIntegrationTest
switches its tagApplication call from PUT to POST to match the
AWS spec for AppConfig (was relying on the dispatcher's prior
leniency on HTTP method).

* fix(tags): normalize empty body and reject non-object payloads

Address Copilot review on floci-io/floci#700.

- doTagResource normalizes null/blank request bodies to "{}" before
  passing to ObjectMapper.readTree, mirroring SchedulerController's
  parseTags. Previously an empty body raised a Jackson parse error
  that strict handlers surfaced as a ValidationException with a raw
  Java/Jackson message instead of the structured AWS error shape.
- parseTags adds a strict-only check that the JSON root is an
  object. A request body like a JSON array or string used to fall
  through to "Value null at 'Tags' ..." which misleads about the
  actual problem; it now reports that the payload itself must be a
  JSON object.

SchedulerIntegrationTest covers both paths with a 400 assertion.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-05-04 17:22:12 -05:00
1 parent 99204e2caf
commit 8e30c09d0a
11 files changed
+615 -23

No files matched your search

@@ -287,6 +287,18 @@ class ApiGatewayIntegrationTest {
.statusCode(204);
}
@Test @Order(23)
void tagResourcePostReturns405() {
// AWS API Gateway only defines PUT for TagResource; POST is not in the spec.
String arn = "arn:aws:apigateway:us-east-1::/restapis/" + apiId;
given()
.contentType(ContentType.JSON)
.body("{\"tags\":{\"env\":\"test\"}}")
.when().post("/tags/" + arn)
.then()
.statusCode(405);
}
@Test @Order(24)
void getTags() {
String arn = "arn:aws:apigateway:us-east-1::/restapis/" + apiId;
@@ -242,7 +242,7 @@ class AppConfigIntegrationTest {
given()
.contentType(ContentType.JSON)
.body("{\"tags\": {\"env\": \"local\", \"team\": \"platform\"}}")
.when().put("/tags/" + arn)
.when().post("/tags/" + arn)
.then()
.statusCode(204);
}
@@ -6,7 +6,11 @@ import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
@QuarkusTest
@@ -172,6 +176,182 @@ class SchedulerIntegrationTest {
.statusCode(404);
}
// ──────────────────────────── Tag tests ────────────────────────────
private static final String TAGGED_GROUP_ARN =
"arn:aws:scheduler:us-east-1:000000000000:schedule-group/tagged-group";
@Test
@Order(13)
void listTagsReturnsTagsFromCreate() {
// tagged-group was created at @Order(2) with env=test and team=platform.
given()
.when()
.get("/tags/" + TAGGED_GROUP_ARN)
.then()
.statusCode(200)
.body("Tags.find { it.Key == 'env' }.Value", equalTo("test"))
.body("Tags.find { it.Key == 'team' }.Value", equalTo("platform"));
}
@Test
@Order(14)
void tagResourceAddsTags() throws InterruptedException {
// createScheduleGroup uses one Instant.now() for both CreationDate and
// LastModificationDate (so they are byte-identical at creation). Sleep here so the
// tagScheduleGroup Instant.now() below is guaranteed to be strictly later, even on
// a system whose clock granularity collides with the test class's startup speed.
Thread.sleep(2);
given()
.contentType("application/json")
.body("""
{
"Tags": [
{"Key": "owner", "Value": "Alice"},
{"Key": "env", "Value": "staging"}
]
}
""")
.when()
.post("/tags/" + TAGGED_GROUP_ARN)
.then()
.statusCode(204);
given()
.when()
.get("/tags/" + TAGGED_GROUP_ARN)
.then()
.statusCode(200)
.body("Tags.find { it.Key == 'owner' }.Value", equalTo("Alice"))
// overwrite of existing key
.body("Tags.find { it.Key == 'env' }.Value", equalTo("staging"))
.body("Tags.find { it.Key == 'team' }.Value", equalTo("platform"));
// Tag mutation must bump LastModificationDate above the initial CreationDate
// so a regression in this AWS-visible field is caught. Parse via Jackson directly
// because RestAssured coerces sub-second epoch doubles to Float and loses the
// sub-second delta the test relies on.
String body = given()
.when()
.get("/schedule-groups/tagged-group")
.then()
.statusCode(200)
.extract().asString();
try {
JsonNode tree = new ObjectMapper().readTree(body);
double creation = tree.get("CreationDate").asDouble();
double lastMod = tree.get("LastModificationDate").asDouble();
assertThat(lastMod, greaterThan(creation));
} catch (Exception e) {
throw new AssertionError("Failed to parse schedule-group response: " + body, e);
}
}
@Test
@Order(15)
void untagResourceRemovesKeys() {
given()
.queryParam("TagKeys", "owner")
.queryParam("TagKeys", "env")
.when()
.delete("/tags/" + TAGGED_GROUP_ARN)
.then()
.statusCode(204);
given()
.when()
.get("/tags/" + TAGGED_GROUP_ARN)
.then()
.statusCode(200)
.body("Tags.find { it.Key == 'owner' }", nullValue())
.body("Tags.find { it.Key == 'env' }", nullValue())
.body("Tags.find { it.Key == 'team' }.Value", equalTo("platform"));
}
@Test
@Order(16)
void tagResourceOnMissingGroupReturns404() {
String arn = "arn:aws:scheduler:us-east-1:000000000000:schedule-group/no-such-group";
given()
.contentType("application/json")
.body("""
{"Tags": [{"Key": "k", "Value": "v"}]}
""")
.when()
.post("/tags/" + arn)
.then()
.statusCode(404);
}
@Test
@Order(16)
void tagResourceWithEmptyBodyReturns400() {
// Null/blank request body must surface as the structured AWS validation error
// ("Value null at 'Tags' ...") rather than leaking a Jackson parser message.
given()
.contentType("application/json")
.when()
.post("/tags/" + TAGGED_GROUP_ARN)
.then()
.statusCode(400);
}
@Test
@Order(16)
void tagResourceWithNonObjectBodyReturns400() {
// A syntactically valid JSON array at the root must be rejected as a wire-shape
// error, not silently treated as "missing 'Tags'".
given()
.contentType("application/json")
.body("[1,2,3]")
.when()
.post("/tags/" + TAGGED_GROUP_ARN)
.then()
.statusCode(400);
}
@Test
@Order(17)
void tagResourceOnScheduleArnReturns400() {
// AWS only allows tagging schedule groups, not individual schedules.
String arn = "arn:aws:scheduler:us-east-1:000000000000:schedule/default/some-schedule";
given()
.contentType("application/json")
.body("""
{"Tags": [{"Key": "k", "Value": "v"}]}
""")
.when()
.post("/tags/" + arn)
.then()
.statusCode(400);
}
@Test
@Order(18)
void tagResourceWithoutTagsReturns400() {
// AWS spec: Tags is required on TagResource. Empty body must surface as
// ValidationException rather than silently succeed.
given()
.contentType("application/json")
.body("{}")
.when()
.post("/tags/" + TAGGED_GROUP_ARN)
.then()
.statusCode(400);
}
@Test
@Order(19)
void untagResourceWithoutTagKeysReturns400() {
// AWS spec: TagKeys is required on UntagResource.
given()
.when()
.delete("/tags/" + TAGGED_GROUP_ARN)
.then()
.statusCode(400);
}
// ──────────────────────────── Schedule tests ────────────────────────────
@Test
@@ -533,4 +713,54 @@ class SchedulerIntegrationTest {
.then()
.statusCode(404);
}
// ──────────────────────────── Tag validation tests ────────────────────────────
@Test
@Order(36)
void tagResourceEntryMissingKeyOrValueReturns400() {
// AWS Tag shape requires both Key and Value; entries with either missing
// must surface as ValidationException, not be silently dropped.
given()
.contentType("application/json")
.body("""
{"Tags": [{"Key": "k"}]}
""")
.when()
.post("/tags/" + TAGGED_GROUP_ARN)
.then()
.statusCode(400);
}
@Test
@Order(37)
void tagResourceTagsWrongTypeReturns400() {
// Tags must be a list. An object or string in its place is a wire-format
// error, not a missing value, so the message should differ from "Value null".
given()
.contentType("application/json")
.body("""
{"Tags": "not-a-list"}
""")
.when()
.post("/tags/" + TAGGED_GROUP_ARN)
.then()
.statusCode(400);
}
@Test
@Order(38)
void tagResourceWithPutMethodReturns405() {
// AWS Scheduler only defines POST for TagResource. PUT is not in the spec
// and must be rejected so floci does not expose a non-AWS mutation route.
given()
.contentType("application/json")
.body("""
{"Tags": [{"Key": "k", "Value": "v"}]}
""")
.when()
.put("/tags/" + TAGGED_GROUP_ARN)
.then()
.statusCode(405);
}
}