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

@@ -157,6 +157,8 @@ public class AwsQueryController {
"CreateKeyPair", "DescribeKeyPairs", "DeleteKeyPair", "ImportKeyPair",
"DescribeImages", "RegisterImage", "DeregisterImage", "CreateImage", "CopyImage",
"DescribeSnapshots",
"EnableSnapshotBlockPublicAccess", "DisableSnapshotBlockPublicAccess",
"GetSnapshotBlockPublicAccessState",
"CreateTags", "DeleteTags", "DescribeTags",
"CreateInternetGateway", "DescribeInternetGateways", "DeleteInternetGateway",
"AttachInternetGateway", "DetachInternetGateway",
@@ -41,15 +41,19 @@ public class Ec2QueryHandler {
private final EmulatorConfig config;
private final FlowLogService flowLogService;
private final Ec2EbsEncryptionService ebsEncryptionService;
private final Ec2SnapshotBlockPublicAccessService snapshotBlockPublicAccessService;
private final Ec2IpamService ipamService;
@Inject
public Ec2QueryHandler(Ec2Service service, EmulatorConfig config, FlowLogService flowLogService,
Ec2EbsEncryptionService ebsEncryptionService, Ec2IpamService ipamService) {
Ec2EbsEncryptionService ebsEncryptionService,
Ec2SnapshotBlockPublicAccessService snapshotBlockPublicAccessService,
Ec2IpamService ipamService) {
this.service = service;
this.config = config;
this.flowLogService = flowLogService;
this.ebsEncryptionService = ebsEncryptionService;
this.snapshotBlockPublicAccessService = snapshotBlockPublicAccessService;
this.ipamService = ipamService;
}
@@ -80,6 +84,10 @@ public class Ec2QueryHandler {
case "GetEbsDefaultKmsKeyId" -> handleGetEbsDefaultKmsKeyId(region);
case "ModifyEbsDefaultKmsKeyId" -> handleModifyEbsDefaultKmsKeyId(params, region);
case "ResetEbsDefaultKmsKeyId" -> handleResetEbsDefaultKmsKeyId(region);
// Snapshot block public access
case "EnableSnapshotBlockPublicAccess" -> handleEnableSnapshotBlockPublicAccess(params, region);
case "DisableSnapshotBlockPublicAccess" -> handleDisableSnapshotBlockPublicAccess(params, region);
case "GetSnapshotBlockPublicAccessState" -> handleGetSnapshotBlockPublicAccessState(params, region);
// VPCs
case "CreateVpc" -> handleCreateVpc(params, region);
case "DescribeVpcs" -> handleDescribeVpcs(params, region);
@@ -581,6 +589,41 @@ public class Ec2QueryHandler {
return xmlResponse(xml.build());
}
private Response handleEnableSnapshotBlockPublicAccess(MultivaluedMap<String, String> p, String region) {
// Validate State before honoring DryRun. AWS returns DryRunOperation only once the
// request would otherwise have succeeded, so a bad State still fails on its own error.
String state = p.getFirst("State");
snapshotBlockPublicAccessService.validateEnableState(state);
checkDryRun(p);
return snapshotBlockPublicAccessResponse("EnableSnapshotBlockPublicAccessResponse",
snapshotBlockPublicAccessService.enableSnapshotBlockPublicAccess(region, state),
null);
}
private Response handleDisableSnapshotBlockPublicAccess(MultivaluedMap<String, String> p, String region) {
checkDryRun(p);
return snapshotBlockPublicAccessResponse("DisableSnapshotBlockPublicAccessResponse",
snapshotBlockPublicAccessService.disableSnapshotBlockPublicAccess(region), null);
}
private Response handleGetSnapshotBlockPublicAccessState(MultivaluedMap<String, String> p, String region) {
// Only GetSnapshotBlockPublicAccessState carries managedBy, and Floci has no
// declarative-policy layer, so the account always owns the state.
checkDryRun(p);
return snapshotBlockPublicAccessResponse("GetSnapshotBlockPublicAccessStateResponse",
snapshotBlockPublicAccessService.getSnapshotBlockPublicAccessState(region), "account");
}
private Response snapshotBlockPublicAccessResponse(String rootElement, String state, String managedBy) {
XmlBuilder xml = new XmlBuilder()
.start(rootElement, AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
.elem("state", state)
.elem("managedBy", managedBy)
.end(rootElement);
return xmlResponse(xml.build());
}
private Response ebsKmsKeyResponse(String rootElement, String kmsKeyId) {
XmlBuilder xml = new XmlBuilder()
.start(rootElement, AwsNamespaces.EC2)
@@ -0,0 +1,88 @@
package io.github.hectorvent.floci.services.ec2;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.util.Map;
import java.util.Set;
/**
* Block public access for snapshots: an account-level setting scoped to one region,
* not a resource, so the region is the whole key and there is nothing to tag or
* describe by id. The storage backend namespaces keys by the calling account, which
* is what makes the setting account-and-region scoped.
*
* <p>A region that was never configured reads back {@code unblocked}, matching the
* value AWS reports for an account that never enabled the setting.</p>
*/
@ApplicationScoped
public class Ec2SnapshotBlockPublicAccessService {
private static final Logger LOG = Logger.getLogger(Ec2SnapshotBlockPublicAccessService.class);
/** The state AWS reports for an account and region that never enabled the setting. */
public static final String UNBLOCKED = "unblocked";
static final String BLOCK_ALL_SHARING = "block-all-sharing";
static final String BLOCK_NEW_SHARING = "block-new-sharing";
/**
* SnapshotBlockPublicAccessState also carries {@code unblocked}, but the EC2 model
* documents it as invalid for EnableSnapshotBlockPublicAccess. Disabling goes through
* DisableSnapshotBlockPublicAccess instead.
*/
private static final Set<String> BLOCKING_STATES = Set.of(BLOCK_ALL_SHARING, BLOCK_NEW_SHARING);
// region -> SnapshotBlockPublicAccessState
private final StorageBackend<String, String> states;
@Inject
public Ec2SnapshotBlockPublicAccessService(StorageFactory storageFactory) {
this(storageFactory.create("ec2", "ec2-snapshot-block-public-access.json",
new TypeReference<Map<String, String>>() {}));
}
// Package-private for hermetic tests (pass an in-memory StorageBackend directly).
Ec2SnapshotBlockPublicAccessService(StorageBackend<String, String> states) {
this.states = states;
}
/**
* Validate the requested State without touching stored state, so a caller can reject a
* bad request before it honors DryRun. AWS reports an invalid parameter ahead of
* DryRunOperation, which it only returns once the request could otherwise succeed.
*/
public void validateEnableState(String state) {
if (state == null || state.isBlank()) {
throw new AwsException("MissingParameter",
"The request must contain the parameter State", 400);
}
if (!BLOCKING_STATES.contains(state)) {
throw new AwsException("InvalidParameterValue",
"Value (" + state + ") for parameter State is invalid. Valid values are "
+ BLOCK_ALL_SHARING + " and " + BLOCK_NEW_SHARING, 400);
}
}
public String enableSnapshotBlockPublicAccess(String region, String state) {
validateEnableState(state);
states.put(region, state);
LOG.infov("Snapshot block public access in {0} set to {1}", region, state);
return state;
}
public String disableSnapshotBlockPublicAccess(String region) {
states.put(region, UNBLOCKED);
LOG.infov("Snapshot block public access in {0} set to {1}", region, UNBLOCKED);
return UNBLOCKED;
}
public String getSnapshotBlockPublicAccessState(String region) {
return states.get(region).orElse(UNBLOCKED);
}
}