mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(rds): refresh backend endpoints after container restarts (#6090)
* fix(rds): resolve backend endpoint for each connection * fix(rds): reuse effective network for endpoint refresh * test(rds): preserve postgres readiness failures * fix(rds): report endpoint resolution outages * fix(rds): require runtime identity for proxy backends * refactor(rds): share instance backend identity lookup
This commit is contained in:
1 parent
0750128966
commit
db76a5e5fd
13 files changed
+1079
-171
No files matched your search
+20
-6
@@ -1097,7 +1097,7 @@ public class ContainerLifecycleManager {
|
||||
*/
|
||||
public EndpointInfo resolveEndpoint(String containerId, int containerPort) {
|
||||
InspectContainerResponse inspect = dockerClient.inspectContainerCmd(containerId).exec();
|
||||
return resolveEndpoint(inspect, containerPort);
|
||||
return resolveEndpoint(inspect, containerPort, null, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1109,7 +1109,7 @@ public class ContainerLifecycleManager {
|
||||
*/
|
||||
public EndpointInfo resolveEndpoint(String containerId, int containerPort, String preferredNetwork) {
|
||||
InspectContainerResponse inspect = dockerClient.inspectContainerCmd(containerId).exec();
|
||||
return resolveEndpoint(inspect, containerPort, preferredNetwork);
|
||||
return resolveEndpoint(inspect, containerPort, preferredNetwork, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1492,7 +1492,13 @@ public class ContainerLifecycleManager {
|
||||
return resolveEndpoint(inspect, containerPort, null);
|
||||
}
|
||||
|
||||
private EndpointInfo resolveEndpoint(InspectContainerResponse inspect, int containerPort, String preferredNetwork) {
|
||||
private EndpointInfo resolveEndpoint(InspectContainerResponse inspect, int containerPort,
|
||||
String preferredNetwork) {
|
||||
return resolveEndpoint(inspect, containerPort, preferredNetwork, false);
|
||||
}
|
||||
|
||||
private EndpointInfo resolveEndpoint(InspectContainerResponse inspect, int containerPort,
|
||||
String preferredNetwork, boolean requireReachable) {
|
||||
if (!containerDetector.isRunningInContainer()) {
|
||||
// Native mode: use localhost and the bound host port
|
||||
Map<ExposedPort, Ports.Binding[]> bindings = inspect.getNetworkSettings().getPorts().getBindings();
|
||||
@@ -1502,11 +1508,19 @@ public class ContainerLifecycleManager {
|
||||
int hostPort = Integer.parseInt(binding[0].getHostPortSpec());
|
||||
return new EndpointInfo("localhost", hostPort);
|
||||
}
|
||||
// Fallback to container port
|
||||
if (requireReachable) {
|
||||
throw new IllegalStateException(
|
||||
"Container " + inspect.getId() + " has no published binding for port " + containerPort);
|
||||
}
|
||||
// Creation can still expose an internal-only port to another container.
|
||||
return new EndpointInfo("localhost", containerPort);
|
||||
} else {
|
||||
return new EndpointInfo(resolveReachableHost(inspect, preferredNetwork), containerPort);
|
||||
}
|
||||
String host = resolveReachableHost(inspect, preferredNetwork);
|
||||
if (requireReachable && (host == null || host.isBlank())) {
|
||||
throw new IllegalStateException(
|
||||
"Container " + inspect.getId() + " has no reachable address for port " + containerPort);
|
||||
}
|
||||
return new EndpointInfo(host, containerPort);
|
||||
}
|
||||
|
||||
private String resolveReachableHost(InspectContainerResponse inspect, String preferredNetwork) {
|
||||
|
||||
@@ -903,15 +903,17 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
|
||||
if (!mock && hasBackend(backendHost, backendPort)) {
|
||||
final String instanceRegion = regionFromArn(instance.getDbInstanceArn());
|
||||
InstanceBackendIdentity backendIdentity = instanceBackendIdentity(instance);
|
||||
try {
|
||||
proxyManager.startProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id),
|
||||
backendIdentity.runtimeId(),
|
||||
engine, iamEnabled, proxyPort, backendHost, backendPort,
|
||||
instance.getEndpoint().address(),
|
||||
instance.getMasterUsername(), instance.getMasterPassword(), dbName,
|
||||
(user, pw) -> validateDbPasswordForScope(
|
||||
accountId, instanceRegion, id, user, pw),
|
||||
proxyBinding(engine, instance.getEndpoint().address(), instance.getEndpoint().port(),
|
||||
instanceRegion, accountId, dbUserResourceId(instance)));
|
||||
instanceRegion, accountId, backendIdentity.dbUserResourceId()));
|
||||
} catch (RuntimeException | Error e) {
|
||||
try {
|
||||
deleteInstanceForScope(accountId, effectiveRegion, id);
|
||||
@@ -3572,6 +3574,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
final String accountId = accountIdFromArn(instance.getDbInstanceArn());
|
||||
final String instanceRegion = regionFromArn(instance.getDbInstanceArn());
|
||||
proxyManager.startProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id),
|
||||
instanceBackendIdentity(instance).runtimeId(),
|
||||
instance.getEngine(),
|
||||
instance.isIamDatabaseAuthenticationEnabled(),
|
||||
instance.getProxyPort(), instance.getContainerHost(), instance.getContainerPort(),
|
||||
@@ -3752,6 +3755,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
final String accountId = accountIdFromArn(cluster.getDbClusterArn());
|
||||
final String clusterRegion = regionFromArn(cluster.getDbClusterArn());
|
||||
proxyManager.startProxy(rdsResourceRelayKey(cluster.getDbClusterArn(), id),
|
||||
cluster.getDbClusterArn(),
|
||||
cluster.getEngine(), cluster.isIamDatabaseAuthenticationEnabled(), cluster.getProxyPort(),
|
||||
handle.getHost(), handle.getPort(), cluster.getEndpoint().address(),
|
||||
cluster.getMasterUsername() != null ? cluster.getMasterUsername() : "root",
|
||||
@@ -3820,7 +3824,9 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
? instance.getMasterUsername() : "root";
|
||||
final String accountId = accountIdFromArn(instance.getDbInstanceArn());
|
||||
final String instanceRegion = regionFromArn(instance.getDbInstanceArn());
|
||||
InstanceBackendIdentity backendIdentity = instanceBackendIdentity(instance);
|
||||
proxyManager.startProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id),
|
||||
backendIdentity.runtimeId(),
|
||||
instance.getEngine(),
|
||||
instance.isIamDatabaseAuthenticationEnabled(),
|
||||
instance.getProxyPort(), instance.getContainerHost(), instance.getContainerPort(),
|
||||
@@ -3829,7 +3835,8 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
(user, pw) -> validateDbPasswordForScope(
|
||||
accountId, instanceRegion, id, user, pw),
|
||||
proxyBinding(instance.getEngine(), instance.getEndpoint().address(),
|
||||
instance.getEndpoint().port(), instanceRegion, accountId, dbUserResourceId(instance)));
|
||||
instance.getEndpoint().port(), instanceRegion, accountId,
|
||||
backendIdentity.dbUserResourceId()));
|
||||
} else {
|
||||
// No backing container: created or last rebooted while no daemon was reachable.
|
||||
instance = doEnsureInstanceBackend(id, effectiveRegion);
|
||||
@@ -3840,18 +3847,22 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
return instance;
|
||||
}
|
||||
|
||||
private String dbUserResourceId(DbInstance instance) {
|
||||
private InstanceBackendIdentity instanceBackendIdentity(DbInstance instance) {
|
||||
String clusterId = instance.getDbClusterIdentifier();
|
||||
if (clusterId != null && !clusterId.isBlank()) {
|
||||
DbCluster cluster = findClusterForScope(accountIdFromArn(instance.getDbInstanceArn()),
|
||||
regionFromArn(instance.getDbInstanceArn()), clusterId);
|
||||
if (cluster != null) {
|
||||
return cluster.getDbClusterResourceId();
|
||||
return new InstanceBackendIdentity(
|
||||
cluster.getDbClusterArn(), cluster.getDbClusterResourceId());
|
||||
}
|
||||
}
|
||||
return instance.getDbiResourceId();
|
||||
return new InstanceBackendIdentity(
|
||||
instance.getDbInstanceArn(), instance.getDbiResourceId());
|
||||
}
|
||||
|
||||
private record InstanceBackendIdentity(String runtimeId, String dbUserResourceId) {}
|
||||
|
||||
/**
|
||||
* The binding the proxy validates IAM auth tokens against. As on RDS a token is good for the
|
||||
* endpoint it was generated for (hostname, port and region); MySQL and MariaDB proxies always
|
||||
@@ -3937,6 +3948,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
? instance.getMasterUsername() : "root";
|
||||
final String accountId = accountIdFromArn(instance.getDbInstanceArn());
|
||||
final String instanceRegion = regionFromArn(instance.getDbInstanceArn());
|
||||
InstanceBackendIdentity backendIdentity = instanceBackendIdentity(instance);
|
||||
int relayPort = instance.getProxyPort();
|
||||
DbEndpoint relayEndpoint = instance.getEndpoint();
|
||||
boolean allocatedRelayPort = false;
|
||||
@@ -3947,13 +3959,15 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
relayEndpoint = proxyEndpoint(relayPort);
|
||||
}
|
||||
proxyManager.startProxy(rdsResourceRelayKey(instance.getDbInstanceArn(), id),
|
||||
backendIdentity.runtimeId(),
|
||||
instance.getEngine(), instance.isIamDatabaseAuthenticationEnabled(),
|
||||
relayPort, backendHost, backendPort, relayEndpoint.address(),
|
||||
effectiveMasterUser, instance.getMasterPassword(), instance.getDbName(),
|
||||
(user, pw) -> validateDbPasswordForScope(
|
||||
accountId, instanceRegion, id, user, pw),
|
||||
proxyBinding(instance.getEngine(), relayEndpoint.address(),
|
||||
relayEndpoint.port(), instanceRegion, accountId, dbUserResourceId(instance)));
|
||||
relayEndpoint.port(), instanceRegion, accountId,
|
||||
backendIdentity.dbUserResourceId()));
|
||||
} catch (RuntimeException e) {
|
||||
if (allocatedRelayPort) {
|
||||
releaseProxyPort(relayPort);
|
||||
@@ -4045,6 +4059,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
relayEndpoint = proxyEndpoint(relayPort);
|
||||
}
|
||||
proxyManager.startProxy(rdsResourceRelayKey(cluster.getDbClusterArn(), id),
|
||||
cluster.getDbClusterArn(),
|
||||
cluster.getEngine(), cluster.isIamDatabaseAuthenticationEnabled(),
|
||||
relayPort, started.getHost(), started.getPort(), relayEndpoint.address(),
|
||||
effectiveMasterUser, cluster.getMasterPassword(), cluster.getDatabaseName(),
|
||||
@@ -4481,6 +4496,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
final String accountId = accountIdFromArn(cluster.getDbClusterArn());
|
||||
final String clusterRegion = regionFromArn(cluster.getDbClusterArn());
|
||||
proxyManager.startProxy(rdsResourceRelayKey(cluster.getDbClusterArn(), id),
|
||||
cluster.getDbClusterArn(),
|
||||
engine, iamEnabled, proxyPort,
|
||||
cluster.getContainerHost(), cluster.getContainerPort(),
|
||||
cluster.getEndpoint().address(),
|
||||
@@ -5890,6 +5906,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
|
||||
String backendHost;
|
||||
int backendPort;
|
||||
String backendRuntimeId;
|
||||
DatabaseEngine engine;
|
||||
String masterUser;
|
||||
String masterPassword;
|
||||
@@ -5911,6 +5928,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
effectiveRegion, "DB cluster", clusterId);
|
||||
backendHost = cluster.getContainerHost();
|
||||
backendPort = cluster.getContainerPort();
|
||||
backendRuntimeId = cluster.getDbClusterArn();
|
||||
engine = cluster.getEngine();
|
||||
masterUser = cluster.getMasterUsername();
|
||||
masterPassword = cluster.getMasterPassword();
|
||||
@@ -5931,6 +5949,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
effectiveRegion, "DB instance", instanceId);
|
||||
backendHost = instance.getContainerHost();
|
||||
backendPort = instance.getContainerPort();
|
||||
backendRuntimeId = instanceBackendIdentity(instance).runtimeId();
|
||||
engine = instance.getEngine();
|
||||
masterUser = instance.getMasterUsername();
|
||||
masterPassword = instance.getMasterPassword();
|
||||
@@ -5982,7 +6001,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
final String targetId = target.getRdsResourceId();
|
||||
final boolean isCluster = "TRACKED_CLUSTER".equals(target.getType());
|
||||
final String targetRegion = regionFromArn(proxy.getDbProxyArn());
|
||||
proxyManager.startProxy(dbProxyRelayKey(proxy), engine, proxy.isIamAuth(),
|
||||
proxyManager.startProxy(dbProxyRelayKey(proxy), backendRuntimeId, engine, proxy.isIamAuth(),
|
||||
proxy.getProxyPort(), backendHost, backendPort, proxy.getEndpointHost(),
|
||||
effectiveMasterUser, masterPassword, dbName,
|
||||
(user, pw) -> isCluster
|
||||
@@ -8204,6 +8223,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
}
|
||||
boolean clusterTarget = "TRACKED_CLUSTER".equals(target.getType());
|
||||
DatabaseEngine engine;
|
||||
String backendRuntimeId;
|
||||
String backendHost;
|
||||
int backendPort;
|
||||
String masterUser;
|
||||
@@ -8219,6 +8239,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
validateProxyTargetScope(cluster.getDbClusterArn(), accountId, proxyRegion,
|
||||
"DB cluster", target.getRdsResourceId());
|
||||
engine = cluster.getEngine();
|
||||
backendRuntimeId = cluster.getDbClusterArn();
|
||||
backendHost = cluster.getContainerHost();
|
||||
backendPort = cluster.getContainerPort();
|
||||
masterUser = cluster.getMasterUsername();
|
||||
@@ -8234,6 +8255,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
validateProxyTargetScope(instance.getDbInstanceArn(), accountId, proxyRegion,
|
||||
"DB instance", target.getRdsResourceId());
|
||||
engine = instance.getEngine();
|
||||
backendRuntimeId = instanceBackendIdentity(instance).runtimeId();
|
||||
backendHost = instance.getContainerHost();
|
||||
backendPort = instance.getContainerPort();
|
||||
masterUser = instance.getMasterUsername();
|
||||
@@ -8247,7 +8269,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
validateTargetEngineFamily(proxy, engine);
|
||||
String effectiveMasterUser = masterUser != null ? masterUser : "root";
|
||||
String targetId = target.getRdsResourceId();
|
||||
proxyManager.startProxy(dbProxyRelayKey(proxy), engine, proxy.isIamAuth(), proxy.getProxyPort(),
|
||||
proxyManager.startProxy(dbProxyRelayKey(proxy), backendRuntimeId, engine, proxy.isIamAuth(), proxy.getProxyPort(),
|
||||
backendHost, backendPort, proxy.getEndpointHost(), effectiveMasterUser, masterPassword, dbName,
|
||||
(user, password) -> clusterTarget
|
||||
? validateDbClusterPasswordForScope(
|
||||
@@ -8356,7 +8378,7 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
? cluster.getMasterUsername() : "root";
|
||||
proxyManager.startProxy(rdsResourceRelayKey(
|
||||
cluster.getDbClusterArn(), cluster.getDbClusterIdentifier()),
|
||||
cluster.getEngine(),
|
||||
cluster.getDbClusterArn(), cluster.getEngine(),
|
||||
cluster.isIamDatabaseAuthenticationEnabled(), proxyPort,
|
||||
restoredHandle.getHost(), restoredHandle.getPort(), cluster.getEndpoint().address(),
|
||||
effectiveMasterUser, cluster.getMasterPassword(), cluster.getDatabaseName(),
|
||||
@@ -8497,17 +8519,19 @@ public class RdsService implements Resettable, ResourceProvider {
|
||||
if (hasBackend(backendHost, backendPort)) {
|
||||
String effectiveMasterUser = instance.getMasterUsername() != null
|
||||
? instance.getMasterUsername() : "root";
|
||||
InstanceBackendIdentity backendIdentity = instanceBackendIdentity(instance);
|
||||
proxyManager.startProxy(rdsResourceRelayKey(
|
||||
instance.getDbInstanceArn(), instance.getDbInstanceIdentifier()),
|
||||
instance.getEngine(),
|
||||
backendIdentity.runtimeId(), instance.getEngine(),
|
||||
instance.isIamDatabaseAuthenticationEnabled(), proxyPort,
|
||||
backendHost, backendPort, instance.getEndpoint().address(),
|
||||
effectiveMasterUser, instance.getMasterPassword(), instance.getDbName(),
|
||||
(user, pw) -> validateDbPasswordForScope(
|
||||
accountId, instanceRegion,
|
||||
instance.getDbInstanceIdentifier(), user, pw),
|
||||
proxyBinding(instance.getEngine(), instance.getEndpoint().address(), instance.getEndpoint().port(),
|
||||
instanceRegion, accountId, dbUserResourceId(instance)));
|
||||
proxyBinding(instance.getEngine(), instance.getEndpoint().address(),
|
||||
instance.getEndpoint().port(), instanceRegion, accountId,
|
||||
backendIdentity.dbUserResourceId()));
|
||||
}
|
||||
instance.setStatus(DbInstanceStatus.AVAILABLE);
|
||||
putInstanceForScope(accountId, instanceRegion,
|
||||
|
||||
@@ -28,6 +28,14 @@ public interface RdsBackendGate {
|
||||
return enter(host, port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enters the container owned by {@code runtimeId}. Backends that do not track runtime
|
||||
* identities may use this default implementation.
|
||||
*/
|
||||
default Lease enterRuntime(String runtimeId) throws InterruptedException {
|
||||
return Lease.NONE;
|
||||
}
|
||||
|
||||
/** The backend is being resumed; no lease was acquired and the caller must retry. */
|
||||
class ResumingException extends RuntimeException {
|
||||
}
|
||||
|
||||
+21
@@ -460,6 +460,21 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {
|
||||
return lifecycleManager.probeContainer(containerId);
|
||||
}
|
||||
|
||||
/** Resolves the current endpoint for the container owned by an RDS runtime. */
|
||||
public EndpointInfo resolveBackendEndpoint(String runtimeId, DatabaseEngine engine) {
|
||||
if (runtimeId == null || runtimeId.isBlank()) {
|
||||
throw new IllegalArgumentException("RDS runtime ID is required to resolve its backend endpoint");
|
||||
}
|
||||
RdsContainerHandle handle = activeContainers.get(runtimeId);
|
||||
if (handle == null) {
|
||||
throw new IllegalStateException("RDS runtime " + runtimeId + " has no active container");
|
||||
}
|
||||
String preferredNetwork = containerBuilder.resolveDockerNetwork(
|
||||
config.services().rds().dockerNetwork()).orElse(null);
|
||||
return lifecycleManager.resolveEndpoint(
|
||||
handle.getContainerId(), engine.defaultPort(), preferredNetwork);
|
||||
}
|
||||
|
||||
/** Returns the retained runtime handle used to persist cleanup identity after a failed start. */
|
||||
public RdsContainerHandle getActiveHandle(String runtimeId) {
|
||||
if (runtimeId == null || runtimeId.isBlank()) {
|
||||
@@ -499,6 +514,12 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {
|
||||
controller.configure(enabled ? secondsUntilAutoPause : null, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Lease enterRuntime(String runtimeId) throws InterruptedException {
|
||||
AutoPauseController controller = runtimeId != null ? autoPauses.get(runtimeId) : null;
|
||||
return controller != null ? controller.acquire() : Lease.NONE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Lease enter(String host, int port) throws InterruptedException {
|
||||
return enter(host, port, true);
|
||||
|
||||
@@ -29,6 +29,8 @@ public class RdsAuthProxy {
|
||||
|
||||
private final String instanceId;
|
||||
private final String backendHost;
|
||||
private final BackendEndpointResolver backendEndpointResolver;
|
||||
private final String backendRuntimeId;
|
||||
private final String masterUsername;
|
||||
private volatile String masterPassword;
|
||||
private final String dbName;
|
||||
@@ -98,9 +100,46 @@ public class RdsAuthProxy {
|
||||
int maxConnections, RdsProxyBinding binding,
|
||||
MySqlProtocolHandler.IamUserChecker iamUserChecker,
|
||||
RdsBackendGate backendGate, EndpointKind endpointKind) {
|
||||
this(instanceId, backendHost, backendPort,
|
||||
() -> new BackendEndpoint(backendHost, backendPort),
|
||||
engine, iamEnabled, masterUsername, masterPassword, dbName, sigV4,
|
||||
tlsCertificates, passwordValidator, handshakeTimeoutMillis,
|
||||
backendConnectTimeoutMillis, maxConnections, binding, iamUserChecker,
|
||||
backendGate, endpointKind);
|
||||
}
|
||||
|
||||
public RdsAuthProxy(String instanceId, String backendHost, int backendPort,
|
||||
BackendEndpointResolver backendEndpointResolver,
|
||||
DatabaseEngine engine, boolean iamEnabled,
|
||||
String masterUsername, String masterPassword, String dbName,
|
||||
RdsSigV4Validator sigV4, RdsProxyTlsCertificates tlsCertificates,
|
||||
MasterPasswordCheck passwordValidator,
|
||||
int handshakeTimeoutMillis, int backendConnectTimeoutMillis,
|
||||
int maxConnections, RdsProxyBinding binding,
|
||||
MySqlProtocolHandler.IamUserChecker iamUserChecker,
|
||||
RdsBackendGate backendGate, EndpointKind endpointKind) {
|
||||
this(instanceId, backendHost, backendPort, backendEndpointResolver, null,
|
||||
engine, iamEnabled, masterUsername, masterPassword, dbName, sigV4,
|
||||
tlsCertificates, passwordValidator, handshakeTimeoutMillis,
|
||||
backendConnectTimeoutMillis, maxConnections, binding, iamUserChecker,
|
||||
backendGate, endpointKind);
|
||||
}
|
||||
|
||||
public RdsAuthProxy(String instanceId, String backendHost, int backendPort,
|
||||
BackendEndpointResolver backendEndpointResolver, String backendRuntimeId,
|
||||
DatabaseEngine engine, boolean iamEnabled,
|
||||
String masterUsername, String masterPassword, String dbName,
|
||||
RdsSigV4Validator sigV4, RdsProxyTlsCertificates tlsCertificates,
|
||||
MasterPasswordCheck passwordValidator,
|
||||
int handshakeTimeoutMillis, int backendConnectTimeoutMillis,
|
||||
int maxConnections, RdsProxyBinding binding,
|
||||
MySqlProtocolHandler.IamUserChecker iamUserChecker,
|
||||
RdsBackendGate backendGate, EndpointKind endpointKind) {
|
||||
this.instanceId = instanceId;
|
||||
this.backendHost = backendHost;
|
||||
this.backendPort = backendPort;
|
||||
this.backendEndpointResolver = backendEndpointResolver;
|
||||
this.backendRuntimeId = backendRuntimeId;
|
||||
this.engine = engine;
|
||||
this.iamEnabled = iamEnabled;
|
||||
this.endpointKind = endpointKind;
|
||||
@@ -202,7 +241,9 @@ public class RdsAuthProxy {
|
||||
try {
|
||||
// An auto-paused Aurora backend resumes before the client is served: the client is
|
||||
// held, not refused, and the backend stays awake for as long as the connection lasts.
|
||||
lease = backendGate.enter(backendHost, backendPort);
|
||||
lease = backendRuntimeId != null
|
||||
? backendGate.enterRuntime(backendRuntimeId)
|
||||
: backendGate.enter(backendHost, backendPort);
|
||||
client.setTcpNoDelay(true);
|
||||
|
||||
// RDS only proxy-validates the master user; a non-master user passes through so the
|
||||
@@ -217,8 +258,9 @@ public class RdsAuthProxy {
|
||||
};
|
||||
|
||||
PostgresProtocolHandler.BackendConnector connector = () -> {
|
||||
BackendEndpoint endpoint = backendEndpointResolver.resolve();
|
||||
Socket backendSocket = new Socket();
|
||||
backendSocket.connect(new InetSocketAddress(backendHost, backendPort),
|
||||
backendSocket.connect(new InetSocketAddress(endpoint.host(), endpoint.port()),
|
||||
backendConnectTimeoutMillis);
|
||||
backendSocket.setTcpNoDelay(true);
|
||||
return backendSocket;
|
||||
@@ -282,6 +324,15 @@ public class RdsAuthProxy {
|
||||
}
|
||||
}
|
||||
|
||||
/** Backend address resolved immediately before each outbound connection attempt. */
|
||||
public record BackendEndpoint(String host, int port) {}
|
||||
|
||||
/** Resolves the current backend address for a new outbound connection. */
|
||||
@FunctionalInterface
|
||||
public interface BackendEndpointResolver {
|
||||
BackendEndpoint resolve() throws IOException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for master-password validation, implemented by RdsService. Returns true when the
|
||||
* supplied master credentials are current. Non-master users are never asked here: the backend
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package io.github.hectorvent.floci.services.rds.proxy;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsBackendGate;
|
||||
import io.github.hectorvent.floci.services.rds.container.RdsContainerManager;
|
||||
import io.github.hectorvent.floci.services.rds.model.DatabaseEngine;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -9,6 +11,7 @@ import org.jboss.logging.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* Registry of all active RDS auth proxies. One proxy per DB instance or cluster.
|
||||
@@ -22,46 +25,78 @@ public class RdsProxyManager {
|
||||
private final RdsProxyTlsCertificates tlsCertificates;
|
||||
private final EmulatorConfig config;
|
||||
private final RdsBackendGate backendGate;
|
||||
private final RdsContainerManager containerManager;
|
||||
private final ConcurrentHashMap<String, RdsAuthProxy> proxies = new ConcurrentHashMap<>();
|
||||
|
||||
@Inject
|
||||
public RdsProxyManager(RdsSigV4Validator sigV4Validator, RdsProxyTlsCertificates tlsCertificates,
|
||||
EmulatorConfig config, RdsBackendGate backendGate) {
|
||||
EmulatorConfig config, RdsBackendGate backendGate,
|
||||
RdsContainerManager containerManager) {
|
||||
this.sigV4Validator = sigV4Validator;
|
||||
this.tlsCertificates = tlsCertificates;
|
||||
this.config = config;
|
||||
this.backendGate = backendGate;
|
||||
this.containerManager = containerManager;
|
||||
}
|
||||
|
||||
RdsProxyManager(RdsSigV4Validator sigV4Validator, RdsProxyTlsCertificates tlsCertificates,
|
||||
EmulatorConfig config) {
|
||||
this(sigV4Validator, tlsCertificates, config, RdsBackendGate.OPEN);
|
||||
}
|
||||
|
||||
public synchronized void startProxy(String instanceId, DatabaseEngine engine, boolean iamEnabled,
|
||||
public synchronized void startProxy(String instanceId, String backendRuntimeId,
|
||||
DatabaseEngine engine, boolean iamEnabled,
|
||||
int proxyPort, String backendHost, int backendPort,
|
||||
String advertisedHost,
|
||||
String masterUsername, String masterPassword, String dbName,
|
||||
RdsAuthProxy.MasterPasswordCheck passwordValidator,
|
||||
RdsProxyBinding binding) {
|
||||
startProxy(instanceId, engine, iamEnabled, proxyPort, backendHost, backendPort,
|
||||
advertisedHost, masterUsername, masterPassword, dbName, passwordValidator,
|
||||
binding, RdsAuthProxy.EndpointKind.DATABASE);
|
||||
startProxy(instanceId, backendRuntimeId, engine, iamEnabled, proxyPort, backendHost,
|
||||
backendPort, advertisedHost, masterUsername, masterPassword, dbName,
|
||||
passwordValidator, binding, RdsAuthProxy.EndpointKind.DATABASE);
|
||||
}
|
||||
|
||||
public synchronized void startProxy(String instanceId, DatabaseEngine engine, boolean iamEnabled,
|
||||
public synchronized void startProxy(String instanceId, String backendRuntimeId,
|
||||
DatabaseEngine engine, boolean iamEnabled,
|
||||
int proxyPort, String backendHost, int backendPort,
|
||||
String advertisedHost,
|
||||
String masterUsername, String masterPassword, String dbName,
|
||||
RdsAuthProxy.MasterPasswordCheck passwordValidator,
|
||||
RdsProxyBinding binding, RdsAuthProxy.EndpointKind endpointKind) {
|
||||
if (containerManager == null) {
|
||||
throw new IllegalStateException("RDS container manager is required for dynamic backend resolution");
|
||||
}
|
||||
AtomicBoolean endpointFailureLogged = new AtomicBoolean();
|
||||
RdsAuthProxy.BackendEndpointResolver endpointResolver = () -> {
|
||||
try {
|
||||
EndpointInfo endpoint = containerManager.resolveBackendEndpoint(backendRuntimeId, engine);
|
||||
endpointFailureLogged.set(false);
|
||||
return new RdsAuthProxy.BackendEndpoint(endpoint.host(), endpoint.port());
|
||||
} catch (RuntimeException e) {
|
||||
if (endpointFailureLogged.compareAndSet(false, true)) {
|
||||
LOG.warnv(e, "Could not resolve backend endpoint for RDS runtime {0}",
|
||||
backendRuntimeId);
|
||||
}
|
||||
throw new IOException(
|
||||
"Could not resolve backend endpoint for RDS runtime " + backendRuntimeId, e);
|
||||
}
|
||||
};
|
||||
startProxy(instanceId, backendRuntimeId, engine, iamEnabled, proxyPort,
|
||||
backendHost, backendPort, endpointResolver, advertisedHost, masterUsername,
|
||||
masterPassword, dbName, passwordValidator, binding, endpointKind);
|
||||
}
|
||||
|
||||
private void startProxy(String instanceId, String backendRuntimeId,
|
||||
DatabaseEngine engine, boolean iamEnabled,
|
||||
int proxyPort, String backendHost, int backendPort,
|
||||
RdsAuthProxy.BackendEndpointResolver endpointResolver,
|
||||
String advertisedHost,
|
||||
String masterUsername, String masterPassword, String dbName,
|
||||
RdsAuthProxy.MasterPasswordCheck passwordValidator,
|
||||
RdsProxyBinding binding, RdsAuthProxy.EndpointKind endpointKind) {
|
||||
tlsCertificates.ensureHost(advertisedHost);
|
||||
EmulatorConfig.RdsServiceConfig rdsConfig = config.services().rds();
|
||||
RdsAuthProxy proxy = new RdsAuthProxy(
|
||||
instanceId, backendHost, backendPort, engine, iamEnabled,
|
||||
masterUsername, masterPassword, dbName, sigV4Validator, tlsCertificates, passwordValidator,
|
||||
rdsConfig.proxyHandshakeTimeoutMillis(), rdsConfig.proxyBackendConnectTimeoutMillis(),
|
||||
rdsConfig.proxyMaxConnections(), binding, username -> binding != null, backendGate, endpointKind);
|
||||
instanceId, backendHost, backendPort, endpointResolver, backendRuntimeId,
|
||||
engine, iamEnabled, masterUsername, masterPassword, dbName, sigV4Validator,
|
||||
tlsCertificates, passwordValidator, rdsConfig.proxyHandshakeTimeoutMillis(),
|
||||
rdsConfig.proxyBackendConnectTimeoutMillis(), rdsConfig.proxyMaxConnections(),
|
||||
binding, username -> binding != null, backendGate, endpointKind);
|
||||
try {
|
||||
proxy.start(proxyPort);
|
||||
} catch (IOException e) {
|
||||
|
||||
Reference in new issue
Block a user