feat(ec2): support snapshot block public access (#3490)

* feat(ec2): support snapshot block public access

Adds EnableSnapshotBlockPublicAccess, DisableSnapshotBlockPublicAccess and
GetSnapshotBlockPublicAccessState over the EC2 Query protocol.

The setting is account-level state scoped to one region, so it lives in a small
Ec2SnapshotBlockPublicAccessService keyed by region, mirroring
Ec2EbsEncryptionService. The account-aware storage backend namespaces the keys by
the calling credential's account. A region that was never configured reads back
unblocked.

Enable accepts only block-all-sharing and block-new-sharing, per the EC2 model,
and rejects unblocked with InvalidParameterValue. Disable returns the resulting
unblocked rather than the prior state. Only the Get response carries managedBy,
always account, since Floci has no declarative-policy layer.

* fix(ec2): honor DryRun on snapshot block public access

EnableSnapshotBlockPublicAccess, DisableSnapshotBlockPublicAccess and
GetSnapshotBlockPublicAccessState all model the standard DryRun parameter, but
none of the three handlers checked it. Enable persisted the requested state,
Disable persisted unblocked and Get answered 200, so DryRun=true had real side
effects on both write paths.

All three handlers now take the parameter map and call the existing
checkDryRun(params). Enable validates State first, through a new
validateEnableState on the service, because AWS reports an invalid parameter
ahead of DryRunOperation and only returns DryRunOperation once the request
would otherwise have succeeded. So State=unblocked with DryRun=true still
fails with InvalidParameterValue, and a missing State still fails with
MissingParameter.
This commit is contained in:
lex00 authored and GitHub committed 2026-09-12 20:21:34 -04:00
1 parent ec2ab07689
commit fe40e06938
8 files changed
+560 -3

No files matched your search

@@ -49,7 +49,8 @@ class Ec2CreateFleetRollbackTest {
when(service.terminateInstances(REGION, List.of("i-second"))).thenReturn(List.of());
Ec2QueryHandler handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class), mock(Ec2IpamService.class));
mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class),
mock(Ec2SnapshotBlockPublicAccessService.class), mock(Ec2IpamService.class));
Response response = handler.handle("CreateFleet", params(), REGION);
assertEquals(400, response.getStatus());
@@ -63,7 +63,7 @@ class Ec2RunInstancesPublicIpParamTest {
Ec2QueryHandler handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class),
mock(Ec2IpamService.class));
mock(Ec2SnapshotBlockPublicAccessService.class), mock(Ec2IpamService.class));
handler.handle("RunInstances", p, REGION);
ArgumentCaptor<Boolean> associatePublicIp = ArgumentCaptor.forClass(Boolean.class);
@@ -0,0 +1,308 @@
package io.github.hectorvent.floci.services.ec2;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
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;
/**
* Integration tests for block public access for snapshots over the EC2 Query Protocol
* (form-encoded POST, XML response).
*
* <p>The setting is account-and-region scoped state rather than a resource: there is no id
* and nothing to tag, only one value that Enable and Disable move and Get reads back. Only
* the Get response carries {@code managedBy}.</p>
*/
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class Ec2SnapshotBlockPublicAccessIntegrationTest {
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
private static final String OTHER_REGION_AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=test/20260205/eu-west-1/ec2/aws4_request";
@Test
@Order(1)
void unconfiguredRegionReportsUnblocked() {
given()
.formParam("Action", "GetSnapshotBlockPublicAccessState")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml")
.body("GetSnapshotBlockPublicAccessStateResponse.state", equalTo("unblocked"))
.body("GetSnapshotBlockPublicAccessStateResponse.managedBy", equalTo("account"));
}
@Test
@Order(2)
void enableStoresBlockAllSharing() {
String body = given()
.formParam("Action", "EnableSnapshotBlockPublicAccess")
.formParam("State", "block-all-sharing")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml")
.body("EnableSnapshotBlockPublicAccessResponse.state", equalTo("block-all-sharing"))
.extract().asString();
// EnableSnapshotBlockPublicAccessResult carries State alone.
assertThat(body, not(containsString("managedBy")));
given()
.formParam("Action", "GetSnapshotBlockPublicAccessState")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("GetSnapshotBlockPublicAccessStateResponse.state", equalTo("block-all-sharing"));
}
@Test
@Order(3)
void enableNarrowsTheBlockToNewSharing() {
given()
.formParam("Action", "EnableSnapshotBlockPublicAccess")
.formParam("State", "block-new-sharing")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("EnableSnapshotBlockPublicAccessResponse.state", equalTo("block-new-sharing"));
given()
.formParam("Action", "GetSnapshotBlockPublicAccessState")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("GetSnapshotBlockPublicAccessStateResponse.state", equalTo("block-new-sharing"));
}
@Test
@Order(4)
void enableRejectsUnblocked() {
given()
.formParam("Action", "EnableSnapshotBlockPublicAccess")
.formParam("State", "unblocked")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
given()
.formParam("Action", "GetSnapshotBlockPublicAccessState")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("GetSnapshotBlockPublicAccessStateResponse.state", equalTo("block-new-sharing"));
}
@Test
@Order(5)
void enableRequiresState() {
given()
.formParam("Action", "EnableSnapshotBlockPublicAccess")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("MissingParameter"));
}
@Test
@Order(6)
void anotherRegionKeepsItsOwnState() {
given()
.formParam("Action", "GetSnapshotBlockPublicAccessState")
.header("Authorization", OTHER_REGION_AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("GetSnapshotBlockPublicAccessStateResponse.state", equalTo("unblocked"));
given()
.formParam("Action", "EnableSnapshotBlockPublicAccess")
.formParam("State", "block-all-sharing")
.header("Authorization", OTHER_REGION_AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("EnableSnapshotBlockPublicAccessResponse.state", equalTo("block-all-sharing"));
given()
.formParam("Action", "GetSnapshotBlockPublicAccessState")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("GetSnapshotBlockPublicAccessStateResponse.state", equalTo("block-new-sharing"));
}
@Test
@Order(7)
void disableReturnsAndStoresUnblocked() {
String body = given()
.formParam("Action", "DisableSnapshotBlockPublicAccess")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml")
.body("DisableSnapshotBlockPublicAccessResponse.state", equalTo("unblocked"))
.extract().asString();
// DisableSnapshotBlockPublicAccessResult carries State alone.
assertThat(body, not(containsString("managedBy")));
given()
.formParam("Action", "GetSnapshotBlockPublicAccessState")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("GetSnapshotBlockPublicAccessStateResponse.state", equalTo("unblocked"));
// Disabling one region leaves the other alone.
given()
.formParam("Action", "GetSnapshotBlockPublicAccessState")
.header("Authorization", OTHER_REGION_AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("GetSnapshotBlockPublicAccessStateResponse.state", equalTo("block-all-sharing"));
}
@Test
@Order(8)
void dryRunGetReturnsDryRunOperation() {
given()
.formParam("Action", "GetSnapshotBlockPublicAccessState")
.formParam("DryRun", "true")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(412)
.body("Response.Errors.Error.Code", equalTo("DryRunOperation"));
}
@Test
@Order(9)
void dryRunEnableLeavesTheStoredStateAlone() {
given()
.formParam("Action", "EnableSnapshotBlockPublicAccess")
.formParam("State", "block-all-sharing")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("EnableSnapshotBlockPublicAccessResponse.state", equalTo("block-all-sharing"));
given()
.formParam("Action", "EnableSnapshotBlockPublicAccess")
.formParam("State", "block-new-sharing")
.formParam("DryRun", "true")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(412)
.body("Response.Errors.Error.Code", equalTo("DryRunOperation"));
given()
.formParam("Action", "GetSnapshotBlockPublicAccessState")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("GetSnapshotBlockPublicAccessStateResponse.state", equalTo("block-all-sharing"));
}
@Test
@Order(10)
void dryRunEnableRejectsAnInvalidStateBeforeReportingDryRun() {
given()
.formParam("Action", "EnableSnapshotBlockPublicAccess")
.formParam("State", "unblocked")
.formParam("DryRun", "true")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
given()
.formParam("Action", "EnableSnapshotBlockPublicAccess")
.formParam("DryRun", "true")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("MissingParameter"));
given()
.formParam("Action", "GetSnapshotBlockPublicAccessState")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("GetSnapshotBlockPublicAccessStateResponse.state", equalTo("block-all-sharing"));
}
@Test
@Order(11)
void dryRunDisableLeavesTheStoredStateAlone() {
given()
.formParam("Action", "DisableSnapshotBlockPublicAccess")
.formParam("DryRun", "true")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(412)
.body("Response.Errors.Error.Code", equalTo("DryRunOperation"));
given()
.formParam("Action", "GetSnapshotBlockPublicAccessState")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("GetSnapshotBlockPublicAccessStateResponse.state", equalTo("block-all-sharing"));
}
}
@@ -0,0 +1,101 @@
package io.github.hectorvent.floci.services.ec2;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class Ec2SnapshotBlockPublicAccessServiceTest {
private Ec2SnapshotBlockPublicAccessService service;
@BeforeEach
void setUp() {
service = new Ec2SnapshotBlockPublicAccessService(new InMemoryStorage<>());
}
@Test
void unconfiguredRegionIsUnblocked() {
assertEquals("unblocked", service.getSnapshotBlockPublicAccessState("us-east-1"));
}
@Test
void enableStoresBlockAllSharing() {
assertEquals("block-all-sharing",
service.enableSnapshotBlockPublicAccess("us-east-1", "block-all-sharing"));
assertEquals("block-all-sharing", service.getSnapshotBlockPublicAccessState("us-east-1"));
}
@Test
void enableNarrowsAnExistingBlockToNewSharing() {
service.enableSnapshotBlockPublicAccess("us-east-1", "block-all-sharing");
assertEquals("block-new-sharing",
service.enableSnapshotBlockPublicAccess("us-east-1", "block-new-sharing"));
assertEquals("block-new-sharing", service.getSnapshotBlockPublicAccessState("us-east-1"));
}
@Test
void enableRejectsUnblocked() {
AwsException thrown = assertThrows(AwsException.class,
() -> service.enableSnapshotBlockPublicAccess("us-east-1", "unblocked"));
assertEquals("InvalidParameterValue", thrown.getErrorCode());
assertEquals("unblocked", service.getSnapshotBlockPublicAccessState("us-east-1"));
}
@Test
void enableRejectsAnUnknownState() {
AwsException thrown = assertThrows(AwsException.class,
() -> service.enableSnapshotBlockPublicAccess("us-east-1", "block-everything"));
assertEquals("InvalidParameterValue", thrown.getErrorCode());
}
@Test
void enableRequiresState() {
AwsException missing = assertThrows(AwsException.class,
() -> service.enableSnapshotBlockPublicAccess("us-east-1", null));
assertEquals("MissingParameter", missing.getErrorCode());
AwsException blank = assertThrows(AwsException.class,
() -> service.enableSnapshotBlockPublicAccess("us-east-1", " "));
assertEquals("MissingParameter", blank.getErrorCode());
}
@Test
void validateEnableStateRejectsWithoutStoringAnything() {
service.enableSnapshotBlockPublicAccess("us-east-1", "block-all-sharing");
assertThrows(AwsException.class, () -> service.validateEnableState("unblocked"));
assertThrows(AwsException.class, () -> service.validateEnableState(null));
assertEquals("block-all-sharing", service.getSnapshotBlockPublicAccessState("us-east-1"));
service.validateEnableState("block-new-sharing");
assertEquals("block-all-sharing", service.getSnapshotBlockPublicAccessState("us-east-1"));
}
@Test
void disableReturnsUnblocked() {
service.enableSnapshotBlockPublicAccess("us-east-1", "block-all-sharing");
assertEquals("unblocked", service.disableSnapshotBlockPublicAccess("us-east-1"));
assertEquals("unblocked", service.getSnapshotBlockPublicAccessState("us-east-1"));
}
@Test
void stateIsRegionScoped() {
service.enableSnapshotBlockPublicAccess("us-east-1", "block-all-sharing");
assertEquals("block-all-sharing", service.getSnapshotBlockPublicAccessState("us-east-1"));
assertEquals("unblocked", service.getSnapshotBlockPublicAccessState("eu-west-1"));
service.enableSnapshotBlockPublicAccess("eu-west-1", "block-new-sharing");
assertEquals("block-all-sharing", service.getSnapshotBlockPublicAccessState("us-east-1"));
assertEquals("block-new-sharing", service.getSnapshotBlockPublicAccessState("eu-west-1"));
service.disableSnapshotBlockPublicAccess("eu-west-1");
assertEquals("block-all-sharing", service.getSnapshotBlockPublicAccessState("us-east-1"));
}
}