mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(eventbridge): leave another account's legacy connection in place when reading by name (#9745)
Connection and API destination reads by name went through the request scoped store, which moves an entry the default account saved before stores kept accounts apart into whichever account reads it. A call of another account naming it took it into its own partition, and could then read and change it, while the default account no longer found it; a CreateConnection of that name in another account was refused as a duplicate. The by-name reads, find() and findApiDestinationByArn now move a legacy entry only into the account its own ARN names, as the connection ARN path already does. A list scans the caller's own keys and reads each one back. When another request deleted that entry in between, the read fell through to the bare legacy key and moved the default account's entry into the caller's account. The read back now goes through the same owner check as the by-name reads. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
00e4ceb1b2
commit
10b5f08654
2 files changed
+203
-17
No files matched your search
@@ -2062,7 +2062,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
throw new AwsException("ValidationException", "AuthParameters is required.", 400);
|
||||
}
|
||||
String key = connectionKey(region, name);
|
||||
if (connectionStore.get(key).isPresent()) {
|
||||
if (ownConnection(key).isPresent()) {
|
||||
// AWS's wording for this duplicate is not recorded, so it keeps Floci's own.
|
||||
throw new AwsException("ResourceAlreadyExistsException",
|
||||
"Connection " + name + " already exists.", 400);
|
||||
@@ -2152,14 +2152,14 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
}
|
||||
String name = names.getFirst();
|
||||
return switch (type) {
|
||||
case "connection" -> connectionStore.get(connectionKey(region, name)).map(Connection::getConnectionArn);
|
||||
case "api-destination" -> apiDestinationStore.get(apiDestinationKey(region, name)).map(ApiDestination::getArn);
|
||||
case "connection" -> ownConnection(connectionKey(region, name)).map(Connection::getConnectionArn);
|
||||
case "api-destination" -> ownApiDestination(apiDestinationKey(region, name)).map(ApiDestination::getArn);
|
||||
default -> Optional.empty();
|
||||
};
|
||||
}
|
||||
|
||||
public Connection describeConnection(String name, String region) {
|
||||
return connectionStore.get(connectionKey(region, name))
|
||||
return ownConnection(connectionKey(region, name))
|
||||
.orElseThrow(() -> EventBridgeErrors.connectionNotFound("describe", name));
|
||||
}
|
||||
|
||||
@@ -2179,7 +2179,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
String authParameters, String invocationConnectivityParameters,
|
||||
String kmsKeyIdentifier, Set<String> cleared, String region) {
|
||||
String key = connectionKey(region, name);
|
||||
Connection connection = connectionStore.get(key)
|
||||
Connection connection = ownConnection(key)
|
||||
.orElseThrow(() -> EventBridgeErrors.connectionNotFound("update", name));
|
||||
if (authorizationType != null) {
|
||||
validateAuthorizationType(authorizationType);
|
||||
@@ -2224,7 +2224,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
*/
|
||||
public synchronized Connection restoreConnection(Connection previous, String region) {
|
||||
String key = connectionKey(region, previous.getName());
|
||||
Connection connection = connectionStore.get(key)
|
||||
Connection connection = ownConnection(key)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"Connection " + previous.getName() + " does not exist.", 400));
|
||||
connection.setDescription(previous.getDescription());
|
||||
@@ -2243,7 +2243,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
|
||||
public Connection deleteConnection(String name, String region) {
|
||||
String key = connectionKey(region, name);
|
||||
Connection connection = connectionStore.get(key)
|
||||
Connection connection = ownConnection(key)
|
||||
// AWS's delete wording is not recorded (the suite covers describe, update and create), so it keeps Floci's own.
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"Connection " + name + " does not exist.", 400));
|
||||
@@ -2256,7 +2256,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
String prefix = "connection:" + region + ":";
|
||||
return connectionStore.scan(k -> {
|
||||
if (!k.startsWith(prefix)) return false;
|
||||
Connection c = connectionStore.get(k).orElse(null);
|
||||
Connection c = ownConnection(k).orElse(null);
|
||||
if (c == null) return false;
|
||||
if (namePrefix != null && !namePrefix.isBlank()
|
||||
&& !c.getName().startsWith(namePrefix)) {
|
||||
@@ -2324,7 +2324,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
}
|
||||
|
||||
String key = apiDestinationKey(region, name);
|
||||
if (apiDestinationStore.get(key).isPresent()) {
|
||||
if (ownApiDestination(key).isPresent()) {
|
||||
// AWS's wording for this duplicate is not recorded, so it keeps Floci's own.
|
||||
throw new AwsException("ResourceAlreadyExistsException",
|
||||
"ApiDestination " + name + " already exists.", 400);
|
||||
@@ -2351,7 +2351,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
}
|
||||
|
||||
public ApiDestination describeApiDestination(String name, String region) {
|
||||
return apiDestinationStore.get(apiDestinationKey(region, name))
|
||||
return ownApiDestination(apiDestinationKey(region, name))
|
||||
.orElseThrow(() -> EventBridgeErrors.apiDestinationNotFound("describe", name));
|
||||
}
|
||||
|
||||
@@ -2359,7 +2359,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
String invocationEndpoint, String httpMethod,
|
||||
Integer invocationRateLimitPerSecond, String region) {
|
||||
String key = apiDestinationKey(region, name);
|
||||
ApiDestination destination = apiDestinationStore.get(key)
|
||||
ApiDestination destination = ownApiDestination(key)
|
||||
.orElseThrow(() -> EventBridgeErrors.apiDestinationNotFound("update", name));
|
||||
|
||||
// Validate everything before assigning: the in-memory store returns the same instance, so a rejected update must not leave partial changes
|
||||
@@ -2404,7 +2404,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
String invocationEndpoint, String httpMethod,
|
||||
Integer invocationRateLimitPerSecond, String region) {
|
||||
String key = apiDestinationKey(region, name);
|
||||
ApiDestination destination = apiDestinationStore.get(key)
|
||||
ApiDestination destination = ownApiDestination(key)
|
||||
.orElseThrow(() -> EventBridgeErrors.apiDestinationNotFound("update", name));
|
||||
destination.setDescription(description);
|
||||
destination.setConnectionArn(connectionArn);
|
||||
@@ -2419,7 +2419,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
|
||||
public synchronized ApiDestination deleteApiDestination(String name, String region) {
|
||||
String key = apiDestinationKey(region, name);
|
||||
ApiDestination destination = apiDestinationStore.get(key)
|
||||
ApiDestination destination = ownApiDestination(key)
|
||||
// AWS's delete wording is not recorded (the suite covers describe, update and create), so it keeps Floci's own.
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"ApiDestination " + name + " does not exist.", 400));
|
||||
@@ -2434,7 +2434,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
if (!k.startsWith(prefix)) {
|
||||
return false;
|
||||
}
|
||||
ApiDestination dest = apiDestinationStore.get(k).orElse(null);
|
||||
ApiDestination dest = ownApiDestination(k).orElse(null);
|
||||
if (dest == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -2509,7 +2509,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
: EventBridgeErrors.apiDestinationConnectionNotFound(verb, connectionArn);
|
||||
}
|
||||
return connectionStore.scan(k -> {
|
||||
Connection c = connectionStore.get(k).orElse(null);
|
||||
Connection c = ownConnection(k).orElse(null);
|
||||
return c != null && connectionArn.equals(c.getConnectionArn());
|
||||
}).stream().findFirst().orElseThrow(() -> verb == null
|
||||
? EventBridgeErrors.plainConnectionNotFound(connectionArn)
|
||||
@@ -2528,7 +2528,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
int slash = remainder.indexOf('/');
|
||||
String name = slash > 0 ? remainder.substring(0, slash) : remainder;
|
||||
String destRegion = parsed.region().isEmpty() ? region : parsed.region();
|
||||
Optional<ApiDestination> opt = apiDestinationStore.get(apiDestinationKey(destRegion, name));
|
||||
Optional<ApiDestination> opt = ownApiDestination(apiDestinationKey(destRegion, name));
|
||||
// Same as connections: an ARN for a destination deleted and recreated under the same name is stale
|
||||
if (opt.isPresent() && arn.equals(opt.get().getArn())) {
|
||||
return opt.get();
|
||||
@@ -2536,11 +2536,36 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
}
|
||||
}
|
||||
return apiDestinationStore.scan(k -> {
|
||||
ApiDestination d = apiDestinationStore.get(k).orElse(null);
|
||||
ApiDestination d = ownApiDestination(k).orElse(null);
|
||||
return d != null && arn.equals(d.getArn());
|
||||
}).stream().findFirst().orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* The connection under {@code key} in the request's account. One the default account saved before stores kept
|
||||
* accounts apart sits under the bare key; it moves into the request's account only when its ARN names that
|
||||
* account, so a call of another account naming it neither finds nor takes it.
|
||||
*/
|
||||
private Optional<Connection> ownConnection(String key) {
|
||||
return readOwned(connectionStore, key, Connection::getConnectionArn);
|
||||
}
|
||||
|
||||
/** As {@link #ownConnection(String)}, for an API destination. */
|
||||
private Optional<ApiDestination> ownApiDestination(String key) {
|
||||
return readOwned(apiDestinationStore, key, ApiDestination::getArn);
|
||||
}
|
||||
|
||||
private <V> Optional<V> readOwned(StorageBackend<String, V> store, String key, Function<V, String> arnOf) {
|
||||
if (store instanceof AccountAwareStorageBackend<V> accounts) {
|
||||
String account = regionResolver.getAccountId();
|
||||
return accounts.getForAccountMigratingLegacy(account, key, legacy -> {
|
||||
String arn = arnOf.apply(legacy);
|
||||
return AwsArnUtils.isArn(arn) && account.equals(AwsArnUtils.parse(arn).accountId());
|
||||
});
|
||||
}
|
||||
return store.get(key);
|
||||
}
|
||||
|
||||
private static String apiDestinationKey(String region, String name) {
|
||||
return "api-destination:" + region + ":" + name;
|
||||
}
|
||||
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
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.ApiDestination;
|
||||
import io.floci.aws.services.eventbridge.model.Connection;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* A connection or API destination saved before stores kept accounts apart sits under a bare key of the default
|
||||
* account. A call of another account naming it reads its own account's namespace: it finds nothing and leaves the
|
||||
* default account's entry where it is, while the default account still reads it and takes it into its own partition.
|
||||
*/
|
||||
class EventBridgeLegacyByNameOwnerTest {
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String OTHER = "222222222222";
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String CONNECTION_KEY = "connection:" + REGION + ":saved";
|
||||
private static final String CONNECTION_ARN = "arn:aws:events:" + REGION + ":" + ACCOUNT + ":connection/saved/abc";
|
||||
private static final String DESTINATION_KEY = "api-destination:" + REGION + ":hook";
|
||||
private static final String DESTINATION_ARN = "arn:aws:events:" + REGION + ":" + ACCOUNT
|
||||
+ ":api-destination/hook/def";
|
||||
|
||||
/** Raw keys whose next read finds nothing, as if another request deleted the entry after a scan listed it. */
|
||||
private final Set<String> deletedOnRead = new HashSet<>();
|
||||
private final InMemoryStorage<String, Connection> connections = new DeletingOnRead<>(deletedOnRead);
|
||||
private final InMemoryStorage<String, ApiDestination> destinations = new DeletingOnRead<>(deletedOnRead);
|
||||
|
||||
@Test
|
||||
void anotherAccountFindsNoLegacyConnectionByNameAndLeavesItInPlace() {
|
||||
connections.put(CONNECTION_KEY, connection());
|
||||
EventBridgeService other = service(OTHER);
|
||||
|
||||
assertEquals("ResourceNotFoundException",
|
||||
assertThrows(AwsException.class, () -> other.describeConnection("saved", REGION)).getErrorCode());
|
||||
assertEquals("ResourceNotFoundException", assertThrows(AwsException.class, () -> other.updateConnection(
|
||||
"saved", "changed", null, null, null, null, REGION)).getErrorCode());
|
||||
assertEquals("ResourceNotFoundException",
|
||||
assertThrows(AwsException.class, () -> other.deleteConnection("saved", REGION)).getErrorCode());
|
||||
assertEquals(Set.of(CONNECTION_KEY), connections.keys());
|
||||
|
||||
Connection own = service(ACCOUNT).describeConnection("saved", REGION);
|
||||
assertEquals(CONNECTION_ARN, own.getConnectionArn());
|
||||
assertNull(own.getDescription());
|
||||
assertEquals(Set.of(ACCOUNT + "/" + CONNECTION_KEY), connections.keys());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anotherAccountCreatesItsOwnConnectionUnderTheNameOfALegacyOne() {
|
||||
connections.put(CONNECTION_KEY, connection());
|
||||
|
||||
Connection created = service(OTHER).createConnection("saved", null, "API_KEY",
|
||||
"{\"ApiKeyAuthParameters\":{\"ApiKeyName\":\"k\",\"ApiKeyValue\":\"v\"}}", null, null, REGION);
|
||||
|
||||
assertEquals(OTHER, created.getConnectionArn().split(":")[4]);
|
||||
assertEquals(Set.of(CONNECTION_KEY, OTHER + "/" + CONNECTION_KEY), connections.keys());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anotherAccountFindsNoLegacyApiDestinationByNameOrArnAndLeavesItInPlace() {
|
||||
destinations.put(DESTINATION_KEY, destination());
|
||||
EventBridgeService other = service(OTHER);
|
||||
|
||||
assertEquals("ResourceNotFoundException",
|
||||
assertThrows(AwsException.class, () -> other.describeApiDestination("hook", REGION)).getErrorCode());
|
||||
assertEquals("ResourceNotFoundException",
|
||||
assertThrows(AwsException.class, () -> other.deleteApiDestination("hook", REGION)).getErrorCode());
|
||||
assertNull(other.findApiDestinationByArn(DESTINATION_ARN, REGION));
|
||||
assertEquals(Set.of(DESTINATION_KEY), destinations.keys());
|
||||
|
||||
assertEquals(DESTINATION_ARN, service(ACCOUNT).describeApiDestination("hook", REGION).getArn());
|
||||
assertEquals(Set.of(ACCOUNT + "/" + DESTINATION_KEY), destinations.keys());
|
||||
}
|
||||
|
||||
/**
|
||||
* A list scans the caller's own keys and reads each one back. When the caller's entry is deleted in between, the
|
||||
* read back must not fall through to the bare key and take the default account's legacy entry instead.
|
||||
*/
|
||||
@Test
|
||||
void aListWhoseOwnEntryIsDeletedMeanwhileDoesNotTakeTheLegacyOne() {
|
||||
connections.put(CONNECTION_KEY, connection());
|
||||
destinations.put(DESTINATION_KEY, destination());
|
||||
connections.put(OTHER + "/" + CONNECTION_KEY, connection());
|
||||
destinations.put(OTHER + "/" + DESTINATION_KEY, destination());
|
||||
deletedOnRead.add(OTHER + "/" + CONNECTION_KEY);
|
||||
deletedOnRead.add(OTHER + "/" + DESTINATION_KEY);
|
||||
EventBridgeService other = service(OTHER);
|
||||
|
||||
assertEquals(List.of(), other.listConnections(null, null, REGION));
|
||||
assertEquals(List.of(), other.listApiDestinations(null, null, REGION));
|
||||
assertTrue(connections.keys().contains(CONNECTION_KEY));
|
||||
assertTrue(destinations.keys().contains(DESTINATION_KEY));
|
||||
}
|
||||
|
||||
/** 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 InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new AccountAwareStorageBackend<>(connections, context, ACCOUNT),
|
||||
new AccountAwareStorageBackend<>(destinations, context, ACCOUNT),
|
||||
regions, new ObjectMapper(), null, null, null, null, null, null);
|
||||
}
|
||||
|
||||
private static Connection connection() {
|
||||
Connection connection = new Connection();
|
||||
connection.setName("saved");
|
||||
connection.setConnectionArn(CONNECTION_ARN);
|
||||
connection.setAuthorizationType("API_KEY");
|
||||
return connection;
|
||||
}
|
||||
|
||||
private static ApiDestination destination() {
|
||||
ApiDestination destination = new ApiDestination();
|
||||
destination.setName("hook");
|
||||
destination.setArn(DESTINATION_ARN);
|
||||
destination.setConnectionArn(CONNECTION_ARN);
|
||||
return destination;
|
||||
}
|
||||
|
||||
private static final class DeletingOnRead<V> extends InMemoryStorage<String, V> {
|
||||
private final Set<String> deletedOnRead;
|
||||
|
||||
DeletingOnRead(Set<String> deletedOnRead) {
|
||||
this.deletedOnRead = deletedOnRead;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<V> get(String key) {
|
||||
if (deletedOnRead.remove(key)) {
|
||||
delete(key);
|
||||
return Optional.empty();
|
||||
}
|
||||
return super.get(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user