fix(ec2): honor IncludePublicKey when describing key pairs (#6055)

* fix(ec2): honor IncludePublicKey when describing key pairs

DescribeKeyPairs ignored IncludePublicKey. With IncludePublicKey=true each
key pair now carries its OpenSSH public key, with the key-pair name as the
comment in place of any comment an imported key had; omitted or false
leaves it out. Imported material that is not a whole OpenSSH key, such as
PEM or RFC 4716 text, a blob missing a field its type needs, a line that
names another type than its blob, or a key type other than RSA, ED25519,
ECDSA and their security-key forms, is kept as given and described without
a public key. SshPublicKeys.carriesAKey checks the fields each type needs.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(ec2): describe a key pair's key only when it is exactly one key

DescribeKeyPairs with IncludePublicKey=true still described a blob with
bytes after its key, which OpenSSH and Go's ssh package refuse, and the
first of several key lines, although an instance launched with the key
pair trusts every line. Both are now described without a public key. A
key pair stored before KeyName was required gets no comment instead of
"null". carriesAKey reads the RSA fields once through the reader.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* fix(ec2): leave a key-pair name that cannot be a comment off the key

ImportKeyPair accepts a key-pair name with a line break, and as the comment
of the described key such a name split it into lines that authorized_keys
reads as further keys. A name holding a control character is now left
off. carriesAKey names each ECDSA curve's coordinate size and reads a
security key's application field directly, and its docs say it checks
the format only.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* docs(ec2): name the key types and formats a described key pair needs

The Key Pairs section now names DER beside PEM and RFC 4716, and the two
security-key types OpenSSH has, instead of a security-key form for every
type. The SshPublicKeys class comment covers the whole-key check, and a
test comment no longer says a tab would split the described key.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

* docs(ec2): list the described key types by their wire names

The Key Pairs section lists the key types a key pair is described with by
their OpenSSH names, which reads one way only, and the SshPublicKeys
comments name the two security-key types instead of implying a
security-key form for every key type.

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>

---------

Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com>
This commit is contained in:
Jackson Price authored and GitHub committed 2026-10-20 09:54:54 -04:00
1 parent cbdeb0c8dc
commit 9f9c16a181
7 files changed
+506 -4

No files matched your search

@@ -0,0 +1,55 @@
package com.floci.test;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.KeyPairInfo;
import static org.assertj.core.api.Assertions.assertThat;
@DisplayName("EC2 DescribeKeyPairs IncludePublicKey")
class Ec2KeyPairPublicKeyTest {
/** A throwaway ed25519 public key, imported under a comment that the description replaces. */
private static final String ED25519_KEY =
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIj7azlDjVHAsSxgcpCRZ3oWnTm0nAFM64y9jd22ioI/";
@Test
@DisplayName("DescribeKeyPairs returns the public key only when IncludePublicKey is true")
void describeKeyPairsReturnsThePublicKeyOnlyWhenAsked() {
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
String name = TestFixtures.uniqueName("public-key");
String keyPairId = ec2.createKeyPair(r -> r.keyName(name)).keyPairId();
try {
KeyPairInfo without = ec2.describeKeyPairs(r -> r.keyPairIds(keyPairId)).keyPairs().get(0);
KeyPairInfo with = ec2.describeKeyPairs(r -> r.keyPairIds(keyPairId).includePublicKey(true))
.keyPairs().get(0);
assertThat(without.publicKey()).isNull();
assertThat(with.publicKey()).startsWith("ssh-rsa ").endsWith(" " + name);
} finally {
ec2.deleteKeyPair(r -> r.keyPairId(keyPairId));
}
}
}
@Test
@DisplayName("An imported key is described with the key-pair name as its comment")
void anImportedKeyIsDescribedWithTheKeyPairNameAsItsComment() {
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
String name = TestFixtures.uniqueName("public-key");
String keyPairId = ec2.importKeyPair(r -> r.keyName(name)
.publicKeyMaterial(SdkBytes.fromUtf8String(ED25519_KEY + " someone@example.com")))
.keyPairId();
try {
KeyPairInfo with = ec2.describeKeyPairs(r -> r.keyNames(name).includePublicKey(true))
.keyPairs().get(0);
assertThat(with.publicKey()).isEqualTo(ED25519_KEY + " " + name);
} finally {
ec2.deleteKeyPair(r -> r.keyPairId(keyPairId));
}
}
}
}