feat(autoscaling): P0 operations and lifecycle-hook delete-by-name (#2673)

* feat(autoscaling): add SetInstanceProtection and SetInstanceHealth

* test(applicationautoscaling): cover DeleteScalingPolicy over the wire

* chore(autoscaling): cede the DeleteScalingPolicy test to feature/applicationautoscaling-delete-policy

The file was byte-identical on both branches; the ApplicationAutoScaling
test belongs on the applicationautoscaling branch by service, and keeping
both would make whichever merges second conflict on an unrelated file.

* fix(autoscaling): validate LifecycleHookName pattern and LifecycleActionToken length

PutLifecycleHook, DeleteLifecycleHook, and CompleteLifecycleAction accepted any
hook name and lifecycle-action token, so a name with characters outside the
modeled [A-Za-z0-9\-_/]+ pattern or a token that wasn't exactly 36 characters
was stored/consumed instead of rejected the way AWS rejects it
(autoscaling/2011-01-01/service-2.json: LifecycleHookName, LifecycleActionToken).

* fix(autoscaling): address maintainer review on instance ops and lifecycle hooks

- SetInstanceProtection: validate InstanceIds before requireGroup (AWS
  validates required members first), compare requested/found instance
  IDs via set containment instead of size-based matching (duplicate
  stored IDs no longer break the check), and report every missing
  instance ID instead of just the first.
- SetInstanceProtection: ProtectedFromScaleIn is a required boolean
  member; reject missing/malformed values instead of silently
  coercing to false via Boolean.parseBoolean.
- SetInstanceHealth: collapse the redundant second instance lookup
  after the orElseThrow already guarantees group membership. Parse
  and pass through ShouldRespectGracePeriod for wire fidelity (not
  yet enforced -- floci has no instance launch-time tracking against
  HealthCheckGracePeriod).
- deleteLifecycleHookByName (CFN delete path): validate the name for
  parity with deleteLifecycleHook instead of silently no-op'ing on an
  unvalidatable name.
- validateLifecycleHookName: split null/length/pattern checks so the
  thrown message matches the actual failure instead of always citing
  the regex pattern.
- Style: import java.util.regex.Pattern instead of fully qualifying
  it, and fix switch-case column alignment for the two new instance
  ops.

* fix(autoscaling): allow completeLifecycleAction without a token when InstanceId identifies the action

LifecycleActionToken and InstanceId are both optional in CompleteLifecycleActionType
per the service model — only LifecycleHookName, AutoScalingGroupName, and
LifecycleActionResult are required. A caller identifying the action by
InstanceId + LifecycleHookName + AutoScalingGroupName alone was rejected with a
length-36 error even though real AWS accepts it.
This commit is contained in:
Sam Fakhreddine authored and GitHub committed 2026-08-28 17:11:56 -04:00
1 parent 3bd9bcc62b
commit e9a379b37a
4 files changed
+405

No files matched your search

@@ -185,6 +185,141 @@ class AutoScalingQueryHandlerTest {
assertTrue(xml.contains("<Version>7</Version>"));
}
@Test
void setInstanceProtectionMutatesInstanceAndReturnsAwsQueryShape() {
AutoScalingService service = new AutoScalingService();
service.regionResolver = new RegionResolver(REGION, "000000000000");
service.createAutoScalingGroup(REGION, "protected-asg", null, "lt", null, "1", null,
0, 2, 1, 300, List.of("us-east-1a"), List.of(), List.of(), List.of(),
"EC2", 0, List.of("Default"), java.util.Map.of(), java.util.Map.of());
AsgInstance instance = new AsgInstance();
instance.setInstanceId("i-protected");
instance.setLifecycleState("InService");
instance.setHealthStatus("Healthy");
service.describeAutoScalingGroups(REGION, List.of("protected-asg")).getFirst().getInstances().add(instance);
AutoScalingQueryHandler handler = new AutoScalingQueryHandler(service);
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
params.add("AutoScalingGroupName", "protected-asg");
params.add("InstanceIds.member.1", "i-protected");
params.add("ProtectedFromScaleIn", "true");
Response response = handler.handle("SetInstanceProtection", params, REGION);
assertEquals(200, response.getStatus());
assertTrue(((String) response.getEntity()).contains("<SetInstanceProtectionResponse"));
assertTrue(service.describeAutoScalingInstances(REGION, List.of("i-protected"))
.getFirst().isProtectedFromScaleIn());
}
@Test
void setInstanceHealthMutatesInstanceAndReturnsAwsQueryShape() {
AutoScalingService service = new AutoScalingService();
service.regionResolver = new RegionResolver(REGION, "000000000000");
service.createAutoScalingGroup(REGION, "health-asg", null, "lt", null, "1", null,
0, 2, 1, 300, List.of("us-east-1a"), List.of(), List.of(), List.of(),
"EC2", 0, List.of("Default"), java.util.Map.of(), java.util.Map.of());
AsgInstance instance = new AsgInstance();
instance.setInstanceId("i-unhealthy");
instance.setLifecycleState("InService");
instance.setHealthStatus("Healthy");
service.describeAutoScalingGroups(REGION, List.of("health-asg")).getFirst().getInstances().add(instance);
AutoScalingQueryHandler handler = new AutoScalingQueryHandler(service);
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
params.add("InstanceId", "i-unhealthy");
params.add("HealthStatus", "Unhealthy");
Response response = handler.handle("SetInstanceHealth", params, REGION);
assertEquals(200, response.getStatus());
assertTrue(((String) response.getEntity()).contains("<SetInstanceHealthResponse"));
assertEquals("Unhealthy", service.describeAutoScalingInstances(REGION, List.of("i-unhealthy"))
.getFirst().getHealthStatus());
}
@Test
void setInstanceProtectionRejectsMissingProtectedFromScaleIn() {
AutoScalingService service = new AutoScalingService();
service.regionResolver = new RegionResolver(REGION, "000000000000");
service.createAutoScalingGroup(REGION, "no-protection-flag-asg", null, "lt", null, "1", null,
0, 2, 1, 300, List.of("us-east-1a"), List.of(), List.of(), List.of(),
"EC2", 0, List.of("Default"), java.util.Map.of(), java.util.Map.of());
AsgInstance instance = new AsgInstance();
instance.setInstanceId("i-flagless");
instance.setLifecycleState("InService");
instance.setHealthStatus("Healthy");
service.describeAutoScalingGroups(REGION, List.of("no-protection-flag-asg")).getFirst()
.getInstances().add(instance);
AutoScalingQueryHandler handler = new AutoScalingQueryHandler(service);
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
params.add("AutoScalingGroupName", "no-protection-flag-asg");
params.add("InstanceIds.member.1", "i-flagless");
// ProtectedFromScaleIn deliberately omitted — it is a required member.
Response response = handler.handle("SetInstanceProtection", params, REGION);
assertEquals(400, response.getStatus());
assertTrue(((String) response.getEntity()).contains("ValidationError"));
assertEquals(false, service.describeAutoScalingInstances(REGION, List.of("i-flagless"))
.getFirst().isProtectedFromScaleIn(),
"a rejected request must not silently strip/apply protection");
}
@Test
void setInstanceProtectionRejectsMalformedProtectedFromScaleIn() {
AutoScalingService service = new AutoScalingService();
service.regionResolver = new RegionResolver(REGION, "000000000000");
service.createAutoScalingGroup(REGION, "malformed-flag-asg", null, "lt", null, "1", null,
0, 2, 1, 300, List.of("us-east-1a"), List.of(), List.of(), List.of(),
"EC2", 0, List.of("Default"), java.util.Map.of(), java.util.Map.of());
AsgInstance instance = new AsgInstance();
instance.setInstanceId("i-malformed");
instance.setLifecycleState("InService");
instance.setHealthStatus("Healthy");
service.describeAutoScalingGroups(REGION, List.of("malformed-flag-asg")).getFirst()
.getInstances().add(instance);
AutoScalingQueryHandler handler = new AutoScalingQueryHandler(service);
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
params.add("AutoScalingGroupName", "malformed-flag-asg");
params.add("InstanceIds.member.1", "i-malformed");
params.add("ProtectedFromScaleIn", "yes");
Response response = handler.handle("SetInstanceProtection", params, REGION);
assertEquals(400, response.getStatus());
assertEquals(false, service.describeAutoScalingInstances(REGION, List.of("i-malformed"))
.getFirst().isProtectedFromScaleIn(),
"a malformed boolean must not silently coerce to false and succeed");
}
@Test
void setInstanceHealthParsesShouldRespectGracePeriodWithoutError() {
AutoScalingService service = new AutoScalingService();
service.regionResolver = new RegionResolver(REGION, "000000000000");
service.createAutoScalingGroup(REGION, "grace-asg", null, "lt", null, "1", null,
0, 2, 1, 300, List.of("us-east-1a"), List.of(), List.of(), List.of(),
"EC2", 0, List.of("Default"), java.util.Map.of(), java.util.Map.of());
AsgInstance instance = new AsgInstance();
instance.setInstanceId("i-grace");
instance.setLifecycleState("InService");
instance.setHealthStatus("Healthy");
service.describeAutoScalingGroups(REGION, List.of("grace-asg")).getFirst().getInstances().add(instance);
AutoScalingQueryHandler handler = new AutoScalingQueryHandler(service);
MultivaluedHashMap<String, String> params = new MultivaluedHashMap<>();
params.add("InstanceId", "i-grace");
params.add("HealthStatus", "Unhealthy");
params.add("ShouldRespectGracePeriod", "true");
Response response = handler.handle("SetInstanceHealth", params, REGION);
assertEquals(200, response.getStatus());
assertTrue(((String) response.getEntity()).contains("<SetInstanceHealthResponse"));
}
@Test
void targetTrackingScalingPolicyUsesAwsQueryXmlShape() {
AutoScalingService service = new AutoScalingService();
@@ -636,6 +636,137 @@ class AutoScalingServiceTest {
java.util.Map.of(), java.util.Map.of());
}
@Test
void putLifecycleHookRejectsNameOutsideModeledPattern() {
AwsException ex = assertThrows(AwsException.class, () -> service.putLifecycleHook(REGION, "test-asg",
"bad name!", "autoscaling:EC2_INSTANCE_LAUNCHING", null, null, null, null, null));
assertEquals("ValidationError", ex.getErrorCode());
}
@Test
void deleteLifecycleHookRejectsNameOutsideModeledPattern() {
assertThrows(AwsException.class,
() -> service.deleteLifecycleHook(REGION, "test-asg", "bad name!"));
}
@Test
void completeLifecycleActionRejectsNameOutsideModeledPattern() {
assertThrows(AwsException.class,
() -> service.completeLifecycleAction(REGION, "test-asg", "bad name!",
"i-0123456789abcdef0", "CONTINUE", "12345678-1234-1234-1234-123456789012"));
}
@Test
void completeLifecycleActionRejectsTokenOfWrongLength() {
service.putLifecycleHook(REGION, "test-asg", "hook-1", "autoscaling:EC2_INSTANCE_LAUNCHING",
null, null, null, null, null);
AwsException ex = assertThrows(AwsException.class,
() -> service.completeLifecycleAction(REGION, "test-asg", "hook-1",
"i-0123456789abcdef0", "CONTINUE", "not-a-uuid"));
assertEquals("ValidationError", ex.getErrorCode());
}
@Test
void completeLifecycleActionAllowsNullTokenWhenInstanceIdIdentifiesTheAction() {
service.putLifecycleHook(REGION, "test-asg", "hook-1", "autoscaling:EC2_INSTANCE_LAUNCHING",
null, null, null, null, null);
assertDoesNotThrow(() -> service.completeLifecycleAction(REGION, "test-asg", "hook-1",
"i-0123456789abcdef0", "CONTINUE", null));
}
@Test
void deleteLifecycleHookByNameRejectsNameOutsideModeledPattern() {
AwsException ex = assertThrows(AwsException.class,
() -> service.deleteLifecycleHookByName(REGION, "bad name!"));
assertEquals("ValidationError", ex.getErrorCode());
}
@Test
void deleteLifecycleHookByNameIsIdempotentOnNullPhysicalId() {
assertDoesNotThrow(() -> service.deleteLifecycleHookByName(REGION, null));
}
@Test
void putLifecycleHookRejectsNullNameWithNotNullMessage() {
AwsException ex = assertThrows(AwsException.class, () -> service.putLifecycleHook(REGION, "test-asg",
null, "autoscaling:EC2_INSTANCE_LAUNCHING", null, null, null, null, null));
assertTrue(ex.getMessage().contains("must not be null"));
}
@Test
void putLifecycleHookRejectsOverlongNameWithLengthMessage() {
String tooLong = "h".repeat(256);
AwsException ex = assertThrows(AwsException.class, () -> service.putLifecycleHook(REGION, "test-asg",
tooLong, "autoscaling:EC2_INSTANCE_LAUNCHING", null, null, null, null, null));
assertTrue(ex.getMessage().contains("length less than or equal to 255"),
"expected a length-specific message, got: " + ex.getMessage());
}
@Test
void putLifecycleHookRejectsBadCharsWithPatternMessage() {
AwsException ex = assertThrows(AwsException.class, () -> service.putLifecycleHook(REGION, "test-asg",
"bad name!", "autoscaling:EC2_INSTANCE_LAUNCHING", null, null, null, null, null));
assertTrue(ex.getMessage().contains("regular expression pattern"));
}
@Test
void setInstanceProtectionValidatesEmptyInstanceIdsBeforeGroupLookup() {
AwsException ex = assertThrows(AwsException.class,
() -> service.setInstanceProtection(REGION, "no-such-group", List.of(), true));
assertEquals("ValidationError", ex.getErrorCode());
assertTrue(ex.getMessage().contains("At least one instance ID is required"),
"a doubly-bad request (unknown group + empty list) must surface the missing-parameter "
+ "error, not 'Group not found'; got: " + ex.getMessage());
}
@Test
void setInstanceProtectionReportsAllMissingInstanceIds() {
AsgInstance instance = new AsgInstance();
instance.setInstanceId("i-real");
instance.setLifecycleState("InService");
instance.setHealthStatus("Healthy");
service.describeAutoScalingGroups(REGION, List.of("test-asg")).getFirst().getInstances().add(instance);
AwsException ex = assertThrows(AwsException.class,
() -> service.setInstanceProtection(REGION, "test-asg",
List.of("i-missing-1", "i-missing-2"), true));
assertTrue(ex.getMessage().contains("i-missing-1") && ex.getMessage().contains("i-missing-2"),
"expected both missing IDs in the message, got: " + ex.getMessage());
}
@Test
void setInstanceProtectionUsesSetContainmentAgainstDuplicateStoredInstanceIds() {
AsgInstance dup1 = new AsgInstance();
dup1.setInstanceId("i-dup");
dup1.setLifecycleState("InService");
dup1.setHealthStatus("Healthy");
AsgInstance dup2 = new AsgInstance();
dup2.setInstanceId("i-dup");
dup2.setLifecycleState("InService");
dup2.setHealthStatus("Healthy");
var instances = service.describeAutoScalingGroups(REGION, List.of("test-asg")).getFirst().getInstances();
instances.add(dup1);
instances.add(dup2);
assertDoesNotThrow(() -> service.setInstanceProtection(REGION, "test-asg", List.of("i-dup"), true));
assertTrue(instances.stream().allMatch(AsgInstance::isProtectedFromScaleIn));
}
@Test
void setInstanceHealthAcceptsShouldRespectGracePeriodFlag() {
AsgInstance instance = new AsgInstance();
instance.setInstanceId("i-health");
instance.setLifecycleState("InService");
instance.setHealthStatus("Healthy");
service.describeAutoScalingGroups(REGION, List.of("test-asg")).getFirst().getInstances().add(instance);
assertDoesNotThrow(() -> service.setInstanceHealth(REGION, "i-health", "Unhealthy", true));
assertEquals("Unhealthy", service.describeAutoScalingInstances(REGION, List.of("i-health"))
.getFirst().getHealthStatus());
}
private static final class AutoScalingGroupFixture {
private static void addInstance(AutoScalingService service, String region, String name, String instanceId,
String lifecycleState, String launchTemplateId, String launchTemplateVersion) {