mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(ec2): support AMI block public access (#7134)
This commit is contained in:
1 parent
664db0cef3
commit
f489a6f1f3
15 files changed
+402
-214
No files matched your search
@@ -1213,6 +1213,18 @@ These are account-level settings scoped per region, not per volume, and nothing
|
||||
|
||||
An account that has never set a key reports `alias/aws/ebs`, the AWS-managed EBS key every account starts with, rather than an empty value — the module runner fails hard on a missing `KmsKeyId`, so the fallback is what keeps it running. `ResetEbsDefaultKmsKeyId` returns to that same alias. `ModifyEbsDefaultKmsKeyId` requires `KmsKeyId` and rejects a blank one with `MissingParameter`; the key is stored as given and is not checked against KMS.
|
||||
|
||||
### AMI Block Public Access
|
||||
|
||||
| Action | Description |
|
||||
|--------|-------------|
|
||||
| EnableImageBlockPublicAccess | Sets the region's AMI sharing block to `block-new-sharing`. |
|
||||
| DisableImageBlockPublicAccess | Returns the region to `unblocked`. |
|
||||
| GetImageBlockPublicAccessState | Reads the region's current state and manager. |
|
||||
|
||||
This is an account-level setting scoped per region. A region that was never configured reports `unblocked`. Enable accepts only `block-new-sharing`; callers use Disable to return to `unblocked`. Get reports `managedBy` as `account` because Floci has no declarative-policy layer.
|
||||
|
||||
All three actions support `DryRun`. A successful dry run returns `DryRunOperation` and does not change the stored state. Enable validates `ImageBlockPublicAccessState` before checking `DryRun`, so missing and invalid values keep their normal errors. The state applies immediately, and existing public AMIs remain public.
|
||||
|
||||
### Snapshot Block Public Access
|
||||
|
||||
| Action | Description |
|
||||
|
||||
@@ -84,7 +84,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac
|
||||
| [Data Firehose](firehose.md) | `POST /` + `X-Amz-Target: Firehose_20150804.*` | JSON 1.1 | 12 |
|
||||
| [ECS](ecs.md) | `POST /` + `X-Amz-Target: AmazonEC2ContainerServiceV20141113.*` | JSON 1.1 | 59 |
|
||||
| [EFS](efs.md) | `/2015-02-01/...` | REST JSON | 26 |
|
||||
| [EC2](ec2.md) | `POST /` with `Action=` param | EC2 Query | 202 |
|
||||
| [EC2](ec2.md) | `POST /` with `Action=` param | EC2 Query | 205 |
|
||||
| [Lightsail](lightsail.md) | `POST /` + `X-Amz-Target: Lightsail_20161128.*` | JSON 1.1 | 79 local responses; 161 recognized actions |
|
||||
| [ACM](acm.md) | `POST /` + `X-Amz-Target: CertificateManager.*` | JSON 1.1 | 16 |
|
||||
| [ECR](ecr.md) | `POST /` + `X-Amz-Target: AmazonEC2ContainerRegistry_V20150921.*` (control plane) and `/v2/...` (data plane proxied to `registry:2`) | JSON 1.1 + OCI Distribution | 34 |
|
||||
|
||||
+110
@@ -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());
|
||||
|
||||
-88
@@ -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);
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
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 Ec2BlockPublicAccessServiceTest {
|
||||
|
||||
private Ec2BlockPublicAccessService service;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new Ec2BlockPublicAccessService(new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unconfiguredRegionsAreUnblocked() {
|
||||
assertEquals("unblocked",
|
||||
service.getState(Ec2BlockPublicAccessService.Setting.IMAGE, "us-east-1"));
|
||||
assertEquals("unblocked",
|
||||
service.getState(Ec2BlockPublicAccessService.Setting.SNAPSHOT, "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void imageSettingAcceptsOnlyBlockNewSharing() {
|
||||
assertEquals("block-new-sharing", service.enable(Ec2BlockPublicAccessService.Setting.IMAGE,
|
||||
"us-east-1", "block-new-sharing"));
|
||||
|
||||
AwsException unblocked = assertThrows(AwsException.class,
|
||||
() -> service.enable(Ec2BlockPublicAccessService.Setting.IMAGE, "us-east-1", "unblocked"));
|
||||
assertEquals("InvalidParameterValue", unblocked.getErrorCode());
|
||||
|
||||
AwsException blockAll = assertThrows(AwsException.class,
|
||||
() -> service.enable(Ec2BlockPublicAccessService.Setting.IMAGE,
|
||||
"us-east-1", "block-all-sharing"));
|
||||
assertEquals("InvalidParameterValue", blockAll.getErrorCode());
|
||||
assertEquals("block-new-sharing",
|
||||
service.getState(Ec2BlockPublicAccessService.Setting.IMAGE, "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void snapshotSettingAcceptsBothBlockingStates() {
|
||||
assertEquals("block-all-sharing", service.enable(Ec2BlockPublicAccessService.Setting.SNAPSHOT,
|
||||
"us-east-1", "block-all-sharing"));
|
||||
assertEquals("block-new-sharing", service.enable(Ec2BlockPublicAccessService.Setting.SNAPSHOT,
|
||||
"us-east-1", "block-new-sharing"));
|
||||
assertEquals("block-new-sharing",
|
||||
service.getState(Ec2BlockPublicAccessService.Setting.SNAPSHOT, "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingStateUsesTheSettingParameterName() {
|
||||
AwsException image = assertThrows(AwsException.class,
|
||||
() -> service.validateEnableState(Ec2BlockPublicAccessService.Setting.IMAGE, null));
|
||||
assertEquals("MissingParameter", image.getErrorCode());
|
||||
assertEquals("The request must contain the parameter ImageBlockPublicAccessState", image.getMessage());
|
||||
|
||||
AwsException snapshot = assertThrows(AwsException.class,
|
||||
() -> service.validateEnableState(Ec2BlockPublicAccessService.Setting.SNAPSHOT, " "));
|
||||
assertEquals("MissingParameter", snapshot.getErrorCode());
|
||||
assertEquals("The request must contain the parameter State", snapshot.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void validationDoesNotChangeStoredState() {
|
||||
service.enable(Ec2BlockPublicAccessService.Setting.SNAPSHOT, "us-east-1", "block-all-sharing");
|
||||
|
||||
assertThrows(AwsException.class,
|
||||
() -> service.validateEnableState(Ec2BlockPublicAccessService.Setting.SNAPSHOT, "unblocked"));
|
||||
assertThrows(AwsException.class,
|
||||
() -> service.validateEnableState(Ec2BlockPublicAccessService.Setting.SNAPSHOT, null));
|
||||
assertEquals("block-all-sharing",
|
||||
service.getState(Ec2BlockPublicAccessService.Setting.SNAPSHOT, "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void disableReturnsUnblocked() {
|
||||
service.enable(Ec2BlockPublicAccessService.Setting.IMAGE, "us-east-1", "block-new-sharing");
|
||||
|
||||
assertEquals("unblocked",
|
||||
service.disable(Ec2BlockPublicAccessService.Setting.IMAGE, "us-east-1"));
|
||||
assertEquals("unblocked",
|
||||
service.getState(Ec2BlockPublicAccessService.Setting.IMAGE, "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void statesAreScopedBySettingAndRegion() {
|
||||
service.enable(Ec2BlockPublicAccessService.Setting.IMAGE, "us-east-1", "block-new-sharing");
|
||||
service.enable(Ec2BlockPublicAccessService.Setting.SNAPSHOT, "eu-west-1", "block-all-sharing");
|
||||
|
||||
assertEquals("block-new-sharing",
|
||||
service.getState(Ec2BlockPublicAccessService.Setting.IMAGE, "us-east-1"));
|
||||
assertEquals("unblocked",
|
||||
service.getState(Ec2BlockPublicAccessService.Setting.IMAGE, "eu-west-1"));
|
||||
assertEquals("unblocked",
|
||||
service.getState(Ec2BlockPublicAccessService.Setting.SNAPSHOT, "us-east-1"));
|
||||
assertEquals("block-all-sharing",
|
||||
service.getState(Ec2BlockPublicAccessService.Setting.SNAPSHOT, "eu-west-1"));
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -56,7 +56,7 @@ class Ec2CreateFleetRollbackTest {
|
||||
|
||||
Ec2QueryHandler handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
|
||||
mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class),
|
||||
mock(Ec2SnapshotBlockPublicAccessService.class), mock(Ec2IpamService.class),
|
||||
mock(Ec2BlockPublicAccessService.class), mock(Ec2IpamService.class),
|
||||
mock(RegionResolver.class));
|
||||
Response response = handler.handle("CreateFleet", params(), REGION);
|
||||
|
||||
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.github.hectorvent.floci.testing.AwsTestClients;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.DisableImageBlockPublicAccessRequest;
|
||||
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
|
||||
import software.amazon.awssdk.services.ec2.model.GetImageBlockPublicAccessStateRequest;
|
||||
import software.amazon.awssdk.services.ec2.model.GetImageBlockPublicAccessStateResponse;
|
||||
|
||||
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 static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@QuarkusTest
|
||||
class Ec2ImageBlockPublicAccessIntegrationTest {
|
||||
|
||||
private static final String ERROR_AUTH_HEADER =
|
||||
"AWS4-HMAC-SHA256 Credential=814500000002/20261031/us-east-1/ec2/aws4_request";
|
||||
|
||||
@Test
|
||||
void sdkRoundTripUsesAwsResponseShape() {
|
||||
try (Ec2Client client = AwsTestClients.create(Ec2Client.builder(), "814500000001", Region.US_EAST_1)) {
|
||||
GetImageBlockPublicAccessStateResponse initial = getState(client);
|
||||
assertEquals("unblocked", initial.imageBlockPublicAccessState());
|
||||
assertEquals("account", initial.managedByAsString());
|
||||
|
||||
assertEquals("block-new-sharing", client.enableImageBlockPublicAccess(request -> request
|
||||
.imageBlockPublicAccessState("block-new-sharing"))
|
||||
.imageBlockPublicAccessStateAsString());
|
||||
assertEquals("block-new-sharing", getState(client).imageBlockPublicAccessState());
|
||||
|
||||
assertEquals("unblocked", client.disableImageBlockPublicAccess(
|
||||
DisableImageBlockPublicAccessRequest.builder().build())
|
||||
.imageBlockPublicAccessStateAsString());
|
||||
assertEquals("unblocked", getState(client).imageBlockPublicAccessState());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateIsScopedByRegion() {
|
||||
try (Ec2Client home = AwsTestClients.create(Ec2Client.builder(), "814500000003", Region.US_EAST_1);
|
||||
Ec2Client away = AwsTestClients.create(Ec2Client.builder(), "814500000003", Region.EU_WEST_1)) {
|
||||
home.enableImageBlockPublicAccess(request -> request
|
||||
.imageBlockPublicAccessState("block-new-sharing"));
|
||||
|
||||
assertEquals("block-new-sharing", getState(home).imageBlockPublicAccessState());
|
||||
assertEquals("unblocked", getState(away).imageBlockPublicAccessState());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void enableValidatesStateBeforeDryRun() {
|
||||
given()
|
||||
.formParam("Action", "EnableImageBlockPublicAccess")
|
||||
.formParam("ImageBlockPublicAccessState", "unblocked")
|
||||
.formParam("DryRun", "true")
|
||||
.header("Authorization", ERROR_AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
|
||||
|
||||
given()
|
||||
.formParam("Action", "EnableImageBlockPublicAccess")
|
||||
.formParam("DryRun", "true")
|
||||
.header("Authorization", ERROR_AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("MissingParameter"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dryRunDoesNotChangeState() {
|
||||
try (Ec2Client client = AwsTestClients.create(Ec2Client.builder(), "814500000004", Region.US_EAST_1)) {
|
||||
client.enableImageBlockPublicAccess(request -> request
|
||||
.imageBlockPublicAccessState("block-new-sharing"));
|
||||
|
||||
Ec2Exception disableError = assertThrows(Ec2Exception.class,
|
||||
() -> client.disableImageBlockPublicAccess(request -> request.dryRun(true)));
|
||||
assertEquals("DryRunOperation", disableError.awsErrorDetails().errorCode());
|
||||
assertEquals("block-new-sharing", getState(client).imageBlockPublicAccessState());
|
||||
|
||||
Ec2Exception getError = assertThrows(Ec2Exception.class,
|
||||
() -> client.getImageBlockPublicAccessState(request -> request.dryRun(true)));
|
||||
assertEquals("DryRunOperation", getError.awsErrorDetails().errorCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void onlyGetReturnsManagedBy() {
|
||||
String enableBody = given()
|
||||
.formParam("Action", "EnableImageBlockPublicAccess")
|
||||
.formParam("ImageBlockPublicAccessState", "block-new-sharing")
|
||||
.header("Authorization", ERROR_AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("EnableImageBlockPublicAccessResponse.imageBlockPublicAccessState",
|
||||
equalTo("block-new-sharing"))
|
||||
.extract().asString();
|
||||
assertThat(enableBody, not(containsString("managedBy")));
|
||||
|
||||
given()
|
||||
.formParam("Action", "GetImageBlockPublicAccessState")
|
||||
.header("Authorization", ERROR_AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("GetImageBlockPublicAccessStateResponse.managedBy", equalTo("account"));
|
||||
}
|
||||
|
||||
private static GetImageBlockPublicAccessStateResponse getState(Ec2Client client) {
|
||||
return client.getImageBlockPublicAccessState(GetImageBlockPublicAccessStateRequest.builder().build());
|
||||
}
|
||||
}
|
||||
@@ -664,7 +664,7 @@ class Ec2QueryHandlerTest {
|
||||
private Ec2QueryHandler handler(Ec2Service service, RegionResolver regionResolver) {
|
||||
return new Ec2QueryHandler(
|
||||
service, mock(EmulatorConfig.class), mock(FlowLogService.class),
|
||||
mock(Ec2EbsEncryptionService.class), mock(Ec2SnapshotBlockPublicAccessService.class),
|
||||
mock(Ec2EbsEncryptionService.class), mock(Ec2BlockPublicAccessService.class),
|
||||
mock(Ec2IpamService.class), regionResolver);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -61,7 +61,7 @@ class Ec2RepeatedFilterNamesTest {
|
||||
service = new Ec2Service(config, mock(Ec2ContainerManager.class), mock(Ec2PortForwardManager.class),
|
||||
new AmiImageResolver(imageCatalog), imageCatalog, new Ec2InstanceTypeCatalog(), new MemoryFactory());
|
||||
handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class), mock(FlowLogService.class),
|
||||
mock(Ec2EbsEncryptionService.class), mock(Ec2SnapshotBlockPublicAccessService.class),
|
||||
mock(Ec2EbsEncryptionService.class), mock(Ec2BlockPublicAccessService.class),
|
||||
mock(Ec2IpamService.class), new RegionResolver(REGION, "000000000000"));
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -124,7 +124,7 @@ class Ec2RunInstancesPublicIpParamTest {
|
||||
|
||||
private static Ec2QueryHandler handler(Ec2Service service) {
|
||||
return new Ec2QueryHandler(service, mock(EmulatorConfig.class), mock(FlowLogService.class),
|
||||
mock(Ec2EbsEncryptionService.class), mock(Ec2SnapshotBlockPublicAccessService.class),
|
||||
mock(Ec2EbsEncryptionService.class), mock(Ec2BlockPublicAccessService.class),
|
||||
mock(Ec2IpamService.class), mock(RegionResolver.class));
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -721,7 +721,7 @@ class Ec2ServicePersistenceTest {
|
||||
.map(KeyPair::getKeyName).toList());
|
||||
|
||||
String xml = (String) new Ec2QueryHandler(restarted, mock(EmulatorConfig.class), mock(FlowLogService.class),
|
||||
mock(Ec2EbsEncryptionService.class), mock(Ec2SnapshotBlockPublicAccessService.class),
|
||||
mock(Ec2EbsEncryptionService.class), mock(Ec2BlockPublicAccessService.class),
|
||||
mock(Ec2IpamService.class), new RegionResolver(REGION, "000000000000"))
|
||||
.handle("DescribeKeyPairs", new MultivaluedHashMap<>(), REGION).getEntity();
|
||||
assertTrue(xml.contains("<keyType>rsa</keyType>"), xml);
|
||||
@@ -828,7 +828,7 @@ class Ec2ServicePersistenceTest {
|
||||
}
|
||||
Response response = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
|
||||
mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class),
|
||||
mock(Ec2SnapshotBlockPublicAccessService.class), mock(Ec2IpamService.class),
|
||||
mock(Ec2BlockPublicAccessService.class), mock(Ec2IpamService.class),
|
||||
new RegionResolver(REGION, "000000000000"))
|
||||
.handle(action, form, REGION);
|
||||
String body = String.valueOf(response.getEntity());
|
||||
|
||||
@@ -340,7 +340,7 @@ class Ec2ServiceTest {
|
||||
List.of(), null, null);
|
||||
Ec2QueryHandler handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
|
||||
mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class),
|
||||
mock(Ec2SnapshotBlockPublicAccessService.class), mock(Ec2IpamService.class),
|
||||
mock(Ec2BlockPublicAccessService.class), mock(Ec2IpamService.class),
|
||||
new RegionResolver("us-east-1", "000000000000"));
|
||||
clearInvocations(volumeStore);
|
||||
|
||||
@@ -363,7 +363,7 @@ class Ec2ServiceTest {
|
||||
service.associateAddress("us-east-1", elastic.getAllocationId(), launched.get(1).getInstanceId());
|
||||
Ec2QueryHandler handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
|
||||
mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class),
|
||||
mock(Ec2SnapshotBlockPublicAccessService.class), mock(Ec2IpamService.class),
|
||||
mock(Ec2BlockPublicAccessService.class), mock(Ec2IpamService.class),
|
||||
new RegionResolver("us-east-1", "000000000000"));
|
||||
// The first read settles the pending launches, which reads the addresses once per launch.
|
||||
handler.handle("DescribeInstances", new MultivaluedHashMap<>(), "us-east-1");
|
||||
@@ -5613,7 +5613,7 @@ class Ec2ServiceTest {
|
||||
assertTrue(service.describeKeyPairs("us-east-1", List.of(), List.of(),
|
||||
Map.of("key-type", List.of("ed25519"))).isEmpty());
|
||||
String xml = (String) new Ec2QueryHandler(service, mock(EmulatorConfig.class), mock(FlowLogService.class),
|
||||
mock(Ec2EbsEncryptionService.class), mock(Ec2SnapshotBlockPublicAccessService.class),
|
||||
mock(Ec2EbsEncryptionService.class), mock(Ec2BlockPublicAccessService.class),
|
||||
mock(Ec2IpamService.class), new RegionResolver("us-east-1", "000000000000"))
|
||||
.handle("DescribeKeyPairs", new MultivaluedHashMap<>(), "us-east-1").getEntity();
|
||||
assertTrue(xml.contains("<keyPairId>key-unreadable</keyPairId>"), xml);
|
||||
|
||||
-101
@@ -1,101 +0,0 @@
|
||||
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"));
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,7 @@ class Ec2VolumeKmsKeyIdTest {
|
||||
mock(Ec2PortForwardManager.class), new AmiImageResolver(imageCatalog), imageCatalog,
|
||||
new Ec2InstanceTypeCatalog(), new MemoryFactory());
|
||||
handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class), mock(FlowLogService.class),
|
||||
mock(Ec2EbsEncryptionService.class), mock(Ec2SnapshotBlockPublicAccessService.class),
|
||||
mock(Ec2EbsEncryptionService.class), mock(Ec2BlockPublicAccessService.class),
|
||||
mock(Ec2IpamService.class), new RegionResolver(REGION, "000000000000"));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user