fix(msk): isolate MSK resources by account and region (#3353)

* fix: isolate MSK clusters by region

* fix(msk): scope broker resources by account and region

* fix(msk): preserve legacy cluster data during identity migration

* fix(msk): restrict legacy storage fallback to old clusters

* ci: retrigger PR checks after unrelated test flake
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-10 14:42:12 -04:00
1 parent 5198cf29ec
commit 970cd301d1
5 files changed
+185 -23

No files matched your search

@@ -118,18 +118,21 @@ public class MskService implements ResourceProvider {
public MskCluster createCluster(CreateClusterRequest request) {
validateCreateRequest(request);
String clusterName = request.getClusterName();
if (storage.scan(k -> true).stream().anyMatch(c -> c.getClusterName().equals(clusterName))) {
if (storage.scan(k -> true).stream().anyMatch(c -> isCurrentRegion(c)
&& c.getClusterName().equals(clusterName))) {
throw new AwsException("ConflictException", "Cluster already exists: " + clusterName, 409);
}
String accountId = regionResolver.getAccountId();
String clusterArn = AwsArnUtils.Arn.of("kafka", config.defaultRegion(), accountId, "cluster/" + clusterName + "/" + java.util.UUID.randomUUID()).toString();
String clusterArn = AwsArnUtils.Arn.of("kafka", regionResolver.getRegion(), accountId,
"cluster/" + clusterName + "/" + java.util.UUID.randomUUID()).toString();
String kafkaVersion = request.getKafkaVersion();
String resolvedKafkaVersion = (kafkaVersion == null || kafkaVersion.isBlank()) ? DEFAULT_KAFKA_VERSION : kafkaVersion;
MskCluster cluster = new MskCluster(clusterArn, clusterName, resolvedKafkaVersion);
cluster.setClusterType(PROVISIONED_CLUSTER_TYPE);
cluster.setAccountId(accountId);
cluster.setResourceRegion(regionResolver.getRegion());
cluster.setVolumeId(String.format("%06x", new SecureRandom().nextInt(0xFFFFFF)));
if (request.getNumberOfBrokerNodes() != null) {
@@ -326,12 +329,13 @@ public class MskService implements ResourceProvider {
throw badRequest("clusterName",
"clusterName must be between 1 and " + MAX_CLUSTER_NAME_LENGTH + " characters.");
}
if (storage.scan(k -> true).stream().anyMatch(c -> c.getClusterName().equals(clusterName))) {
if (storage.scan(k -> true).stream().anyMatch(c -> isCurrentRegion(c)
&& c.getClusterName().equals(clusterName))) {
throw new AwsException("ConflictException", "Cluster already exists: " + clusterName, 409);
}
String accountId = regionResolver.getAccountId();
String clusterArn = AwsArnUtils.Arn.of("kafka", config.defaultRegion(), accountId,
String clusterArn = AwsArnUtils.Arn.of("kafka", regionResolver.getRegion(), accountId,
"cluster/" + clusterName + "/" + UUID.randomUUID()).toString();
MskCluster cluster = new MskCluster(clusterArn, clusterName, DEFAULT_KAFKA_VERSION);
@@ -339,6 +343,7 @@ public class MskService implements ResourceProvider {
cluster.setServerless(request.getServerless());
cluster.setTags(request.getTags());
cluster.setAccountId(accountId);
cluster.setResourceRegion(regionResolver.getRegion());
cluster.setVolumeId(String.format("%06x", new SecureRandom().nextInt(0xFFFFFF)));
// Provisioned-only members must not surface on a serverless cluster.
@@ -377,21 +382,21 @@ public class MskService implements ResourceProvider {
/** ListClusters for the v1 API, which likewise cannot represent serverless clusters. */
public List<MskCluster> listProvisionedClusters() {
return storage.scan(k -> true).stream().filter(c -> !isServerless(c)).toList();
return storage.scan(k -> true).stream().filter(this::isCurrentRegion)
.filter(c -> !isServerless(c)).toList();
}
public MskCluster describeCluster(String clusterArn) {
return storage.get(clusterArn)
return storage.get(clusterArn).filter(this::isCurrentRegion)
.orElseThrow(() -> new AwsException("NotFoundException", "Cluster not found: " + clusterArn, 404));
}
public List<MskCluster> listClusters() {
return storage.scan(k -> true);
return storage.scan(k -> true).stream().filter(this::isCurrentRegion).toList();
}
public void deleteCluster(String clusterArn) {
MskCluster cluster = storage.get(clusterArn)
.orElseThrow(() -> new AwsException("NotFoundException", "Cluster not found: " + clusterArn, 404));
MskCluster cluster = describeCluster(clusterArn);
cluster.setState(ClusterState.DELETING);
if (!config.services().msk().mock()) {
@@ -541,7 +546,7 @@ public class MskService implements ResourceProvider {
// DescribeConfiguration, whose 400 is a deliberate terraform-provider contract.
public Map<String, String> listTagsForResource(String arn) {
MskCluster cluster = storage.get(arn).orElse(null);
MskCluster cluster = storage.get(arn).filter(this::isCurrentRegion).orElse(null);
if (cluster != null) {
return cluster.getTags() != null ? cluster.getTags() : Map.of();
}
@@ -553,7 +558,7 @@ public class MskService implements ResourceProvider {
}
public void tagResource(String arn, Map<String, String> tags) {
MskCluster cluster = storage.get(arn).orElse(null);
MskCluster cluster = storage.get(arn).filter(this::isCurrentRegion).orElse(null);
if (cluster != null) {
cluster.setTags(merged(cluster.getTags(), tags));
storage.put(arn, cluster);
@@ -569,7 +574,7 @@ public class MskService implements ResourceProvider {
}
public void untagResource(String arn, List<String> tagKeys) {
MskCluster cluster = storage.get(arn).orElse(null);
MskCluster cluster = storage.get(arn).filter(this::isCurrentRegion).orElse(null);
if (cluster != null) {
cluster.setTags(without(cluster.getTags(), tagKeys));
storage.put(arn, cluster);
@@ -643,7 +648,7 @@ public class MskService implements ResourceProvider {
@Override
public List<ExplorerResource> getResources() {
List<ExplorerResource> resources = new ArrayList<>();
for (MskCluster cluster : storage.scan(k -> true)) {
for (MskCluster cluster : storage.scan(k -> true).stream().filter(this::isCurrentRegion).toList()) {
String arn = cluster.getClusterArn();
if (arn == null) {
continue;
@@ -662,4 +667,14 @@ public class MskService implements ResourceProvider {
public Set<SupportedResourceType> getSupportedResourceTypes() {
return Set.of(new SupportedResourceType("kafka:cluster", "kafka", true));
}
private boolean isCurrentRegion(MskCluster cluster) {
if (cluster.getResourceRegion() != null) {
return regionResolver.getRegion().equals(cluster.getResourceRegion());
}
// Older records have no request-region marker and their ARN always used the configured
// default. Keep them visible until they are recreated so an upgrade does not hide or
// strand persisted clusters whose original request region was not stored.
return true;
}
}
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.msk;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
@@ -19,6 +20,7 @@ import org.jboss.logging.Logger;
import java.io.Closeable;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
@@ -149,8 +151,9 @@ public class RedpandaManager {
.withDockerNetwork(config.services().dockerNetwork())
.withLogRotation()
.withLabels(ContainerStorageHelper.resourceIdentityLabels(
"msk", cluster.getClusterName(), regionResolver.getAccountId(),
regionResolver.getDefaultRegion()));
"msk", cluster.getClusterName(),
AwsArnUtils.accountOrDefault(cluster.getClusterArn(), regionResolver.getAccountId()),
AwsArnUtils.regionOrDefault(cluster.getClusterArn(), regionResolver.getDefaultRegion())));
if (!containerDetector.isRunningInContainer()) {
specBuilder.withPortBinding(KAFKA_PORT, kafkaHostPort).withDynamicPort(ADMIN_PORT);
@@ -165,8 +168,7 @@ public class RedpandaManager {
"/var/lib/redpanda/data");
} else {
// Legacy host-path mode: host-persistent-path is an absolute path
String hostDataPath = ContainerStorageHelper.hostResourcePath(config, "msk", cluster.getClusterName())
.toAbsolutePath().toString();
String hostDataPath = legacyCompatibleHostPath(cluster).toAbsolutePath().toString();
if (!containerDetector.isRunningInContainer()) {
ContainerStorageHelper.ensureHostDir(hostDataPath);
}
@@ -200,12 +202,13 @@ public class RedpandaManager {
: info.containerId();
String logGroup = "/aws/msk/cluster/" + cluster.getClusterName();
String logStream = logStreamer.generateLogStreamName(shortId);
String region = regionResolver.getDefaultRegion();
String region = AwsArnUtils.regionOrDefault(cluster.getClusterArn(), regionResolver.getDefaultRegion());
Closeable logHandle = logStreamer.attach(
info.containerId(), logGroup, logStream, region, "msk:" + cluster.getClusterName());
String account = AwsArnUtils.accountOrDefault(cluster.getClusterArn(), regionResolver.getDefaultAccountId());
Closeable logHandle = logStreamer.attachForAccount(
account, info.containerId(), logGroup, logStream, region, "msk:" + cluster.getClusterName());
if (logHandle != null) {
logStreams.put(cluster.getClusterName(), logHandle);
logStreams.put(clusterIdentityKey(cluster), logHandle);
}
}
@@ -249,7 +252,7 @@ public class RedpandaManager {
}
// Close log stream
Closeable logHandle = logStreams.remove(cluster.getClusterName());
Closeable logHandle = logStreams.remove(clusterIdentityKey(cluster));
lifecycleManager.stopAndRemove(cluster.getContainerId(), logHandle);
LOG.infov("Redpanda container {0} stopped and removed", cluster.getContainerId());
@@ -280,4 +283,23 @@ public class RedpandaManager {
ContainerStorageHelper.removeStorage(config, lifecycleManager,
"msk", cluster.getVolumeId(), cluster.getClusterName());
}
private String clusterIdentityKey(MskCluster cluster) {
return cluster.getClusterArn() != null ? cluster.getClusterArn() : cluster.getClusterName();
}
private String clusterStorageId(MskCluster cluster) {
String account = AwsArnUtils.accountOrDefault(cluster.getClusterArn(), regionResolver.getDefaultAccountId());
String region = AwsArnUtils.regionOrDefault(cluster.getClusterArn(), regionResolver.getDefaultRegion());
return ContainerStorageHelper.dockerName(config,
"msk-" + account + "-" + region + "-" + cluster.getClusterName());
}
private Path legacyCompatibleHostPath(MskCluster cluster) {
Path scopedPath = ContainerStorageHelper.hostResourcePath(config, "msk", clusterStorageId(cluster));
Path legacyPath = ContainerStorageHelper.hostResourcePath(config, "msk", cluster.getClusterName());
return cluster.getResourceRegion() == null && Files.exists(legacyPath)
? legacyPath
: scopedPath;
}
}
@@ -97,6 +97,10 @@ public class MskCluster {
@JsonProperty("accountId")
private String accountId;
// Region is persisted separately so records created before regional ARNs remain identifiable.
@JsonProperty("resourceRegion")
private String resourceRegion;
// 6-char hex generated once at creation for stable, collision-free volume/container naming
@JsonProperty("volumeId")
private String volumeId;
@@ -180,6 +184,9 @@ public class MskCluster {
public String getAccountId() { return accountId; }
public void setAccountId(String accountId) { this.accountId = accountId; }
public String getResourceRegion() { return resourceRegion; }
public void setResourceRegion(String resourceRegion) { this.resourceRegion = resourceRegion; }
public String getVolumeId() { return volumeId; }
public void setVolumeId(String volumeId) { this.volumeId = volumeId; }
}