fix(ec2): reject key pair requests without KeyName (#3727)

* fix(ec2): reject key pair requests without KeyName

CreateKeyPair accepted a request with no KeyName and stored a nameless key
pair; the duplicate check then dereferenced that name on every later
CreateKeyPair, and the Query catch-all reported it as
"Unexpected error: null". CreateKeyPair and ImportKeyPair now answer
MissingParameter, ImportKeyPair answers InvalidKeyPair.Format instead of an
InternalFailure for material that is not base64, DeleteKeyPair requires a
name or an id and returns the deleted keyPairId, and the name comparisons
tolerate a nameless record already sitting in a persistent store. The EC2
docs still described the placeholder key material that #3307 replaced.

Closes #3356

* fix(ec2): return InvalidKey.Format for malformed key material on ImportKeyPair

The EC2 API error reference lists InvalidKey.Format as the key pair not being in a valid OpenSSH public key format, which is the message this branch sends. InvalidKeyPair.Format is a separate documented code for a different failure.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-16 19:06:32 -04:00
1 parent 8d755c20d6
commit 996a680cdc
6 files changed
+214 -33

No files matched your search

@@ -488,6 +488,19 @@ class Ec2Tests {
});
}
@Test
@Order(18)
@DisplayName("CreateKeyPair - missing KeyName returns MissingParameter")
void createKeyPairWithoutNameIsRejected() {
assertThatThrownBy(() -> ec2.createKeyPair(CreateKeyPairRequest.builder().build()))
.isInstanceOf(Ec2Exception.class)
.satisfies(e -> {
Ec2Exception ec2Ex = (Ec2Exception) e;
assertThat(ec2Ex.awsErrorDetails().errorCode()).isEqualTo("MissingParameter");
assertThat(ec2Ex.statusCode()).isEqualTo(400);
});
}
@Test
@Order(19)
@DisplayName("CreateInternetGateway - create IGW")
@@ -812,7 +825,9 @@ class Ec2Tests {
@Order(46)
@DisplayName("DeleteKeyPair - delete key pair")
void deleteKeyPair() {
ec2.deleteKeyPair(DeleteKeyPairRequest.builder().keyName(keyName).build());
DeleteKeyPairResponse resp = ec2.deleteKeyPair(DeleteKeyPairRequest.builder().keyName(keyName).build());
assertThat(resp.returnValue()).isTrue();
assertThat(resp.keyPairId()).isNotNull().startsWith("key-");
}
@Test