Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(ec2): DescribeKeyPairs returns InvalidKeyPair.NotFound for missing names/ids (#1932)

Ankit13247d961068e1f1
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
index 43b471fabf..b3066bf5dd 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
@@ -1583,8 +1583,27 @@ public class Ec2Service implements ContainerTeardown {
public List<KeyPair> describeKeyPairs(String region, List<String> keyNames, List<String> keyPairIds) {
ensureDefaultResources(region);
- return keyPairs.scan(k -> true).stream()
+ List<KeyPair> regionKeyPairs = keyPairs.scan(k -> true).stream()
.filter(k -> k.getRegion().equals(region))
+ .collect(Collectors.toList());
+
+ // A named/id lookup for a key pair that does not exist is an error in real
+ // AWS (InvalidKeyPair.NotFound), not an empty result — otherwise idempotent
+ // callers that treat exit 0 as "present" skip creating the key.
+ for (String keyName : keyNames) {
+ if (regionKeyPairs.stream().noneMatch(k -> keyName.equals(k.getKeyName()))) {
+ throw new AwsException("InvalidKeyPair.NotFound",
+ "The key pair '" + keyName + "' does not exist", 400);
+ }
+ }
+ for (String keyPairId : keyPairIds) {
+ if (regionKeyPairs.stream().noneMatch(k -> keyPairId.equals(k.getKeyPairId()))) {
+ throw new AwsException("InvalidKeyPair.NotFound",
+ "The key pair ID '" + keyPairId + "' does not exist", 400);
+ }
+ }
+
+ return regionKeyPairs.stream()
.filter(k -> keyNames.isEmpty() || keyNames.contains(k.getKeyName()))
.filter(k -> keyPairIds.isEmpty() || keyPairIds.contains(k.getKeyPairId()))
.collect(Collectors.toList());
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java
index 71175bbc6f..0b01be4cfa 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java
@@ -930,6 +930,20 @@ class Ec2IntegrationTest {
.body("DescribeKeyPairsResponse.keySet.item.keyName", equalTo("test-key"));
}
+ @Test
+ @Order(41)
+ void describeKeyPairsRejectsMissingName() {
+ given()
+ .formParam("Action", "DescribeKeyPairs")
+ .formParam("KeyName.1", "does-not-exist")
+ .header("Authorization", AUTH_HEADER)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("Response.Errors.Error.Code", equalTo("InvalidKeyPair.NotFound"));
+ }
+
@Test
@Order(39)
void importKeyPair() {
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
index 16b96fa515..eed059623a 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
@@ -297,6 +297,50 @@ class Ec2ServiceTest {
assertEquals("InvalidKeyPair.Duplicate", error.getErrorCode());
}
+ @Test
+ void describeKeyPairsThrowsNotFoundForMissingName() {
+ Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
+ mock(Ec2PortForwardManager.class),
+ mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
+ new InMemoryStorageFactory());
+
+ AwsException error = assertThrows(AwsException.class,
+ () -> service.describeKeyPairs("us-east-1", List.of("does-not-exist"), List.of()));
+ assertEquals("InvalidKeyPair.NotFound", error.getErrorCode());
+ assertEquals(400, error.getHttpStatus());
+ }
+
+ @Test
+ void describeKeyPairsThrowsNotFoundForMissingId() {
+ Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
+ mock(Ec2PortForwardManager.class),
+ mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
+ new InMemoryStorageFactory());
+
+ AwsException error = assertThrows(AwsException.class,
+ () -> service.describeKeyPairs("us-east-1", List.of(), List.of("key-missing")));
+ assertEquals("InvalidKeyPair.NotFound", error.getErrorCode());
+ }
+
+ @Test
+ void describeKeyPairsReturnsRequestedKeyAndAllowsEmptyUnfilteredList() {
+ Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
+ mock(Ec2PortForwardManager.class),
+ mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
+ new InMemoryStorageFactory());
+
+ // Unfiltered describe on an empty account is not an error.
+ assertTrue(service.describeKeyPairs("us-east-1", List.of(), List.of()).isEmpty());
+
+ service.createKeyPair("us-east-1", "present-key");
+ assertEquals(1, service.describeKeyPairs("us-east-1", List.of("present-key"), List.of()).size());
+
+ // A missing name is not masked by a present one in the same request.
+ AwsException error = assertThrows(AwsException.class,
+ () -> service.describeKeyPairs("us-east-1", List.of("present-key", "absent-key"), List.of()));
+ assertEquals("InvalidKeyPair.NotFound", error.getErrorCode());
+ }
+
@Test
void registerImageReusingSnapshotDoesNotOverwriteSnapshotMetadata() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),