mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(eventbridge): list rule names by target (#8632)
* feat(eventbridge): list rule names by target * fix(eventbridge): reuse scoped rule lookup and policy fixtures * fix(eventbridge): avoid redundant target lookup reads Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
7872ef9c51
commit
e281defa12
15 files changed
+1064
-35
No files matched your search
@@ -624,6 +624,24 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Account-relative keys, including unmigrated keys only for the configured default account.
|
||||
* Filters ownership before collecting keys and never fetches or migrates a stored value.
|
||||
*/
|
||||
public Set<String> keysIncludingLegacyForAccount(String accountId, Predicate<String> keyFilter) {
|
||||
Set<String> keys = new LinkedHashSet<>();
|
||||
for (String rawKey : delegate.keys()) {
|
||||
if (!accountId.equals(ownerOf(rawKey))) {
|
||||
continue;
|
||||
}
|
||||
String key = hasAccountPrefix(rawKey) ? rawKey.substring(13) : rawKey;
|
||||
if (keyFilter.test(key)) {
|
||||
keys.add(key);
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
public Set<String> keysForAccount(String accountId) {
|
||||
String prefix = accountId + "/";
|
||||
return delegate.keys().stream()
|
||||
|
||||
@@ -74,6 +74,7 @@ public class EventBridgeHandler {
|
||||
case "PutTargets" -> handlePutTargets(request, region);
|
||||
case "RemoveTargets" -> handleRemoveTargets(request, region);
|
||||
case "ListTargetsByRule" -> handleListTargetsByRule(request, region);
|
||||
case "ListRuleNamesByTarget" -> handleListRuleNamesByTarget(request, region);
|
||||
case "PutEvents" -> handlePutEvents(request, region);
|
||||
case "TestEventPattern" -> handleTestEventPattern(request);
|
||||
case "ListTagsForResource" -> handleListTagsForResource(request, region);
|
||||
@@ -343,6 +344,32 @@ public class EventBridgeHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleListRuleNamesByTarget(JsonNode request, String region) {
|
||||
String targetArn = targetLookupString(request, "TargetArn");
|
||||
String busName = targetLookupString(request, "EventBusName");
|
||||
String nextToken = targetLookupString(request, "NextToken");
|
||||
PaginatedResult<String> page = eventBridgeService.listRuleNamesByTarget(
|
||||
targetArn, busName, readLimit(request.path("Limit")), nextToken, region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode names = response.putArray("RuleNames");
|
||||
page.items().forEach(names::add);
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private static String targetLookupString(JsonNode request, String field) {
|
||||
JsonNode value = request.path(field);
|
||||
if (value.isMissingNode() || value.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (!value.isTextual()) {
|
||||
throw new AwsException("SerializationException", field + " must be a string.", 400);
|
||||
}
|
||||
return value.textValue();
|
||||
}
|
||||
|
||||
private Response handleListTargetsByRule(JsonNode request, String region) {
|
||||
String ruleName = request.path("Rule").asText(null);
|
||||
String busName = request.path("EventBusName").asText(null);
|
||||
|
||||
@@ -15,6 +15,7 @@ import io.floci.aws.core.common.AwsArnShapes;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.AwsPartitions;
|
||||
import io.floci.aws.core.common.Digests;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
@@ -68,6 +69,7 @@ import java.util.regex.Pattern;
|
||||
|
||||
import static io.floci.aws.core.storage.AccountAwareStorageBackend.getInAccount;
|
||||
import static io.floci.aws.core.storage.AccountAwareStorageBackend.putInAccount;
|
||||
import static io.floci.aws.core.storage.AccountAwareStorageBackend.readInAccount;
|
||||
|
||||
@ApplicationScoped
|
||||
public class EventBridgeService implements ResourceProvider, StoredResourceArns {
|
||||
@@ -272,8 +274,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
return getOrCreateDefaultBus(region);
|
||||
}
|
||||
return busStore.get(busKey(region, effectiveName))
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"EventBus not found: " + name, 400));
|
||||
.orElseThrow(() -> eventBusNotFound(name));
|
||||
}
|
||||
|
||||
public EventBus updateEventBus(String name,
|
||||
@@ -522,12 +523,24 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
public List<Rule> listRules(String busName, String namePrefix, String region) {
|
||||
String effectiveBus = resolvedBusName(busName);
|
||||
String prefix = ruleKeyPrefix(region, effectiveBus);
|
||||
return ruleStore.scan(k -> {
|
||||
if (!isRuleKeyOfBus(k, prefix)) return false;
|
||||
if (namePrefix == null || namePrefix.isBlank()) return true;
|
||||
String ruleName = k.substring(prefix.length());
|
||||
return ruleName.startsWith(namePrefix);
|
||||
}).stream().filter(rule -> effectiveBus.equals(rule.getEventBusName())).toList();
|
||||
String account = regionResolver.getAccountId();
|
||||
Set<String> keys = ruleStore instanceof AccountAwareStorageBackend<Rule> aware
|
||||
? aware.keysIncludingLegacyForAccount(account, key -> key.startsWith(prefix))
|
||||
: ruleStore.keys();
|
||||
List<Rule> rules = new ArrayList<>();
|
||||
for (String key : keys) {
|
||||
if (!key.startsWith(prefix)) {
|
||||
continue;
|
||||
}
|
||||
String name = key.substring(prefix.length());
|
||||
if (namePrefix != null && !namePrefix.isBlank() && !name.startsWith(namePrefix)) {
|
||||
continue;
|
||||
}
|
||||
readInAccount(ruleStore, account, key)
|
||||
.filter(rule -> name.equals(rule.getName()) && effectiveBus.equals(rule.getEventBusName()))
|
||||
.ifPresent(rules::add);
|
||||
}
|
||||
return rules;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -744,6 +757,58 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
return new RemoveTargetsResult(removed, ids.size() - removed);
|
||||
}
|
||||
|
||||
public PaginatedResult<String> listRuleNamesByTarget(String targetArn, String busName, Integer limit,
|
||||
String nextToken, String region) {
|
||||
if (targetArn == null || targetArn.isEmpty() || targetArn.length() > 1600) {
|
||||
throw new AwsException("ValidationException", "TargetArn must contain 1 to 1600 characters.", 400);
|
||||
}
|
||||
if (busName != null && (busName.isBlank() || busName.length() > 1600)) {
|
||||
throw new AwsException("ValidationException", "EventBusName must contain 1 to 1600 characters.", 400);
|
||||
}
|
||||
String effectiveBus = resolvedBusName(busName);
|
||||
if (!effectiveBus.matches("[/\\.\\-_A-Za-z0-9]+")) {
|
||||
throw new AwsException("ValidationException", "Invalid EventBridge event bus name.", 400);
|
||||
}
|
||||
requireBusForRead(effectiveBus, region);
|
||||
List<String> names = listRules(effectiveBus, null, region).stream()
|
||||
.filter(rule -> ruleTargetsArn(rule, effectiveBus, targetArn, region))
|
||||
.map(Rule::getName).toList();
|
||||
String scope = targetRuleCursorScope(targetArn, busName, effectiveBus, limit, region);
|
||||
return paginate(names, Function.identity(), limit, nextToken, scope);
|
||||
}
|
||||
|
||||
private void requireBusForRead(String busName, String region) {
|
||||
Optional<EventBus> bus = readInAccount(busStore, regionResolver.getAccountId(), busKey(region, busName));
|
||||
if (bus.isEmpty() && !"default".equals(busName)) {
|
||||
throw eventBusNotFound(busName);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean ruleTargetsArn(Rule rule, String busName, String targetArn, String region) {
|
||||
String account = regionResolver.getAccountId();
|
||||
if (rule.getAccountId() != null && !account.equals(rule.getAccountId())) {
|
||||
return false;
|
||||
}
|
||||
String expectedArn = buildRuleArn(region, busName, rule.getName());
|
||||
if (!expectedArn.equals(rule.getArn())) {
|
||||
return false;
|
||||
}
|
||||
String key = ruleKey(region, busName, rule.getName());
|
||||
List<Target> targets = new ArrayList<>(readInAccount(targetStore, account, key).orElse(List.of()));
|
||||
return targets.stream().anyMatch(target -> target != null && targetArn.equals(target.getArn()));
|
||||
}
|
||||
|
||||
private String targetRuleCursorScope(String targetArn, String suppliedBus, String effectiveBus,
|
||||
Integer limit, String region) {
|
||||
StringBuilder scope = new StringBuilder();
|
||||
for (String value : List.of(regionResolver.getAccountId(), regionResolver.partitionForRegion(region),
|
||||
region, effectiveBus, targetArn, limit == null ? "omitted" : limit.toString(),
|
||||
suppliedBus == null ? "" : suppliedBus)) {
|
||||
scope.append(value.length()).append(':').append(value);
|
||||
}
|
||||
return "target-rules:" + Digests.sha256Hex(scope.toString());
|
||||
}
|
||||
|
||||
public List<Target> listTargetsByRule(String ruleName, String busName, String region) {
|
||||
String effectiveBus = resolvedBusName(busName);
|
||||
validateRuleReference("rule", ruleName, effectiveBus, region);
|
||||
@@ -1577,8 +1642,11 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
return;
|
||||
}
|
||||
busStore.get(busKey(region, busName))
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"EventBus not found: " + busName, 400));
|
||||
.orElseThrow(() -> eventBusNotFound(busName));
|
||||
}
|
||||
|
||||
private static AwsException eventBusNotFound(String busName) {
|
||||
return new AwsException("ResourceNotFoundException", "EventBus not found: " + busName, 400);
|
||||
}
|
||||
|
||||
private static boolean isBlank(Object value) {
|
||||
|
||||
Reference in new issue
Block a user