feat(ec2): support AMI block public access (#7134)

This commit is contained in:
Ovidiu (Ovi) authored and GitHub committed 2026-11-01 07:51:55 -04:00
1 parent 664db0cef3
commit f489a6f1f3
15 files changed
+402 -214

No files matched your search

@@ -0,0 +1,110 @@
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.EnumMap;
import java.util.List;
import java.util.Map;
/** Account-level block public access settings, scoped to one region. */
@ApplicationScoped
public class Ec2BlockPublicAccessService {
private static final Logger LOG = Logger.getLogger(Ec2BlockPublicAccessService.class);
public static final String UNBLOCKED = "unblocked";
static final String BLOCK_ALL_SHARING = "block-all-sharing";
static final String BLOCK_NEW_SHARING = "block-new-sharing";
public enum Setting {
IMAGE("AMI", "ImageBlockPublicAccessState", "ec2-image-block-public-access.json",
List.of(BLOCK_NEW_SHARING)),
SNAPSHOT("Snapshot", "State", "ec2-snapshot-block-public-access.json",
List.of(BLOCK_ALL_SHARING, BLOCK_NEW_SHARING));
private final String displayName;
private final String parameterName;
private final String storageFile;
private final List<String> blockingStates;
Setting(String displayName, String parameterName, String storageFile, List<String> blockingStates) {
this.displayName = displayName;
this.parameterName = parameterName;
this.storageFile = storageFile;
this.blockingStates = blockingStates;
}
String parameterName() {
return parameterName;
}
private String validValuesMessage() {
String label = blockingStates.size() == 1 ? "Valid value is " : "Valid values are ";
return label + String.join(" and ", blockingStates);
}
}
private final Map<Setting, StorageBackend<String, String>> states;
@Inject
public Ec2BlockPublicAccessService(StorageFactory storageFactory) {
this(createStorage(storageFactory));
}
Ec2BlockPublicAccessService(StorageBackend<String, String> imageStates,
StorageBackend<String, String> snapshotStates) {
EnumMap<Setting, StorageBackend<String, String>> configuredStates = new EnumMap<>(Setting.class);
configuredStates.put(Setting.IMAGE, imageStates);
configuredStates.put(Setting.SNAPSHOT, snapshotStates);
this.states = configuredStates;
}
private Ec2BlockPublicAccessService(Map<Setting, StorageBackend<String, String>> states) {
this.states = states;
}
public void validateEnableState(Setting setting, String state) {
if (state == null || state.isBlank()) {
throw new AwsException("MissingParameter",
"The request must contain the parameter " + setting.parameterName, 400);
}
if (!setting.blockingStates.contains(state)) {
throw new AwsException("InvalidParameterValue",
"Value (" + state + ") for parameter " + setting.parameterName + " is invalid. "
+ setting.validValuesMessage(), 400);
}
}
public String enable(Setting setting, String region, String state) {
validateEnableState(setting, state);
states.get(setting).put(region, state);
LOG.infov("{0} block public access in {1} set to {2}", setting.displayName, region, state);
return state;
}
public String disable(Setting setting, String region) {
states.get(setting).put(region, UNBLOCKED);
LOG.infov("{0} block public access in {1} set to {2}", setting.displayName, region, UNBLOCKED);
return UNBLOCKED;
}
public String getState(Setting setting, String region) {
return states.get(setting).get(region).orElse(UNBLOCKED);
}
private static Map<Setting, StorageBackend<String, String>> createStorage(StorageFactory storageFactory) {
EnumMap<Setting, StorageBackend<String, String>> states = new EnumMap<>(Setting.class);
for (Setting setting : Setting.values()) {
StorageBackend<String, String> backend = storageFactory.create("ec2", setting.storageFile,
new TypeReference<Map<String, String>>() {});
states.put(setting, backend);
}
return states;
}
}
@@ -52,20 +52,20 @@ public class Ec2QueryHandler {
private final EmulatorConfig config;
private final FlowLogService flowLogService;
private final Ec2EbsEncryptionService ebsEncryptionService;
private final Ec2SnapshotBlockPublicAccessService snapshotBlockPublicAccessService;
private final Ec2BlockPublicAccessService blockPublicAccessService;
private final Ec2IpamService ipamService;
private final RegionResolver regionResolver;
@Inject
public Ec2QueryHandler(Ec2Service service, EmulatorConfig config, FlowLogService flowLogService,
Ec2EbsEncryptionService ebsEncryptionService,
Ec2SnapshotBlockPublicAccessService snapshotBlockPublicAccessService,
Ec2BlockPublicAccessService blockPublicAccessService,
Ec2IpamService ipamService, RegionResolver regionResolver) {
this.service = service;
this.config = config;
this.flowLogService = flowLogService;
this.ebsEncryptionService = ebsEncryptionService;
this.snapshotBlockPublicAccessService = snapshotBlockPublicAccessService;
this.blockPublicAccessService = blockPublicAccessService;
this.ipamService = ipamService;
this.regionResolver = regionResolver;
}
@@ -102,6 +102,10 @@ public class Ec2QueryHandler {
case "GetEbsDefaultKmsKeyId" -> handleGetEbsDefaultKmsKeyId(region);
case "ModifyEbsDefaultKmsKeyId" -> handleModifyEbsDefaultKmsKeyId(params, region);
case "ResetEbsDefaultKmsKeyId" -> handleResetEbsDefaultKmsKeyId(region);
// AMI block public access
case "EnableImageBlockPublicAccess" -> handleEnableImageBlockPublicAccess(params, region);
case "DisableImageBlockPublicAccess" -> handleDisableImageBlockPublicAccess(params, region);
case "GetImageBlockPublicAccessState" -> handleGetImageBlockPublicAccessState(params, region);
// Snapshot block public access
case "EnableSnapshotBlockPublicAccess" -> handleEnableSnapshotBlockPublicAccess(params, region);
case "DisableSnapshotBlockPublicAccess" -> handleDisableSnapshotBlockPublicAccess(params, region);
@@ -720,36 +724,56 @@ public class Ec2QueryHandler {
return xmlResponse(xml.build());
}
private Response handleEnableImageBlockPublicAccess(MultivaluedMap<String, String> p, String region) {
String state = p.getFirst(Ec2BlockPublicAccessService.Setting.IMAGE.parameterName());
blockPublicAccessService.validateEnableState(Ec2BlockPublicAccessService.Setting.IMAGE, state);
checkDryRun(p);
return blockPublicAccessResponse("EnableImageBlockPublicAccessResponse", "imageBlockPublicAccessState",
blockPublicAccessService.enable(Ec2BlockPublicAccessService.Setting.IMAGE, region, state), null);
}
private Response handleDisableImageBlockPublicAccess(MultivaluedMap<String, String> p, String region) {
checkDryRun(p);
return blockPublicAccessResponse("DisableImageBlockPublicAccessResponse", "imageBlockPublicAccessState",
blockPublicAccessService.disable(Ec2BlockPublicAccessService.Setting.IMAGE, region), null);
}
private Response handleGetImageBlockPublicAccessState(MultivaluedMap<String, String> p, String region) {
checkDryRun(p);
return blockPublicAccessResponse("GetImageBlockPublicAccessStateResponse", "imageBlockPublicAccessState",
blockPublicAccessService.getState(Ec2BlockPublicAccessService.Setting.IMAGE, region), "account");
}
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);
String state = p.getFirst(Ec2BlockPublicAccessService.Setting.SNAPSHOT.parameterName());
blockPublicAccessService.validateEnableState(Ec2BlockPublicAccessService.Setting.SNAPSHOT, state);
checkDryRun(p);
return snapshotBlockPublicAccessResponse("EnableSnapshotBlockPublicAccessResponse",
snapshotBlockPublicAccessService.enableSnapshotBlockPublicAccess(region, state),
null);
return blockPublicAccessResponse("EnableSnapshotBlockPublicAccessResponse", "state",
blockPublicAccessService.enable(Ec2BlockPublicAccessService.Setting.SNAPSHOT, region, state), null);
}
private Response handleDisableSnapshotBlockPublicAccess(MultivaluedMap<String, String> p, String region) {
checkDryRun(p);
return snapshotBlockPublicAccessResponse("DisableSnapshotBlockPublicAccessResponse",
snapshotBlockPublicAccessService.disableSnapshotBlockPublicAccess(region), null);
return blockPublicAccessResponse("DisableSnapshotBlockPublicAccessResponse", "state",
blockPublicAccessService.disable(Ec2BlockPublicAccessService.Setting.SNAPSHOT, 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");
return blockPublicAccessResponse("GetSnapshotBlockPublicAccessStateResponse", "state",
blockPublicAccessService.getState(Ec2BlockPublicAccessService.Setting.SNAPSHOT, region), "account");
}
private Response snapshotBlockPublicAccessResponse(String rootElement, String state, String managedBy) {
private Response blockPublicAccessResponse(String rootElement, String stateElement, String state,
String managedBy) {
XmlBuilder xml = new XmlBuilder()
.start(rootElement, AwsNamespaces.EC2)
.elem("requestId", UUID.randomUUID().toString())
.elem("state", state)
.elem(stateElement, state)
.elem("managedBy", managedBy)
.end(rootElement);
return xmlResponse(xml.build());
@@ -1,88 +0,0 @@
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);
}
}