mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(redshift): manage master password with secrets manager (#3871)
* feat(redshift): manage master password with secrets manager * fix(redshift): align managed password contract * fix(redshift): expose managed password secret arn
This commit is contained in:
1 parent
004d562f34
commit
023af51627
9 files changed
+236
-23
No files matched your search
+18
-11
@@ -95,14 +95,11 @@ public class RedshiftClusterCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private void provisionCluster(StackResource r, JsonNode props, ProvisionContext ctx,
|
||||
Map<String, String> attributesBefore) {
|
||||
if (Boolean.parseBoolean(ctx.resolveOptional(props, "ManageMasterPassword"))) {
|
||||
throw new AwsException("ValidationException",
|
||||
"ManageMasterPassword is not emulated by Floci; set MasterUserPassword instead", 400);
|
||||
}
|
||||
boolean manageMasterPassword = Boolean.parseBoolean(ctx.resolveOptional(props, "ManageMasterPassword"));
|
||||
String nodeType = ctx.resolveOptional(props, "NodeType");
|
||||
String masterUsername = ctx.resolveOptional(props, "MasterUsername");
|
||||
String masterUserPassword = ctx.resolveOptional(props, "MasterUserPassword");
|
||||
if (masterUserPassword == null || masterUserPassword.isBlank()) {
|
||||
if (!manageMasterPassword && (masterUserPassword == null || masterUserPassword.isBlank())) {
|
||||
throw new AwsException("ValidationException",
|
||||
"MasterUserPassword is required unless ManageMasterPassword is set", 400);
|
||||
}
|
||||
@@ -135,12 +132,19 @@ public class RedshiftClusterCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
// provision() is the update path too. A same-id cluster already on file is reconciled;
|
||||
// a derived id that differs from the prior physical id is a replacement handled via ReplacementCleanup.
|
||||
Cluster cluster = ctx.reusesPriorEntity(id)
|
||||
? redshiftService.modifyCluster(id, nodeType, numberOfNodes(props, ctx),
|
||||
masterUserPassword, ctx.resolveOptional(props, "ClusterParameterGroupName"),
|
||||
securityGroups)
|
||||
: redshiftService.createCluster(id, nodeType, masterUsername, masterUserPassword,
|
||||
subnetGroup, securityGroups);
|
||||
Cluster cluster;
|
||||
if (ctx.reusesPriorEntity(id)) {
|
||||
cluster = redshiftService.modifyCluster(id, nodeType, numberOfNodes(props, ctx),
|
||||
masterUserPassword, ctx.resolveOptional(props, "ClusterParameterGroupName"),
|
||||
securityGroups);
|
||||
} else if (manageMasterPassword) {
|
||||
String kmsKeyId = props.path("MasterPasswordSecretKmsKeyId").asText(null);
|
||||
cluster = redshiftService.createClusterWithManagedMasterPassword(id, nodeType, masterUsername,
|
||||
subnetGroup, securityGroups, List.of(), kmsKeyId, ctx.region());
|
||||
} else {
|
||||
cluster = redshiftService.createCluster(id, nodeType, masterUsername, masterUserPassword,
|
||||
subnetGroup, securityGroups);
|
||||
}
|
||||
|
||||
Map<String, String> tags = ctx.resolveTags(props, "Tags");
|
||||
if (!tags.isEmpty()) {
|
||||
@@ -155,6 +159,9 @@ public class RedshiftClusterCfnProvisioner implements CfnResourceProvisioner {
|
||||
r.getAttributes().put("Endpoint.Address", cluster.getEndpoint().getAddress());
|
||||
r.getAttributes().put("Endpoint.Port", String.valueOf(cluster.getEndpoint().getPort()));
|
||||
}
|
||||
if (cluster.getMasterPasswordSecretArn() != null) {
|
||||
r.getAttributes().put("MasterPasswordSecretArn", cluster.getMasterPasswordSecretArn());
|
||||
}
|
||||
r.getAttributes().put("ClusterNamespaceArn", namespaceArn(ctx, id));
|
||||
}
|
||||
|
||||
|
||||
+12
-1
@@ -65,11 +65,17 @@ public class RedshiftQueryHandler {
|
||||
String nodeType = params.getFirst("NodeType");
|
||||
String masterUsername = params.getFirst("MasterUsername");
|
||||
String masterUserPassword = params.getFirst("MasterUserPassword");
|
||||
boolean manageMasterPassword = Boolean.parseBoolean(params.getFirst("ManageMasterPassword"));
|
||||
String clusterSubnetGroupName = params.getFirst("ClusterSubnetGroupName");
|
||||
List<String> vpcSecurityGroupIds = memberList(params, "VpcSecurityGroupIds");
|
||||
List<String> iamRoleArns = memberList(params, "IamRoles");
|
||||
|
||||
Cluster cluster = iamRoleArns.isEmpty()
|
||||
String region = regionResolver.resolveRegionFromAuth(authorizationHeader);
|
||||
Cluster cluster = manageMasterPassword
|
||||
? service.createClusterWithManagedMasterPassword(identifier, nodeType, masterUsername,
|
||||
clusterSubnetGroupName, vpcSecurityGroupIds, iamRoleArns,
|
||||
params.getFirst("MasterPasswordSecretKmsKeyId"), region)
|
||||
: iamRoleArns.isEmpty()
|
||||
? service.createCluster(identifier, nodeType, masterUsername, masterUserPassword,
|
||||
clusterSubnetGroupName, vpcSecurityGroupIds)
|
||||
: service.createCluster(identifier, nodeType, masterUsername, masterUserPassword,
|
||||
@@ -618,6 +624,11 @@ public class RedshiftQueryHandler {
|
||||
.elem("AvailabilityZoneRelocationStatus", "disabled")
|
||||
.elem("ClusterSubnetGroupName", cluster.getClusterSubnetGroupName());
|
||||
|
||||
if (cluster.getMasterPasswordSecretArn() != null) {
|
||||
builder.elem("MasterPasswordSecretArn", cluster.getMasterPasswordSecretArn())
|
||||
.elem("MasterPasswordSecretKmsKeyId", cluster.getMasterPasswordSecretKmsKeyId());
|
||||
}
|
||||
|
||||
if (cluster.getVpcSecurityGroupIds() != null && !cluster.getVpcSecurityGroupIds().isEmpty()) {
|
||||
builder.start("VpcSecurityGroups");
|
||||
for (String sgId : cluster.getVpcSecurityGroupIds()) {
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package io.github.hectorvent.floci.services.redshift;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.services.rds.proxy.PasswordValidator;
|
||||
import io.github.hectorvent.floci.services.redshift.proxy.RedshiftProxyManager;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.rds.proxy.PasswordValidator;
|
||||
import io.github.hectorvent.floci.services.redshift.container.RedshiftContainerHandle;
|
||||
import io.github.hectorvent.floci.services.redshift.container.RedshiftContainerManager;
|
||||
import io.github.hectorvent.floci.services.redshift.model.Integration;
|
||||
@@ -22,6 +22,10 @@ import io.github.hectorvent.floci.services.redshift.model.ClusterSubnetGroup;
|
||||
import io.github.hectorvent.floci.services.redshift.model.Endpoint;
|
||||
import io.github.hectorvent.floci.services.redshift.model.Parameter;
|
||||
import io.github.hectorvent.floci.services.redshift.model.Snapshot;
|
||||
import io.github.hectorvent.floci.services.redshift.proxy.RedshiftProxyManager;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.RandomPasswordGenerator;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -68,6 +72,8 @@ public class RedshiftService {
|
||||
private final RedshiftProxyManager proxyManager;
|
||||
private final DockerHostResolver dockerHostResolver;
|
||||
private final RedshiftCredentialBroker credentialBroker;
|
||||
private final SecretsManagerService secretsManagerService;
|
||||
private final ObjectMapper objectMapper;
|
||||
// Proxy ports currently handed out, so allocateProxyPort never double-assigns within this JVM.
|
||||
private final Set<Integer> usedPorts = ConcurrentHashMap.newKeySet();
|
||||
|
||||
@@ -75,7 +81,8 @@ public class RedshiftService {
|
||||
public RedshiftService(StorageFactory storageFactory, RedshiftContainerManager containerManager,
|
||||
EmulatorConfig config, RegionResolver regionResolver,
|
||||
RedshiftProxyManager proxyManager, DockerHostResolver dockerHostResolver,
|
||||
RedshiftCredentialBroker credentialBroker) {
|
||||
RedshiftCredentialBroker credentialBroker,
|
||||
SecretsManagerService secretsManagerService, ObjectMapper objectMapper) {
|
||||
this.clusters = storageFactory.create("redshift", "redshift-clusters.json", new TypeReference<Map<String, Cluster>>() {});
|
||||
this.snapshots = storageFactory.create("redshift", "redshift-snapshots.json", new TypeReference<Map<String, Snapshot>>() {});
|
||||
this.parameterGroups = storageFactory.create("redshift", "redshift-parameter-groups.json", new TypeReference<Map<String, ClusterParameterGroup>>() {});
|
||||
@@ -87,6 +94,16 @@ public class RedshiftService {
|
||||
this.proxyManager = proxyManager;
|
||||
this.dockerHostResolver = dockerHostResolver;
|
||||
this.credentialBroker = credentialBroker;
|
||||
this.secretsManagerService = secretsManagerService;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
RedshiftService(StorageFactory storageFactory, RedshiftContainerManager containerManager,
|
||||
EmulatorConfig config, RegionResolver regionResolver,
|
||||
RedshiftProxyManager proxyManager, DockerHostResolver dockerHostResolver,
|
||||
RedshiftCredentialBroker credentialBroker) {
|
||||
this(storageFactory, containerManager, config, regionResolver, proxyManager, dockerHostResolver,
|
||||
credentialBroker, null, new ObjectMapper());
|
||||
}
|
||||
|
||||
// Recreate Docker containers for persisted clusters on app restart (across every account, not just default)
|
||||
@@ -226,6 +243,53 @@ public class RedshiftService {
|
||||
return cluster;
|
||||
}
|
||||
|
||||
public synchronized Cluster createClusterWithManagedMasterPassword(
|
||||
String identifier, String nodeType, String username, String clusterSubnetGroupName,
|
||||
List<String> vpcSecurityGroupIds, List<String> iamRoleArns,
|
||||
String kmsKeyId, String region) {
|
||||
if (secretsManagerService == null) {
|
||||
throw new AwsException("InternalFailure", "Secrets Manager is unavailable", 500);
|
||||
}
|
||||
String password = RandomPasswordGenerator.generate(objectMapper.createObjectNode());
|
||||
Cluster cluster = createCluster(identifier, nodeType, username, password,
|
||||
clusterSubnetGroupName, vpcSecurityGroupIds, iamRoleArns);
|
||||
String secretName = "redshift/" + identifier;
|
||||
String secretString = managedMasterSecret(cluster, password);
|
||||
Secret secret;
|
||||
try {
|
||||
secret = secretsManagerService.createSecret(secretName, secretString, null,
|
||||
"Managed master user secret for Redshift cluster " + identifier,
|
||||
kmsKeyId, List.of(), "redshift", region);
|
||||
} catch (RuntimeException e) {
|
||||
rollbackManagedMasterPasswordCluster(cluster);
|
||||
throw e;
|
||||
}
|
||||
cluster.setMasterPasswordSecretArn(secret.getArn());
|
||||
cluster.setMasterPasswordSecretKmsKeyId(kmsKeyId);
|
||||
clusters.put(identifier, cluster);
|
||||
clusters.flush();
|
||||
return cluster;
|
||||
}
|
||||
|
||||
private void rollbackManagedMasterPasswordCluster(Cluster cluster) {
|
||||
boolean proxyStopped = stopProxyAndReleasePortSafely(
|
||||
cluster.getClusterIdentifier(), cluster.getProxyPort());
|
||||
try {
|
||||
containerManager.stop(clusters.accountId(), cluster.getClusterIdentifier());
|
||||
} catch (Exception e) {
|
||||
LOG.warnv(e, "Failed to stop managed-password cluster container {0} during rollback",
|
||||
cluster.getClusterIdentifier());
|
||||
}
|
||||
if (proxyStopped) {
|
||||
clusters.delete(cluster.getClusterIdentifier());
|
||||
credentialBroker.revokeCluster(clusters.accountId(), cluster.getClusterIdentifier());
|
||||
} else {
|
||||
cluster.setClusterStatus("failed");
|
||||
clusters.put(cluster.getClusterIdentifier(), cluster);
|
||||
}
|
||||
clusters.flush();
|
||||
}
|
||||
|
||||
// ── Zero-ETL integrations ────────────────────────────────────
|
||||
//
|
||||
// Metadata only: no data is replicated from the source. The shape and the lower case status
|
||||
@@ -425,6 +489,15 @@ public class RedshiftService {
|
||||
// identifier does not accept them as master-equivalent.
|
||||
credentialBroker.revokeCluster(clusters.accountId(), identifier);
|
||||
|
||||
if (cluster.getMasterPasswordSecretArn() != null && secretsManagerService != null) {
|
||||
try {
|
||||
secretsManagerService.deleteSecret(cluster.getMasterPasswordSecretArn(), null, true,
|
||||
regionResolver.getRegion());
|
||||
} catch (AwsException e) {
|
||||
LOG.warnv(e, "Failed to remove managed master secret for cluster {0}", identifier);
|
||||
}
|
||||
}
|
||||
|
||||
cluster.setClusterStatus("deleting");
|
||||
return cluster;
|
||||
}
|
||||
@@ -444,6 +517,7 @@ public class RedshiftService {
|
||||
// Keep the proxy's password check in sync so new connections use the new secret.
|
||||
proxyManager.updateMasterPassword(
|
||||
relayKey(clusters.accountId(), clusterIdentifier), masterUserPassword);
|
||||
updateManagedMasterSecret(cluster, masterUserPassword);
|
||||
}
|
||||
|
||||
// NodeType only updates metadata — it does not resize the underlying Postgres container
|
||||
@@ -464,6 +538,30 @@ public class RedshiftService {
|
||||
return cluster;
|
||||
}
|
||||
|
||||
private void updateManagedMasterSecret(Cluster cluster, String password) {
|
||||
if (cluster.getMasterPasswordSecretArn() == null || secretsManagerService == null) {
|
||||
return;
|
||||
}
|
||||
secretsManagerService.putSecretValue(cluster.getMasterPasswordSecretArn(),
|
||||
managedMasterSecret(cluster, password), null, null, regionResolver.getRegion(),
|
||||
List.of("AWSCURRENT"));
|
||||
}
|
||||
|
||||
private String managedMasterSecret(Cluster cluster, String password) {
|
||||
try {
|
||||
return objectMapper.createObjectNode()
|
||||
.put("engine", "redshift")
|
||||
.put("username", cluster.getMasterUsername())
|
||||
.put("password", password)
|
||||
.put("host", cluster.getEndpoint() == null ? "" : cluster.getEndpoint().getAddress())
|
||||
.put("port", cluster.getEndpoint() == null ? 0 : cluster.getEndpoint().getPort())
|
||||
.put("dbname", CLUSTER_DB_NAME)
|
||||
.toString();
|
||||
} catch (RuntimeException e) {
|
||||
throw new AwsException("InternalFailure", "Failed to encode managed master secret", 500);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized Cluster rebootCluster(String clusterIdentifier) {
|
||||
Cluster cluster = clusters.get(clusterIdentifier)
|
||||
.orElseThrow(() -> new AwsException("ClusterNotFound", "Cluster " + clusterIdentifier + " not found", 404));
|
||||
|
||||
@@ -12,6 +12,8 @@ public class Cluster {
|
||||
private String nodeType;
|
||||
private String masterUsername;
|
||||
private String masterPassword;
|
||||
private String masterPasswordSecretArn;
|
||||
private String masterPasswordSecretKmsKeyId;
|
||||
private String clusterStatus;
|
||||
private Endpoint endpoint;
|
||||
private String clusterSubnetGroupName;
|
||||
@@ -27,6 +29,10 @@ public class Cluster {
|
||||
public void setMasterUsername(String masterUsername) { this.masterUsername = masterUsername; }
|
||||
public String getMasterPassword() { return masterPassword; }
|
||||
public void setMasterPassword(String masterPassword) { this.masterPassword = masterPassword; }
|
||||
public String getMasterPasswordSecretArn() { return masterPasswordSecretArn; }
|
||||
public void setMasterPasswordSecretArn(String masterPasswordSecretArn) { this.masterPasswordSecretArn = masterPasswordSecretArn; }
|
||||
public String getMasterPasswordSecretKmsKeyId() { return masterPasswordSecretKmsKeyId; }
|
||||
public void setMasterPasswordSecretKmsKeyId(String masterPasswordSecretKmsKeyId) { this.masterPasswordSecretKmsKeyId = masterPasswordSecretKmsKeyId; }
|
||||
public String getClusterStatus() { return clusterStatus; }
|
||||
public void setClusterStatus(String clusterStatus) { this.clusterStatus = clusterStatus; }
|
||||
public Endpoint getEndpoint() { return endpoint; }
|
||||
|
||||
Reference in new issue
Block a user