mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(eventbridge): move resources saved before account isolation into the default account at load (#9753)
EventBridge read buses, rules, archives, replays and targets by name through the request scoped store, which moves an entry saved under a bare key before stores kept accounts apart into whichever account reads it. A call of another account naming such a resource took the default account's into its own partition, where it could read, change or delete it (a DeleteArchive also dropped its archived events). At load the service now moves every bare key of its eight stores into the default account, which owns them, through a new AccountAwareStorageBackend.moveLegacyEntriesToTheDefaultAccount, so no later read finds one. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
db844fc14b
commit
610ff85fe1
3 files changed
+144
No files matched your search
@@ -325,6 +325,15 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
(ignored, value) -> destinationKey.apply(value), legacyOwner);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves every entry saved before stores kept accounts apart, one under a key with no account prefix, into the
|
||||
* default account, which owns all of them, under the same key. A service whose keys kept their shape calls this
|
||||
* at load, so no later {@link #get} can move such an entry into whichever account happens to ask.
|
||||
*/
|
||||
public synchronized void moveLegacyEntriesToTheDefaultAccount() {
|
||||
migrateLegacyEntries(defaultAccountId, key -> true, (key, value) -> key, value -> true);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@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
|
||||
|
||||
@@ -167,8 +167,23 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
this.v2Service = v2Service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the buses, rules, targets, archives, archived events, replays, connections and API destinations saved
|
||||
* before stores kept accounts apart, which sit under bare keys and are the default account's, into the default
|
||||
* account. A read by name in another account then finds none of them instead of taking it into its own.
|
||||
*/
|
||||
void moveLegacyKeysToTheDefaultAccount() {
|
||||
for (StorageBackend<String, ?> store : List.of(busStore, ruleStore, targetStore, archiveStore,
|
||||
archivedEventStore, replayStore, connectionStore, apiDestinationStore)) {
|
||||
if (store instanceof AccountAwareStorageBackend<?> accounts) {
|
||||
accounts.moveLegacyEntriesToTheDefaultAccount();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
void init() {
|
||||
moveLegacyKeysToTheDefaultAccount();
|
||||
if (ruleScheduler != null) {
|
||||
List<Rule> allRules = ruleStore instanceof AccountAwareStorageBackend<Rule> aware
|
||||
? aware.scanAllAccounts()
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
package io.floci.aws.services.eventbridge;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.RequestContext;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.services.eventbridge.model.Archive;
|
||||
import io.floci.aws.services.eventbridge.model.Connection;
|
||||
import io.floci.aws.services.eventbridge.model.EventBus;
|
||||
import io.floci.aws.services.eventbridge.model.Replay;
|
||||
import io.floci.aws.services.eventbridge.model.Rule;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Resources saved before stores kept accounts apart sit under bare keys, and they are the default account's. At load
|
||||
* they move into the default account, so a call of another account naming one finds nothing and cannot take it.
|
||||
*/
|
||||
class EventBridgeLegacyKeysAtLoadTest {
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String OTHER = "222222222222";
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String BUS_KEY = "bus:" + REGION + ":orders";
|
||||
private static final String RULE_KEY = "rule:" + REGION + ":default/nightly";
|
||||
private static final String ARCHIVE_KEY = "archive:" + REGION + ":billing";
|
||||
private static final String REPLAY_KEY = "replay:" + REGION + ":rerun";
|
||||
private static final String CONNECTION_KEY = "connection:" + REGION + ":saved";
|
||||
|
||||
private final InMemoryStorage<String, EventBus> buses = new InMemoryStorage<>();
|
||||
private final InMemoryStorage<String, Rule> rules = new InMemoryStorage<>();
|
||||
private final InMemoryStorage<String, Archive> archives = new InMemoryStorage<>();
|
||||
private final InMemoryStorage<String, Replay> replays = new InMemoryStorage<>();
|
||||
private final InMemoryStorage<String, Connection> connections = new InMemoryStorage<>();
|
||||
|
||||
@Test
|
||||
void legacyResourcesMoveIntoTheDefaultAccountAtLoad() {
|
||||
saveLegacyResources();
|
||||
EventBus othersBus = new EventBus("orders",
|
||||
"arn:aws:events:" + REGION + ":" + OTHER + ":event-bus/orders", null, Instant.now());
|
||||
buses.put(OTHER + "/" + BUS_KEY, othersBus);
|
||||
|
||||
service(ACCOUNT).moveLegacyKeysToTheDefaultAccount();
|
||||
|
||||
assertEquals(Set.of(ACCOUNT + "/" + BUS_KEY, OTHER + "/" + BUS_KEY), buses.keys());
|
||||
assertEquals(Set.of(ACCOUNT + "/" + RULE_KEY), rules.keys());
|
||||
assertEquals(Set.of(ACCOUNT + "/" + ARCHIVE_KEY), archives.keys());
|
||||
assertEquals(Set.of(ACCOUNT + "/" + REPLAY_KEY), replays.keys());
|
||||
assertEquals(Set.of(ACCOUNT + "/" + CONNECTION_KEY), connections.keys());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anotherAccountFindsNoLegacyResourceByNameOnceLoaded() {
|
||||
saveLegacyResources();
|
||||
service(ACCOUNT).moveLegacyKeysToTheDefaultAccount();
|
||||
EventBridgeService other = service(OTHER);
|
||||
|
||||
assertNotFound(() -> other.describeArchive("billing", REGION));
|
||||
assertNotFound(() -> other.describeReplay("rerun", REGION));
|
||||
assertNotFound(() -> other.describeRule("nightly", null, REGION));
|
||||
assertNotFound(() -> other.describeEventBus("orders", REGION));
|
||||
|
||||
EventBridgeService own = service(ACCOUNT);
|
||||
assertEquals("billing", own.describeArchive("billing", REGION).getArchiveName());
|
||||
assertEquals("rerun", own.describeReplay("rerun", REGION).getReplayName());
|
||||
assertEquals("nightly", own.describeRule("nightly", null, REGION).getName());
|
||||
assertEquals("orders", own.describeEventBus("orders", REGION).getName());
|
||||
}
|
||||
|
||||
private void saveLegacyResources() {
|
||||
buses.put(BUS_KEY, new EventBus("orders",
|
||||
"arn:aws:events:" + REGION + ":" + ACCOUNT + ":event-bus/orders", null, Instant.now()));
|
||||
Rule rule = new Rule();
|
||||
rule.setName("nightly");
|
||||
rule.setEventBusName("default");
|
||||
rule.setArn("arn:aws:events:" + REGION + ":" + ACCOUNT + ":rule/nightly");
|
||||
rules.put(RULE_KEY, rule);
|
||||
Archive archive = new Archive();
|
||||
archive.setArchiveName("billing");
|
||||
archives.put(ARCHIVE_KEY, archive);
|
||||
Replay replay = new Replay();
|
||||
replay.setReplayName("rerun");
|
||||
replays.put(REPLAY_KEY, replay);
|
||||
Connection connection = new Connection();
|
||||
connection.setName("saved");
|
||||
connection.setConnectionArn("arn:aws:events:" + REGION + ":" + ACCOUNT + ":connection/saved/abc");
|
||||
connections.put(CONNECTION_KEY, connection);
|
||||
}
|
||||
|
||||
private static void assertNotFound(Runnable call) {
|
||||
assertEquals("ResourceNotFoundException", assertThrows(AwsException.class, call::run).getErrorCode());
|
||||
}
|
||||
|
||||
/** A service whose stores and resolver read {@code account} from the request, as they do in a running Floci. */
|
||||
private EventBridgeService service(String account) {
|
||||
RequestContext request = new RequestContext();
|
||||
request.setAccountId(account);
|
||||
@SuppressWarnings("unchecked")
|
||||
Instance<RequestContext> context = mock(Instance.class);
|
||||
when(context.get()).thenReturn(request);
|
||||
RegionResolver regions = spy(new RegionResolver(REGION, ACCOUNT));
|
||||
when(regions.getAccountId()).thenReturn(account);
|
||||
return new EventBridgeService(new AccountAwareStorageBackend<>(buses, context, ACCOUNT),
|
||||
new AccountAwareStorageBackend<>(rules, context, ACCOUNT), new InMemoryStorage<>(),
|
||||
new AccountAwareStorageBackend<>(archives, context, ACCOUNT), new InMemoryStorage<>(),
|
||||
new AccountAwareStorageBackend<>(replays, context, ACCOUNT),
|
||||
new AccountAwareStorageBackend<>(connections, context, ACCOUNT), new InMemoryStorage<>(),
|
||||
regions, new ObjectMapper(), null, null, null, null, null, null);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user