mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(ec2): refuse key pairs AWS refuses and describe their type, create time and filters (#6093)
* fix(ec2): refuse key pairs AWS refuses and describe their type, create time and filters ImportKeyPair stored whatever material it was given. It now reads the key as AWS documents: RSA or ED25519 as an OpenSSH line, RSA also as RFC 4716 or base64 DER, bare or in PEM armour, and RSA from 1024 to 16384 bits. DSA, ECDSA, security-key and certificate keys, blobs that are incomplete, carry trailing bytes or name another type, and material with more than one key line are refused with InvalidKey.Format. The key is stored as one OpenSSH line, which is what RunInstances writes to authorized_keys. CreateKeyPair and ImportKeyPair take a KeyName of up to 255 ASCII characters with no leading or trailing space, and answer any other with InvalidParameterValue. DescribeKeyPairs now returns keyType and createTime, and honours Filter.N with key-pair-id, key-name, fingerprint, key-type, tag-key and tag:<key>, refusing any other name. Key pairs persisted before this change load as they are: they have no createTime, and their keyType is read from the stored key. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(ec2): describe malformed key pairs from saved records, not imports ImportKeyPair now refuses material that is not exactly one whole key and converts RFC 4716, so the IncludePublicKey cases that imported partial or mislabelled blobs, trailing bytes, two key lines or UTF-16 text could no longer create their key pairs. Such material is now held only by key pairs saved by an earlier version, so those cases describe a stored record through the handler instead, and the RFC 4716 case checks that the converted key is described as its OpenSSH line. The handler test stubs the DescribeKeyPairs overload that takes filters, which the handler now calls. The Key Pairs docs and the javadoc of the public-key description say what is stored now and what an older record may still hold. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(ec2): refuse a DER key whose bit string declares pad bits A SubjectPublicKeyInfo whose key BIT STRING declares unused bits made Bouncy Castle throw an IllegalStateException, which the key reader did not catch. ImportKeyPair answered such base64 DER or PEM with a 500 instead of InvalidKey.Format, and a key pair saved earlier with that material failed every DescribeKeyPairs in its region, since the key type of an older record is read from its material. The reader now refuses a key bit string with pad bits, and treats Bouncy Castle's IllegalStateException like its other parse errors. A new test sets every byte of each binary form to values that matter to DER and SSH framing and checks that nothing escapes. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(ec2): refuse an IncludePublicKey that is neither true nor false DescribeKeyPairs read IncludePublicKey with Boolean.parseBoolean, so a value such as "invalid" or "yes" was taken as false and the key pairs were described without their public keys instead of the request being refused. It now goes through the handler's strict boolean reader, the one the block device mapping and peering options use: true or false in any case, and InvalidParameterValue for anything else. TRUE was already read as true, and still is. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(ec2): send each describe filter once and allow clock skew in the SDK check The DescribeKeyPairs integration helper always sent a key-name filter of the test's prefix, so the cases that test key-name itself sent the name twice and Floci kept only the second. A key-name filter under test is now sent alone. The SDK compatibility check gives createTime a minute either side, since Floci may run in a container with another clock. The Key Pairs docs and the RSA length test now say what AWS lists and what Floci takes beyond it, citing the 8192-bit key AWS was seen to import. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(ec2): refuse a DER key with no key in its bit string An rsaEncryption SubjectPublicKeyInfo whose key bit string holds no bytes parses to no structure at all, and the reader passed that null on to a NullPointerException: ImportKeyPair answered such DER or PEM with a 500, and a key pair saved earlier with it failed DescribeKeyPairs for every listing that included it. The reader now reads the PKCS#1 sequence itself and refuses anything that is not two integers. It reads those integers signed, as an ssh-rsa blob's are, so a modulus missing its sign byte is refused rather than imported as a different, positive key. And the key type of an older record no longer depends on the reader never failing: if it does, the type is left out and the failure logged, instead of failing the whole description. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * docs(ec2): keep key pair comments and docs true to the new checks SshPublicKeyException's javadoc said EC2 answers an unreadable imported key with no fingerprint; ImportKeyPair now refuses it with InvalidKey.Format. The Key Pairs docs say that an empty IncludePublicKey reads as false. The RSA length test links the report of an 8192-bit import in full, and the describe test says that sending one key-name filter is a Floci limit, not an AWS rule. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(ec2): cover the stored key-type guard on its own The stored-record test passed even with the reader broken, because the guard in keyTypeOf turned the reader's exception into the expected missing type. It now checks that the reader refuses each stored material by itself, and a new test makes the reader fail, through an inline static mock as other tests here do, and checks that the record is still described, with no type, and that a key-type filter passes over it. The keyTypeOf javadoc and the docs now mention that case, the docs say a blank IncludePublicKey reads as false and a missing or blank KeyName is MissingParameter, and a comment no longer says every imported key is fingerprinted with MD5. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(ec2): make the stored key-type guard test depend on its failing reader The guard test stored text the real reader already refuses without throwing, so it passed even if the failing reader was never consulted. It now stores a valid ed25519 key, whose type would be ed25519 without the failure, checks that the stubbed reader was called, and describes the record through the handler too, where each item's type is read. A comment on the stored-record test no longer says the reader refuses the records it accepts. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * fix(ec2): AND a repeated DescribeKeyPairs filter name DescribeKeyPairs read Filter.N into a map keyed by name, so a second filter of one name replaced the first: key-name=X-prod-* with key-name=X-*-web returned X-dev-web too. AWS joins every filter with AND. The handler now passes the filters in request order, as DescribeAvailabilityZones already does, and a key pair has to match each of them, with the values of one filter still ORed. The other EC2 describe actions share the map reader and keep its behaviour; that is for a separate change. The describe test's helper goes back to ANDing a second key-name filter with its prefix filter. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(ec2): show the key-type guard covers only key pairs saved without a type With the key reader made to fail, ImportKeyPair still fails with the error, and a key pair whose type was recorded is described and filtered by key-type without the reader being consulted. Only a record saved before the type was recorded goes through the guard that leaves its type out and logs the failure. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(ec2): check repeated key pair filters in the service, and route unfiltered calls through them A service test now passes Filter.N with a repeated name to the ordered DescribeKeyPairs overload and checks that the two filters are ANDed, and that a later unknown name is still refused. The unfiltered overload goes through the ordered one too, leaving the map form as a convenience for filters of distinct names, and the docs sentence on filters is split in two. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(ec2): pick repeated key pair filters that each match more alone The service check of a repeated key-name filter used `*-rsa` then `g*`, and `g*` alone gives the same answer, so it passed when only the last filter was kept. Each filter now matches two key pairs alone and one together, so keeping either one instead fails the test. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(ec2): share one fixture between the key pair tests The import, describe and public-key integration tests each had their own CreateKeyPair, ImportKeyPair and DeleteKeyPair calls, and the same ED25519 key was pasted into five classes. Ec2KeyPairFixtures now holds those calls and the one key, which the handler, service and persistence tests use too. SshKeyBlobs builds the OpenSSH blobs and lines that SshPublicKeysTest, Ec2KeyMaterialTest and Ec2QueryHandlerTest each built and joined themselves, and the import test reads RSA keys with SshPublicKeys.rsaKeyOf instead of its own copy. Test-only: every class runs the same tests as before. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(ec2): build key blobs with the SshPublicKeysTest helpers main has SshPublicKeysTest on main already builds OpenSSH blobs with blob and joins bytes with withTrailing. The EC2 key pair tests now call those two, made public for them, instead of the SshKeyBlobs class the previous commit added, which is removed. Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> * test(ec2): keep the shared throwaway keys in Ec2KeyPairFixtures The pinned RSA key and its fingerprint, the ED25519 fingerprint, the DSA and ECDSA P-256 keys and the truncated ssh-rsa blob were each written out in two to four of the key pair test classes. They now live beside the ED25519 key in Ec2KeyPairFixtures; keys only Ec2KeyMaterialTest uses stay there. The handler test loses a comment and an import the earlier move left behind. The earlier move also switched Ec2KeyPairPublicKeyIntegrationTest and Ec2QueryHandlerTest from the ED25519 key #6055 added to the shared one; no assertion in them depends on which valid key it is. 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:
1 parent
f2b62fbbec
commit
fbf3bfff84
18 files changed
+1801
-222
No files matched your search
+111
@@ -0,0 +1,111 @@
|
||||
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.Ec2Exception;
|
||||
import software.amazon.awssdk.services.ec2.model.Filter;
|
||||
import software.amazon.awssdk.services.ec2.model.KeyPairInfo;
|
||||
import software.amazon.awssdk.services.ec2.model.KeyType;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@DisplayName("EC2 ImportKeyPair validation and DescribeKeyPairs keyType, createTime and filters")
|
||||
class Ec2KeyPairImportAndDescribeTest {
|
||||
|
||||
/** A throwaway ed25519 public key. */
|
||||
private static final String ED25519_KEY =
|
||||
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAII0fBPBUZHaEOBc2mySfmI5btu4mkvFfNRujmF7RH2fj";
|
||||
|
||||
/** A throwaway ECDSA public key from ssh-keygen; EC2 imports RSA and ED25519 keys only. */
|
||||
private static final String ECDSA_KEY = "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABB"
|
||||
+ "BFaxjAHsoCE0FmjbKLGzgVRKfOKFmYj1/JpnSjsuLjy23mHIUPkLJ5thgE47igHp+KufC1V2KqJE4OP5w4EK9SU= throwaway";
|
||||
|
||||
@Test
|
||||
@DisplayName("Created and imported key pairs are described with their key type and create time")
|
||||
void keyPairsAreDescribedWithTheirTypeAndCreateTime() {
|
||||
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
|
||||
Instant before = Instant.now().truncatedTo(ChronoUnit.SECONDS);
|
||||
String created = ec2.createKeyPair(r -> r.keyName(TestFixtures.uniqueName("typed-rsa"))).keyPairId();
|
||||
String imported = ec2.importKeyPair(r -> r.keyName(TestFixtures.uniqueName("typed-ed25519"))
|
||||
.publicKeyMaterial(SdkBytes.fromUtf8String(ED25519_KEY + " someone@example.com"))).keyPairId();
|
||||
try {
|
||||
KeyPairInfo rsa = ec2.describeKeyPairs(r -> r.keyPairIds(created)).keyPairs().get(0);
|
||||
KeyPairInfo ed25519 = ec2.describeKeyPairs(r -> r.keyPairIds(imported)).keyPairs().get(0);
|
||||
|
||||
assertThat(rsa.keyType()).isEqualTo(KeyType.RSA);
|
||||
assertThat(ed25519.keyType()).isEqualTo(KeyType.ED25519);
|
||||
// A minute either side: Floci may run in a container whose clock is not this one's.
|
||||
Instant after = Instant.now();
|
||||
assertThat(rsa.createTime()).isBetween(before.minusSeconds(60), after.plusSeconds(60));
|
||||
assertThat(ed25519.createTime()).isBetween(before.minusSeconds(60), after.plusSeconds(60));
|
||||
} finally {
|
||||
ec2.deleteKeyPair(r -> r.keyPairId(created));
|
||||
ec2.deleteKeyPair(r -> r.keyPairId(imported));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("DescribeKeyPairs filters by key-name, key-type, fingerprint and tag")
|
||||
void describeKeyPairsHonoursItsFilters() {
|
||||
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
|
||||
String prefix = TestFixtures.uniqueName("filtered");
|
||||
String rsaId = ec2.createKeyPair(r -> r.keyName(prefix + "-rsa")).keyPairId();
|
||||
String ed25519Id = ec2.importKeyPair(r -> r.keyName(prefix + "-ed25519")
|
||||
.publicKeyMaterial(SdkBytes.fromUtf8String(ED25519_KEY))).keyPairId();
|
||||
try {
|
||||
ec2.createTags(r -> r.resources(ed25519Id).tags(t -> t.key("Team").value("blue")));
|
||||
Filter ours = Filter.builder().name("key-name").values(prefix + "-*").build();
|
||||
String fingerprint = ec2.describeKeyPairs(r -> r.keyPairIds(ed25519Id)).keyPairs().get(0).keyFingerprint();
|
||||
|
||||
assertThat(ids(ec2, ours)).containsExactlyInAnyOrder(rsaId, ed25519Id);
|
||||
assertThat(ids(ec2, ours, Filter.builder().name("key-type").values("rsa").build()))
|
||||
.containsExactly(rsaId);
|
||||
assertThat(ids(ec2, ours, Filter.builder().name("fingerprint").values(fingerprint).build()))
|
||||
.containsExactly(ed25519Id);
|
||||
assertThat(ids(ec2, ours, Filter.builder().name("tag:Team").values("blue").build()))
|
||||
.containsExactly(ed25519Id);
|
||||
assertThat(ids(ec2, ours, Filter.builder().name("tag-key").values("Team").build()))
|
||||
.containsExactly(ed25519Id);
|
||||
} finally {
|
||||
ec2.deleteKeyPair(r -> r.keyPairId(rsaId));
|
||||
ec2.deleteKeyPair(r -> r.keyPairId(ed25519Id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("ImportKeyPair refuses an ECDSA key with InvalidKey.Format")
|
||||
void importKeyPairRefusesAnEcdsaKey() {
|
||||
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
|
||||
assertThatThrownBy(() -> ec2.importKeyPair(r -> r.keyName(TestFixtures.uniqueName("ecdsa"))
|
||||
.publicKeyMaterial(SdkBytes.fromUtf8String(ECDSA_KEY))))
|
||||
.isInstanceOfSatisfying(Ec2Exception.class, e -> {
|
||||
assertThat(e.awsErrorDetails().errorCode()).isEqualTo("InvalidKey.Format");
|
||||
assertThat(e.awsErrorDetails().errorMessage())
|
||||
.isEqualTo("Key is not in valid OpenSSH public key format");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("CreateKeyPair refuses a name longer than 255 characters")
|
||||
void createKeyPairRefusesAnOverlongName() {
|
||||
try (Ec2Client ec2 = TestFixtures.ec2Client()) {
|
||||
assertThatThrownBy(() -> ec2.createKeyPair(r -> r.keyName("n".repeat(256))))
|
||||
.isInstanceOfSatisfying(Ec2Exception.class, e ->
|
||||
assertThat(e.awsErrorDetails().errorCode()).isEqualTo("InvalidParameterValue"));
|
||||
}
|
||||
}
|
||||
|
||||
private static List<String> ids(Ec2Client ec2, Filter... filters) {
|
||||
return ec2.describeKeyPairs(r -> r.filters(filters)).keyPairs().stream().map(KeyPairInfo::keyPairId).toList();
|
||||
}
|
||||
}
|
||||
+31
-10
@@ -314,16 +314,37 @@ private gateway lifecycles are not modelled.
|
||||
|
||||
### Key Pairs
|
||||
|
||||
`DescribeKeyPairs` omits the public key unless `IncludePublicKey=true`. When requested,
|
||||
it returns the OpenSSH public key with the key-pair name as its comment, including for
|
||||
imported keys, whatever comment they were imported with; a name holding a control character,
|
||||
such as a line break, is left off. It never returns private key material. `ImportKeyPair`
|
||||
stores the material as it was given, and a key pair whose material is not exactly one whole
|
||||
OpenSSH public key is described without one: PEM, DER or RFC 4716 text, material of more than
|
||||
one line, a line whose blob lacks a field its type requires (such as an `ssh-rsa` blob with no
|
||||
modulus), has bytes after its key or declares a different type than the line names, and a
|
||||
key of any type but `ssh-rsa`, `ssh-ed25519`, `ecdsa-sha2-nistp256`, `ecdsa-sha2-nistp384`,
|
||||
`ecdsa-sha2-nistp521`, `sk-ssh-ed25519@openssh.com` and `sk-ecdsa-sha2-nistp256@openssh.com`.
|
||||
`ImportKeyPair` takes the key types and formats AWS documents: an RSA or ED25519 key as an OpenSSH
|
||||
public key line, and an RSA key also as an RFC 4716 file or as base64 DER, bare or in PEM armour.
|
||||
AWS lists 1024, 2048 and 4096 bits as the supported RSA lengths but imports others too, such as
|
||||
8192 bits; Floci takes any length from 1024 to 16384 bits, the range OpenSSH reads, which also
|
||||
covers ssh-keygen's default of 3072. A DSA, ECDSA, security-key or certificate key, a key line
|
||||
whose blob is incomplete, has bytes after its key or declares another type, and material holding
|
||||
more than one key line are refused with `InvalidKey.Format`. The key is stored as one OpenSSH line,
|
||||
which is what `RunInstances` copies into `authorized_keys`. `CreateKeyPair` and `ImportKeyPair`
|
||||
take a `KeyName` of up to 255 ASCII characters with no leading or trailing space; a missing or
|
||||
blank one is `MissingParameter`, and they answer any other with `InvalidParameterValue`.
|
||||
|
||||
`DescribeKeyPairs` returns each key pair's `keyType` and `createTime`. It filters by `key-pair-id`,
|
||||
`key-name`, `fingerprint`, `tag-key` and `tag:<key>`, as AWS documents, and also by `key-type`,
|
||||
which AWS does not document; `*` and `?` are wildcards. Filters are ANDed, a repeated name
|
||||
included, and the values of one filter are ORed. Any other filter name is `InvalidParameterValue`.
|
||||
It omits the public key unless `IncludePublicKey=true`, read as `true` or `false` in any case and
|
||||
as `false` when empty or blank; any other value is `InvalidParameterValue`. When requested, it
|
||||
returns the OpenSSH public key with the key-pair name as its comment, whatever comment the key was
|
||||
imported with; a name holding a control character, such as a line break, is left off. It never
|
||||
returns private key material.
|
||||
|
||||
A key pair saved by an earlier version of Floci is described from what it stored. It has no
|
||||
`createTime`, and its `keyType` is read from the stored key: `rsa` for a created key pair,
|
||||
otherwise the type of the imported key, left out when that material would not import now or cannot
|
||||
be read. Its imported material is kept as it was given, and is what `RunInstances` copies into
|
||||
`authorized_keys`. Material that is not exactly one whole OpenSSH public key is described without
|
||||
one: PEM, DER or RFC 4716 text, material of more than one line, a line whose blob lacks a field its
|
||||
type requires (such as an `ssh-rsa` blob with no modulus), has bytes after its key or declares a
|
||||
different type than the line names, and a key of any type but `ssh-rsa`, `ssh-ed25519`,
|
||||
`ecdsa-sha2-nistp256`, `ecdsa-sha2-nistp384`, `ecdsa-sha2-nistp521`, `sk-ssh-ed25519@openssh.com`
|
||||
and `sk-ecdsa-sha2-nistp256@openssh.com`.
|
||||
|
||||
| Action | Description |
|
||||
|--------|-------------|
|
||||
|
||||
@@ -2,8 +2,8 @@ package io.github.hectorvent.floci.core.common;
|
||||
|
||||
/**
|
||||
* Public key material that could not be read. Thrown by {@link SshPublicKeys} so that each caller
|
||||
* maps it onto whatever its own API calls the failure, rather than the parser deciding: EC2
|
||||
* answers an unreadable imported key with no fingerprint at all.
|
||||
* maps it onto whatever its own API calls the failure, rather than the parser deciding: EC2's
|
||||
* ImportKeyPair answers an unreadable key with InvalidKey.Format, IAM with InvalidPublicKey.
|
||||
*/
|
||||
public class SshPublicKeyException extends RuntimeException {
|
||||
|
||||
|
||||
@@ -1,17 +1,27 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.Digests;
|
||||
import io.github.hectorvent.floci.core.common.PublicKeys;
|
||||
import io.github.hectorvent.floci.core.common.SshPublicKeyException;
|
||||
import io.github.hectorvent.floci.core.common.SshPublicKeys;
|
||||
import org.bouncycastle.asn1.ASN1Integer;
|
||||
import org.bouncycastle.asn1.ASN1Primitive;
|
||||
import org.bouncycastle.asn1.ASN1Sequence;
|
||||
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
|
||||
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
|
||||
import org.bouncycastle.openssl.jcajce.JcaPEMWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.util.Base64;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Real key material for EC2 key pairs.
|
||||
@@ -32,6 +42,15 @@ public final class Ec2KeyMaterial {
|
||||
/** An ed25519 public key is a fixed 32-byte value, so any other length is not one. */
|
||||
private static final int ED25519_KEY_LENGTH = 32;
|
||||
|
||||
/** The shortest RSA key ImportKeyPair takes, the shortest of the lengths AWS lists as supported. */
|
||||
private static final int MIN_IMPORTED_RSA_BITS = 1024;
|
||||
|
||||
/** The longest RSA key ImportKeyPair takes, which is also the longest OpenSSH reads. */
|
||||
private static final int MAX_IMPORTED_RSA_BITS = 16384;
|
||||
|
||||
private static final String RFC_4716_BEGIN = "---- BEGIN SSH2 PUBLIC KEY ----";
|
||||
private static final String RFC_4716_END = "---- END SSH2 PUBLIC KEY ----";
|
||||
|
||||
private Ec2KeyMaterial() {}
|
||||
|
||||
/**
|
||||
@@ -41,6 +60,16 @@ public final class Ec2KeyMaterial {
|
||||
*/
|
||||
public record Generated(String privateKeyPem, String openSshPublicKey, String fingerprint) {}
|
||||
|
||||
/**
|
||||
* A public key ImportKeyPair accepted.
|
||||
*
|
||||
* @param keyType {@code rsa} or {@code ed25519}, as DescribeKeyPairs reports it
|
||||
* @param fingerprint the fingerprint AWS reports for an imported key of that type
|
||||
* @param openSshPublicKey the key as one {@code "ssh-rsa AAAA..."} line without a comment,
|
||||
* whatever format it was imported in, for authorized_keys
|
||||
*/
|
||||
public record Imported(String keyType, String fingerprint, String openSshPublicKey) {}
|
||||
|
||||
public static Generated generateRsa() {
|
||||
try {
|
||||
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
||||
@@ -56,7 +85,7 @@ public final class Ec2KeyMaterial {
|
||||
}
|
||||
|
||||
// "The SHA-1 digest of the DER encoded private key" for a key pair AWS created.
|
||||
// (Imported keys use the MD5 of the public key instead -- see fingerprintOf.)
|
||||
// (Imported keys are fingerprinted another way, by key type -- see readImportedKey.)
|
||||
String fingerprint = colonHex(Digests.sha1(pair.getPrivate().getEncoded()));
|
||||
|
||||
return new Generated(
|
||||
@@ -69,51 +98,63 @@ public final class Ec2KeyMaterial {
|
||||
}
|
||||
|
||||
/**
|
||||
* Fingerprint for an imported public key, in the scheme AWS uses for that key type.
|
||||
* Reads the public key given to ImportKeyPair, accepting the key types and formats AWS
|
||||
* documents: an RSA or ED25519 key as an OpenSSH line, and an RSA key also as an RFC 4716
|
||||
* file or as base64 DER, bare or in PEM armour. An RSA key must be 1024 to 16384 bits. EC2
|
||||
* does not accept DSA keys, and ECDSA and security-key types are not among those it supports.
|
||||
*
|
||||
* <p>ImportKeyPair does not fingerprint every key the same way, and it does not agree
|
||||
* with CreateKeyPair either:
|
||||
* <p>An OpenSSH line holds one key, whose blob has to be exactly the key of the type the line
|
||||
* names, as OpenSSH requires: no field missing, nothing after the key, no other type. Material
|
||||
* with a second line is refused rather than read as its first key, since an instance launched
|
||||
* with the key pair would trust every line of it.
|
||||
*
|
||||
* <p>The fingerprint is the one AWS reports for an imported key, which is not the scheme
|
||||
* CreateKeyPair uses:
|
||||
*
|
||||
* <ul>
|
||||
* <li><b>ssh-rsa</b>: "the MD5 public key fingerprint", taken over the DER
|
||||
* SubjectPublicKeyInfo rather than over the SSH wire blob. AWS documents the check
|
||||
* as {@code openssl rsa -in key -pubout -outform DER | openssl md5 -c}, and
|
||||
* {@link java.security.Key#getEncoded()} produces exactly that DER, so the blob is
|
||||
* decoded back to a public key first. Colon-separated hex, as AWS reports it.</li>
|
||||
* <li><b>ssh-ed25519</b>: the base64-encoded SHA-256 digest of the wire blob, "which is
|
||||
* the default for OpenSSH". This is the {@code ssh-keygen -l} value without its
|
||||
* {@code SHA256:} prefix, and padded: AWS keeps the trailing {@code =} that
|
||||
* ssh-keygen drops.</li>
|
||||
* <li><b>rsa</b>: "the MD5 public key fingerprint", taken over the DER SubjectPublicKeyInfo
|
||||
* rather than over the SSH wire blob. AWS documents the check as
|
||||
* {@code openssl rsa -in key -pubout -outform DER | openssl md5 -c}, and
|
||||
* {@link java.security.Key#getEncoded()} produces exactly that DER. Colon-separated
|
||||
* hex, as AWS reports it.</li>
|
||||
* <li><b>ed25519</b>: the base64-encoded SHA-256 digest of the wire blob, "which is the
|
||||
* default for OpenSSH". This is the {@code ssh-keygen -l} value without its
|
||||
* {@code SHA256:} prefix, and padded: AWS keeps the trailing {@code =} that ssh-keygen
|
||||
* drops.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>Any other key type keeps the MD5 of the wire blob: stable and distinct per key,
|
||||
* which is what callers depend on, but not a value AWS would report. AWS accepts only
|
||||
* RSA and ed25519 material for import, so nothing else has a documented answer to match.
|
||||
*
|
||||
* <p>Returns null for material that does not parse, so the caller can decide rather than
|
||||
* getting a fingerprint of garbage.
|
||||
*
|
||||
* @return the key, or null when AWS would refuse the material
|
||||
* @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/create-key-pairs.html#how-to-generate-your-own-key-and-import-it-to-aws">Requirements for key pairs</a>
|
||||
* @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/verify-keys.html">Verify the fingerprint of your key pair</a>
|
||||
*/
|
||||
public static String fingerprintOf(String openSshPublicKey) {
|
||||
byte[] blob = SshPublicKeys.decodeBlob(openSshPublicKey);
|
||||
if (blob == null) {
|
||||
public static Imported readImportedKey(String material) {
|
||||
if (material == null) {
|
||||
return null;
|
||||
}
|
||||
List<String> lines = material.trim().lines().map(String::strip).toList();
|
||||
if (lines.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
String first = lines.getFirst();
|
||||
try {
|
||||
SshPublicKeys.Reader reader = SshPublicKeys.reader(blob);
|
||||
String type = new String(reader.readField(), StandardCharsets.UTF_8);
|
||||
if ("ssh-ed25519".equals(type)) {
|
||||
if (reader.readField().length != ED25519_KEY_LENGTH) {
|
||||
return null;
|
||||
}
|
||||
return Base64.getEncoder().encodeToString(Digests.sha256(blob));
|
||||
if (first.equals(RFC_4716_BEGIN)) {
|
||||
byte[] blob = rfc4716Blob(lines);
|
||||
return importedRsa(rsaKeyOfWholeBlob(blob), "ssh-rsa " + base64(blob));
|
||||
}
|
||||
// EC2 documents its ssh-rsa fingerprint as the MD5 of the DER, which is not the
|
||||
// OpenSSH fingerprint of the same key. SshPublicKeys keeps the two apart on purpose.
|
||||
byte[] der = "ssh-rsa".equals(type) ? SshPublicKeys.rsaKeyOf(blob).getEncoded() : blob;
|
||||
return SshPublicKeys.md5ColonHex(der);
|
||||
} catch (Exception e) { // malformed material is the caller's problem, not a crash
|
||||
if (first.startsWith("-----BEGIN ")) {
|
||||
RSAPublicKey key = rsaKeyOfDer(pemBody(lines));
|
||||
return importedRsa(key, SshPublicKeys.toOpenSsh(key));
|
||||
}
|
||||
String[] fields = first.split("\\s+", 3);
|
||||
if (fields.length > 1) {
|
||||
return lines.size() == 1 ? openSshLine(fields[0], Base64.getDecoder().decode(fields[1])) : null;
|
||||
}
|
||||
RSAPublicKey key = rsaKeyOfDer(Base64.getDecoder().decode(String.join("", lines)));
|
||||
return importedRsa(key, SshPublicKeys.toOpenSsh(key));
|
||||
} catch (SshPublicKeyException | IllegalArgumentException | IllegalStateException
|
||||
| GeneralSecurityException | IOException expected) {
|
||||
// Material AWS refuses, which the caller answers with InvalidKey.Format. Bouncy Castle
|
||||
// reports some malformed DER with an IllegalStateException rather than an IOException.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -122,6 +163,133 @@ public final class Ec2KeyMaterial {
|
||||
return SshPublicKeys.toOpenSsh(key);
|
||||
}
|
||||
|
||||
/** The key of an OpenSSH line whose type field is {@code type} and whose blob is {@code blob}. */
|
||||
private static Imported openSshLine(String type, byte[] blob) throws GeneralSecurityException {
|
||||
return switch (type) {
|
||||
case "ssh-rsa" -> importedRsa(rsaKeyOfWholeBlob(blob), "ssh-rsa " + base64(blob));
|
||||
case "ssh-ed25519" -> importedEd25519(blob);
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The RSA key of an {@code ssh-rsa} blob that holds its type, exponent and modulus and nothing
|
||||
* else. {@link SshPublicKeys#rsaKeyOf} stops once it has the key; an import must not.
|
||||
*/
|
||||
private static RSAPublicKey rsaKeyOfWholeBlob(byte[] blob) throws GeneralSecurityException {
|
||||
SshPublicKeys.Reader reader = SshPublicKeys.reader(blob);
|
||||
byte[] type = reader.readField();
|
||||
byte[] exponent = reader.readField();
|
||||
byte[] modulus = reader.readField();
|
||||
BigInteger e = new BigInteger(exponent);
|
||||
BigInteger n = new BigInteger(modulus);
|
||||
if (!"ssh-rsa".equals(new String(type, StandardCharsets.UTF_8)) || !fillsTheBlob(blob, type, exponent, modulus)
|
||||
|| e.signum() <= 0 || n.signum() <= 0) {
|
||||
throw new SshPublicKeyException("not exactly one ssh-rsa public key");
|
||||
}
|
||||
return PublicKeys.rsa(n, e);
|
||||
}
|
||||
|
||||
private static Imported importedEd25519(byte[] blob) {
|
||||
SshPublicKeys.Reader reader = SshPublicKeys.reader(blob);
|
||||
byte[] type = reader.readField();
|
||||
byte[] key = reader.readField();
|
||||
if (!"ssh-ed25519".equals(new String(type, StandardCharsets.UTF_8)) || key.length != ED25519_KEY_LENGTH
|
||||
|| !fillsTheBlob(blob, type, key)) {
|
||||
return null;
|
||||
}
|
||||
return new Imported("ed25519", Base64.getEncoder().encodeToString(Digests.sha256(blob)),
|
||||
"ssh-ed25519 " + base64(blob));
|
||||
}
|
||||
|
||||
private static Imported importedRsa(RSAPublicKey key, String openSshPublicKey) {
|
||||
int bits = key.getModulus().bitLength();
|
||||
if (bits < MIN_IMPORTED_RSA_BITS || bits > MAX_IMPORTED_RSA_BITS) {
|
||||
return null;
|
||||
}
|
||||
return new Imported("rsa", SshPublicKeys.md5ColonHex(key.getEncoded()), openSshPublicKey);
|
||||
}
|
||||
|
||||
/** Whether the length-prefixed fields read so far are the whole blob, with nothing after them. */
|
||||
private static boolean fillsTheBlob(byte[] blob, byte[]... fields) {
|
||||
int length = 0;
|
||||
for (byte[] field : fields) {
|
||||
length += Integer.BYTES + field.length;
|
||||
}
|
||||
return length == blob.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* The key blob of an RFC 4716 file: the base64 body between the begin and end markers, after
|
||||
* its header lines, each a {@code Tag: value} that a trailing backslash continues onto the next.
|
||||
*/
|
||||
private static byte[] rfc4716Blob(List<String> lines) {
|
||||
if (lines.size() < 3 || !RFC_4716_END.equals(lines.getLast())) {
|
||||
throw new SshPublicKeyException("not an RFC 4716 public key");
|
||||
}
|
||||
StringBuilder body = new StringBuilder();
|
||||
boolean continued = false;
|
||||
for (String line : lines.subList(1, lines.size() - 1)) {
|
||||
if (body.isEmpty() && (continued || line.contains(":"))) {
|
||||
continued = line.endsWith("\\");
|
||||
continue;
|
||||
}
|
||||
body.append(line);
|
||||
}
|
||||
return Base64.getDecoder().decode(body.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* The DER in PEM armour labelled as a public key: {@code PUBLIC KEY}, a SubjectPublicKeyInfo,
|
||||
* or {@code RSA PUBLIC KEY}, the bare PKCS#1 key that {@code ssh-keygen -e -m PEM} writes. Any
|
||||
* other label, such as a private key pasted by mistake, is not a public key.
|
||||
*/
|
||||
private static byte[] pemBody(List<String> lines) {
|
||||
String label = lines.getFirst().substring("-----BEGIN ".length());
|
||||
if (!("PUBLIC KEY-----".equals(label) || "RSA PUBLIC KEY-----".equals(label))
|
||||
|| lines.size() < 3 || !lines.getLast().equals("-----END " + label)) {
|
||||
throw new SshPublicKeyException("not a PEM public key");
|
||||
}
|
||||
return Base64.getDecoder().decode(String.join("", lines.subList(1, lines.size() - 1)));
|
||||
}
|
||||
|
||||
/**
|
||||
* The RSA key a DER SubjectPublicKeyInfo or PKCS#1 RSAPublicKey encodes. Another algorithm,
|
||||
* such as an EC key, is refused, as are bytes after the structure.
|
||||
*/
|
||||
private static RSAPublicKey rsaKeyOfDer(byte[] der) throws IOException, GeneralSecurityException {
|
||||
if (!(ASN1Primitive.fromByteArray(der) instanceof ASN1Sequence structure) || structure.size() == 0) {
|
||||
throw new SshPublicKeyException("not a DER public key");
|
||||
}
|
||||
// A SubjectPublicKeyInfo opens with its algorithm, itself a sequence; PKCS#1 with the modulus.
|
||||
ASN1Primitive rsaKey = structure;
|
||||
if (structure.getObjectAt(0) instanceof ASN1Sequence) {
|
||||
SubjectPublicKeyInfo info = SubjectPublicKeyInfo.getInstance(structure);
|
||||
// The key is a whole number of bytes, so a bit string declaring unused bits holds none.
|
||||
if (!PKCSObjectIdentifiers.rsaEncryption.equals(info.getAlgorithm().getAlgorithm())
|
||||
|| info.getPublicKeyData().getPadBits() != 0) {
|
||||
throw new SshPublicKeyException("not an RSA public key");
|
||||
}
|
||||
rsaKey = info.parsePublicKey();
|
||||
}
|
||||
// PKCS#1 RSAPublicKey: SEQUENCE { modulus INTEGER, publicExponent INTEGER }. An empty bit string
|
||||
// parses to nothing at all. The integers are read signed, as an ssh-rsa blob's are, so a
|
||||
// modulus missing its sign byte is refused rather than read as another, positive key.
|
||||
if (!(rsaKey instanceof ASN1Sequence fields) || fields.size() != 2) {
|
||||
throw new SshPublicKeyException("not an RSA public key");
|
||||
}
|
||||
BigInteger modulus = ASN1Integer.getInstance(fields.getObjectAt(0)).getValue();
|
||||
BigInteger exponent = ASN1Integer.getInstance(fields.getObjectAt(1)).getValue();
|
||||
if (modulus.signum() <= 0 || exponent.signum() <= 0) {
|
||||
throw new SshPublicKeyException("not an RSA public key");
|
||||
}
|
||||
return PublicKeys.rsa(modulus, exponent);
|
||||
}
|
||||
|
||||
private static String base64(byte[] bytes) {
|
||||
return Base64.getEncoder().encodeToString(bytes);
|
||||
}
|
||||
|
||||
private static String colonHex(byte[] digest) {
|
||||
return HexFormat.of().withDelimiter(":").formatHex(digest);
|
||||
}
|
||||
|
||||
@@ -3433,10 +3433,10 @@ public class Ec2QueryHandler {
|
||||
}
|
||||
|
||||
private Response handleDescribeKeyPairs(MultivaluedMap<String, String> p, String region) {
|
||||
boolean includePublicKey = Boolean.parseBoolean(p.getFirst("IncludePublicKey"));
|
||||
boolean includePublicKey = Boolean.TRUE.equals(parseOptionalBoolean(p.getFirst("IncludePublicKey"), "IncludePublicKey"));
|
||||
List<String> keyNames = getList(p, "KeyName");
|
||||
List<String> keyPairIds = getList(p, "KeyPairId");
|
||||
List<KeyPair> kps = service.describeKeyPairs(region, keyNames, keyPairIds);
|
||||
List<KeyPair> kps = service.describeKeyPairs(region, keyNames, keyPairIds, getFiltersList(p));
|
||||
XmlBuilder xml = new XmlBuilder()
|
||||
.start("DescribeKeyPairsResponse", AwsNamespaces.EC2)
|
||||
.elem("requestId", UUID.randomUUID().toString())
|
||||
@@ -3444,6 +3444,8 @@ public class Ec2QueryHandler {
|
||||
for (KeyPair kp : kps) {
|
||||
xml.start("item")
|
||||
.elem("keyPairId", kp.getKeyPairId())
|
||||
.elem("keyType", Ec2Service.keyTypeOf(kp))
|
||||
.elem("createTime", kp.getCreateTime() != null ? ISO_FMT.format(kp.getCreateTime()) : null)
|
||||
.elem("keyName", kp.getKeyName())
|
||||
.elem("keyFingerprint", kp.getKeyFingerprint());
|
||||
if (includePublicKey) {
|
||||
@@ -3457,13 +3459,14 @@ public class Ec2QueryHandler {
|
||||
|
||||
/**
|
||||
* The stored public key as an OpenSSH line whose comment is the key-pair name, the form
|
||||
* DescribeKeyPairs reports for created and imported keys alike. ImportKeyPair keeps what it was
|
||||
* given, so this is null unless the material is a single OpenSSH line whose blob is a whole key
|
||||
* of the type the line names: a partial blob, a line labelled with another type and material of
|
||||
* more than one line are not one key to report. PEM, DER and RFC 4716 bodies are not converted,
|
||||
* since ImportKeyPair does not read those formats yet. The line is rebuilt from the blob's type,
|
||||
* the blob and the name rather than copied, so no character of the stored text, such as a NUL
|
||||
* that XML cannot carry, reaches the response.
|
||||
* DescribeKeyPairs reports for created and imported keys alike. ImportKeyPair stores one OpenSSH
|
||||
* line, but a key pair saved by an earlier version kept the material it was given, so this is
|
||||
* null unless the material is a single OpenSSH line whose blob is a whole key of the type the
|
||||
* line names: a partial blob, a line labelled with another type and material of more than one
|
||||
* line are not one key to report, and PEM, DER and RFC 4716 bodies saved that way are not
|
||||
* converted here. The line is rebuilt from the blob's type, the blob and the name rather than
|
||||
* copied, so no character of the stored text, such as a NUL that XML cannot carry, reaches the
|
||||
* response.
|
||||
*/
|
||||
private static String openSshPublicKeyNamedFor(KeyPair kp) {
|
||||
String material = kp.getPublicKey();
|
||||
|
||||
@@ -236,6 +236,14 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
private static final Set<String> EGRESS_ONLY_INTERNET_GATEWAY_FILTERS = Set.of(
|
||||
"attachment.state", "attachment.vpc-id",
|
||||
"egress-only-internet-gateway-id", "tag-key", "tag-value");
|
||||
/**
|
||||
* The filter names DescribeKeyPairs documents, with tag:<key> admitted through tag-key,
|
||||
* plus key-type, which the documentation omits but which every key pair carries.
|
||||
*/
|
||||
private static final Set<String> KEY_PAIR_FILTERS = Set.of(
|
||||
"fingerprint", "key-name", "key-pair-id", "key-type", "tag-key");
|
||||
/** The longest key-pair name CreateKeyPair and ImportKeyPair take. */
|
||||
private static final int MAX_KEY_NAME_LENGTH = 255;
|
||||
private static final Set<String> AVAILABILITY_ZONE_FILTERS = Set.of(
|
||||
"group-long-name", "group-name", "message", "opt-in-status",
|
||||
"parent-zone-id", "parent-zone-name", "region-name", "state",
|
||||
@@ -7416,13 +7424,33 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
// The public half is what RunInstances injects into the guest's authorized_keys.
|
||||
// Without storing it, a key pair created here could never authenticate anything.
|
||||
kp.setPublicKey(generated.openSshPublicKey());
|
||||
kp.setKeyType("rsa");
|
||||
kp.setCreateTime(Instant.now());
|
||||
kp.setRegion(region);
|
||||
keyPairs.put(key(region, keyPairId), kp);
|
||||
return kp;
|
||||
}
|
||||
|
||||
public List<KeyPair> describeKeyPairs(String region, List<String> keyNames, List<String> keyPairIds) {
|
||||
return describeKeyPairs(region, keyNames, keyPairIds, List.of());
|
||||
}
|
||||
|
||||
/** A convenience for filters of distinct names; a request's Filter.N goes through the ordered overload. */
|
||||
public List<KeyPair> describeKeyPairs(String region, List<String> keyNames, List<String> keyPairIds,
|
||||
Map<String, List<String>> filters) {
|
||||
return describeKeyPairs(region, keyNames, keyPairIds, List.copyOf(filters.entrySet()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter.N in request order, each a name and its values: AWS ANDs the filters, a repeated name
|
||||
* included, and ORs the values within one.
|
||||
*/
|
||||
public List<KeyPair> describeKeyPairs(String region, List<String> keyNames, List<String> keyPairIds,
|
||||
List<Map.Entry<String, List<String>>> filters) {
|
||||
ensureDefaultResources(region);
|
||||
// matchesFilter answers true for a name it does not know, so an unknown name would match
|
||||
// every key pair rather than fail as it does in AWS.
|
||||
requireSupportedFilters(filters.stream().map(Map.Entry::getKey).toList(), KEY_PAIR_FILTERS);
|
||||
List<KeyPair> regionKeyPairs = keyPairs.scan(k -> true).stream()
|
||||
.filter(k -> k.getRegion().equals(region))
|
||||
.collect(Collectors.toList());
|
||||
@@ -7446,9 +7474,34 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return regionKeyPairs.stream()
|
||||
.filter(k -> keyNames.isEmpty() || keyNames.contains(k.getKeyName()))
|
||||
.filter(k -> keyPairIds.isEmpty() || keyPairIds.contains(k.getKeyPairId()))
|
||||
.filter(k -> filters.stream().allMatch(f -> matchesFilter(k, f.getKey(), f.getValue(), region)))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* The key type DescribeKeyPairs reports. A key pair stored before the type was recorded has it
|
||||
* read from what it holds: rsa when CreateKeyPair made it, since that has only ever generated
|
||||
* RSA keys, and otherwise the type of its imported key, or none when ImportKeyPair would now
|
||||
* refuse that material or reading it fails, which is logged.
|
||||
*/
|
||||
static String keyTypeOf(KeyPair keyPair) {
|
||||
if (keyPair.getKeyType() != null) {
|
||||
return keyPair.getKeyType();
|
||||
}
|
||||
if (keyPair.getKeyMaterial() != null) {
|
||||
return "rsa";
|
||||
}
|
||||
try {
|
||||
Ec2KeyMaterial.Imported imported = Ec2KeyMaterial.readImportedKey(keyPair.getPublicKey());
|
||||
return imported != null ? imported.keyType() : null;
|
||||
} catch (RuntimeException e) {
|
||||
// The record may hold any text an earlier ImportKeyPair stored. Text the reader cannot
|
||||
// handle leaves the type out instead of failing every description in the region.
|
||||
LOG.warnv(e, "Could not read the key type of stored key pair {0}", keyPair.getKeyPairId());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes by id when one is given, otherwise by name, and returns the deleted key pair so
|
||||
* the response can carry its id. Null when nothing matched: AWS answers a delete of an
|
||||
@@ -7472,19 +7525,23 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
public KeyPair importKeyPair(String region, String keyName, String publicKeyMaterial) {
|
||||
ensureDefaultResources(region);
|
||||
requireKeyName(keyName);
|
||||
Ec2KeyMaterial.Imported imported = Ec2KeyMaterial.readImportedKey(publicKeyMaterial);
|
||||
if (imported == null) {
|
||||
throw new AwsException("InvalidKey.Format", "Key is not in valid OpenSSH public key format", 400);
|
||||
}
|
||||
rejectDuplicateKeyName(region, keyName);
|
||||
String keyPairId = "key-" + randomHex(17);
|
||||
KeyPair kp = new KeyPair();
|
||||
kp.setKeyPairId(keyPairId);
|
||||
kp.setKeyName(keyName);
|
||||
// A per-key fingerprint, not a constant: callers compare it to detect that the key
|
||||
// under a given name has been replaced. Falls back to the old placeholder only when
|
||||
// the material does not parse, rather than reporting a digest of garbage.
|
||||
String fingerprint = Ec2KeyMaterial.fingerprintOf(publicKeyMaterial);
|
||||
kp.setKeyFingerprint(fingerprint != null
|
||||
? fingerprint
|
||||
: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00");
|
||||
kp.setPublicKey(publicKeyMaterial);
|
||||
// under a given name has been replaced.
|
||||
kp.setKeyFingerprint(imported.fingerprint());
|
||||
kp.setKeyType(imported.keyType());
|
||||
// The key as one OpenSSH line, whatever format it came in: RunInstances writes it to the
|
||||
// guest's authorized_keys, which reads neither RFC 4716 nor DER.
|
||||
kp.setPublicKey(imported.openSshPublicKey());
|
||||
kp.setCreateTime(Instant.now());
|
||||
kp.setRegion(region);
|
||||
keyPairs.put(key(region, keyPairId), kp);
|
||||
return kp;
|
||||
@@ -7542,10 +7599,26 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
return containerManager.isContainerRunning(instance.getDockerContainerId());
|
||||
}
|
||||
|
||||
/**
|
||||
* A key-pair name as AWS documents it for CreateKeyPair and ImportKeyPair alike: up to 255
|
||||
* ASCII characters, with no leading or trailing space.
|
||||
*/
|
||||
private static void requireKeyName(String keyName) {
|
||||
if (keyName == null || keyName.isBlank()) {
|
||||
throw new AwsException("MissingParameter", "The request must contain the parameter KeyName", 400);
|
||||
}
|
||||
String reason = null;
|
||||
if (keyName.length() > MAX_KEY_NAME_LENGTH) {
|
||||
reason = "Length exceeds maximum of " + MAX_KEY_NAME_LENGTH + " characters.";
|
||||
} else if (keyName.chars().anyMatch(c -> c > 0x7F)) {
|
||||
reason = "Character sets beyond ASCII are not supported.";
|
||||
} else if (keyName.startsWith(" ") || keyName.endsWith(" ")) {
|
||||
reason = "Leading and trailing spaces are not allowed.";
|
||||
}
|
||||
if (reason != null) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"Value (" + keyName + ") for parameter KeyName is invalid. " + reason, 400);
|
||||
}
|
||||
}
|
||||
|
||||
// Compared name-first so a nameless record can never throw here. One such record, stored
|
||||
@@ -11872,6 +11945,15 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
|
||||
default -> true;
|
||||
};
|
||||
}
|
||||
if (resource instanceof KeyPair keyPair) {
|
||||
return switch (filterName) {
|
||||
case "key-pair-id" -> matchesValue(values, keyPair.getKeyPairId());
|
||||
case "key-name" -> matchesValue(values, keyPair.getKeyName());
|
||||
case "fingerprint" -> matchesValue(values, keyPair.getKeyFingerprint());
|
||||
case "key-type" -> matchesValue(values, keyTypeOf(keyPair));
|
||||
default -> true;
|
||||
};
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.ec2.model;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -15,6 +16,8 @@ public class KeyPair {
|
||||
private String keyFingerprint;
|
||||
private String keyMaterial;
|
||||
private String publicKey;
|
||||
private String keyType;
|
||||
private Instant createTime;
|
||||
private String region;
|
||||
private List<Tag> tags = new ArrayList<>();
|
||||
|
||||
@@ -35,6 +38,14 @@ public class KeyPair {
|
||||
public String getPublicKey() { return publicKey; }
|
||||
public void setPublicKey(String publicKey) { this.publicKey = publicKey; }
|
||||
|
||||
/** {@code rsa} or {@code ed25519}; null for a key pair stored before the type was recorded. */
|
||||
public String getKeyType() { return keyType; }
|
||||
public void setKeyType(String keyType) { this.keyType = keyType; }
|
||||
|
||||
/** When the key pair was created or imported; null for one stored before this was recorded. */
|
||||
public Instant getCreateTime() { return createTime; }
|
||||
public void setCreateTime(Instant createTime) { this.createTime = createTime; }
|
||||
|
||||
public String getRegion() { return region; }
|
||||
public void setRegion(String region) { this.region = region; }
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class SshPublicKeysTest {
|
||||
public class SshPublicKeysTest {
|
||||
|
||||
private static final RSAPublicKey KEY = rsaKeyQuietly();
|
||||
|
||||
@@ -245,7 +245,8 @@ class SshPublicKeysTest {
|
||||
blob("sk-ssh-ed25519@openssh.com", new byte[32], ascii("ssh:"), ascii("extra"))));
|
||||
}
|
||||
|
||||
private static byte[] withTrailing(byte[] blob, byte[] trailing) {
|
||||
/** The bytes of {@code blob} and then {@code trailing}. The EC2 key pair tests use it too. */
|
||||
public static byte[] withTrailing(byte[] blob, byte[] trailing) {
|
||||
return ByteBuffer.allocate(blob.length + trailing.length).put(blob).put(trailing).array();
|
||||
}
|
||||
|
||||
@@ -260,8 +261,11 @@ class SshPublicKeysTest {
|
||||
return value.getBytes(StandardCharsets.US_ASCII);
|
||||
}
|
||||
|
||||
/** An OpenSSH blob of the given type followed by the given fields, each length-prefixed. */
|
||||
private static byte[] blob(String type, byte[]... fields) {
|
||||
/**
|
||||
* An OpenSSH blob of the given type followed by the given fields, each length-prefixed. The EC2
|
||||
* key pair tests use it too.
|
||||
*/
|
||||
public static byte[] blob(String type, byte[]... fields) {
|
||||
byte[] typeBytes = type.getBytes(StandardCharsets.US_ASCII);
|
||||
int length = Integer.BYTES + typeBytes.length;
|
||||
for (byte[] field : fields) {
|
||||
|
||||
@@ -2540,7 +2540,7 @@ class Ec2IntegrationTest {
|
||||
given()
|
||||
.formParam("Action", "ImportKeyPair")
|
||||
.formParam("KeyName", importedKeyName)
|
||||
.formParam("PublicKeyMaterial", "c3NoLXJzYSBBQUFBQjNOemFDMXljMkVBQUFBREFRQUJBQUFCQVFD")
|
||||
.formParam("PublicKeyMaterial", encodedPublicKey())
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
@@ -2556,7 +2556,7 @@ class Ec2IntegrationTest {
|
||||
given()
|
||||
.formParam("Action", "ImportKeyPair")
|
||||
.formParam("KeyName", importedKeyName)
|
||||
.formParam("PublicKeyMaterial", "c3NoLXJzYSBBQUFBQjNOemFDMXljMkVBQUFBREFRQUJBQUFCQVFD")
|
||||
.formParam("PublicKeyMaterial", encodedPublicKey())
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
@@ -2599,6 +2599,12 @@ class Ec2IntegrationTest {
|
||||
.body("Response.Errors.Error.Code", equalTo("MissingParameter"));
|
||||
}
|
||||
|
||||
/** A whole RSA public key, base64-encoded as ImportKeyPair takes it: EC2 refuses a partial one. */
|
||||
private static String encodedPublicKey() {
|
||||
return Base64.getEncoder().encodeToString(
|
||||
Ec2KeyMaterial.generateRsa().openSshPublicKey().getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(41)
|
||||
void importKeyPairWithInvalidBase64ReturnsInvalidKeyFormat() {
|
||||
|
||||
@@ -4,18 +4,35 @@ import org.bouncycastle.openssl.PEMKeyPair;
|
||||
import org.bouncycastle.openssl.PEMParser;
|
||||
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.interfaces.RSAPrivateCrtKey;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import static io.github.hectorvent.floci.core.common.SshPublicKeysTest.blob;
|
||||
import static io.github.hectorvent.floci.core.common.SshPublicKeysTest.withTrailing;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.DSA_KEY;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ECDSA_P256_KEY;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ED25519_BLOB;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ED25519_FINGERPRINT;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ED25519_KEY;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.RSA_FINGERPRINT;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.RSA_KEY;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
@@ -28,16 +45,68 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
*/
|
||||
class Ec2KeyMaterialTest {
|
||||
|
||||
/** Throwaway keys, kept fixed so their fingerprints can be pinned to known good values. */
|
||||
private static final String RSA_PUBLIC_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCp7mGC9"
|
||||
+ "NkQI+loxf1G9bM6HnCs9iR1nnzZA/f/o7hx/Wv1oDhx03k6H83I+Q49eE1XO56WBPxnr8/2G6UmS9D0R"
|
||||
+ "FKe9L+HJrfiZF7oLQ09JwEK91VLNSkD0Bq2zhnfWJe/ULkaPQ7FgHEghRi8aI5PsATH6VCaJDKWxl+2b"
|
||||
+ "zM7MWlbKRAo8uuu2evnGrgnu+RmuXJQCRYz6lG+JESVzm6MnHXYxme+UD+7c/tTYwzoswfXh8VN8QVzX"
|
||||
+ "mjfHi2Ve3PJ+YuF2X2gKpRMNMf7cLWMCTOhZI2AgXX+NLDlCG0dEUm/DXdSKRTDhm3mIJmF67eGYuff+"
|
||||
+ "zHusBZ9cSBkW9i9";
|
||||
/** RSA_KEY as RFC 4716, with a header continued onto a second line by a backslash. */
|
||||
private static final String RSA_RFC_4716 = """
|
||||
---- BEGIN SSH2 PUBLIC KEY ----
|
||||
Subject: throwaway
|
||||
Comment: "a comment that a trailing backslash \\
|
||||
continues onto the next line"
|
||||
AAAAB3NzaC1yc2EAAAADAQABAAABAQCp7mGC9NkQI+loxf1G9bM6HnCs9iR1nnzZA/f/o7
|
||||
hx/Wv1oDhx03k6H83I+Q49eE1XO56WBPxnr8/2G6UmS9D0RFKe9L+HJrfiZF7oLQ09JwEK
|
||||
91VLNSkD0Bq2zhnfWJe/ULkaPQ7FgHEghRi8aI5PsATH6VCaJDKWxl+2bzM7MWlbKRAo8u
|
||||
uu2evnGrgnu+RmuXJQCRYz6lG+JESVzm6MnHXYxme+UD+7c/tTYwzoswfXh8VN8QVzXmjf
|
||||
Hi2Ve3PJ+YuF2X2gKpRMNMf7cLWMCTOhZI2AgXX+NLDlCG0dEUm/DXdSKRTDhm3mIJmF67
|
||||
eGYuff+zHusBZ9cSBkW9i9
|
||||
---- END SSH2 PUBLIC KEY ----
|
||||
""";
|
||||
|
||||
private static final String ED25519_PUBLIC_KEY =
|
||||
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAII0fBPBUZHaEOBc2mySfmI5btu4mkvFfNRujmF7RH2fj";
|
||||
/** RSA_KEY as a PEM SubjectPublicKeyInfo. */
|
||||
private static final String RSA_PEM_SPKI = """
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqe5hgvTZECPpaMX9RvWz
|
||||
Oh5wrPYkdZ582QP3/6O4cf1r9aA4cdN5Oh/NyPkOPXhNVzuelgT8Z6/P9hulJkvQ
|
||||
9ERSnvS/hya34mRe6C0NPScBCvdVSzUpA9Aats4Z31iXv1C5Gj0OxYBxIIUYvGiO
|
||||
T7AEx+lQmiQylsZftm8zOzFpWykQKPLrrtnr5xq4J7vkZrlyUAkWM+pRviRElc5u
|
||||
jJx12MZnvlA/u3P7U2MM6LMH14fFTfEFc15o3x4tlXtzyfmLhdl9oCqUTDTH+3C1
|
||||
jAkzoWSNgIF1/jSw5QhtHRFJvw13UikUw4Zt5iCZheu3hmLn3/sx7rAWfXEgZFvY
|
||||
vQIDAQAB
|
||||
-----END PUBLIC KEY-----
|
||||
""";
|
||||
|
||||
/** RSA_KEY as the bare PKCS#1 PEM that {@code ssh-keygen -e -m PEM} writes. */
|
||||
private static final String RSA_PEM_PKCS1 = """
|
||||
-----BEGIN RSA PUBLIC KEY-----
|
||||
MIIBCgKCAQEAqe5hgvTZECPpaMX9RvWzOh5wrPYkdZ582QP3/6O4cf1r9aA4cdN5
|
||||
Oh/NyPkOPXhNVzuelgT8Z6/P9hulJkvQ9ERSnvS/hya34mRe6C0NPScBCvdVSzUp
|
||||
A9Aats4Z31iXv1C5Gj0OxYBxIIUYvGiOT7AEx+lQmiQylsZftm8zOzFpWykQKPLr
|
||||
rtnr5xq4J7vkZrlyUAkWM+pRviRElc5ujJx12MZnvlA/u3P7U2MM6LMH14fFTfEF
|
||||
c15o3x4tlXtzyfmLhdl9oCqUTDTH+3C1jAkzoWSNgIF1/jSw5QhtHRFJvw13UikU
|
||||
w4Zt5iCZheu3hmLn3/sx7rAWfXEgZFvYvQIDAQAB
|
||||
-----END RSA PUBLIC KEY-----
|
||||
""";
|
||||
|
||||
/** RSA_KEY as base64 DER, a SubjectPublicKeyInfo. */
|
||||
private static final String RSA_DER_SPKI = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqe5hgvTZECPpaMX9RvWzOh5"
|
||||
+ "wrPYkdZ582QP3/6O4cf1r9aA4cdN5Oh/NyPkOPXhNVzuelgT8Z6/P9hulJkvQ9ERSnvS/hya34mRe6C0NPScBCvdVSzUpA9Aats"
|
||||
+ "4Z31iXv1C5Gj0OxYBxIIUYvGiOT7AEx+lQmiQylsZftm8zOzFpWykQKPLrrtnr5xq4J7vkZrlyUAkWM+pRviRElc5ujJx12MZnvl"
|
||||
+ "A/u3P7U2MM6LMH14fFTfEFc15o3x4tlXtzyfmLhdl9oCqUTDTH+3C1jAkzoWSNgIF1/jSw5QhtHRFJvw13UikUw4Zt5iCZheu3h"
|
||||
+ "mLn3/sx7rAWfXEgZFvYvQIDAQAB";
|
||||
|
||||
/** RSA_KEY as base64 DER, the bare PKCS#1 RSAPublicKey. */
|
||||
private static final String RSA_DER_PKCS1 = "MIIBCgKCAQEAqe5hgvTZECPpaMX9RvWzOh5wrPYkdZ582QP3/6O4cf1r9aA4cdN5"
|
||||
+ "Oh/NyPkOPXhNVzuelgT8Z6/P9hulJkvQ9ERSnvS/hya34mRe6C0NPScBCvdVSzUpA9Aats4Z31iXv1C5Gj0OxYBxIIUYvGiOT7AE"
|
||||
+ "x+lQmiQylsZftm8zOzFpWykQKPLrrtnr5xq4J7vkZrlyUAkWM+pRviRElc5ujJx12MZnvlA/u3P7U2MM6LMH14fFTfEFc15o3x4t"
|
||||
+ "lXtzyfmLhdl9oCqUTDTH+3C1jAkzoWSNgIF1/jSw5QhtHRFJvw13UikUw4Zt5iCZheu3hmLn3/sx7rAWfXEgZFvYvQIDAQAB";
|
||||
|
||||
/** A throwaway 1024-bit RSA key from {@code ssh-keygen -t rsa -b 1024}. */
|
||||
private static final String RSA_1024_PUBLIC_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDDMEMPnK6zq4YbIZrEVotEGUJIAS"
|
||||
+ "cQzFZSvmFegy0PHGRK6QNJWIK0eh+dNZ+hckR/X/LQvliDhCndX++/r/n3eZxmD3OXam4yg8RHT5k1Yj9eZCPxC8w1gfdDicoFIpa8"
|
||||
+ "BZLVUf0/7u3KH6rpgJyW0sH2Fg7nI4WcOfEJRc+qUw== throwaway";
|
||||
|
||||
/** A throwaway ECDSA P-384 key from {@code ssh-keygen -t ecdsa -b 384}. */
|
||||
private static final String ECDSA_P384_PUBLIC_KEY = "ecdsa-sha2-nistp384 AAAAE2VjZHNhLXNoYTItbmlzdHAzODQAAAAIbmlzdHAz"
|
||||
+ "ODQAAABhBEFD+laPV5eyv8Xz7vR3C2ghLq0L8tSwATYS5Nalon9hyyVbf9+wSWLKJH7rmq374ljT0Uh0vuy47nzXJT9pEVtSthFnpue9"
|
||||
+ "nxG0TEvsjbQgmcNNCt8vYZPrYWBLc1x7zg== throwaway";
|
||||
|
||||
@Test
|
||||
void generatedPrivateKeyParsesAsA2048BitRsaKey() throws Exception {
|
||||
@@ -103,8 +172,8 @@ class Ec2KeyMaterialTest {
|
||||
|
||||
@Test
|
||||
void importedKeyFingerprintIsPerKeyRatherThanAConstant() {
|
||||
String first = Ec2KeyMaterial.fingerprintOf(Ec2KeyMaterial.generateRsa().openSshPublicKey());
|
||||
String second = Ec2KeyMaterial.fingerprintOf(Ec2KeyMaterial.generateRsa().openSshPublicKey());
|
||||
String first = fingerprintOf(Ec2KeyMaterial.generateRsa().openSshPublicKey());
|
||||
String second = fingerprintOf(Ec2KeyMaterial.generateRsa().openSshPublicKey());
|
||||
|
||||
assertNotNull(first);
|
||||
// MD5 of the DER public key: 16 bytes.
|
||||
@@ -118,15 +187,15 @@ class Ec2KeyMaterialTest {
|
||||
// so re-importing the same key under a different comment must not change its identity.
|
||||
String publicKey = Ec2KeyMaterial.generateRsa().openSshPublicKey();
|
||||
|
||||
assertEquals(Ec2KeyMaterial.fingerprintOf(publicKey),
|
||||
Ec2KeyMaterial.fingerprintOf(publicKey + " someone@example.com"));
|
||||
assertEquals(fingerprintOf(publicKey),
|
||||
fingerprintOf(publicKey + " someone@example.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unparseableImportedMaterialYieldsNoFingerprintRatherThanADigestOfGarbage() {
|
||||
assertEquals(null, Ec2KeyMaterial.fingerprintOf("not-a-key"));
|
||||
assertEquals(null, Ec2KeyMaterial.fingerprintOf(""));
|
||||
assertEquals(null, Ec2KeyMaterial.fingerprintOf(null));
|
||||
assertEquals(null, fingerprintOf("not-a-key"));
|
||||
assertEquals(null, fingerprintOf(""));
|
||||
assertEquals(null, fingerprintOf(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -149,16 +218,16 @@ class Ec2KeyMaterialTest {
|
||||
// which is the command AWS gives for verifying an imported RSA key pair. A test that
|
||||
// compared the service against fingerprintOf, or that only checked the digest shape,
|
||||
// would pass under either scheme and so could not tell them apart.
|
||||
String expected = "4d:1a:39:2e:6a:18:60:9a:c5:2a:cb:cc:6c:de:22:b5";
|
||||
String expected = RSA_FINGERPRINT;
|
||||
|
||||
assertEquals(expected, Ec2KeyMaterial.fingerprintOf(RSA_PUBLIC_KEY));
|
||||
assertEquals(expected, Ec2KeyMaterial.fingerprintOf(RSA_PUBLIC_KEY + " someone@example.com"));
|
||||
assertEquals(expected, fingerprintOf(RSA_KEY));
|
||||
assertEquals(expected, fingerprintOf(RSA_KEY + " someone@example.com"));
|
||||
|
||||
// The other candidate scheme for the same key: MD5 over the SSH wire blob, which is
|
||||
// what "ssh-keygen -l -E md5" reports. AWS does not use it for imported RSA keys, and
|
||||
// it is a different value, which is what makes the assertion above load bearing.
|
||||
assertNotEquals("e7:d6:55:60:68:18:8f:b7:4f:2a:72:20:0b:2f:f2:d9",
|
||||
Ec2KeyMaterial.fingerprintOf(RSA_PUBLIC_KEY));
|
||||
fingerprintOf(RSA_KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -167,17 +236,17 @@ class Ec2KeyMaterialTest {
|
||||
// Derived independently with "ssh-keygen -l -f ed25519.pub", which prints
|
||||
// SHA256:UOyzahv0Ty520U89wfCvKdTlp2TbtpmnlpJHPW3MbMk
|
||||
// AWS reports the same digest without the prefix and with base64 padding kept.
|
||||
String fingerprint = Ec2KeyMaterial.fingerprintOf(ED25519_PUBLIC_KEY);
|
||||
String fingerprint = fingerprintOf(ED25519_KEY);
|
||||
|
||||
assertEquals("UOyzahv0Ty520U89wfCvKdTlp2TbtpmnlpJHPW3MbMk=", fingerprint);
|
||||
assertEquals(ED25519_FINGERPRINT, fingerprint);
|
||||
assertEquals(fingerprint,
|
||||
Ec2KeyMaterial.fingerprintOf(ED25519_PUBLIC_KEY + " someone@example.com"));
|
||||
fingerprintOf(ED25519_KEY + " someone@example.com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ed25519MaterialThatIsNotA32ByteKeyYieldsNoFingerprint() {
|
||||
assertEquals(null, Ec2KeyMaterial.fingerprintOf(
|
||||
"ssh-ed25519 " + Base64.getEncoder().encodeToString(blobOf("ssh-ed25519", new byte[31]))));
|
||||
assertEquals(null, fingerprintOf(
|
||||
line("ssh-ed25519", new byte[31])));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -185,33 +254,336 @@ class Ec2KeyMaterialTest {
|
||||
// ImportKeyPair passes the caller's material to this parser, so the declared field
|
||||
// length is attacker controlled. A blob that announces a two-gigabyte field is a few
|
||||
// bytes on the wire; allocating first turns that into an OutOfMemoryError, which is an
|
||||
// Error and so escapes the catch inside fingerprintOf and takes the request thread
|
||||
// Error and so escapes the catch inside readImportedKey and takes the request thread
|
||||
// with it. The length has to be rejected before the array exists.
|
||||
ByteBuffer overstated = ByteBuffer.allocate(15);
|
||||
byte[] type = "ssh-rsa".getBytes(StandardCharsets.UTF_8);
|
||||
overstated.putInt(type.length).put(type).putInt(Integer.MAX_VALUE);
|
||||
|
||||
assertEquals(null, Ec2KeyMaterial.fingerprintOf(
|
||||
assertEquals(null, fingerprintOf(
|
||||
"ssh-rsa " + Base64.getEncoder().encodeToString(overstated.array())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void negativeAndTruncatedFieldLengthsYieldNoFingerprint() {
|
||||
ByteBuffer negative = ByteBuffer.allocate(4).putInt(-1);
|
||||
assertEquals(null, Ec2KeyMaterial.fingerprintOf(
|
||||
assertEquals(null, fingerprintOf(
|
||||
"ssh-rsa " + Base64.getEncoder().encodeToString(negative.array())));
|
||||
|
||||
// Fewer than four bytes left: not even a length prefix.
|
||||
assertEquals(null, Ec2KeyMaterial.fingerprintOf(
|
||||
assertEquals(null, fingerprintOf(
|
||||
"ssh-rsa " + Base64.getEncoder().encodeToString(new byte[]{0, 0, 1})));
|
||||
}
|
||||
|
||||
private static byte[] blobOf(String type, byte[] key) {
|
||||
byte[] typeBytes = type.getBytes(StandardCharsets.UTF_8);
|
||||
return ByteBuffer.allocate(8 + typeBytes.length + key.length)
|
||||
.putInt(typeBytes.length).put(typeBytes)
|
||||
.putInt(key.length).put(key)
|
||||
.array();
|
||||
/**
|
||||
* The same RSA key in each format AWS documents for ImportKeyPair, all read as one key with the
|
||||
* fingerprint pinned above. The exports were made outside this code from RSA_KEY:
|
||||
* {@code ssh-keygen -e -m RFC4716}, {@code -m PKCS8} and {@code -m PEM}, and
|
||||
* {@code openssl pkey -pubin -outform DER | base64} and {@code openssl rsa -pubin
|
||||
* -RSAPublicKey_out -outform DER | base64} for the bare DER.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"openssh", "openssh-comment", "openssh-tab-crlf", "rfc4716", "pem-spki", "pem-pkcs1",
|
||||
"der-spki", "der-spki-wrapped", "der-pkcs1"})
|
||||
void anRsaKeyIsReadFromEveryFormatAwsDocumentsAsTheSameKey(String format) {
|
||||
String material = switch (format) {
|
||||
case "openssh" -> RSA_KEY;
|
||||
case "openssh-comment" -> RSA_KEY + " someone@example.com\n";
|
||||
case "openssh-tab-crlf" -> RSA_KEY.replace(' ', '\t') + "\tsomeone@example.com\r\n";
|
||||
case "rfc4716" -> RSA_RFC_4716;
|
||||
case "pem-spki" -> RSA_PEM_SPKI;
|
||||
case "pem-pkcs1" -> RSA_PEM_PKCS1;
|
||||
case "der-spki" -> RSA_DER_SPKI;
|
||||
case "der-spki-wrapped" -> String.join("\n", RSA_DER_SPKI.split("(?<=\\G.{64})")) + "\n";
|
||||
case "der-pkcs1" -> RSA_DER_PKCS1;
|
||||
default -> throw new IllegalArgumentException(format);
|
||||
};
|
||||
|
||||
Ec2KeyMaterial.Imported key = Ec2KeyMaterial.readImportedKey(material);
|
||||
|
||||
assertNotNull(key, format);
|
||||
assertEquals("rsa", key.keyType());
|
||||
assertEquals(RSA_FINGERPRINT, key.fingerprint());
|
||||
// One OpenSSH line without a comment, whatever came in: what authorized_keys can read.
|
||||
assertEquals(RSA_KEY, key.openSshPublicKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEd25519KeyIsReadFromAnOpenSshLine() {
|
||||
Ec2KeyMaterial.Imported key = Ec2KeyMaterial.readImportedKey(ED25519_KEY + " someone@example.com\n");
|
||||
|
||||
assertNotNull(key);
|
||||
assertEquals("ed25519", key.keyType());
|
||||
assertEquals(ED25519_FINGERPRINT, key.fingerprint());
|
||||
assertEquals(ED25519_KEY, key.openSshPublicKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* EC2 imports RSA and ED25519 keys only. DSA it names as refused; ECDSA, the security-key types
|
||||
* and certificates are not among the types it supports, and AWS answers an ECDSA import with
|
||||
* InvalidKey.Format. The DSA and ECDSA lines are throwaway keys from ssh-keygen.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"dsa", "ecdsa-p256", "ecdsa-p384", "sk-ed25519", "sk-ecdsa", "ed25519-cert", "unknown"})
|
||||
void aKeyOfATypeEc2DoesNotImportIsRefused(String type) {
|
||||
String material = switch (type) {
|
||||
case "dsa" -> DSA_KEY;
|
||||
case "ecdsa-p256" -> ECDSA_P256_KEY;
|
||||
case "ecdsa-p384" -> ECDSA_P384_PUBLIC_KEY;
|
||||
case "sk-ed25519" -> line("sk-ssh-ed25519@openssh.com", new byte[32], utf8("ssh:"));
|
||||
case "sk-ecdsa" -> line("sk-ecdsa-sha2-nistp256@openssh.com", utf8("nistp256"), new byte[65], utf8("ssh:"));
|
||||
case "ed25519-cert" -> line("ssh-ed25519-cert-v01@openssh.com", new byte[32], new byte[32]);
|
||||
case "unknown" -> line("ssh-foo", new byte[32]);
|
||||
default -> throw new IllegalArgumentException(type);
|
||||
};
|
||||
|
||||
assertNull(Ec2KeyMaterial.readImportedKey(material), type);
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS lists 1024, 2048 and 4096 bits as the supported RSA lengths but imports others too, an
|
||||
* 8192-bit key among them (https://github.com/terraform-aws-modules/terraform-aws-key-pair/issues/16),
|
||||
* so Floci takes the range OpenSSH reads, up to 16384 bits, which also covers ssh-keygen's default
|
||||
* of 3072. The moduli are synthetic: the length is what is checked, not whether the key factors.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({"512, false", "1023, false", "1024, true", "2048, true", "3072, true", "4096, true",
|
||||
"8192, true", "16384, true", "16385, false"})
|
||||
void anRsaKeyIsImportedOnlyFrom1024To16384Bits(int bits, boolean accepted) {
|
||||
BigInteger modulus = BigInteger.ONE.shiftLeft(bits - 1).setBit(0);
|
||||
String material = line("ssh-rsa", BigInteger.valueOf(65537).toByteArray(), modulus.toByteArray());
|
||||
|
||||
Ec2KeyMaterial.Imported key = Ec2KeyMaterial.readImportedKey(material);
|
||||
|
||||
assertEquals(accepted, key != null, bits + " bits");
|
||||
if (accepted) {
|
||||
assertEquals("rsa", key.keyType());
|
||||
assertEquals(material, key.openSshPublicKey());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRealKeyOfTheShortestSupportedLengthIsImported() {
|
||||
assertEquals("rsa", Ec2KeyMaterial.readImportedKey(RSA_1024_PUBLIC_KEY).keyType());
|
||||
}
|
||||
|
||||
/**
|
||||
* The blob has to be exactly the key of the type the line names, as OpenSSH requires: a field
|
||||
* missing, anything after the key, a type other than the line's, or a negative modulus is not a
|
||||
* key OpenSSH would read.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"rsa-blob-labelled-ed25519", "ed25519-blob-labelled-rsa", "rsa-without-modulus", "rsa-extra-field",
|
||||
"rsa-trailing-bytes", "ed25519-trailing-bytes", "ed25519-33-bytes", "rsa-negative-modulus",
|
||||
"rsa-empty-exponent"})
|
||||
void aKeyLineWhoseBlobIsNotExactlyTheKeyItNamesIsRefused(String shape) {
|
||||
byte[] rsaBlob = Base64.getDecoder().decode(RSA_KEY.split(" ")[1]);
|
||||
byte[] ed25519Blob = Base64.getDecoder().decode(ED25519_BLOB);
|
||||
byte[] exponent = BigInteger.valueOf(65537).toByteArray();
|
||||
byte[] modulus = BigInteger.ONE.shiftLeft(2047).setBit(0).toByteArray();
|
||||
String material = switch (shape) {
|
||||
case "rsa-blob-labelled-ed25519" -> "ssh-ed25519 " + base64(rsaBlob);
|
||||
case "ed25519-blob-labelled-rsa" -> "ssh-rsa " + base64(ed25519Blob);
|
||||
case "rsa-without-modulus" -> line("ssh-rsa", exponent);
|
||||
case "rsa-extra-field" -> line("ssh-rsa", exponent, modulus, utf8("extra"));
|
||||
case "rsa-trailing-bytes" -> "ssh-rsa " + base64(withTrailing(rsaBlob, new byte[] {1, 2, 3}));
|
||||
case "ed25519-trailing-bytes" -> "ssh-ed25519 " + base64(withTrailing(ed25519Blob, new byte[] {1, 2, 3}));
|
||||
case "ed25519-33-bytes" -> line("ssh-ed25519", new byte[33]);
|
||||
// Two's complement with the top bit set and no leading zero: a negative number.
|
||||
case "rsa-negative-modulus" -> line("ssh-rsa", exponent, Arrays.copyOfRange(modulus, 1, modulus.length));
|
||||
case "rsa-empty-exponent" -> line("ssh-rsa", new byte[0], modulus);
|
||||
default -> throw new IllegalArgumentException(shape);
|
||||
};
|
||||
|
||||
assertNull(Ec2KeyMaterial.readImportedKey(material), shape);
|
||||
}
|
||||
|
||||
/** A key pair holds one key: an instance launched with it would trust every line of the material. */
|
||||
@Test
|
||||
void materialHoldingASecondKeyLineIsRefused() {
|
||||
assertNull(Ec2KeyMaterial.readImportedKey(ED25519_KEY + " first\n" + RSA_KEY + " second\n"));
|
||||
assertNull(Ec2KeyMaterial.readImportedKey(RSA_KEY + "\n\n" + RSA_KEY));
|
||||
}
|
||||
|
||||
/** RFC 4716 and DER are RSA-only formats for ImportKeyPair, and PEM has to label a public key. */
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"rfc4716-ed25519", "rfc4716-unterminated", "rfc4716-empty", "pem-private-key", "pem-certificate-label",
|
||||
"pem-mismatched-end", "pem-ec-key", "der-ec-key", "der-trailing-bytes"})
|
||||
void anRsaOnlyFormatHoldingAnythingButAnRsaPublicKeyIsRefused(String shape) throws Exception {
|
||||
String ecDer = base64(KeyPairGenerator.getInstance("EC").generateKeyPair().getPublic().getEncoded());
|
||||
String material = switch (shape) {
|
||||
case "rfc4716-ed25519" -> "---- BEGIN SSH2 PUBLIC KEY ----\nComment: \"throwaway\"\n"
|
||||
+ ED25519_BLOB + "\n---- END SSH2 PUBLIC KEY ----\n";
|
||||
case "rfc4716-unterminated" -> RSA_RFC_4716.replace("---- END SSH2 PUBLIC KEY ----", "");
|
||||
case "rfc4716-empty" -> "---- BEGIN SSH2 PUBLIC KEY ----\nComment: \"throwaway\"\n---- END SSH2 PUBLIC KEY ----";
|
||||
case "pem-private-key" -> Ec2KeyMaterial.generateRsa().privateKeyPem();
|
||||
case "pem-certificate-label" -> RSA_PEM_SPKI.replace("PUBLIC KEY", "CERTIFICATE");
|
||||
case "pem-mismatched-end" -> RSA_PEM_SPKI.replace("END PUBLIC KEY", "END RSA PUBLIC KEY");
|
||||
case "pem-ec-key" -> "-----BEGIN PUBLIC KEY-----\n" + ecDer + "\n-----END PUBLIC KEY-----\n";
|
||||
case "der-ec-key" -> ecDer;
|
||||
case "der-trailing-bytes" -> base64(withTrailing(Base64.getDecoder().decode(RSA_DER_SPKI), new byte[] {0}));
|
||||
default -> throw new IllegalArgumentException(shape);
|
||||
};
|
||||
|
||||
assertNull(Ec2KeyMaterial.readImportedKey(material), shape);
|
||||
}
|
||||
|
||||
/**
|
||||
* Text that is not a key in any format, including the two mistakes AWS users report most: the
|
||||
* key line base64-encoded a second time, and a UTF-16 file.
|
||||
*/
|
||||
@Test
|
||||
void materialThatIsNoKeyAtAllIsRefused() {
|
||||
assertNull(Ec2KeyMaterial.readImportedKey(null));
|
||||
assertNull(Ec2KeyMaterial.readImportedKey(""));
|
||||
assertNull(Ec2KeyMaterial.readImportedKey(" \r\n\t"));
|
||||
assertNull(Ec2KeyMaterial.readImportedKey("not-a-key"));
|
||||
assertNull(Ec2KeyMaterial.readImportedKey("ssh-rsa"));
|
||||
assertNull(Ec2KeyMaterial.readImportedKey("ssh-rsa !!!not-base64!!!"));
|
||||
assertNull(Ec2KeyMaterial.readImportedKey(base64(utf8(RSA_KEY))));
|
||||
assertNull(Ec2KeyMaterial.readImportedKey(
|
||||
new String(RSA_KEY.getBytes(StandardCharsets.UTF_16LE), StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Every truncation and many corruptions of each accepted format, and random bytes, are read
|
||||
* without an exception escaping: ImportKeyPair has to answer them all with InvalidKey.Format.
|
||||
*/
|
||||
@Test
|
||||
void noMaterialMakesTheReaderThrow() {
|
||||
Random random = new Random(20261020L);
|
||||
List<String> valid = List.of(RSA_KEY + " c", ED25519_KEY, RSA_RFC_4716, RSA_PEM_SPKI,
|
||||
RSA_PEM_PKCS1, RSA_DER_SPKI, RSA_DER_PKCS1, DSA_KEY, ECDSA_P256_KEY);
|
||||
for (String material : valid) {
|
||||
for (int end = 0; end <= material.length(); end++) {
|
||||
assertDoesNotThrow(material.substring(0, end));
|
||||
}
|
||||
for (int i = 0; i < 200; i++) {
|
||||
char[] chars = material.toCharArray();
|
||||
chars[random.nextInt(chars.length)] = (char) random.nextInt(128);
|
||||
assertDoesNotThrow(new String(chars));
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 2000; i++) {
|
||||
byte[] bytes = new byte[random.nextInt(600)];
|
||||
random.nextBytes(bytes);
|
||||
assertDoesNotThrow(new String(bytes, StandardCharsets.ISO_8859_1));
|
||||
assertDoesNotThrow("ssh-rsa " + base64(bytes));
|
||||
assertDoesNotThrow(base64(withTrailing(new byte[] {0x30}, bytes)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A SubjectPublicKeyInfo whose key BIT STRING declares unused pad bits holds no whole key. Bouncy
|
||||
* Castle answers such a bit string with an IllegalStateException rather than an IOException, which
|
||||
* escaped the reader as a 500 and, from a stored key pair, failed every DescribeKeyPairs.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({"1, der", "1, pem", "4, der", "7, der", "7, pem"})
|
||||
void aDerKeyWhoseBitStringDeclaresPadBitsIsRefused(int padBits, String armour) {
|
||||
byte[] der = Base64.getDecoder().decode(RSA_DER_SPKI);
|
||||
// 30 82 01 22, then the 15-byte algorithm, then the BIT STRING header 03 82 01 0f: byte 23 is
|
||||
// the count of unused bits in the bit string's last byte.
|
||||
assertEquals(0, der[23]);
|
||||
der[23] = (byte) padBits;
|
||||
String material = "der".equals(armour) ? base64(der)
|
||||
: "-----BEGIN PUBLIC KEY-----\n" + base64(der) + "\n-----END PUBLIC KEY-----\n";
|
||||
|
||||
assertNull(Ec2KeyMaterial.readImportedKey(material));
|
||||
}
|
||||
|
||||
/**
|
||||
* Every byte of each binary form, set in turn to values that matter to DER and SSH framing, is
|
||||
* read without an exception escaping. Corrupting the base64 text, as the test above does, almost
|
||||
* never lands on a byte such as a bit string's pad count.
|
||||
*/
|
||||
@Test
|
||||
void noByteOfABinaryKeyMakesTheReaderThrow() {
|
||||
int[] values = {0, 1, 2, 3, 4, 7, 8, 0x0f, 0x10, 0x1f, 0x30, 0x7f, 0x80, 0x81, 0x82, 0x84, 0xfe, 0xff};
|
||||
List<byte[]> binaries = List.of(Base64.getDecoder().decode(RSA_DER_SPKI), Base64.getDecoder().decode(RSA_DER_PKCS1),
|
||||
Base64.getDecoder().decode(RSA_KEY.split(" ")[1]), Base64.getDecoder().decode(ED25519_BLOB));
|
||||
for (int form = 0; form < binaries.size(); form++) {
|
||||
byte[] original = binaries.get(form);
|
||||
for (int position = 0; position < original.length; position++) {
|
||||
for (int value : values) {
|
||||
byte[] corrupted = original.clone();
|
||||
corrupted[position] = (byte) value;
|
||||
String encoded = base64(corrupted);
|
||||
if (form < 2) {
|
||||
assertDoesNotThrow(encoded);
|
||||
assertDoesNotThrow("-----BEGIN PUBLIC KEY-----\n" + encoded + "\n-----END PUBLIC KEY-----");
|
||||
} else {
|
||||
String type = form == 2 ? "ssh-rsa " : "ssh-ed25519 ";
|
||||
assertDoesNotThrow(type + encoded);
|
||||
assertDoesNotThrow("---- BEGIN SSH2 PUBLIC KEY ----\n" + encoded + "\n---- END SSH2 PUBLIC KEY ----");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An rsaEncryption SubjectPublicKeyInfo whose key bit string holds no bytes, primitive or
|
||||
* constructed, has no key in it. Bouncy Castle reads such a bit string as no structure at all,
|
||||
* which the reader once passed on to a null dereference.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@CsvSource({"MBIwDQYJKoZIhvcNAQEBBQADAQA=, der", "MBIwDQYJKoZIhvcNAQEBBQADAQA=, pem",
|
||||
"MBEwDQYJKoZIhvcNAQEBBQAjAA==, der", "MBEwDQYJKoZIhvcNAQEBBQAjAA==, pem"})
|
||||
void aDerKeyWhoseBitStringIsEmptyIsRefused(String der, String armour) {
|
||||
String material = "der".equals(armour) ? der : "-----BEGIN PUBLIC KEY-----\n" + der + "\n-----END PUBLIC KEY-----\n";
|
||||
|
||||
assertNull(Ec2KeyMaterial.readImportedKey(material));
|
||||
}
|
||||
|
||||
/**
|
||||
* A PKCS#1 DER key whose modulus has the top bit set and no leading zero byte encodes a negative
|
||||
* number, as the same bytes do in an ssh-rsa blob, and is refused the same way rather than read
|
||||
* as some other, positive key.
|
||||
*/
|
||||
@Test
|
||||
void aDerKeyWithANegativeModulusIsRefused() {
|
||||
byte[] pkcs1 = Base64.getDecoder().decode(RSA_DER_PKCS1);
|
||||
// 30 82 01 0a, then 02 82 01 01 00 and the 256 modulus bytes: drop the sign byte and shorten
|
||||
// both lengths by one, leaving a modulus whose top bit is set.
|
||||
assertEquals(0, pkcs1[8]);
|
||||
byte[] negative = new byte[pkcs1.length - 1];
|
||||
System.arraycopy(pkcs1, 0, negative, 0, 8);
|
||||
System.arraycopy(pkcs1, 9, negative, 8, pkcs1.length - 9);
|
||||
negative[3] = (byte) (negative[3] - 1);
|
||||
negative[7] = (byte) (negative[7] - 1);
|
||||
|
||||
assertNull(Ec2KeyMaterial.readImportedKey(base64(negative)));
|
||||
}
|
||||
|
||||
private static void assertDoesNotThrow(String material) {
|
||||
try {
|
||||
Ec2KeyMaterial.readImportedKey(material);
|
||||
} catch (RuntimeException e) {
|
||||
throw new AssertionError("readImportedKey threw for: " + material, e);
|
||||
}
|
||||
}
|
||||
|
||||
/** The fingerprint ImportKeyPair records for the material, or null when it refuses it. */
|
||||
private static String fingerprintOf(String material) {
|
||||
Ec2KeyMaterial.Imported key = Ec2KeyMaterial.readImportedKey(material);
|
||||
return key != null ? key.fingerprint() : null;
|
||||
}
|
||||
|
||||
/** An OpenSSH line of the given type whose blob holds the type and then these fields. */
|
||||
private static String line(String type, byte[]... fields) {
|
||||
return type + " " + base64(blob(type, fields));
|
||||
}
|
||||
|
||||
private static String base64(byte[] bytes) {
|
||||
return Base64.getEncoder().encodeToString(bytes);
|
||||
}
|
||||
|
||||
private static byte[] utf8(String text) {
|
||||
return text.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static byte[] read(ByteBuffer blob) {
|
||||
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.xml.XmlPath;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ED25519_KEY;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.createdKeyPairId;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.deleteKeyPair;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ec2;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.importedKeyPairId;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
/**
|
||||
* DescribeKeyPairs reports each key pair's keyType and createTime, and Filter.N narrows the result
|
||||
* by the filters AWS documents and by key-type.
|
||||
*
|
||||
* @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeKeyPairs.html">DescribeKeyPairs</a>
|
||||
*/
|
||||
@QuarkusTest
|
||||
class Ec2KeyPairDescribeIntegrationTest {
|
||||
|
||||
@Test
|
||||
void createdAndImportedKeyPairsAreDescribedWithTheirTypeAndCreateTime() {
|
||||
String prefix = "described-" + UUID.randomUUID();
|
||||
Instant before = Instant.now().truncatedTo(ChronoUnit.MILLIS);
|
||||
String created = createdKeyPairId(prefix + "-created");
|
||||
String importedRsa = importedKeyPairId(prefix + "-rsa", Ec2KeyMaterial.generateRsa().openSshPublicKey());
|
||||
String importedEd25519 = importedKeyPairId(prefix + "-ed25519", ED25519_KEY + " someone@example.com");
|
||||
Instant after = Instant.now();
|
||||
try {
|
||||
for (String[] expected : new String[][] {{created, "rsa"}, {importedRsa, "rsa"}, {importedEd25519, "ed25519"}}) {
|
||||
XmlPath item = ec2("DescribeKeyPairs").formParam("KeyPairId.1", expected[0])
|
||||
.post("/").then().statusCode(200)
|
||||
.body("DescribeKeyPairsResponse.keySet.item.keyType", equalTo(expected[1]))
|
||||
.extract().xmlPath();
|
||||
Instant createTime = Instant.parse(item.getString("DescribeKeyPairsResponse.keySet.item.createTime"));
|
||||
assertFalse(createTime.isBefore(before), createTime + " is before " + before);
|
||||
assertFalse(createTime.isAfter(after), createTime + " is after " + after);
|
||||
}
|
||||
} finally {
|
||||
deleteKeyPair(created);
|
||||
deleteKeyPair(importedRsa);
|
||||
deleteKeyPair(importedEd25519);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Every filter name DescribeKeyPairs documents, plus key-type, with wildcards; the values of
|
||||
* one filter are ORed, separate filters ANDed. The key pairs share a prefix so the other key
|
||||
* pairs this application holds stay out of the answers.
|
||||
*/
|
||||
@Test
|
||||
void filtersNarrowTheKeyPairsDescribed() {
|
||||
String prefix = "filtered-" + UUID.randomUUID();
|
||||
String alpha = createdKeyPairId(prefix + "-alpha");
|
||||
String beta = importedKeyPairId(prefix + "-beta", ED25519_KEY);
|
||||
String gamma = importedKeyPairId(prefix + "-gamma", Ec2KeyMaterial.generateRsa().openSshPublicKey());
|
||||
try {
|
||||
ec2("CreateTags")
|
||||
.formParam("ResourceId.1", beta).formParam("Tag.1.Key", "Team").formParam("Tag.1.Value", "blue")
|
||||
.post("/").then().statusCode(200);
|
||||
String gammaFingerprint = ec2("DescribeKeyPairs").formParam("KeyPairId.1", gamma)
|
||||
.post("/").then().statusCode(200)
|
||||
.extract().path("DescribeKeyPairsResponse.keySet.item.keyFingerprint");
|
||||
|
||||
assertEquals(List.of("alpha", "beta", "gamma"), described(prefix));
|
||||
assertEquals(List.of("beta"), described(prefix, "key-pair-id", beta));
|
||||
assertEquals(List.of("alpha", "gamma"), described(prefix, "key-name", prefix + "-alpha", prefix + "-gamma"));
|
||||
assertEquals(List.of("beta"), described(prefix, "key-name", prefix + "-b?ta"));
|
||||
assertEquals(List.of("gamma"), described(prefix, "fingerprint", gammaFingerprint));
|
||||
assertEquals(List.of("alpha", "gamma"), described(prefix, "key-type", "rsa"));
|
||||
assertEquals(List.of("beta"), described(prefix, "key-type", "ed25519"));
|
||||
assertEquals(List.of("beta"), described(prefix, "tag-key", "Team"));
|
||||
assertEquals(List.of("beta"), described(prefix, "tag:Team", "blue"));
|
||||
assertEquals(List.of(), described(prefix, "tag:Team", "red"));
|
||||
} finally {
|
||||
deleteKeyPair(alpha);
|
||||
deleteKeyPair(beta);
|
||||
deleteKeyPair(gamma);
|
||||
}
|
||||
}
|
||||
|
||||
/** A filter name DescribeKeyPairs does not know is an error, not a filter that matches everything. */
|
||||
@Test
|
||||
void anUnknownFilterIsRefused() {
|
||||
ec2("DescribeKeyPairs")
|
||||
.formParam("Filter.1.Name", "owner-id").formParam("Filter.1.Value.1", "000000000000")
|
||||
.post("/").then().statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("Response.Errors.Error.Message", equalTo("The filter 'owner-id' is invalid"));
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS joins every Filter.N with AND, a repeated name included: two key-name filters narrow the
|
||||
* answer to the key pairs matching both, rather than the second replacing the first.
|
||||
*/
|
||||
@Test
|
||||
void aRepeatedFilterNameIsAndedWithTheFirst() {
|
||||
String prefix = "repeated-" + UUID.randomUUID();
|
||||
String prodWeb = createdKeyPairId(prefix + "-prod-web");
|
||||
String devWeb = createdKeyPairId(prefix + "-dev-web");
|
||||
String prodDb = createdKeyPairId(prefix + "-prod-db");
|
||||
try {
|
||||
List<String> ids = ec2("DescribeKeyPairs")
|
||||
.formParam("Filter.1.Name", "key-name").formParam("Filter.1.Value.1", prefix + "-prod-*")
|
||||
.formParam("Filter.2.Name", "key-name").formParam("Filter.2.Value.1", prefix + "-*-web")
|
||||
.post("/").then().statusCode(200).extract().xmlPath()
|
||||
.getList("DescribeKeyPairsResponse.keySet.item.keyPairId", String.class);
|
||||
|
||||
assertEquals(List.of(prodWeb), ids);
|
||||
assertEquals(List.of("prod-db", "prod-web"), described(prefix, "key-name", prefix + "-prod-*"));
|
||||
} finally {
|
||||
deleteKeyPair(prodWeb);
|
||||
deleteKeyPair(devWeb);
|
||||
deleteKeyPair(prodDb);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The key pairs DescribeKeyPairs returns for the given filter, a name and its values, ANDed with
|
||||
* a key-name filter of {@code prefix-*}: their names without the prefix, sorted. Without that
|
||||
* first filter every key pair this application holds would come back. A key-name filter under
|
||||
* test is a second key-name filter, which has to narrow the first.
|
||||
*/
|
||||
private static List<String> described(String prefix, String... filter) {
|
||||
RequestSpecification request = ec2("DescribeKeyPairs")
|
||||
.formParam("Filter.1.Name", "key-name").formParam("Filter.1.Value.1", prefix + "-*");
|
||||
if (filter.length > 0) {
|
||||
request.formParam("Filter.2.Name", filter[0]);
|
||||
for (int i = 1; i < filter.length; i++) {
|
||||
request.formParam("Filter.2.Value." + i, filter[i]);
|
||||
}
|
||||
}
|
||||
List<String> names = new ArrayList<>();
|
||||
for (String name : request.post("/").then().statusCode(200).extract().xmlPath()
|
||||
.getList("DescribeKeyPairsResponse.keySet.item.keyName", String.class)) {
|
||||
names.add(name.startsWith(prefix + "-") ? name.substring(prefix.length() + 1) : name);
|
||||
}
|
||||
names.sort(null);
|
||||
return names;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
|
||||
/** The key pair calls and the throwaway keys shared by the EC2 key pair tests. */
|
||||
final class Ec2KeyPairFixtures {
|
||||
|
||||
static final String AUTH = "AWS4-HMAC-SHA256 Credential=test/20261020/us-east-1/ec2/aws4_request";
|
||||
|
||||
/** A throwaway 2048-bit RSA key. */
|
||||
static final String RSA_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCp7mGC9"
|
||||
+ "NkQI+loxf1G9bM6HnCs9iR1nnzZA/f/o7hx/Wv1oDhx03k6H83I+Q49eE1XO56WBPxnr8/2G6UmS9D0R"
|
||||
+ "FKe9L+HJrfiZF7oLQ09JwEK91VLNSkD0Bq2zhnfWJe/ULkaPQ7FgHEghRi8aI5PsATH6VCaJDKWxl+2b"
|
||||
+ "zM7MWlbKRAo8uuu2evnGrgnu+RmuXJQCRYz6lG+JESVzm6MnHXYxme+UD+7c/tTYwzoswfXh8VN8QVzX"
|
||||
+ "mjfHi2Ve3PJ+YuF2X2gKpRMNMf7cLWMCTOhZI2AgXX+NLDlCG0dEUm/DXdSKRTDhm3mIJmF67eGYuff+"
|
||||
+ "zHusBZ9cSBkW9i9";
|
||||
|
||||
/**
|
||||
* RSA_KEY's fingerprint, the MD5 of its DER as EC2 reports it for an imported RSA key:
|
||||
* {@code openssl pkey -pubin -outform DER | openssl md5 -c}.
|
||||
*/
|
||||
static final String RSA_FINGERPRINT = "4d:1a:39:2e:6a:18:60:9a:c5:2a:cb:cc:6c:de:22:b5";
|
||||
|
||||
/** A throwaway ED25519 public key as an OpenSSH line without a comment. */
|
||||
static final String ED25519_KEY =
|
||||
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAII0fBPBUZHaEOBc2mySfmI5btu4mkvFfNRujmF7RH2fj";
|
||||
|
||||
/** The base64 blob of {@link #ED25519_KEY}. */
|
||||
static final String ED25519_BLOB = ED25519_KEY.split(" ")[1];
|
||||
|
||||
/**
|
||||
* ED25519_KEY's fingerprint as EC2 reports it, the base64 SHA-256 with its padding kept:
|
||||
* {@code ssh-keygen -l} prints it as {@code SHA256:UOyzahv0Ty520U89wfCvKdTlp2TbtpmnlpJHPW3MbMk}.
|
||||
*/
|
||||
static final String ED25519_FINGERPRINT = "UOyzahv0Ty520U89wfCvKdTlp2TbtpmnlpJHPW3MbMk=";
|
||||
|
||||
/** A throwaway DSA key from {@code ssh-keygen -t dsa}. */
|
||||
static final String DSA_KEY = "ssh-dss AAAAB3NzaC1kc3MAAACBAOFToDsRbL7Z8AqAZOKXROc1Cy5SA1ZgM5odRC57"
|
||||
+ "MSEruUjfUv2//jGbOLemgQBFsqZY1dyKlrpR3dm+tpfXGQR26Pkqm+5BF8kaykQZAYdwCzRhwzM7c8pei36Qi2XJ1s+MeLbIXZ2TTf"
|
||||
+ "yvkYhckXSrnf90oVJaMpaTIiDzu/CVAAAAFQCk1LBHtzCpTFHCInI4jnrGMjyyQQAAAIBz+pU2CQ0d8tQyxfQJ71wCCowSoVuUk5Zh"
|
||||
+ "1kAnIOoSPOTOuqos4OqicYdSH/AK8G0gKoXbp1BJX8H1yB401LAl8gccL1YbZBheZAtfNjeBd1xOZQZXWSYOEPQWVMcrqkId3un13z"
|
||||
+ "55qV0vl6ohU3Mp9ekGjBHuQWhDk8RYGgL9vwAAAIB6eL+huYUrgr06l0ozSlF9pf87rehfJaCLLJPPco8F9uCqx8sYsi/8b6gkbfq4"
|
||||
+ "w/k75H62ew8LVhpab9bP3/6/HQodnGrh1rthcapynsUzpFcjS86okexUcDkR3OnPLJ3CNGBS7MM0yw+NeXZN2BkeTdKAfbAdFw1mrb"
|
||||
+ "/AKu9GDg== throwaway";
|
||||
|
||||
/** A throwaway ECDSA P-256 key from {@code ssh-keygen -t ecdsa -b 256}. */
|
||||
static final String ECDSA_P256_KEY = "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAy"
|
||||
+ "NTYAAABBBFaxjAHsoCE0FmjbKLGzgVRKfOKFmYj1/JpnSjsuLjy23mHIUPkLJ5thgE47igHp+KufC1V2KqJE4OP5w4EK9SU= throwaway";
|
||||
|
||||
/** The truncated ssh-rsa blob Floci's own tests once imported: a type, an exponent, and part of a modulus. */
|
||||
static final String TRUNCATED_RSA_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDHnLTTbpnrFWkfVt test@test";
|
||||
|
||||
private Ec2KeyPairFixtures() {
|
||||
}
|
||||
|
||||
/** A request for the given EC2 action, signed for us-east-1. */
|
||||
static RequestSpecification ec2(String action) {
|
||||
return given().header("Authorization", AUTH).formParam("Action", action);
|
||||
}
|
||||
|
||||
/** CreateKeyPair with this name, whatever the answer. */
|
||||
static ValidatableResponse createKeyPair(String name) {
|
||||
return ec2("CreateKeyPair").formParam("KeyName", name).post("/").then();
|
||||
}
|
||||
|
||||
/** ImportKeyPair with this name and material, the material base64-encoded as the API takes it. */
|
||||
static ValidatableResponse importKeyPair(String name, byte[] material) {
|
||||
return ec2("ImportKeyPair").formParam("KeyName", name)
|
||||
.formParam("PublicKeyMaterial", Base64.getEncoder().encodeToString(material))
|
||||
.post("/").then();
|
||||
}
|
||||
|
||||
/** ImportKeyPair with this name and the UTF-8 bytes of this material. */
|
||||
static ValidatableResponse importKeyPair(String name, String material) {
|
||||
return importKeyPair(name, material.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/** Creates a key pair and returns its id. */
|
||||
static String createdKeyPairId(String name) {
|
||||
return createKeyPair(name).statusCode(200).extract().path("CreateKeyPairResponse.keyPairId");
|
||||
}
|
||||
|
||||
/** Imports the material and returns the new key pair's id. */
|
||||
static String importedKeyPairId(String name, byte[] material) {
|
||||
return importKeyPair(name, material).statusCode(200).extract().path("ImportKeyPairResponse.keyPairId");
|
||||
}
|
||||
|
||||
/** Imports the UTF-8 bytes of the material and returns the new key pair's id. */
|
||||
static String importedKeyPairId(String name, String material) {
|
||||
return importedKeyPairId(name, material.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/** Deletes the key pair with this id, and checks DeleteKeyPair answered 200. */
|
||||
static void deleteKeyPair(String id) {
|
||||
ec2("DeleteKeyPair").formParam("KeyPairId", id).post("/").then().statusCode(200);
|
||||
}
|
||||
}
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.SshPublicKeys;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.util.Base64;
|
||||
import java.util.HexFormat;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.core.common.SshPublicKeysTest.blob;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.DSA_KEY;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ECDSA_P256_KEY;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ED25519_BLOB;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ED25519_KEY;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.TRUNCATED_RSA_KEY;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.createKeyPair;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.deleteKeyPair;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ec2;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.importKeyPair;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* ImportKeyPair takes what AWS documents, RSA and ED25519 keys in the formats listed for each, and
|
||||
* refuses the rest with InvalidKey.Format; CreateKeyPair and ImportKeyPair hold names to the rule
|
||||
* AWS documents for both.
|
||||
*
|
||||
* @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/create-key-pairs.html#how-to-generate-your-own-key-and-import-it-to-aws">Requirements for key pairs</a>
|
||||
*/
|
||||
@QuarkusTest
|
||||
class Ec2KeyPairImportIntegrationTest {
|
||||
|
||||
/**
|
||||
* Each refusal is the error AWS gives an import it cannot read, and stores nothing: DSA and
|
||||
* ECDSA keys, an RSA key shorter than 1024 bits, the truncated ssh-rsa blob Floci's tests once
|
||||
* imported, two key lines, an ED25519 key in RFC 4716 (an RSA-only format), a private key, and
|
||||
* a key line base64-encoded twice.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"dsa", "ecdsa", "rsa-768", "truncated-rsa", "two-lines", "rfc4716-ed25519",
|
||||
"private-key", "double-base64", "der-pad-bits", "der-empty-bit-string"})
|
||||
void materialEc2DoesNotImportIsRefusedWithInvalidKeyFormat(String shape) {
|
||||
String name = "refused-" + UUID.randomUUID();
|
||||
String material = switch (shape) {
|
||||
case "dsa" -> DSA_KEY;
|
||||
case "ecdsa" -> ECDSA_P256_KEY;
|
||||
case "rsa-768" -> "ssh-rsa " + base64(blob("ssh-rsa", BigInteger.valueOf(65537).toByteArray(),
|
||||
BigInteger.ONE.shiftLeft(767).setBit(0).toByteArray()));
|
||||
// A SubjectPublicKeyInfo whose key bit string declares an unused bit: no whole key.
|
||||
case "der-pad-bits" -> base64(withPadBits(Ec2KeyMaterial.generateRsa().openSshPublicKey()));
|
||||
// An rsaEncryption SubjectPublicKeyInfo whose key bit string is empty: no key at all.
|
||||
case "der-empty-bit-string" -> "MBIwDQYJKoZIhvcNAQEBBQADAQA=";
|
||||
case "truncated-rsa" -> TRUNCATED_RSA_KEY;
|
||||
case "two-lines" -> ED25519_KEY + " first\n" + Ec2KeyMaterial.generateRsa().openSshPublicKey() + " second\n";
|
||||
case "rfc4716-ed25519" -> "---- BEGIN SSH2 PUBLIC KEY ----\n" + ED25519_BLOB
|
||||
+ "\n---- END SSH2 PUBLIC KEY ----\n";
|
||||
case "private-key" -> Ec2KeyMaterial.generateRsa().privateKeyPem();
|
||||
case "double-base64" -> base64(Ec2KeyMaterial.generateRsa().openSshPublicKey()
|
||||
.getBytes(StandardCharsets.UTF_8));
|
||||
default -> throw new IllegalArgumentException(shape);
|
||||
};
|
||||
|
||||
importKeyPair(name, material).statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidKey.Format"))
|
||||
.body("Response.Errors.Error.Message", equalTo("Key is not in valid OpenSSH public key format"));
|
||||
ec2("DescribeKeyPairs").formParam("KeyName.1", name)
|
||||
.post("/").then().statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidKeyPair.NotFound"));
|
||||
}
|
||||
|
||||
/**
|
||||
* One RSA key imported as an OpenSSH line, as RFC 4716, as PEM and as bare base64 DER is the same
|
||||
* key each time: the fingerprint is the MD5 of its DER, as AWS reports for an imported RSA key.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"openssh", "rfc4716", "pem", "der"})
|
||||
void anRsaKeyIsImportedFromEachFormatAwsDocuments(String format) throws Exception {
|
||||
String openSsh = Ec2KeyMaterial.generateRsa().openSshPublicKey();
|
||||
String blob = openSsh.split(" ")[1];
|
||||
RSAPublicKey key = SshPublicKeys.rsaKeyOf(Base64.getDecoder().decode(blob));
|
||||
String der = base64(key.getEncoded());
|
||||
String material = switch (format) {
|
||||
case "openssh" -> openSsh + " someone@example.com\n";
|
||||
case "rfc4716" -> "---- BEGIN SSH2 PUBLIC KEY ----\nComment: \"throwaway\"\n"
|
||||
+ String.join("\n", blob.split("(?<=\\G.{70})")) + "\n---- END SSH2 PUBLIC KEY ----\n";
|
||||
case "pem" -> "-----BEGIN PUBLIC KEY-----\n" + String.join("\n", der.split("(?<=\\G.{64})"))
|
||||
+ "\n-----END PUBLIC KEY-----\n";
|
||||
case "der" -> der;
|
||||
default -> throw new IllegalArgumentException(format);
|
||||
};
|
||||
String name = "imported-" + format + "-" + UUID.randomUUID();
|
||||
String expected = HexFormat.of().withDelimiter(":")
|
||||
.formatHex(MessageDigest.getInstance("MD5").digest(key.getEncoded()));
|
||||
|
||||
String id = importKeyPair(name, material).statusCode(200)
|
||||
.body("ImportKeyPairResponse.keyName", equalTo(name))
|
||||
.body("ImportKeyPairResponse.keyFingerprint", equalTo(expected))
|
||||
.extract().path("ImportKeyPairResponse.keyPairId");
|
||||
try {
|
||||
ec2("DescribeKeyPairs").formParam("KeyPairId.1", id)
|
||||
.post("/").then().statusCode(200)
|
||||
.body("DescribeKeyPairsResponse.keySet.item.keyFingerprint", equalTo(expected))
|
||||
.body("DescribeKeyPairsResponse.keySet.item.keyType", equalTo("rsa"));
|
||||
} finally {
|
||||
deleteKeyPair(id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* CreateKeyPair and ImportKeyPair take a name of up to 255 ASCII characters with no leading or
|
||||
* trailing space, and answer any other with InvalidParameterValue.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void keyPairNamesAreHeldToTheRuleAwsDocuments(boolean imported) {
|
||||
String longest = ("k-" + UUID.randomUUID()).repeat(10).substring(0, 255);
|
||||
String id = createOrImport(imported, longest).statusCode(200)
|
||||
.body((imported ? "ImportKeyPairResponse" : "CreateKeyPairResponse") + ".keyName", equalTo(longest))
|
||||
.extract().path((imported ? "ImportKeyPairResponse" : "CreateKeyPairResponse") + ".keyPairId");
|
||||
deleteKeyPair(id);
|
||||
|
||||
for (String name : new String[] {longest + "k", "cl\u00e9-" + UUID.randomUUID(), " leading-" + UUID.randomUUID(),
|
||||
"trailing-" + UUID.randomUUID() + " "}) {
|
||||
createOrImport(imported, name).statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void anOverlongNameIsRefusedWithTheValueAndTheReason() {
|
||||
String name = "n".repeat(256);
|
||||
createOrImport(false, name).statusCode(400)
|
||||
.body("Response.Errors.Error.Message", equalTo(
|
||||
"Value (" + name + ") for parameter KeyName is invalid. Length exceeds maximum of 255 characters."));
|
||||
}
|
||||
|
||||
private static ValidatableResponse createOrImport(boolean imported, String name) {
|
||||
return imported
|
||||
? importKeyPair(name, Ec2KeyMaterial.generateRsa().openSshPublicKey())
|
||||
: createKeyPair(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* The DER SubjectPublicKeyInfo of a 2048-bit OpenSSH RSA key with its bit string's pad count, the
|
||||
* byte after the BIT STRING header, set to one.
|
||||
*/
|
||||
private static byte[] withPadBits(String openSshRsaKey) {
|
||||
byte[] der = SshPublicKeys.rsaKeyOf(SshPublicKeys.decodeBlob(openSshRsaKey)).getEncoded();
|
||||
der[23] = 1;
|
||||
return der;
|
||||
}
|
||||
|
||||
private static String base64(byte[] bytes) {
|
||||
return Base64.getEncoder().encodeToString(bytes);
|
||||
}
|
||||
}
|
||||
+48
-92
@@ -10,28 +10,25 @@ import org.bouncycastle.openssl.PEMParser;
|
||||
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ED25519_BLOB;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.createKeyPair;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.deleteKeyPair;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ec2;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.importKeyPair;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.importedKeyPairId;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
|
||||
@QuarkusTest
|
||||
class Ec2KeyPairPublicKeyIntegrationTest {
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20261009/us-east-1/ec2/aws4_request";
|
||||
|
||||
/** The blob of a throwaway ed25519 public key. */
|
||||
private static final String ED25519_BLOB = "AAAAC3NzaC1lZDI1NTE5AAAAIIj7azlDjVHAsSxgcpCRZ3oWnTm0nAFM64y9jd22ioI/";
|
||||
|
||||
/**
|
||||
* The described key is the public half of the key the caller holds, with the key-pair name as
|
||||
@@ -43,15 +40,9 @@ class Ec2KeyPairPublicKeyIntegrationTest {
|
||||
void publicKeyIsReturnedOnlyWhenRequested(boolean imported) throws IOException {
|
||||
String name = "public-key-" + UUID.randomUUID();
|
||||
String importedKey = imported ? Ec2KeyMaterial.generateRsa().openSshPublicKey() : null;
|
||||
RequestSpecification create = given().header("Authorization", AUTH)
|
||||
.formParam("Action", imported ? "ImportKeyPair" : "CreateKeyPair")
|
||||
.formParam("KeyName", name);
|
||||
if (imported) {
|
||||
create.formParam("PublicKeyMaterial", Base64.getEncoder().encodeToString(
|
||||
utf8(importedKey + " supplied-comment")));
|
||||
}
|
||||
String responseName = imported ? "ImportKeyPairResponse" : "CreateKeyPairResponse";
|
||||
XmlPath created = create.post("/").then().statusCode(200).extract().xmlPath();
|
||||
XmlPath created = (imported ? importKeyPair(name, importedKey + " supplied-comment") : createKeyPair(name))
|
||||
.statusCode(200).extract().xmlPath();
|
||||
String id = created.getString(responseName + ".keyPairId");
|
||||
try {
|
||||
String expected = (imported ? importedKey : publicHalfOf(created.getString(responseName + ".keyMaterial")))
|
||||
@@ -59,8 +50,7 @@ class Ec2KeyPairPublicKeyIntegrationTest {
|
||||
for (String selection : new String[]{"KeyName.1", "KeyPairId.1"}) {
|
||||
String value = selection.startsWith("KeyName") ? name : id;
|
||||
for (String include : new String[]{null, "false", "true"}) {
|
||||
RequestSpecification describe = given().header("Authorization", AUTH)
|
||||
.formParam("Action", "DescribeKeyPairs").formParam(selection, value);
|
||||
RequestSpecification describe = ec2("DescribeKeyPairs").formParam(selection, value);
|
||||
if (include != null) {
|
||||
describe.formParam("IncludePublicKey", include);
|
||||
}
|
||||
@@ -88,7 +78,7 @@ class Ec2KeyPairPublicKeyIntegrationTest {
|
||||
@Test
|
||||
void anOpenSshLineWithoutACommentIsDescribedWithTheKeyPairName() {
|
||||
String name = "public-key-" + UUID.randomUUID();
|
||||
describeImported(name, utf8("ssh-ed25519\t" + ED25519_BLOB + "\r\n"), response -> response
|
||||
describeImported(name, "ssh-ed25519\t" + ED25519_BLOB + "\r\n", response -> response
|
||||
.body("DescribeKeyPairsResponse.keySet.item.publicKey",
|
||||
equalTo("ssh-ed25519 " + ED25519_BLOB + " " + name)));
|
||||
}
|
||||
@@ -100,94 +90,54 @@ class Ec2KeyPairPublicKeyIntegrationTest {
|
||||
@Test
|
||||
void aNameWithALineBreakIsNotUsedAsTheComment() {
|
||||
String name = "public-key-" + UUID.randomUUID() + "\nssh-ed25519 " + ED25519_BLOB + " another";
|
||||
describeImported(name, utf8("ssh-ed25519 " + ED25519_BLOB), response -> response
|
||||
describeImported(name, "ssh-ed25519 " + ED25519_BLOB, response -> response
|
||||
.body("DescribeKeyPairsResponse.keySet.item.publicKey", equalTo("ssh-ed25519 " + ED25519_BLOB)));
|
||||
}
|
||||
|
||||
/**
|
||||
* ImportKeyPair keeps material it cannot read as it was given, so the key pair is described
|
||||
* without a public key rather than with a rewrite of text that is not an OpenSSH line. A UTF-16
|
||||
* file decodes to text with NUL characters, which XML cannot carry at all.
|
||||
* ImportKeyPair reads an RSA key from RFC 4716 and stores it as its OpenSSH line, so the key
|
||||
* pair is described with that line and its name, as one imported as an OpenSSH line would be.
|
||||
* The material ImportKeyPair refuses can only be held by a key pair saved by an earlier version;
|
||||
* Ec2QueryHandlerTest describes those.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"rfc4716", "utf16"})
|
||||
void materialThatIsNotAnOpenSshLineIsDescribedWithoutAPublicKey(String format) {
|
||||
@Test
|
||||
void anRfc4716KeyIsDescribedAsItsOpenSshLine() {
|
||||
String name = "public-key-" + UUID.randomUUID();
|
||||
String openSsh = Ec2KeyMaterial.generateRsa().openSshPublicKey();
|
||||
byte[] material = "rfc4716".equals(format)
|
||||
? utf8("---- BEGIN SSH2 PUBLIC KEY ----\nComment: \"" + name + "\"\n" + openSsh.split(" ")[1]
|
||||
+ "\n---- END SSH2 PUBLIC KEY ----\n")
|
||||
: (openSsh + " " + name).getBytes(StandardCharsets.UTF_16LE);
|
||||
String material = "---- BEGIN SSH2 PUBLIC KEY ----\nComment: \"" + name + "\"\n" + openSsh.split(" ")[1]
|
||||
+ "\n---- END SSH2 PUBLIC KEY ----\n";
|
||||
describeImported(name, material, response -> response
|
||||
.body("DescribeKeyPairsResponse.keySet.item.keyName", equalTo(name))
|
||||
.body("DescribeKeyPairsResponse.keySet.item.keyFingerprint", startsWith("00:00"))
|
||||
.body("DescribeKeyPairsResponse.keySet.item.publicKey.size()", equalTo(0)));
|
||||
.body("DescribeKeyPairsResponse.keySet.item.publicKey", equalTo(openSsh + " " + name)));
|
||||
}
|
||||
|
||||
/**
|
||||
* A blob that declares a key type but lacks a field that type requires, here an ssh-rsa type
|
||||
* with no exponent or modulus and an ed25519 key one byte short, is not a public key.
|
||||
* IncludePublicKey is a boolean, read as true or false in any case, as the EC2 handler reads its
|
||||
* other strictly parsed booleans; a value that is neither is refused rather than read as false.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"AAAAB3NzaC1yc2E=", "AAAAC3NzaC1lZDI1NTE5AAAAHwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="})
|
||||
void aBlobMissingAFieldItsTypeRequiresIsDescribedWithoutAPublicKey(String blob) {
|
||||
String name = "public-key-" + UUID.randomUUID();
|
||||
String type = blob.startsWith("AAAAB3") ? "ssh-rsa" : "ssh-ed25519";
|
||||
describeImported(name, utf8(type + " " + blob + " " + name), response -> response
|
||||
.body("DescribeKeyPairsResponse.keySet.item.publicKey.size()", equalTo(0)));
|
||||
@CsvSource({"true, 1", "TRUE, 1", "True, 1", "false, 0", "FALSE, 0"})
|
||||
void includePublicKeyIsReadAsTrueOrFalseInAnyCase(String include, int publicKeys) {
|
||||
describeWithIncludePublicKey(include, response -> response.statusCode(200)
|
||||
.body("DescribeKeyPairsResponse.keySet.item.publicKey.size()", equalTo(publicKeys)));
|
||||
}
|
||||
|
||||
/**
|
||||
* A blob with anything after its key, here a whole extra field or three stray bytes after an
|
||||
* ed25519 key, is one OpenSSH refuses, so it is not described as a key either.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"field", "bytes"})
|
||||
void aBlobWithAnythingAfterItsKeyIsDescribedWithoutAPublicKey(String trailing) {
|
||||
String name = "public-key-" + UUID.randomUUID();
|
||||
byte[] key = Base64.getDecoder().decode(ED25519_BLOB);
|
||||
byte[] extra = "field".equals(trailing) ? new byte[] {0, 0, 0, 5, 'e', 'x', 't', 'r', 'a'} : new byte[] {1, 2, 3};
|
||||
String blob = Base64.getEncoder().encodeToString(
|
||||
ByteBuffer.allocate(key.length + extra.length).put(key).put(extra).array());
|
||||
describeImported(name, utf8("ssh-ed25519 " + blob + " " + name), response -> response
|
||||
.body("DescribeKeyPairsResponse.keySet.item.publicKey.size()", equalTo(0)));
|
||||
}
|
||||
|
||||
/**
|
||||
* A line that names another type than its blob declares, here a whole ssh-rsa blob labelled
|
||||
* ssh-ed25519, is not described as the RSA key its blob holds.
|
||||
*/
|
||||
@Test
|
||||
void aLineLabelledWithAnotherTypeThanItsBlobIsDescribedWithoutAPublicKey() {
|
||||
String name = "public-key-" + UUID.randomUUID();
|
||||
String rsaBlob = Ec2KeyMaterial.generateRsa().openSshPublicKey().split(" ")[1];
|
||||
describeImported(name, utf8("ssh-ed25519 " + rsaBlob + " " + name), response -> response
|
||||
.body("DescribeKeyPairsResponse.keySet.item.publicKey.size()", equalTo(0)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Material holding several key lines is not one key pair's key: an instance launched with it
|
||||
* trusts every line, so describing only the first would hide the others.
|
||||
*/
|
||||
@Test
|
||||
void materialHoldingSeveralKeysIsDescribedWithoutAPublicKey() {
|
||||
String name = "public-key-" + UUID.randomUUID();
|
||||
String second = Ec2KeyMaterial.generateRsa().openSshPublicKey();
|
||||
describeImported(name, utf8("ssh-ed25519 " + ED25519_BLOB + " first\n" + second + " second\n"),
|
||||
response -> response.body("DescribeKeyPairsResponse.keySet.item.publicKey.size()", equalTo(0)));
|
||||
@ValueSource(strings = {"invalid", "yes", "1", "truee"})
|
||||
void anIncludePublicKeyThatIsNeitherTrueNorFalseIsRefused(String include) {
|
||||
describeWithIncludePublicKey(include, response -> response.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("Response.Errors.Error.Message", equalTo("IncludePublicKey is not a valid boolean.")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports the material under the given name, describes the key pair with IncludePublicKey=true
|
||||
* and hands the response to the check, then deletes the key pair whatever the check found.
|
||||
*/
|
||||
private static void describeImported(String name, byte[] material, Consumer<ValidatableResponse> check) {
|
||||
String id = given().header("Authorization", AUTH)
|
||||
.formParam("Action", "ImportKeyPair").formParam("KeyName", name)
|
||||
.formParam("PublicKeyMaterial", Base64.getEncoder().encodeToString(material))
|
||||
.post("/").then().statusCode(200).extract().path("ImportKeyPairResponse.keyPairId");
|
||||
private static void describeImported(String name, String material, Consumer<ValidatableResponse> check) {
|
||||
String id = importedKeyPairId(name, material);
|
||||
try {
|
||||
check.accept(given().header("Authorization", AUTH).formParam("Action", "DescribeKeyPairs")
|
||||
check.accept(ec2("DescribeKeyPairs")
|
||||
.formParam("KeyPairId.1", id).formParam("IncludePublicKey", "true")
|
||||
.post("/").then().statusCode(200)
|
||||
.body("DescribeKeyPairsResponse.keySet.item.keyPairId", equalTo(id)));
|
||||
@@ -196,9 +146,19 @@ class Ec2KeyPairPublicKeyIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static void deleteKeyPair(String id) {
|
||||
given().header("Authorization", AUTH).formParam("Action", "DeleteKeyPair")
|
||||
.formParam("KeyPairId", id).post("/").then().statusCode(200);
|
||||
/**
|
||||
* Imports an ed25519 key, describes it by id with this IncludePublicKey value, whatever the
|
||||
* answer, and hands the response to the check, then deletes the key pair.
|
||||
*/
|
||||
private static void describeWithIncludePublicKey(String includePublicKey, Consumer<ValidatableResponse> check) {
|
||||
String id = importedKeyPairId("public-key-" + UUID.randomUUID(), "ssh-ed25519 " + ED25519_BLOB);
|
||||
try {
|
||||
check.accept(ec2("DescribeKeyPairs")
|
||||
.formParam("KeyPairId.1", id).formParam("IncludePublicKey", includePublicKey)
|
||||
.post("/").then());
|
||||
} finally {
|
||||
deleteKeyPair(id);
|
||||
}
|
||||
}
|
||||
|
||||
/** The OpenSSH line, without a comment, of the public half of a PEM private key. */
|
||||
@@ -208,8 +168,4 @@ class Ec2KeyPairPublicKeyIntegrationTest {
|
||||
return SshPublicKeys.toOpenSsh((RSAPublicKey) new JcaPEMKeyConverter().getKeyPair(pair).getPublic());
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] utf8(String text) {
|
||||
return text.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
@@ -170,8 +170,9 @@ class Ec2Phase2IntegrationTest {
|
||||
@Test
|
||||
@Order(20)
|
||||
void importKeyPairForSsh() {
|
||||
// AWS wire format: PublicKeyMaterial must be base64-encoded
|
||||
String publicKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDHnLTTbpnrFWkfVt test@test";
|
||||
// AWS wire format: PublicKeyMaterial must be base64-encoded. The key has to be whole, as
|
||||
// ImportKeyPair refuses a truncated blob.
|
||||
String publicKey = Ec2KeyMaterial.generateRsa().openSshPublicKey() + " test@test";
|
||||
String encodedKey = Base64.getEncoder().encodeToString(publicKey.getBytes());
|
||||
|
||||
given()
|
||||
|
||||
@@ -13,12 +13,19 @@ import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.NullAndEmptySource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.github.hectorvent.floci.core.common.SshPublicKeysTest.withTrailing;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ED25519_BLOB;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ED25519_KEY;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
@@ -273,21 +280,63 @@ class Ec2QueryHandlerTest {
|
||||
@NullAndEmptySource
|
||||
@ValueSource(strings = {"two\nlines", "carriage\rreturn", "a\ttab"})
|
||||
void describeKeyPairsGivesTheKeyNoCommentWhenTheNameCannotBeOne(String name) {
|
||||
String line = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIj7azlDjVHAsSxgcpCRZ3oWnTm0nAFM64y9jd22ioI/";
|
||||
KeyPair keyPair = new KeyPair();
|
||||
keyPair.setKeyPairId("key-0123456789abcdef0");
|
||||
keyPair.setKeyName(name);
|
||||
keyPair.setPublicKey(line + " imported-comment");
|
||||
keyPair.setPublicKey(ED25519_KEY + " imported-comment");
|
||||
|
||||
String xml = describeKeyPairWithItsPublicKey(keyPair);
|
||||
|
||||
assertTrue(xml.contains("<publicKey>" + ED25519_KEY + "</publicKey>"), xml);
|
||||
}
|
||||
|
||||
/**
|
||||
* ImportKeyPair now refuses material that is not exactly one whole key, but a key pair saved by
|
||||
* an earlier version kept what it was given, so its description must still leave out a public
|
||||
* key it does not hold: an ssh-rsa type with no exponent or modulus, an ed25519 key one byte
|
||||
* short, an ed25519 blob followed by an extra field or by stray bytes, an RSA blob labelled
|
||||
* ssh-ed25519, two key lines, RFC 4716 text, and a UTF-16 file read as text.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("storedMaterialThatIsNotOneWholeKey")
|
||||
void aKeyPairSavedWithMaterialThatIsNotOneWholeKeyIsDescribedWithoutAPublicKey(String material) {
|
||||
KeyPair keyPair = new KeyPair();
|
||||
keyPair.setKeyPairId("key-0123456789abcdef0");
|
||||
keyPair.setKeyName("saved-earlier");
|
||||
keyPair.setPublicKey(material);
|
||||
|
||||
String xml = describeKeyPairWithItsPublicKey(keyPair);
|
||||
|
||||
assertTrue(xml.contains("<keyName>saved-earlier</keyName>"), xml);
|
||||
assertFalse(xml.contains("<publicKey>"), xml);
|
||||
}
|
||||
|
||||
static Stream<String> storedMaterialThatIsNotOneWholeKey() {
|
||||
byte[] ed25519 = Base64.getDecoder().decode(ED25519_BLOB);
|
||||
String rsaBlob = Ec2KeyMaterial.generateRsa().openSshPublicKey().split(" ")[1];
|
||||
return Stream.of(
|
||||
"ssh-rsa AAAAB3NzaC1yc2E= saved-earlier",
|
||||
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAHwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= saved-earlier",
|
||||
"ssh-ed25519 " + Base64.getEncoder().encodeToString(withTrailing(ed25519, new byte[] {0, 0, 0, 5, 'e', 'x', 't', 'r', 'a'})),
|
||||
"ssh-ed25519 " + Base64.getEncoder().encodeToString(withTrailing(ed25519, new byte[] {1, 2, 3})),
|
||||
"ssh-ed25519 " + rsaBlob + " saved-earlier",
|
||||
"ssh-ed25519 " + ED25519_BLOB + " first\n" + Ec2KeyMaterial.generateRsa().openSshPublicKey() + " second\n",
|
||||
"---- BEGIN SSH2 PUBLIC KEY ----\nComment: \"saved-earlier\"\n" + rsaBlob + "\n---- END SSH2 PUBLIC KEY ----\n",
|
||||
new String(("ssh-ed25519 " + ED25519_BLOB + " saved-earlier").getBytes(StandardCharsets.UTF_16LE),
|
||||
StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/** DescribeKeyPairs with IncludePublicKey=true over a service that holds only this key pair. */
|
||||
private String describeKeyPairWithItsPublicKey(KeyPair keyPair) {
|
||||
Ec2Service service = mock(Ec2Service.class);
|
||||
when(service.describeKeyPairs(eq("us-east-1"), anyList(), anyList())).thenReturn(List.of(keyPair));
|
||||
when(service.describeKeyPairs(eq("us-east-1"), anyList(), anyList(), anyList())).thenReturn(List.of(keyPair));
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("IncludePublicKey", "true");
|
||||
|
||||
Response response = handler(service).handle("DescribeKeyPairs", params, "us-east-1");
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
String xml = (String) response.getEntity();
|
||||
assertTrue(xml.contains("<publicKey>" + line + "</publicKey>"), xml);
|
||||
return (String) response.getEntity();
|
||||
}
|
||||
|
||||
private String describeRegions(RegionResolver regionResolver, String region) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.ec2;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Address;
|
||||
@@ -31,16 +32,21 @@ import io.github.hectorvent.floci.services.ec2.model.Volume;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Vpc;
|
||||
import io.github.hectorvent.floci.services.ec2.model.VpcEndpoint;
|
||||
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ED25519_FINGERPRINT;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ED25519_KEY;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -557,6 +563,87 @@ class Ec2ServicePersistenceTest {
|
||||
List.of(), Map.of()).getFirst().getData().getUserData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void keyPairTypeAndCreateTimeSurviveRestart(@TempDir Path dir) {
|
||||
Ec2Service first = newService(dir);
|
||||
KeyPair created = first.createKeyPair(REGION, "restart-created");
|
||||
KeyPair imported = first.importKeyPair(REGION, "restart-imported", ED25519_KEY);
|
||||
|
||||
Ec2Service restarted = newService(dir);
|
||||
Map<String, KeyPair> described = new HashMap<>();
|
||||
for (KeyPair keyPair : restarted.describeKeyPairs(REGION, List.of(), List.of())) {
|
||||
described.put(keyPair.getKeyName(), keyPair);
|
||||
}
|
||||
|
||||
assertEquals("rsa", described.get("restart-created").getKeyType());
|
||||
assertEquals(created.getCreateTime(), described.get("restart-created").getCreateTime());
|
||||
assertEquals("ed25519", described.get("restart-imported").getKeyType());
|
||||
assertEquals(imported.getCreateTime(), described.get("restart-imported").getCreateTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* Key pairs saved before keyType and createTime were recorded still load and are described:
|
||||
* with no createTime, which was never known, and with the type read from the stored key, rsa for
|
||||
* the created one and ed25519 for the imported one. The imported material is kept exactly as
|
||||
* the older ImportKeyPair stored it, comment and newline included.
|
||||
*/
|
||||
@Test
|
||||
void keyPairsSavedBeforeTypeAndCreateTimeWereRecordedStillLoad(@TempDir Path dir) throws IOException {
|
||||
Ec2KeyMaterial.Generated generated = Ec2KeyMaterial.generateRsa();
|
||||
String importedKey = ED25519_KEY + " me@host\n";
|
||||
String legacyJson = """
|
||||
{
|
||||
"us-east-1::key-0aaaaaaaaaaaaaaaa": {
|
||||
"keyName": "legacy-created",
|
||||
"keyPairId": "key-0aaaaaaaaaaaaaaaa",
|
||||
"keyFingerprint": "%s",
|
||||
"keyMaterial": %s,
|
||||
"publicKey": "%s",
|
||||
"region": "us-east-1",
|
||||
"tags": []
|
||||
},
|
||||
"us-east-1::key-0bbbbbbbbbbbbbbbb": {
|
||||
"keyName": "legacy-imported",
|
||||
"keyPairId": "key-0bbbbbbbbbbbbbbbb",
|
||||
"keyFingerprint": "%s",
|
||||
"publicKey": %s,
|
||||
"region": "us-east-1",
|
||||
"tags": [{"key": "Team", "value": "blue"}]
|
||||
}
|
||||
}
|
||||
""".formatted(generated.fingerprint(), jsonString(generated.privateKeyPem()),
|
||||
generated.openSshPublicKey(), ED25519_FINGERPRINT, jsonString(importedKey));
|
||||
Files.writeString(dir.resolve("ec2-key-pairs.json"), legacyJson);
|
||||
|
||||
Ec2Service restarted = newService(dir);
|
||||
Map<String, KeyPair> described = new HashMap<>();
|
||||
for (KeyPair keyPair : restarted.describeKeyPairs(REGION, List.of(), List.of())) {
|
||||
described.put(keyPair.getKeyName(), keyPair);
|
||||
}
|
||||
|
||||
assertEquals(2, described.size());
|
||||
assertEquals("rsa", Ec2Service.keyTypeOf(described.get("legacy-created")));
|
||||
assertEquals("ed25519", Ec2Service.keyTypeOf(described.get("legacy-imported")));
|
||||
assertNull(described.get("legacy-created").getCreateTime());
|
||||
assertNull(described.get("legacy-imported").getCreateTime());
|
||||
assertEquals(importedKey, described.get("legacy-imported").getPublicKey());
|
||||
assertEquals(List.of("legacy-imported"), restarted.describeKeyPairs(REGION, List.of(), List.of(),
|
||||
Map.of("key-type", List.of("ed25519"), "tag:Team", List.of("blue"))).stream()
|
||||
.map(KeyPair::getKeyName).toList());
|
||||
|
||||
String xml = (String) new Ec2QueryHandler(restarted, mock(EmulatorConfig.class), mock(FlowLogService.class),
|
||||
mock(Ec2EbsEncryptionService.class), mock(Ec2SnapshotBlockPublicAccessService.class),
|
||||
mock(Ec2IpamService.class), new RegionResolver(REGION, "000000000000"))
|
||||
.handle("DescribeKeyPairs", new MultivaluedHashMap<>(), REGION).getEntity();
|
||||
assertTrue(xml.contains("<keyType>rsa</keyType>"), xml);
|
||||
assertTrue(xml.contains("<keyType>ed25519</keyType>"), xml);
|
||||
assertFalse(xml.contains("<createTime>"), xml);
|
||||
}
|
||||
|
||||
private static String jsonString(String value) {
|
||||
return "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n") + "\"";
|
||||
}
|
||||
|
||||
private BlockDeviceMapping blockDeviceMapping(String snapshotId, int volumeSize) {
|
||||
EbsBlockDevice ebs = new EbsBlockDevice();
|
||||
ebs.setSnapshotId(snapshotId);
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.github.dockerjava.api.command.ListNetworksCmd;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
@@ -60,6 +61,7 @@ import io.github.hectorvent.floci.services.ec2.model.VpcEndpoint;
|
||||
import io.github.hectorvent.floci.services.ec2.model.VpcEndpointSubnetConfiguration;
|
||||
import io.github.hectorvent.floci.services.ec2.net.VpcNetworkManager;
|
||||
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import org.awaitility.Awaitility;
|
||||
import org.bouncycastle.openssl.PEMKeyPair;
|
||||
import org.bouncycastle.openssl.PEMParser;
|
||||
@@ -70,13 +72,17 @@ import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
import org.mockito.MockMakers;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.interfaces.RSAPrivateCrtKey;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
@@ -94,11 +100,19 @@ import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ECDSA_P256_KEY;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ED25519_FINGERPRINT;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.ED25519_KEY;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.RSA_FINGERPRINT;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.RSA_KEY;
|
||||
import static io.github.hectorvent.floci.services.ec2.Ec2KeyPairFixtures.TRUNCATED_RSA_KEY;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
@@ -111,16 +125,20 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.Mockito.CALLS_REAL_METHODS;
|
||||
import static org.mockito.Mockito.RETURNS_SELF;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.withSettings;
|
||||
|
||||
class Ec2ServiceTest {
|
||||
|
||||
@@ -4425,7 +4443,7 @@ class Ec2ServiceTest {
|
||||
KeyPair second = service.importKeyPair("us-east-1", "imported-b", secondKey);
|
||||
|
||||
assertNotEquals(first.getKeyFingerprint(), second.getKeyFingerprint());
|
||||
assertEquals(Ec2KeyMaterial.fingerprintOf(firstKey), first.getKeyFingerprint());
|
||||
assertEquals(Ec2KeyMaterial.readImportedKey(firstKey).fingerprint(), first.getKeyFingerprint());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -4441,19 +4459,10 @@ class Ec2ServiceTest {
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
|
||||
String rsaKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCp7mGC9NkQI+loxf1G9bM6HnCs9iR1nn"
|
||||
+ "zZA/f/o7hx/Wv1oDhx03k6H83I+Q49eE1XO56WBPxnr8/2G6UmS9D0RFKe9L+HJrfiZF7oLQ09Jw"
|
||||
+ "EK91VLNSkD0Bq2zhnfWJe/ULkaPQ7FgHEghRi8aI5PsATH6VCaJDKWxl+2bzM7MWlbKRAo8uuu2e"
|
||||
+ "vnGrgnu+RmuXJQCRYz6lG+JESVzm6MnHXYxme+UD+7c/tTYwzoswfXh8VN8QVzXmjfHi2Ve3PJ+Y"
|
||||
+ "uF2X2gKpRMNMf7cLWMCTOhZI2AgXX+NLDlCG0dEUm/DXdSKRTDhm3mIJmF67eGYuff+zHusBZ9cS"
|
||||
+ "BkW9i9";
|
||||
String ed25519Key =
|
||||
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAII0fBPBUZHaEOBc2mySfmI5btu4mkvFfNRujmF7RH2fj";
|
||||
|
||||
assertEquals("4d:1a:39:2e:6a:18:60:9a:c5:2a:cb:cc:6c:de:22:b5",
|
||||
service.importKeyPair("us-east-1", "pinned-rsa", rsaKey).getKeyFingerprint());
|
||||
assertEquals("UOyzahv0Ty520U89wfCvKdTlp2TbtpmnlpJHPW3MbMk=",
|
||||
service.importKeyPair("us-east-1", "pinned-ed25519", ed25519Key).getKeyFingerprint());
|
||||
assertEquals(RSA_FINGERPRINT,
|
||||
service.importKeyPair("us-east-1", "pinned-rsa", RSA_KEY).getKeyFingerprint());
|
||||
assertEquals(ED25519_FINGERPRINT,
|
||||
service.importKeyPair("us-east-1", "pinned-ed25519", ED25519_KEY).getKeyFingerprint());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -4532,15 +4541,16 @@ class Ec2ServiceTest {
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
|
||||
service.importKeyPair("us-east-1", "duplicate-key", "c3NoLXJzYSBBQUFB");
|
||||
String publicKey = Ec2KeyMaterial.generateRsa().openSshPublicKey();
|
||||
service.importKeyPair("us-east-1", "duplicate-key", publicKey);
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.importKeyPair("us-east-1", "duplicate-key", "c3NoLXJzYSBBQUFB"));
|
||||
() -> service.importKeyPair("us-east-1", "duplicate-key", publicKey));
|
||||
assertEquals("InvalidKeyPair.Duplicate", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
|
||||
// same name in another region is allowed
|
||||
service.importKeyPair("us-west-2", "duplicate-key", "c3NoLXJzYSBBQUFB");
|
||||
service.importKeyPair("us-west-2", "duplicate-key", publicKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -4552,8 +4562,8 @@ class Ec2ServiceTest {
|
||||
|
||||
service.createKeyPair("us-east-1", "shared-key-name");
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.importKeyPair("us-east-1", "shared-key-name", "c3NoLXJzYSBBQUFB"));
|
||||
AwsException error = assertThrows(AwsException.class, () -> service.importKeyPair(
|
||||
"us-east-1", "shared-key-name", Ec2KeyMaterial.generateRsa().openSshPublicKey()));
|
||||
assertEquals("InvalidKeyPair.Duplicate", error.getErrorCode());
|
||||
}
|
||||
|
||||
@@ -4601,6 +4611,286 @@ class Ec2ServiceTest {
|
||||
assertEquals("InvalidKeyPair.NotFound", error.getErrorCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* ImportKeyPair refuses what EC2 does not import, as AWS does, with InvalidKey.Format: an ECDSA
|
||||
* key, and the truncated ssh-rsa blob Floci's own tests used to import, which no SSH server
|
||||
* could use. Nothing is stored for either.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
ECDSA_P256_KEY,
|
||||
TRUNCATED_RSA_KEY})
|
||||
void importKeyPairRefusesAKeyEc2DoesNotImport(String material) {
|
||||
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.importKeyPair("us-east-1", "refused-key", material));
|
||||
|
||||
assertEquals("InvalidKey.Format", error.getErrorCode());
|
||||
assertEquals("Key is not in valid OpenSSH public key format", error.getMessage());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
assertTrue(service.describeKeyPairs("us-east-1", List.of(), List.of()).isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS documents one rule for the names CreateKeyPair and ImportKeyPair take: up to 255 ASCII
|
||||
* characters, with no leading or trailing space. A refused name stores nothing.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void keyPairNamesFollowTheRuleAwsDocuments(boolean imported) {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
String publicKey = Ec2KeyMaterial.generateRsa().openSshPublicKey();
|
||||
Function<String, KeyPair> make = name -> imported
|
||||
? service.importKeyPair("us-east-1", name, publicKey)
|
||||
: service.createKeyPair("us-east-1", name);
|
||||
|
||||
String longest = "k".repeat(255);
|
||||
assertEquals(longest, make.apply(longest).getKeyName());
|
||||
assertEquals("a name with inner spaces", make.apply("a name with inner spaces").getKeyName());
|
||||
|
||||
Map<String, String> refused = Map.of(
|
||||
"k".repeat(256), "Length exceeds maximum of 255 characters.",
|
||||
"cl\u00e9", "Character sets beyond ASCII are not supported.",
|
||||
" leading", "Leading and trailing spaces are not allowed.",
|
||||
"trailing ", "Leading and trailing spaces are not allowed.");
|
||||
for (Map.Entry<String, String> name : refused.entrySet()) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> make.apply(name.getKey()));
|
||||
assertEquals("InvalidParameterValue", error.getErrorCode());
|
||||
assertEquals("Value (" + name.getKey() + ") for parameter KeyName is invalid. " + name.getValue(),
|
||||
error.getMessage());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
}
|
||||
assertEquals(2, service.describeKeyPairs("us-east-1", List.of(), List.of()).size());
|
||||
}
|
||||
|
||||
/**
|
||||
* A key pair records its type and when it was made, and an import keeps its key as one OpenSSH
|
||||
* line whatever format it came in, since that is what RunInstances writes to authorized_keys.
|
||||
*/
|
||||
@Test
|
||||
void keyPairsRecordTheirTypeAndCreateTime() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
String rsaLine = Ec2KeyMaterial.generateRsa().openSshPublicKey();
|
||||
String rfc4716 = "---- BEGIN SSH2 PUBLIC KEY ----\nComment: \"throwaway\"\n" + rsaLine.split(" ")[1]
|
||||
+ "\n---- END SSH2 PUBLIC KEY ----\n";
|
||||
Instant before = Instant.now();
|
||||
|
||||
KeyPair created = service.createKeyPair("us-east-1", "created");
|
||||
KeyPair importedRsa = service.importKeyPair("us-east-1", "imported-rsa", rfc4716);
|
||||
KeyPair importedEd25519 = service.importKeyPair("us-east-1", "imported-ed25519", ED25519_KEY + " someone@host\n");
|
||||
|
||||
Instant after = Instant.now();
|
||||
assertEquals("rsa", created.getKeyType());
|
||||
assertEquals("rsa", importedRsa.getKeyType());
|
||||
assertEquals("ed25519", importedEd25519.getKeyType());
|
||||
for (KeyPair keyPair : List.of(created, importedRsa, importedEd25519)) {
|
||||
assertFalse(keyPair.getCreateTime().isBefore(before), keyPair.getKeyName());
|
||||
assertFalse(keyPair.getCreateTime().isAfter(after), keyPair.getKeyName());
|
||||
}
|
||||
assertEquals(rsaLine, importedRsa.getPublicKey());
|
||||
assertEquals(ED25519_KEY, importedEd25519.getPublicKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter.N narrows DescribeKeyPairs by the names AWS documents, key-pair-id, key-name,
|
||||
* fingerprint, tag-key and tag:<key>, and by key-type, with wildcards, values of one filter
|
||||
* ORed and filters ANDed with each other and with KeyName.N. Any other name is refused, as AWS
|
||||
* refuses it, rather than matching every key pair.
|
||||
*/
|
||||
@Test
|
||||
void describeKeyPairsHonoursItsFilters() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
KeyPair alpha = service.createKeyPair("us-east-1", "alpha-rsa");
|
||||
KeyPair beta = service.importKeyPair("us-east-1", "beta-ed25519", ED25519_KEY);
|
||||
KeyPair gamma = service.importKeyPair("us-east-1", "gamma-rsa", Ec2KeyMaterial.generateRsa().openSshPublicKey());
|
||||
service.createKeyPair("us-west-2", "alpha-rsa");
|
||||
service.createTags("us-east-1", List.of(beta.getKeyPairId()), List.of(new Tag("Team", "blue")));
|
||||
BiFunction<List<String>, Map<String, List<String>>, List<String>> names = (keyNames, filters) ->
|
||||
service.describeKeyPairs("us-east-1", keyNames, List.of(), filters).stream()
|
||||
.map(KeyPair::getKeyName).sorted().toList();
|
||||
|
||||
assertEquals(List.of("alpha-rsa", "beta-ed25519", "gamma-rsa"), names.apply(List.of(), Map.of()));
|
||||
assertEquals(List.of("beta-ed25519"), names.apply(List.of(), Map.of("key-pair-id", List.of(beta.getKeyPairId()))));
|
||||
assertEquals(List.of("alpha-rsa"), names.apply(List.of(), Map.of("key-name", List.of("alpha-rsa"))));
|
||||
assertEquals(List.of("alpha-rsa", "gamma-rsa"), names.apply(List.of(), Map.of("key-name", List.of("*-rsa"))));
|
||||
assertEquals(List.of("beta-ed25519"), names.apply(List.of(), Map.of("key-name", List.of("?eta-*"))));
|
||||
assertEquals(List.of("gamma-rsa"), names.apply(List.of(), Map.of("fingerprint", List.of(gamma.getKeyFingerprint()))));
|
||||
assertEquals(List.of("beta-ed25519"), names.apply(List.of(), Map.of("fingerprint", List.of(beta.getKeyFingerprint()))));
|
||||
assertEquals(List.of("alpha-rsa", "gamma-rsa"), names.apply(List.of(), Map.of("key-type", List.of("rsa"))));
|
||||
assertEquals(List.of("beta-ed25519"), names.apply(List.of(), Map.of("key-type", List.of("ed25519"))));
|
||||
assertEquals(List.of("beta-ed25519"), names.apply(List.of(), Map.of("tag-key", List.of("Team"))));
|
||||
assertEquals(List.of("beta-ed25519"), names.apply(List.of(), Map.of("tag:Team", List.of("blue"))));
|
||||
assertEquals(List.of(), names.apply(List.of(), Map.of("tag:Team", List.of("red"))));
|
||||
assertEquals(List.of("alpha-rsa", "beta-ed25519"),
|
||||
names.apply(List.of(), Map.of("key-name", List.of("alpha-rsa", "beta-ed25519"))));
|
||||
assertEquals(List.of("gamma-rsa"), names.apply(List.of(),
|
||||
Map.of("key-type", List.of("rsa"), "key-name", List.of("g*"))));
|
||||
assertEquals(List.of("alpha-rsa"), names.apply(List.of("alpha-rsa", "beta-ed25519"),
|
||||
Map.of("key-type", List.of("rsa"))));
|
||||
assertEquals(alpha.getKeyPairId(), service.describeKeyPairs("us-east-1", List.of(), List.of(),
|
||||
Map.of("key-name", List.of("alpha-rsa"))).getFirst().getKeyPairId());
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> names.apply(List.of(), Map.of("owner-id", List.of("000000000000"))));
|
||||
assertEquals("InvalidParameterValue", error.getErrorCode());
|
||||
assertEquals("The filter 'owner-id' is invalid", error.getMessage());
|
||||
|
||||
// Filter.N in request order: a repeated name is ANDed with the earlier filter, not replacing it.
|
||||
// Each filter alone matches two key pairs, so keeping either one instead gives a longer list.
|
||||
List<Map.Entry<String, List<String>>> repeated = List.of(
|
||||
Map.entry("key-name", List.of("alpha-rsa", "gamma-rsa")),
|
||||
Map.entry("key-name", List.of("beta-ed25519", "gamma-rsa")));
|
||||
assertEquals(List.of("gamma-rsa"), service.describeKeyPairs("us-east-1", List.of(), List.of(), repeated)
|
||||
.stream().map(KeyPair::getKeyName).toList());
|
||||
AwsException repeatedUnknown = assertThrows(AwsException.class, () -> service.describeKeyPairs("us-east-1",
|
||||
List.of(), List.of(), List.of(Map.entry("key-name", List.of("*")), Map.entry("owner-id", List.of("x")))));
|
||||
assertEquals("The filter 'owner-id' is invalid", repeatedUnknown.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* A key pair stored before its type was recorded has none, so DescribeKeyPairs reads it from
|
||||
* what the record holds: a created key pair is RSA, an imported one has the type of its key,
|
||||
* and material ImportKeyPair would now refuse has no type. key-type filters by the same answer.
|
||||
*/
|
||||
@Test
|
||||
void theTypeOfAKeyPairStoredWithoutOneIsReadFromItsKey() throws Exception {
|
||||
AccountAwareStorageBackend<KeyPair> keyPairStore = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
Map<String, String> legacy = new LinkedHashMap<>();
|
||||
legacy.put("key-created", null);
|
||||
legacy.put("key-ed25519", ED25519_KEY + " me\n");
|
||||
legacy.put("key-ecdsa", ECDSA_P256_KEY);
|
||||
legacy.put("key-garbage", "c3NoLXJzYSBBQUFB");
|
||||
// A SubjectPublicKeyInfo whose bit string declares an unused bit (byte 23 of a 2048-bit RSA
|
||||
// key): the earlier ImportKeyPair stored it, and reading it must not fail the whole listing.
|
||||
byte[] padded = KeyPairGenerator.getInstance("RSA").generateKeyPair().getPublic().getEncoded();
|
||||
padded[23] = 1;
|
||||
legacy.put("key-pad-bits", "-----BEGIN PUBLIC KEY-----\n" + Base64.getEncoder().encodeToString(padded)
|
||||
+ "\n-----END PUBLIC KEY-----\n");
|
||||
// An rsaEncryption SubjectPublicKeyInfo whose key bit string is empty.
|
||||
legacy.put("key-empty-bit-string", "-----BEGIN PUBLIC KEY-----\nMBIwDQYJKoZIhvcNAQEBBQADAQA=\n-----END PUBLIC KEY-----\n");
|
||||
for (Map.Entry<String, String> record : legacy.entrySet()) {
|
||||
KeyPair keyPair = new KeyPair();
|
||||
keyPair.setKeyPairId(record.getKey());
|
||||
keyPair.setKeyName(record.getKey());
|
||||
keyPair.setRegion("us-east-1");
|
||||
if (record.getValue() == null) {
|
||||
Ec2KeyMaterial.Generated generated = Ec2KeyMaterial.generateRsa();
|
||||
keyPair.setKeyMaterial(generated.privateKeyPem());
|
||||
keyPair.setPublicKey(generated.openSshPublicKey());
|
||||
} else {
|
||||
keyPair.setPublicKey(record.getValue());
|
||||
}
|
||||
keyPairStore.put("us-east-1::" + record.getKey(), keyPair);
|
||||
}
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory(Map.of("ec2-key-pairs.json", keyPairStore)));
|
||||
|
||||
Map<String, String> types = new HashMap<>();
|
||||
for (KeyPair keyPair : service.describeKeyPairs("us-east-1", List.of(), List.of())) {
|
||||
// The reader answers each of these itself, without throwing, so keyTypeOf's guard is not
|
||||
// what yields the expected types.
|
||||
assertDoesNotThrow(() -> Ec2KeyMaterial.readImportedKey(keyPair.getPublicKey()), keyPair.getKeyPairId());
|
||||
types.put(keyPair.getKeyPairId(), Ec2Service.keyTypeOf(keyPair));
|
||||
assertNull(keyPair.getKeyType(), "the stored record is not rewritten");
|
||||
assertNull(keyPair.getCreateTime());
|
||||
}
|
||||
Map<String, String> expected = new HashMap<>();
|
||||
expected.put("key-created", "rsa");
|
||||
expected.put("key-ed25519", "ed25519");
|
||||
expected.put("key-ecdsa", null);
|
||||
expected.put("key-garbage", null);
|
||||
expected.put("key-pad-bits", null);
|
||||
expected.put("key-empty-bit-string", null);
|
||||
assertEquals(expected, types);
|
||||
assertEquals(List.of("key-ed25519"), service.describeKeyPairs("us-east-1", List.of(), List.of(),
|
||||
Map.of("key-type", List.of("ed25519"))).stream().map(KeyPair::getKeyPairId).toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Should the reader ever fail on what an older record holds, the record is still described, by
|
||||
* the service and by the handler, with no type, and a key-type filter passes over it, rather than
|
||||
* every description in the region failing with it. The stored key is a valid ed25519 line, so the
|
||||
* type would be ed25519 if the failing reader were not consulted.
|
||||
*/
|
||||
@Test
|
||||
void aStoredKeyPairWhoseKeyTheReaderFailsOnIsDescribedWithoutAType() {
|
||||
AccountAwareStorageBackend<KeyPair> keyPairStore = AccountAwareStorageBackend.inMemory("000000000000");
|
||||
KeyPair stored = new KeyPair();
|
||||
stored.setKeyPairId("key-unreadable");
|
||||
stored.setKeyName("key-unreadable");
|
||||
stored.setRegion("us-east-1");
|
||||
stored.setPublicKey(ED25519_KEY);
|
||||
keyPairStore.put("us-east-1::key-unreadable", stored);
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory(Map.of("ec2-key-pairs.json", keyPairStore)));
|
||||
|
||||
try (MockedStatic<Ec2KeyMaterial> reader = mockStatic(Ec2KeyMaterial.class,
|
||||
withSettings().mockMaker(MockMakers.INLINE).defaultAnswer(CALLS_REAL_METHODS))) {
|
||||
reader.when(() -> Ec2KeyMaterial.readImportedKey(anyString()))
|
||||
.thenThrow(new IllegalStateException("a reader that fails"));
|
||||
|
||||
List<KeyPair> described = service.describeKeyPairs("us-east-1", List.of(), List.of());
|
||||
|
||||
assertEquals(List.of("key-unreadable"), described.stream().map(KeyPair::getKeyPairId).toList());
|
||||
assertNull(Ec2Service.keyTypeOf(described.getFirst()));
|
||||
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(Ec2IpamService.class), new RegionResolver("us-east-1", "000000000000"))
|
||||
.handle("DescribeKeyPairs", new MultivaluedHashMap<>(), "us-east-1").getEntity();
|
||||
assertTrue(xml.contains("<keyPairId>key-unreadable</keyPairId>"), xml);
|
||||
assertFalse(xml.contains("<keyType>"), xml);
|
||||
reader.verify(() -> Ec2KeyMaterial.readImportedKey(anyString()), atLeastOnce());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The guard in keyTypeOf covers only records saved before the type was recorded. A reader failure
|
||||
* still surfaces everywhere else: ImportKeyPair fails with it, and a key pair whose type was
|
||||
* recorded is described without consulting the reader at all.
|
||||
*/
|
||||
@Test
|
||||
void aReaderFailureIsGuardedOnlyForKeyPairsSavedWithoutAType() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class),
|
||||
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
|
||||
new InMemoryStorageFactory());
|
||||
KeyPair recorded = service.importKeyPair("us-east-1", "recorded", ED25519_KEY);
|
||||
|
||||
try (MockedStatic<Ec2KeyMaterial> reader = mockStatic(Ec2KeyMaterial.class,
|
||||
withSettings().mockMaker(MockMakers.INLINE).defaultAnswer(CALLS_REAL_METHODS))) {
|
||||
reader.when(() -> Ec2KeyMaterial.readImportedKey(anyString()))
|
||||
.thenThrow(new IllegalStateException("a reader that fails"));
|
||||
|
||||
IllegalStateException failure = assertThrows(IllegalStateException.class,
|
||||
() -> service.importKeyPair("us-east-1", "another", ED25519_KEY));
|
||||
assertEquals("a reader that fails", failure.getMessage());
|
||||
assertEquals("ed25519", Ec2Service.keyTypeOf(service.describeKeyPairs("us-east-1",
|
||||
List.of("recorded"), List.of()).getFirst()));
|
||||
assertEquals(recorded.getKeyPairId(), service.describeKeyPairs("us-east-1", List.of(), List.of(),
|
||||
Map.of("key-type", List.of("ed25519"))).getFirst().getKeyPairId());
|
||||
reader.verify(() -> Ec2KeyMaterial.readImportedKey(anyString()), times(1));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteKeyPairByNameRemovesItFromTheStore() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
|
||||
Reference in new issue
Block a user