mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(rds): serve a CA-signed proxy TLS leaf certificate (#5700)
* fix(rds): serve a CA-signed proxy TLS leaf certificate * fix(rds): renew proxy TLS leaves before expiry * fix(rds): sign proxy leaves with the shared Floci CA Reuse the canonical CA and preserve legacy SAN metadata without loading or modifying the old RDS-only certificate and key. Keep leaf renewal and expiry rejection unchanged. * fix(rds): preserve legacy CA certificate paths Refresh rds-ca.crt from the shared Floci root and remove the unused RDS-only key. Replace the certificate path atomically so legacy links cannot overwrite shared key material, and leave failed initialization retryable.
This commit is contained in:
1 parent
4353e4232b
commit
e3442de5bd
7 files changed
+354
-167
No files matched your search
+3
-8
@@ -1,6 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.rds.proxy;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.FlociCertificateAuthority;
|
||||
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
|
||||
import io.github.hectorvent.floci.testutil.IamServiceTestHelper;
|
||||
import io.github.hectorvent.floci.testutil.SigV4TokenTestHelper;
|
||||
@@ -37,8 +37,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class MySqlProtocolHandlerTest {
|
||||
|
||||
@@ -696,11 +694,8 @@ class MySqlProtocolHandlerTest {
|
||||
}
|
||||
|
||||
private RdsProxyTlsCertificates testTlsCertificates() {
|
||||
EmulatorConfig.StorageConfig storage = mock(EmulatorConfig.StorageConfig.class);
|
||||
when(storage.persistentPath()).thenReturn(tempDir.toString());
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.storage()).thenReturn(storage);
|
||||
return new RdsProxyTlsCertificates(config, new CertificateGenerator());
|
||||
return new RdsProxyTlsCertificates(FlociCertificateAuthority.loadOrCreate(tempDir.resolve("tls")),
|
||||
new CertificateGenerator());
|
||||
}
|
||||
|
||||
private static RdsSigV4Validator testSigV4Validator() {
|
||||
|
||||
+37
-13
@@ -1,7 +1,8 @@
|
||||
package io.github.hectorvent.floci.services.rds.proxy;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.FlociCertificateAuthority;
|
||||
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
|
||||
import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm;
|
||||
import io.github.hectorvent.floci.testutil.IamServiceTestHelper;
|
||||
import io.github.hectorvent.floci.testutil.SigV4TokenTestHelper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -45,8 +46,7 @@ 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.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PostgresProtocolHandlerTest {
|
||||
|
||||
@@ -447,14 +447,32 @@ class PostgresProtocolHandlerTest {
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"172.17.0.5, true", "never-registered.example.com, false"})
|
||||
void hostnameVerificationRequiresRegisteredAdvertisedHost(String advertisedHost, boolean registered) throws Exception {
|
||||
@CsvSource({"172.17.0.5, true, false", "never-registered.example.com, false, false",
|
||||
"host.docker.internal, true, true", "never-registered.example.com, false, true"})
|
||||
void hostnameVerificationRequiresRegisteredAdvertisedHost(String advertisedHost, boolean registered,
|
||||
boolean legacyCa) throws Exception {
|
||||
try (ServerSocket clientServer = new ServerSocket(0);
|
||||
Socket ourClient = new Socket("localhost", clientServer.getLocalPort());
|
||||
Socket proxyClient = clientServer.accept()) {
|
||||
|
||||
RdsProxyTlsCertificates tlsCertificates = testTlsCertificates();
|
||||
Path caCertFile = tempDir.resolve("tls/rds-ca.crt");
|
||||
if (legacyCa) {
|
||||
CertificateGenerator.GeneratedCertificate legacy = new CertificateGenerator()
|
||||
.generateSelfSignedCertificate("localhost", List.of("localhost", "172.17.0.5"),
|
||||
KeyAlgorithm.RSA_2048);
|
||||
Files.writeString(caCertFile.resolveSibling("rds-ca.crt"), legacy.certificatePem());
|
||||
Files.writeString(caCertFile.resolveSibling("rds-ca.key"), legacy.privateKeyPem());
|
||||
Files.writeString(caCertFile.resolveSibling("rds-ca.metadata.json"),
|
||||
"{\"hostnames\":[\"localhost\",\"172.17.0.5\"]}");
|
||||
}
|
||||
tlsCertificates.ensureHost("172.17.0.5");
|
||||
Path trustedCaFile = Files.copy(caCertFile, tempDir.resolve("trusted-ca.crt"));
|
||||
if (legacyCa) {
|
||||
tlsCertificates.ensureHost("host.docker.internal");
|
||||
tlsCertificates = testTlsCertificates();
|
||||
}
|
||||
RdsProxyTlsCertificates serverCertificates = tlsCertificates;
|
||||
|
||||
AtomicReference<Throwable> authFailure = new AtomicReference<>();
|
||||
Thread authThread = Thread.ofVirtual()
|
||||
@@ -464,7 +482,7 @@ class PostgresProtocolHandlerTest {
|
||||
PostgresProtocolHandler.authenticate(
|
||||
proxyClient, NEVER_CONNECT,
|
||||
"dbadmin", "adminpass", "postgres",
|
||||
false, testSigV4Validator(), testBinding(), tlsCertificates,
|
||||
false, testSigV4Validator(), testBinding(), serverCertificates,
|
||||
(user, pass) -> PasswordValidator.AuthResult.MASTER_EQUIVALENT, 5000);
|
||||
} catch (IOException expected) {
|
||||
// Closing or rejecting TLS ends authentication before PostgreSQL startup.
|
||||
@@ -478,10 +496,19 @@ class PostgresProtocolHandlerTest {
|
||||
writeSslRequest(clientOut);
|
||||
assertEquals('S', clientIn.readUnsignedByte());
|
||||
|
||||
Path caCertFile = tempDir.resolve("tls").resolve("rds-ca.crt");
|
||||
try (SSLSocket sslClient = verifyingClientSocket(ourClient, caCertFile, advertisedHost)) {
|
||||
try (SSLSocket sslClient = verifyingClientSocket(ourClient, trustedCaFile, advertisedHost)) {
|
||||
if (registered) {
|
||||
sslClient.startHandshake();
|
||||
X509Certificate serverCertificate = (X509Certificate)
|
||||
sslClient.getSession().getPeerCertificates()[0];
|
||||
assertEquals(-1, serverCertificate.getBasicConstraints(),
|
||||
"strict TLS clients require an end-entity server certificate, not the trusted CA");
|
||||
assertTrue(serverCertificate.getExtendedKeyUsage().contains("1.3.6.1.5.5.7.3.1"));
|
||||
assertEquals(2, sslClient.getSession().getPeerCertificates().length);
|
||||
try (InputStream trustedCa = Files.newInputStream(trustedCaFile)) {
|
||||
assertEquals(CertificateFactory.getInstance("X.509").generateCertificate(trustedCa),
|
||||
sslClient.getSession().getPeerCertificates()[1]);
|
||||
}
|
||||
} else {
|
||||
assertThrows(SSLHandshakeException.class, sslClient::startHandshake);
|
||||
}
|
||||
@@ -1877,11 +1904,8 @@ class PostgresProtocolHandlerTest {
|
||||
}
|
||||
|
||||
private RdsProxyTlsCertificates testTlsCertificates() {
|
||||
EmulatorConfig.StorageConfig storage = mock(EmulatorConfig.StorageConfig.class);
|
||||
when(storage.persistentPath()).thenReturn(tempDir.toString());
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.storage()).thenReturn(storage);
|
||||
return new RdsProxyTlsCertificates(config, new CertificateGenerator());
|
||||
return new RdsProxyTlsCertificates(FlociCertificateAuthority.loadOrCreate(tempDir.resolve("tls")),
|
||||
new CertificateGenerator());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+3
-8
@@ -1,6 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.rds.proxy;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.FlociCertificateAuthority;
|
||||
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsBackendGate;
|
||||
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
@@ -23,7 +23,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Covers the connection bounds (handshake timeout, backend-connect deferral, permit pool) added
|
||||
@@ -145,11 +144,7 @@ class RdsAuthProxyBoundsTest {
|
||||
}
|
||||
|
||||
private RdsProxyTlsCertificates realTls() {
|
||||
// The real bean generates a self-signed cert on demand; no Docker or network needed.
|
||||
EmulatorConfig.StorageConfig storage = mock(EmulatorConfig.StorageConfig.class);
|
||||
when(storage.persistentPath()).thenReturn(tempDir.toString());
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.storage()).thenReturn(storage);
|
||||
return new RdsProxyTlsCertificates(config, new CertificateGenerator());
|
||||
return new RdsProxyTlsCertificates(FlociCertificateAuthority.loadOrCreate(tempDir.resolve("tls")),
|
||||
new CertificateGenerator());
|
||||
}
|
||||
}
|
||||
+179
-15
@@ -1,10 +1,13 @@
|
||||
package io.github.hectorvent.floci.services.rds.proxy;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.FlociCertificateAuthority;
|
||||
import io.github.hectorvent.floci.core.common.Pem;
|
||||
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.NullSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import java.nio.file.Files;
|
||||
@@ -12,13 +15,20 @@ import java.nio.file.Path;
|
||||
import java.nio.file.attribute.PosixFileAttributeView;
|
||||
import java.nio.file.attribute.PosixFilePermission;
|
||||
import java.nio.file.attribute.PosixFilePermissions;
|
||||
import java.security.cert.CertificateExpiredException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -29,6 +39,52 @@ class RdsProxyTlsCertificatesTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void usesTheExistingSharedCaWithoutCreatingAnotherRdsAuthority() throws Exception {
|
||||
FlociCertificateAuthority sharedCa = FlociCertificateAuthority.loadOrCreate(tempDir.resolve("tls"));
|
||||
newCertificates().sslContext();
|
||||
|
||||
assertEquals(sharedCa.caPem(), Files.readString(sharedCa.certificatePath()));
|
||||
assertEquals(sharedCa.caPem(), Files.readString(tempDir.resolve("tls/rds-ca.crt")));
|
||||
assertFalse(Files.exists(tempDir.resolve("tls/rds-ca.key")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyPathsCannotOverwriteOrDeleteTheSharedPrivateKeyThroughSymlinks() throws Exception {
|
||||
FlociCertificateAuthority sharedCa = FlociCertificateAuthority.loadOrCreate(tempDir.resolve("tls"));
|
||||
Path tlsDir = sharedCa.certificatePath().getParent();
|
||||
Path sharedKeyFile = tlsDir.resolve(FlociCertificateAuthority.CA_KEY_NAME);
|
||||
String sharedKey = Files.readString(sharedKeyFile);
|
||||
Path legacyCertificate = Files.createSymbolicLink(tlsDir.resolve("rds-ca.crt"), sharedKeyFile);
|
||||
Path legacyKey = Files.createSymbolicLink(tlsDir.resolve("rds-ca.key"), sharedKeyFile);
|
||||
|
||||
newCertificates().sslContext();
|
||||
|
||||
assertFalse(Files.isSymbolicLink(legacyCertificate));
|
||||
assertEquals(sharedCa.caPem(), Files.readString(legacyCertificate));
|
||||
assertFalse(Files.isSymbolicLink(legacyKey));
|
||||
assertEquals(sharedKey, Files.readString(sharedKeyFile));
|
||||
assertEquals(sharedCa.caPem(), Files.readString(sharedCa.certificatePath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFailedLegacyCertificateWriteDoesNotPublishAnInitializedContext() throws Exception {
|
||||
FlociCertificateAuthority sharedCa = FlociCertificateAuthority.loadOrCreate(tempDir.resolve("tls"));
|
||||
Path legacyCertificate = Files.createDirectory(tempDir.resolve("tls/rds-ca.crt"));
|
||||
Path obstruction = Files.writeString(legacyCertificate.resolve("keep"), "not a certificate");
|
||||
RdsProxyTlsCertificates certs = newCertificates();
|
||||
|
||||
assertThrows(IllegalStateException.class, certs::sslContext);
|
||||
Files.delete(obstruction);
|
||||
Files.delete(legacyCertificate);
|
||||
assertDoesNotThrow(certs::sslContext);
|
||||
|
||||
assertEquals(sharedCa.caPem(), Files.readString(legacyCertificate));
|
||||
try (Stream<Path> files = Files.list(legacyCertificate.getParent())) {
|
||||
assertFalse(files.anyMatch(path -> path.getFileName().toString().endsWith(".tmp")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void ensureHostIsIdempotentWhenHostAlreadyCovered() {
|
||||
RdsProxyTlsCertificates certs = newCertificates();
|
||||
@@ -57,21 +113,20 @@ class RdsProxyTlsCertificatesTest {
|
||||
first.ensureHost("172.17.0.5");
|
||||
first.ensureHost("host.docker.internal");
|
||||
|
||||
Path certFile = tempDir.resolve("tls").resolve("rds-ca.crt");
|
||||
Path keyFile = tempDir.resolve("tls").resolve("rds-ca.key");
|
||||
Path certFile = tempDir.resolve("tls").resolve(FlociCertificateAuthority.CA_CERT_NAME);
|
||||
Path keyFile = tempDir.resolve("tls").resolve(FlociCertificateAuthority.CA_KEY_NAME);
|
||||
Path metadataFile = tempDir.resolve("tls").resolve("rds-ca.metadata.json");
|
||||
assertTrue(Files.exists(certFile));
|
||||
assertTrue(Files.exists(keyFile));
|
||||
assertTrue(Files.exists(metadataFile));
|
||||
String persistedCertPem = Files.readString(certFile);
|
||||
|
||||
// A fresh instance (simulating a restart) backed by the same persistent path must reuse
|
||||
// the same cert rather than regenerating — otherwise a user's trusted sslrootcert goes stale
|
||||
// on every restart even when no new host has appeared.
|
||||
RdsProxyTlsCertificates reloaded = newCertificates();
|
||||
SSLContext reloadedContext = reloaded.sslContext();
|
||||
reloaded.ensureHost("172.17.0.5");
|
||||
reloaded.ensureHost("host.docker.internal");
|
||||
|
||||
assertSame(reloadedContext, reloaded.sslContext(), "persisted hosts do not need registration again");
|
||||
assertEquals(persistedCertPem, Files.readString(certFile));
|
||||
}
|
||||
|
||||
@@ -81,12 +136,12 @@ class RdsProxyTlsCertificatesTest {
|
||||
certs.ensureHost("172.17.0.5");
|
||||
|
||||
X509Certificate firstCertificate = Pem.parseCertificate(
|
||||
Files.readString(tempDir.resolve("tls").resolve("rds-ca.crt")));
|
||||
Files.readString(tempDir.resolve("tls").resolve(FlociCertificateAuthority.CA_CERT_NAME)));
|
||||
|
||||
certs.ensureHost("host.docker.internal");
|
||||
|
||||
X509Certificate secondCertificate = Pem.parseCertificate(
|
||||
Files.readString(tempDir.resolve("tls").resolve("rds-ca.crt")));
|
||||
Files.readString(tempDir.resolve("tls").resolve(FlociCertificateAuthority.CA_CERT_NAME)));
|
||||
|
||||
assertEquals(firstCertificate.getPublicKey(), secondCertificate.getPublicKey(),
|
||||
"growing the SAN list must reuse the existing key pair, not mint a new one, "
|
||||
@@ -104,7 +159,7 @@ class RdsProxyTlsCertificatesTest {
|
||||
RdsProxyTlsCertificates certs = newCertificates();
|
||||
certs.ensureHost("172.17.0.5");
|
||||
|
||||
Path keyFile = tlsDir.resolve("rds-ca.key");
|
||||
Path keyFile = tlsDir.resolve(FlociCertificateAuthority.CA_KEY_NAME);
|
||||
assertEquals(Set.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE),
|
||||
Files.getPosixFilePermissions(keyFile),
|
||||
"the CA private key must not be readable by anyone but the owner");
|
||||
@@ -118,16 +173,125 @@ class RdsProxyTlsCertificatesTest {
|
||||
certs.ensureHost("172.17.0.5");
|
||||
|
||||
X509Certificate certificate = Pem.parseCertificate(
|
||||
Files.readString(tempDir.resolve("tls").resolve("rds-ca.crt")));
|
||||
Files.readString(tempDir.resolve("tls").resolve(FlociCertificateAuthority.CA_CERT_NAME)));
|
||||
|
||||
assertEquals(certificate.getIssuerX500Principal(), certificate.getSubjectX500Principal());
|
||||
assertTrue(certificate.getBasicConstraints() >= 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void replacesAnExpiredLegacyCertificateAndRetainsItsSansWithoutChangingTheSharedCa() throws Exception {
|
||||
FlociCertificateAuthority sharedCa = FlociCertificateAuthority.loadOrCreate(tempDir.resolve("tls"));
|
||||
Path tlsDir = sharedCa.certificatePath().getParent();
|
||||
CertificateGenerator.GeneratedCertificate legacy = new CertificateGenerator()
|
||||
.generateCaCertificate("Legacy RDS CA", Instant.now().minusSeconds(60));
|
||||
assertThrows(CertificateExpiredException.class, () -> Pem.parseCertificate(legacy.certificatePem()).checkValidity());
|
||||
Path certFile = tlsDir.resolve("rds-ca.crt");
|
||||
Path keyFile = tlsDir.resolve("rds-ca.key");
|
||||
Files.writeString(certFile, legacy.certificatePem());
|
||||
Files.writeString(keyFile, legacy.privateKeyPem());
|
||||
Files.writeString(tlsDir.resolve("rds-ca.metadata.json"), "{\"hostnames\":[\"localhost\",\"172.17.0.5\"]}");
|
||||
|
||||
RdsProxyTlsCertificates migrated = newCertificates();
|
||||
SSLContext migratedContext = migrated.sslContext();
|
||||
migrated.ensureHost("172.17.0.5");
|
||||
assertSame(migratedContext, migrated.sslContext(), "legacy SAN metadata is retained");
|
||||
migrated.ensureHost("host.docker.internal");
|
||||
newCertificates().ensureHost("host.docker.internal");
|
||||
|
||||
assertEquals(sharedCa.caPem(), Files.readString(sharedCa.certificatePath()));
|
||||
assertEquals(sharedCa.caPem(), Files.readString(certFile));
|
||||
assertFalse(Files.exists(keyFile));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@NullSource
|
||||
@ValueSource(strings = {"{", "null", "{\"hostnames\":[null]}", "{\"hostnames\":[\" \"]}", "{\"hostnames\":[]}"})
|
||||
void recoveringMetadataDoesNotReplaceAValidTrustAnchor(String metadata) throws Exception {
|
||||
newCertificates().sslContext();
|
||||
Path tlsDir = tempDir.resolve("tls");
|
||||
Path certFile = tlsDir.resolve(FlociCertificateAuthority.CA_CERT_NAME);
|
||||
String trustedCertificate = Files.readString(certFile);
|
||||
if (metadata != null) {
|
||||
Files.writeString(tlsDir.resolve("rds-ca.metadata.json"), metadata);
|
||||
} else {
|
||||
Files.delete(tlsDir.resolve("rds-ca.metadata.json"));
|
||||
}
|
||||
|
||||
newCertificates().sslContext();
|
||||
|
||||
assertEquals(trustedCertificate, Files.readString(certFile));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void removesAnUnusableLegacyCaKeyWithoutReplacingTheSharedTrustAnchor(boolean malformedKey) throws Exception {
|
||||
FlociCertificateAuthority sharedCa = FlociCertificateAuthority.loadOrCreate(tempDir.resolve("tls"));
|
||||
CertificateGenerator.GeneratedCertificate legacy = new CertificateGenerator().generateCaCertificate("Legacy RDS CA");
|
||||
Path certFile = tempDir.resolve("tls/rds-ca.crt");
|
||||
Path keyFile = tempDir.resolve("tls/rds-ca.key");
|
||||
Files.writeString(certFile, legacy.certificatePem());
|
||||
String replacementKey = malformedKey ? "not a private key"
|
||||
: new CertificateGenerator().generateCaCertificate("Unrelated CA").privateKeyPem();
|
||||
Files.writeString(keyFile, replacementKey);
|
||||
|
||||
newCertificates().sslContext();
|
||||
|
||||
assertEquals(sharedCa.caPem(), Files.readString(sharedCa.certificatePath()));
|
||||
assertEquals(sharedCa.caPem(), Files.readString(certFile));
|
||||
assertFalse(Files.exists(keyFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void renewsAnExpiringLeafWithoutChangingTheCaOrRegisteringANewHost() throws Exception {
|
||||
Clock clock = mock(Clock.class);
|
||||
when(clock.instant()).thenAnswer(invocation -> Instant.now());
|
||||
RdsProxyTlsCertificates certs = newCertificates(clock);
|
||||
certs.ensureHost("host.docker.internal");
|
||||
SSLContext previous = certs.sslContext();
|
||||
Path certFile = tempDir.resolve("tls").resolve(FlociCertificateAuthority.CA_CERT_NAME);
|
||||
String trustedCa = Files.readString(certFile);
|
||||
|
||||
when(clock.instant()).thenReturn(Instant.now().plus(365, ChronoUnit.DAYS).minusSeconds(30));
|
||||
SSLContext renewed = certs.sslContext();
|
||||
|
||||
assertNotSame(previous, renewed);
|
||||
assertSame(renewed, certs.sslContext());
|
||||
certs.ensureHost("host.docker.internal");
|
||||
assertSame(renewed, certs.sslContext(), "renewal retains the previous SAN set");
|
||||
assertEquals(trustedCa, Files.readString(certFile));
|
||||
RdsProxyTlsCertificates reloaded = newCertificates(clock);
|
||||
SSLContext reloadedContext = reloaded.sslContext();
|
||||
reloaded.ensureHost("host.docker.internal");
|
||||
assertSame(reloadedContext, reloaded.sslContext(), "renewal persists the previous SAN set");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLeafCappedAtCaExpiryDoesNotRenewContinuouslyOrRotateTheCa() throws Exception {
|
||||
CertificateGenerator.GeneratedCertificate ca = new CertificateGenerator().generateCaCertificate(
|
||||
"Short-lived CA", Instant.now().plusSeconds(60));
|
||||
Path tlsDir = Files.createDirectories(tempDir.resolve("tls"));
|
||||
Path certFile = tlsDir.resolve(FlociCertificateAuthority.CA_CERT_NAME);
|
||||
Files.writeString(certFile, ca.certificatePem());
|
||||
Files.writeString(tlsDir.resolve(FlociCertificateAuthority.CA_KEY_NAME), ca.privateKeyPem());
|
||||
Clock clock = mock(Clock.class);
|
||||
when(clock.instant()).thenReturn(Instant.now());
|
||||
RdsProxyTlsCertificates certs = newCertificates(clock);
|
||||
SSLContext context = certs.sslContext();
|
||||
|
||||
when(clock.instant()).thenReturn(ca.notAfter().minusSeconds(1));
|
||||
assertSame(context, certs.sslContext());
|
||||
when(clock.instant()).thenReturn(ca.notAfter().plusSeconds(1));
|
||||
assertThrows(IllegalStateException.class, certs::sslContext);
|
||||
assertEquals(ca.certificatePem(), Files.readString(certFile));
|
||||
}
|
||||
|
||||
private RdsProxyTlsCertificates newCertificates() {
|
||||
EmulatorConfig.StorageConfig storage = mock(EmulatorConfig.StorageConfig.class);
|
||||
when(storage.persistentPath()).thenReturn(tempDir.toString());
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.storage()).thenReturn(storage);
|
||||
return new RdsProxyTlsCertificates(config, new CertificateGenerator());
|
||||
return newCertificates(Clock.systemUTC());
|
||||
}
|
||||
|
||||
private RdsProxyTlsCertificates newCertificates(Clock clock) {
|
||||
return new RdsProxyTlsCertificates(FlociCertificateAuthority.loadOrCreate(tempDir.resolve("tls")),
|
||||
new CertificateGenerator(), clock);
|
||||
}
|
||||
}
|
||||
+3
-8
@@ -1,6 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.redshift.proxy;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.FlociCertificateAuthority;
|
||||
import io.github.hectorvent.floci.services.acm.CertificateGenerator;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.rds.proxy.PasswordValidator;
|
||||
@@ -31,7 +31,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class RedshiftAuthProxyTest {
|
||||
|
||||
@@ -218,12 +217,8 @@ class RedshiftAuthProxyTest {
|
||||
}
|
||||
|
||||
private RdsProxyTlsCertificates realTls() {
|
||||
// The real bean generates a self-signed cert on demand; no Docker or network needed.
|
||||
EmulatorConfig.StorageConfig storage = mock(EmulatorConfig.StorageConfig.class);
|
||||
when(storage.persistentPath()).thenReturn(tempDir.toString());
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
when(config.storage()).thenReturn(storage);
|
||||
return new RdsProxyTlsCertificates(config, new CertificateGenerator());
|
||||
return new RdsProxyTlsCertificates(FlociCertificateAuthority.loadOrCreate(tempDir.resolve("tls")),
|
||||
new CertificateGenerator());
|
||||
}
|
||||
|
||||
private static boolean portAccepts(int port) {
|
||||
|
||||
Reference in new issue
Block a user