fix(mq): scope brokers by account and Region (#8433)

* fix(mq): scope brokers by account and Region
* fix(mq): write a ready broker back under the account and key it is stored under
* test(mq): check broker Regions over HTTP in an account of its own
* fix(mq): check a broker's name and store the broker under one lock
* fix(mq): keep a broker name unique across the account's Regions
* refactor(mq): derive a broker's Region in one place
* test(mq): race two broker creates on the shared parked-store helper
* refactor(mq): run the readiness poller's pass from a method tests can call
* test(mq): check the error type of each refusal from another Region
* test(mq): run the other Region's refusals through one loop
* refactor(storage): name the all-accounts migration like its neighbours
* docs(mq): say that brokers are Regional and names unique in the account
This commit is contained in:
mrfelek authored and GitHub committed 2026-11-15 08:33:24 -04:00
1 parent adfe173764
commit e9f842765d
11 files changed
+424 -140

No files matched your search

@@ -15,6 +15,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@@ -219,9 +220,8 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
public List<AccountEntry<V>> scanAllAccountEntries(Predicate<String> keyFilter) {
List<AccountEntry<V>> result = new ArrayList<>();
for (String rawKey : delegate.keys()) {
boolean hasAccountPrefix = hasAccountPrefix(rawKey);
String accountId = hasAccountPrefix ? rawKey.substring(0, 12) : defaultAccountId;
String logicalKey = hasAccountPrefix ? rawKey.substring(13) : rawKey;
String accountId = ownerOf(rawKey);
String logicalKey = hasAccountPrefix(rawKey) ? rawKey.substring(13) : rawKey;
if (!keyFilter.test(logicalKey)) {
continue;
}
@@ -244,6 +244,34 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
(ignored, value) -> destinationKey.apply(value), legacyOwner);
}
/**
* {@link #migrateLegacyEntries(String, Predicate, Function, Predicate)} in every account that
* holds an entry here, an unprefixed entry counting as the default account's, moving every legacy
* entry. For a store moving every record onto new keys at startup, when no request names an account.
*/
public synchronized void migrateLegacyEntriesInAllAccounts(
Predicate<String> legacyKeyFilter,
Function<V, String> destinationKey) {
migrateLegacyEntriesInAllAccounts(legacyKeyFilter, destinationKey, (accountId, value) -> true);
}
/**
* As above; only the legacy entries {@code legacyOwner} accepts, given the account holding each.
*/
public synchronized void migrateLegacyEntriesInAllAccounts(
Predicate<String> legacyKeyFilter,
Function<V, String> destinationKey,
BiPredicate<String, V> legacyOwner) {
Set<String> accounts = new LinkedHashSet<>();
for (String rawKey : delegate.keys()) {
accounts.add(ownerOf(rawKey));
}
for (String accountId : accounts) {
migrateLegacyEntries(accountId, legacyKeyFilter, destinationKey,
value -> legacyOwner.test(accountId, value));
}
}
private synchronized void migrateLegacyEntries(
String accountId,
Predicate<String> legacyKeyFilter,
@@ -251,8 +279,7 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
Predicate<V> legacyOwner) {
for (String rawKey : new ArrayList<>(delegate.keys())) {
boolean prefixed = hasAccountPrefix(rawKey);
String owner = prefixed ? rawKey.substring(0, 12) : defaultAccountId;
if (!accountId.equals(owner)) {
if (!accountId.equals(ownerOf(rawKey))) {
continue;
}
@@ -700,6 +727,11 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
return prefix() + "/" + key;
}
/** The account owning {@code rawKey}: its account prefix, or the default account for an unprefixed key. */
private String ownerOf(String rawKey) {
return hasAccountPrefix(rawKey) ? rawKey.substring(0, 12) : defaultAccountId;
}
/** Whether {@code key} starts with a 12-digit account id and a slash, as account-scoped keys do. */
public static boolean hasAccountPrefix(String key) {
if (key.length() < 13 || key.charAt(12) != '/') {
@@ -5,12 +5,13 @@ import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.StripedLocks;
import io.floci.aws.core.common.docker.ContainerStorageHelper;
import io.floci.aws.core.resource.ExplorerResource;
import io.floci.aws.core.resource.ResourceProvider;
import io.floci.aws.core.resource.SupportedResourceType;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.StorageBackend;
import io.floci.aws.core.storage.AccountAwareStorageBackend.AccountEntry;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.amazonmq.container.RabbitMqManager;
import io.floci.aws.services.amazonmq.model.Broker;
@@ -42,11 +43,14 @@ public class AmazonMqService implements ResourceProvider {
private static final String ENGINE_RABBITMQ = "RABBITMQ";
private static final String DEFAULT_ENGINE_VERSION = "3.13";
private static final String DEPLOYMENT_SINGLE_INSTANCE = "SINGLE_INSTANCE";
private static final String KEY_SEPARATOR = "::";
private final StorageBackend<String, Broker> storage;
/** Brokers by {@code region::brokerId}, in their owner's account partition: brokers are Regional. */
private final AccountAwareStorageBackend<Broker> storage;
private final EmulatorConfig config;
private final RegionResolver regionResolver;
private final RabbitMqManager rabbitMqManager;
private final StripedLocks brokerLocks = new StripedLocks(256);
private final ScheduledExecutorService poller = Executors.newSingleThreadScheduledExecutor();
@Inject
@@ -61,6 +65,7 @@ public class AmazonMqService implements ResourceProvider {
@PostConstruct
public void init() {
migrateLegacyKeys();
startReadinessPoller();
}
@@ -114,75 +119,83 @@ public class AmazonMqService implements ResourceProvider {
}
validateUserPassword(admin.getPassword());
if (storage.scan(k -> true).stream().anyMatch(b -> name.equals(b.getBrokerName()))) {
throw new AwsException("ConflictException", "Broker already exists: " + name, 409);
}
String brokerId = "b-" + UUID.randomUUID();
String region = regionResolver.getRegion();
String accountId = regionResolver.getAccountId();
String brokerArn = AwsArnUtils.Arn.of("mq", regionResolver.getRegion(), accountId,
"broker:" + name + ":" + brokerId).toString();
String engineVersion = (params.engineVersion() == null || params.engineVersion().isBlank())
? DEFAULT_ENGINE_VERSION : params.engineVersion();
Broker broker = new Broker(brokerId, brokerArn, name, ENGINE_RABBITMQ,
engineVersion, deploymentMode, params.hostInstanceType());
broker.setAccountId(accountId);
broker.setVolumeId(String.format("%06x", new SecureRandom().nextInt(0xFFFFFF)));
// Stamp the volume name now, with the current prefix, so it is persisted rather than
// recomputed later. Only records predating this field fall back to the legacy name.
broker.setDockerVolumeName(ContainerStorageHelper.resourceName(
config, "amazonmq", broker.getVolumeId(), broker.getBrokerId()));
broker.setPubliclyAccessible(params.publiclyAccessible());
broker.setAutoMinorVersionUpgrade(params.autoMinorVersionUpgrade());
if (params.users() != null) {
broker.setUsers(new ArrayList<>(params.users()));
}
if (params.tags() != null) {
broker.setTags(new HashMap<>(params.tags()));
}
broker.setSecurityGroups(params.securityGroups());
broker.setSubnetIds(params.subnetIds());
broker.setLogs(params.logs());
broker.setMaintenanceWindowStartTime(params.maintenanceWindowStartTime());
broker.setStorageType(params.storageType());
broker.setAuthenticationStrategy(params.authenticationStrategy());
broker.setEncryptionOptions(params.encryptionOptions());
broker.setConfiguration(params.configuration());
if (config.services().amazonmq().mock()) {
// No backing container: come up immediately with synthetic endpoints.
applyLocalEndpoints(broker);
broker.setBrokerState(BrokerState.RUNNING);
} else {
try {
// Start the container; the broker stays CREATION_IN_PROGRESS until
// the readiness poller observes the management API answering.
rabbitMqManager.startContainer(broker);
} catch (RuntimeException e) {
broker.setBrokerState(BrokerState.CREATION_FAILED);
storage.put(brokerId, broker);
// Keep the cause in the logs; don't leak internal details (or a null
// message) into the AWS error envelope returned to the client.
LOG.errorv(e, "Failed to provision broker {0} ({1})", name, brokerId);
throw new AwsException("InternalServerErrorException",
"Failed to provision broker " + name, 500);
// A broker name is unique in the account, across its Regions, as the CreateBroker reference documents.
// The name check and the store hold one lock for the name in its account, so two creates of one name
// can't both find it free.
synchronized (brokerLocks.lockFor(accountId + ":" + name)) {
if (storage.scan(key -> true).stream().anyMatch(b -> name.equals(b.getBrokerName()))) {
throw new AwsException("ConflictException", "Broker already exists: " + name, 409);
}
}
storage.put(brokerId, broker);
LOG.infov("Created Amazon MQ broker {0} ({1})", name, brokerId);
return broker;
String brokerId = "b-" + UUID.randomUUID();
String brokerArn = AwsArnUtils.Arn.of("mq", region, accountId,
"broker:" + name + ":" + brokerId).toString();
String engineVersion = (params.engineVersion() == null || params.engineVersion().isBlank())
? DEFAULT_ENGINE_VERSION : params.engineVersion();
Broker broker = new Broker(brokerId, brokerArn, name, ENGINE_RABBITMQ,
engineVersion, deploymentMode, params.hostInstanceType());
broker.setAccountId(accountId);
broker.setVolumeId(String.format("%06x", new SecureRandom().nextInt(0xFFFFFF)));
// Stamp the volume name now, with the current prefix, so it is persisted rather than
// recomputed later. Only records predating this field fall back to the legacy name.
broker.setDockerVolumeName(ContainerStorageHelper.resourceName(
config, "amazonmq", broker.getVolumeId(), broker.getBrokerId()));
broker.setPubliclyAccessible(params.publiclyAccessible());
broker.setAutoMinorVersionUpgrade(params.autoMinorVersionUpgrade());
if (params.users() != null) {
broker.setUsers(new ArrayList<>(params.users()));
}
if (params.tags() != null) {
broker.setTags(new HashMap<>(params.tags()));
}
broker.setSecurityGroups(params.securityGroups());
broker.setSubnetIds(params.subnetIds());
broker.setLogs(params.logs());
broker.setMaintenanceWindowStartTime(params.maintenanceWindowStartTime());
broker.setStorageType(params.storageType());
broker.setAuthenticationStrategy(params.authenticationStrategy());
broker.setEncryptionOptions(params.encryptionOptions());
broker.setConfiguration(params.configuration());
if (config.services().amazonmq().mock()) {
// No backing container: come up immediately with synthetic endpoints.
applyLocalEndpoints(broker);
broker.setBrokerState(BrokerState.RUNNING);
} else {
try {
// Start the container; the broker stays CREATION_IN_PROGRESS until
// the readiness poller observes the management API answering.
rabbitMqManager.startContainer(broker);
} catch (RuntimeException e) {
broker.setBrokerState(BrokerState.CREATION_FAILED);
storage.put(brokerKey(region, brokerId), broker);
// Keep the cause in the logs; don't leak internal details (or a null
// message) into the AWS error envelope returned to the client.
LOG.errorv(e, "Failed to provision broker {0} ({1})", name, brokerId);
throw new AwsException("InternalServerErrorException",
"Failed to provision broker " + name, 500);
}
}
storage.put(brokerKey(region, brokerId), broker);
LOG.infov("Created Amazon MQ broker {0} ({1})", name, brokerId);
return broker;
}
}
public Broker describeBroker(String brokerId) {
return storage.get(brokerId)
return storage.get(brokerKey(regionResolver.getRegion(), brokerId))
.orElseThrow(() -> new AwsException("NotFoundException",
"Broker not found: " + brokerId, 404));
}
/** The calling account's brokers in the request's Region. */
public List<Broker> listBrokers() {
return storage.scan(k -> true);
String prefix = regionResolver.getRegion() + KEY_SEPARATOR;
return storage.scan(key -> key.startsWith(prefix));
}
@Override
@@ -215,7 +228,7 @@ public class AmazonMqService implements ResourceProvider {
rabbitMqManager.stopContainer(broker);
rabbitMqManager.removeBrokerStorage(broker);
}
storage.delete(brokerId);
storage.delete(brokerKey(regionResolver.getRegion(), brokerId));
LOG.infov("Deleted Amazon MQ broker {0}", brokerId);
}
@@ -244,38 +257,48 @@ public class AmazonMqService implements ResourceProvider {
}
private void startReadinessPoller() {
poller.scheduleAtFixedRate(() -> {
try {
if (config.services().amazonmq().mock()) {
return;
}
for (Broker broker : allBrokers()) {
if (broker.getBrokerState() == BrokerState.CREATION_IN_PROGRESS
&& rabbitMqManager.isReady(broker)) {
LOG.infov("Amazon MQ broker {0} is now RUNNING", broker.getBrokerName());
broker.setBrokerState(BrokerState.RUNNING);
putBroker(broker);
}
}
} catch (Exception e) {
LOG.error("Error in Amazon MQ readiness poller", e);
poller.scheduleAtFixedRate(this::pollReadiness, 1, 2, TimeUnit.SECONDS);
}
/** One pass of the readiness poller: marks each starting broker whose container answers {@code RUNNING}. */
void pollReadiness() {
try {
if (config.services().amazonmq().mock()) {
return;
}
}, 1, 2, TimeUnit.SECONDS);
// Every account's brokers, each written back under the account and key it is stored
// under: the poller has no request context, so a plain put would land in the default
// account. Brokers stored under a bare id have moved to their Region's key at startup.
for (AccountEntry<Broker> entry : storage.scanAllAccountEntries(key -> true)) {
Broker broker = entry.value();
if (broker.getBrokerState() == BrokerState.CREATION_IN_PROGRESS
&& rabbitMqManager.isReady(broker)) {
LOG.infov("Amazon MQ broker {0} is now RUNNING", broker.getBrokerName());
broker.setBrokerState(BrokerState.RUNNING);
storage.putForAccount(entry.accountId(), entry.key(), broker);
}
}
} catch (Exception e) {
LOG.error("Error in Amazon MQ readiness poller", e);
}
}
private List<Broker> allBrokers() {
if (storage instanceof AccountAwareStorageBackend<Broker> aware) {
return aware.scanAllAccounts();
}
return storage.scan(k -> true);
static String brokerKey(String region, String brokerId) {
return region + KEY_SEPARATOR + brokerId;
}
private void putBroker(Broker broker) {
if (broker.getAccountId() != null && storage instanceof AccountAwareStorageBackend<Broker> aware) {
aware.putForAccount(broker.getAccountId(), broker.getBrokerId(), broker);
} else {
storage.put(broker.getBrokerId(), broker);
}
/** A broker's key in its own Region, which its ARN has always named. */
private String brokerKey(Broker broker) {
return brokerKey(broker.region(regionResolver.getDefaultRegion()), broker.getBrokerId());
}
/**
* Moves brokers persisted before they were keyed by Region, in every account, from the bare
* broker id onto the Region-scoped key, in the Region their ARN names. Each stays in the
* account partition that stored it.
*/
private void migrateLegacyKeys() {
storage.migrateLegacyEntriesInAllAccounts(key -> !key.contains(KEY_SEPARATOR), this::brokerKey);
}
// --- Users ---
@@ -1,7 +1,6 @@
package io.floci.aws.services.amazonmq.container;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.docker.ContainerBuilder;
import io.floci.aws.core.common.docker.ContainerDetector;
@@ -184,9 +183,9 @@ public class RabbitMqManager {
}
}
/** The region in the broker's ARN; a broker recorded without one counts as the default region's. */
/** The broker's Region, {@link Broker#region} with the configured default Region. */
private String brokerRegion(Broker broker) {
return AwsArnUtils.regionOrDefault(broker.getBrokerArn(), regionResolver.getDefaultRegion());
return broker.region(regionResolver.getDefaultRegion());
}
/**
@@ -2,6 +2,7 @@ package io.floci.aws.services.amazonmq.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.floci.aws.core.common.AwsArnUtils;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
@@ -138,6 +139,12 @@ public class Broker {
public String getBrokerArn() { return brokerArn; }
public void setBrokerArn(String brokerArn) { this.brokerArn = brokerArn; }
/**
* The Region the broker's ARN names; a broker recorded without one counts as {@code defaultRegion}'s.
* Not a getter, so Jackson neither persists nor returns it.
*/
public String region(String defaultRegion) { return AwsArnUtils.regionOrDefault(brokerArn, defaultRegion); }
public String getBrokerName() { return brokerName; }
public void setBrokerName(String brokerName) { this.brokerName = brokerName; }
@@ -665,14 +665,7 @@ public class EksService implements TagHandler, ResourceProvider, Ec2InstanceLaun
}
return;
}
Set<String> accounts = new LinkedHashSet<>();
for (String rawKey : aware.scanAllAccountsWithRawKeys().keySet()) {
accounts.add(AccountAwareStorageBackend.hasAccountPrefix(rawKey)
? rawKey.substring(0, 12) : config.defaultAccountId());
}
for (String accountId : accounts) {
aware.migrateLegacyEntries(accountId, key -> !key.contains(KEY_SEPARATOR), destinationKey, value -> true);
}
aware.migrateLegacyEntriesInAllAccounts(key -> !key.contains(KEY_SEPARATOR), destinationKey);
}
private static String randomHex(int len) {
@@ -33,7 +33,6 @@ import java.security.SecureRandom;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -627,13 +626,8 @@ public class OpenSearchService implements ResourceProvider {
* own region; a record whose ARN cannot be parsed is left where it is.
*/
private void migrateLegacyDomainKeys() {
Set<String> accounts = new LinkedHashSet<>();
for (String rawKey : domainStore.scanAllAccountsWithRawKeys().keySet()) {
accounts.add(AccountAwareStorageBackend.hasAccountPrefix(rawKey)
? rawKey.substring(0, 12)
: regionResolver.getDefaultAccountId());
}
accounts.forEach(this::migrateLegacyDomainKeys);
domainStore.migrateLegacyEntriesInAllAccounts(key -> !key.contains("::"),
this::legacyDomainDestinationKey, (accountId, domain) -> ownsAccount(domain, accountId));
}
private void migrateLegacyDomainKeys(String accountId) {