mirror of
https://github.com/floci-io/floci.git
synced 2026-12-05 19:18:01 -05:00
refactor: read ARN resources through AwsArnUtils in EventBridge, Pipes and three more (#7145)
* refactor: read ARN resources through AwsArnUtils in EventBridge, Pipes and three more Archive, replay and Pipes event-bus targets, Pipes Kinesis sources, MicroVM image and network connector tags, Cloud Map tags and ECS service registries cut an ARN at its last '/' or ':' by hand. They now read the resource with AwsArnUtils.resourceIfArnFor, which also checks the service. A Pipes Kinesis source given as a stream-consumer ARN names its stream instead of "consumer:timestamp", and an ECS registryArn that is not a Cloud Map service ARN is ignored with the existing warning. Signed-off-by: Hector Ventura <hectorvent@gmail.com> * fix(lambda): read a MicroVM tag target the same way for IAM and the tag paths TagResource takes only an ARN, and a network connector without a version. LambdaMicrovmsService.taggedResource is now the one reader for both the tag paths and ResourceArnBuilder, so a version-qualified connector is refused instead of reaching the bare connector while IAM checks another resource. Signed-off-by: Hector Ventura <hectorvent@gmail.com> --------- Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
cf17be9dd0
commit
c865784540
13 files changed
+137
-62
No files matched your search
@@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.MissingNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.Digests;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
@@ -895,9 +896,9 @@ public class CloudMapService {
|
||||
if (resourceArn == null) {
|
||||
throw new AwsException("InvalidInput", "ResourceARN is required.", 400);
|
||||
}
|
||||
int slash = resourceArn.lastIndexOf('/');
|
||||
int colon = resourceArn.lastIndexOf(':');
|
||||
String id = resourceArn.substring(Math.max(slash, colon) + 1);
|
||||
String id = AwsArnUtils.resourceIfArnFor(resourceArn, "servicediscovery")
|
||||
.map(resource -> resource.substring(resource.lastIndexOf('/') + 1))
|
||||
.orElse(resourceArn);
|
||||
Optional<Namespace> ns = namespaceStore.get(id);
|
||||
if (ns.isPresent()) {
|
||||
return ns.get();
|
||||
|
||||
+12
-9
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.ecs;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
@@ -292,13 +293,14 @@ public class EcsServiceDiscoveryRegistrar {
|
||||
}
|
||||
}
|
||||
|
||||
/** The trailing name of an ARN, which for a cluster ARN is the cluster name. */
|
||||
/** The trailing name of an ECS ARN, which for a cluster ARN is the cluster name. */
|
||||
private static String nameFromArn(String arn) {
|
||||
if (arn == null) {
|
||||
return null;
|
||||
}
|
||||
int lastSlash = arn.lastIndexOf('/');
|
||||
return lastSlash >= 0 ? arn.substring(lastSlash + 1) : arn;
|
||||
return AwsArnUtils.resourceIfArnFor(arn, "ecs")
|
||||
.map(resource -> resource.substring(resource.lastIndexOf('/') + 1))
|
||||
.orElse(arn);
|
||||
}
|
||||
|
||||
/** The family out of {@code .../task-definition/<family>:<revision>}, without the revision. */
|
||||
@@ -356,20 +358,21 @@ public class EcsServiceDiscoveryRegistrar {
|
||||
if (registryArn == null || registryArn.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
int lastSlash = registryArn.lastIndexOf('/');
|
||||
if (lastSlash < 0 || lastSlash == registryArn.length() - 1) {
|
||||
String serviceId = AwsArnUtils.resourceIfArnFor(registryArn, "servicediscovery")
|
||||
.filter(resource -> resource.startsWith("service/"))
|
||||
.map(resource -> resource.substring("service/".length()))
|
||||
.orElse("");
|
||||
if (serviceId.isEmpty() || serviceId.contains("/")) {
|
||||
LOG.warnv("Ignoring an ECS service registry whose registryArn names no Cloud Map service: {0}",
|
||||
registryArn);
|
||||
return null;
|
||||
}
|
||||
return registryArn.substring(lastSlash + 1);
|
||||
return serviceId;
|
||||
}
|
||||
|
||||
/** ECS registers a task under its task id, so a replacement task supersedes its predecessor. */
|
||||
private String instanceId(EcsTask task) {
|
||||
String arn = task.getTaskArn();
|
||||
int lastSlash = arn.lastIndexOf('/');
|
||||
return lastSlash >= 0 ? arn.substring(lastSlash + 1) : arn;
|
||||
return nameFromArn(task.getTaskArn());
|
||||
}
|
||||
|
||||
private static String string(Map<String, Object> registry, String key) {
|
||||
|
||||
+9
-4
@@ -1506,7 +1506,8 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
"Parameter EventSourceArn is not valid. Reason: Creating cross-region archive is not permitted.",
|
||||
400);
|
||||
}
|
||||
String busName = eventSourceArn.substring(eventSourceArn.indexOf('/') + 1);
|
||||
String busName = source != null && source.resource().startsWith("event-bus/")
|
||||
? source.resource().substring("event-bus/".length()) : eventSourceArn;
|
||||
EventBus bus = "default".equals(busName)
|
||||
? getOrCreateDefaultBus(region)
|
||||
: busStore.get(busKey(region, busName)).orElse(null);
|
||||
@@ -2167,9 +2168,13 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
}
|
||||
|
||||
private static String archiveNameFromArn(String arn) {
|
||||
if (arn == null) return null;
|
||||
int idx = arn.lastIndexOf("archive/");
|
||||
return idx >= 0 ? arn.substring(idx + "archive/".length()) : arn;
|
||||
if (arn == null) {
|
||||
return null;
|
||||
}
|
||||
return AwsArnUtils.resourceIfArnFor(arn, "events")
|
||||
.filter(resource -> resource.startsWith("archive/"))
|
||||
.map(resource -> resource.substring("archive/".length()))
|
||||
.orElse(arn);
|
||||
}
|
||||
|
||||
private void startSchedulerIfNeeded(Rule rule) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.ArchivedEvent;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.Replay;
|
||||
import io.github.hectorvent.floci.services.eventbridge.model.ReplayState;
|
||||
@@ -111,7 +112,9 @@ public class ReplayDispatcher {
|
||||
if (arn == null) {
|
||||
return "default";
|
||||
}
|
||||
int idx = arn.lastIndexOf("event-bus/");
|
||||
return idx >= 0 ? arn.substring(idx + "event-bus/".length()) : arn;
|
||||
return AwsArnUtils.resourceIfArnFor(arn, "events")
|
||||
.filter(resource -> resource.startsWith("event-bus/"))
|
||||
.map(resource -> resource.substring("event-bus/".length()))
|
||||
.orElse(arn);
|
||||
}
|
||||
}
|
||||
@@ -1135,16 +1135,12 @@ public class ResourceArnBuilder {
|
||||
* refused, so it is checked as written.
|
||||
*/
|
||||
private static String lambdaTaggedArn(String tagged, String accountId) {
|
||||
if (LambdaMicrovmsService.ownsArn(tagged)) {
|
||||
// Read as LambdaTagController and LambdaMicrovmsService read it: the Region is the fourth field,
|
||||
// the name what follows the last colon.
|
||||
String[] fields = tagged.split(":");
|
||||
String region = fields.length > 3 ? fields[3] : "";
|
||||
String type = tagged.contains(":microvm-image:") ? "microvm-image:"
|
||||
: tagged.contains(":network-connector:") ? "network-connector:" : null;
|
||||
if (type != null && !region.isBlank()) {
|
||||
return AwsArnUtils.Arn.of("lambda", region, accountId,
|
||||
type + tagged.substring(tagged.lastIndexOf(':') + 1)).toString();
|
||||
// The image or connector LambdaMicrovmsService tags: the caller's own, in the ARN's Region.
|
||||
Optional<String> microvmResource = LambdaMicrovmsService.taggedResource(tagged);
|
||||
if (microvmResource.isPresent()) {
|
||||
String region = AwsArnUtils.parse(tagged).region();
|
||||
if (!region.isBlank()) {
|
||||
return AwsArnUtils.Arn.of("lambda", region, accountId, microvmResource.get()).toString();
|
||||
}
|
||||
}
|
||||
if (!AwsArnUtils.looksLikeArn(tagged)) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.lambdamicrovms.LambdaMicrovmsService;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -50,8 +51,7 @@ public class LambdaTagController {
|
||||
* the ARN's resource type. The region comes from the ARN itself.
|
||||
*/
|
||||
private static String arnRegion(String arn) {
|
||||
String[] parts = arn.split(":"); // partition-literal: Lambda reads the fourth field of any tag target, ARN or not
|
||||
return parts.length > 3 ? parts[3] : "";
|
||||
return AwsArnUtils.parse(arn).region();
|
||||
}
|
||||
|
||||
@GET
|
||||
|
||||
+44
-13
@@ -53,6 +53,10 @@ public class LambdaMicrovmsService {
|
||||
private static final Pattern CONNECTOR_VERSION = Pattern.compile("[1-9][0-9]{0,17}");
|
||||
/** The one AWS-managed base image the catalog serves. */
|
||||
private static final String MANAGED_IMAGE = "al2023-1";
|
||||
private static final String IMAGE_RESOURCE = "microvm-image:";
|
||||
private static final String CONNECTOR_RESOURCE = "network-connector:";
|
||||
/** The name or id a tag target's image or connector resource carries: no version qualifier. */
|
||||
private static final Pattern TAGGED_NAME = Pattern.compile("[a-zA-Z0-9-_]+");
|
||||
|
||||
private final RegionResolver regionResolver;
|
||||
private final StorageFactory storageFactory;
|
||||
@@ -708,9 +712,36 @@ public class LambdaMicrovmsService {
|
||||
|
||||
/** True when the ARN names a MicroVM-family resource this service owns. */
|
||||
public static boolean ownsArn(String arn) {
|
||||
return arn != null && (arn.contains(":microvm-image:")
|
||||
|| arn.contains(":microvm:")
|
||||
|| arn.contains(":network-connector:"));
|
||||
String resource = AwsArnUtils.resourceIfArnFor(arn, "lambda").orElse("");
|
||||
return resource.startsWith(IMAGE_RESOURCE) || resource.startsWith("microvm:")
|
||||
|| resource.startsWith(CONNECTOR_RESOURCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* The image or connector a tag target names, {@code microvm-image:<name>} or
|
||||
* {@code network-connector:<id>}, read from a Lambda ARN. Empty for anything else, a
|
||||
* version-qualified connector included, which the TagResource pattern does not take.
|
||||
*/
|
||||
public static Optional<String> taggedResource(String arn) {
|
||||
return AwsArnUtils.resourceIfArnFor(arn, "lambda").filter(LambdaMicrovmsService::isTaggedResource);
|
||||
}
|
||||
|
||||
private static boolean isTaggedResource(String resource) {
|
||||
String name = resource.startsWith(IMAGE_RESOURCE) ? resource.substring(IMAGE_RESOURCE.length())
|
||||
: resource.startsWith(CONNECTOR_RESOURCE) ? resource.substring(CONNECTOR_RESOURCE.length())
|
||||
: null;
|
||||
return name != null && TAGGED_NAME.matcher(name).matches();
|
||||
}
|
||||
|
||||
/** The tag target's resource, refusing an image or connector the TagResource pattern does not take. */
|
||||
private static String tagTargetResource(String arn) {
|
||||
String resource = AwsArnUtils.resourceIfArnFor(arn, "lambda").orElse("");
|
||||
if ((resource.startsWith(IMAGE_RESOURCE) || resource.startsWith(CONNECTOR_RESOURCE))
|
||||
&& !isTaggedResource(resource)) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Resource must name an image or a network connector without a version: " + arn, 400);
|
||||
}
|
||||
return resource;
|
||||
}
|
||||
|
||||
public Map<String, String> listTags(String region, String arn) {
|
||||
@@ -734,14 +765,15 @@ public class LambdaMicrovmsService {
|
||||
* stored object, so mutating it alone would never reach the backend.
|
||||
*/
|
||||
private void mutateTags(String region, String arn, Consumer<Map<String, String>> mutation) {
|
||||
if (arn.contains(":microvm-image:")) {
|
||||
MicrovmImage image = getImage(region, arn.substring(arn.lastIndexOf(':') + 1));
|
||||
String resource = tagTargetResource(arn);
|
||||
if (resource.startsWith(IMAGE_RESOURCE)) {
|
||||
MicrovmImage image = getImage(region, resource.substring(IMAGE_RESOURCE.length()));
|
||||
mutation.accept(image.tags);
|
||||
persist(images, region, image.name, image);
|
||||
return;
|
||||
}
|
||||
if (arn.contains(":network-connector:")) {
|
||||
NetworkConnector connector = getConnector(region, arn.substring(arn.lastIndexOf(':') + 1));
|
||||
if (resource.startsWith(CONNECTOR_RESOURCE)) {
|
||||
NetworkConnector connector = getConnector(region, resource.substring(CONNECTOR_RESOURCE.length()));
|
||||
mutation.accept(connector.tags);
|
||||
persist(connectors, region, connector.id, connector);
|
||||
return;
|
||||
@@ -751,13 +783,12 @@ public class LambdaMicrovmsService {
|
||||
}
|
||||
|
||||
private Map<String, String> taggable(String region, String arn) {
|
||||
if (arn.contains(":microvm-image:")) {
|
||||
String name = arn.substring(arn.lastIndexOf(':') + 1);
|
||||
return getImage(region, name).tags;
|
||||
String resource = tagTargetResource(arn);
|
||||
if (resource.startsWith(IMAGE_RESOURCE)) {
|
||||
return getImage(region, resource.substring(IMAGE_RESOURCE.length())).tags;
|
||||
}
|
||||
if (arn.contains(":network-connector:")) {
|
||||
String id = arn.substring(arn.lastIndexOf(':') + 1);
|
||||
return getConnector(region, id).tags;
|
||||
if (resource.startsWith(CONNECTOR_RESOURCE)) {
|
||||
return getConnector(region, resource.substring(CONNECTOR_RESOURCE.length())).tags;
|
||||
}
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"Resource not found: " + arn, 404);
|
||||
|
||||
@@ -248,7 +248,7 @@ public class PipesPoller implements Resettable {
|
||||
|
||||
void pollKinesis(Pipe pipe, String region) {
|
||||
String pipeKey = pipeKey(pipe);
|
||||
String streamName = extractResourceName(pipe.getSource());
|
||||
String streamName = kinesisStreamName(pipe.getSource());
|
||||
String pipeAccountId = pipe.getAccountId();
|
||||
int batchSize = getBatchSize(pipe, "KinesisStreamParameters");
|
||||
String iterator = kinesisIterators.get(pipeKey);
|
||||
@@ -922,12 +922,14 @@ public class PipesPoller implements Resettable {
|
||||
return AwsArnUtils.regionOrDefault(arn, defaultRegion);
|
||||
}
|
||||
|
||||
private static String extractResourceName(String arn) {
|
||||
int slashIdx = arn.lastIndexOf('/');
|
||||
if (slashIdx >= 0) {
|
||||
return arn.substring(slashIdx + 1);
|
||||
}
|
||||
int colonIdx = arn.lastIndexOf(':');
|
||||
return colonIdx >= 0 ? arn.substring(colonIdx + 1) : arn;
|
||||
/**
|
||||
* The stream a Kinesis source names: the segment after {@code stream/}, so a stream-consumer ARN
|
||||
* ({@code stream/S/consumer/C:123}) still names stream {@code S}.
|
||||
*/
|
||||
static String kinesisStreamName(String source) {
|
||||
String resource = AwsArnUtils.resourceIfArnFor(source, "kinesis").orElse(source);
|
||||
String name = resource.startsWith("stream/") ? resource.substring("stream/".length()) : resource;
|
||||
int slash = name.indexOf('/');
|
||||
return slash >= 0 ? name.substring(0, slash) : name;
|
||||
}
|
||||
}
|
||||
@@ -176,7 +176,10 @@ public class PipesTargetInvoker {
|
||||
}
|
||||
|
||||
private void invokeEventBridge(String arn, String payload, String region, JsonNode tp) {
|
||||
String busName = arn.substring(arn.lastIndexOf('/') + 1);
|
||||
String busName = AwsArnUtils.resourceIfArnFor(arn, "events")
|
||||
.filter(resource -> resource.startsWith("event-bus/"))
|
||||
.map(resource -> resource.substring("event-bus/".length()))
|
||||
.orElse(arn);
|
||||
String ebRegion = extractRegionFromArn(arn, region);
|
||||
String source = "aws.pipes";
|
||||
String detailType = "PipeForwarded";
|
||||
|
||||
Reference in new issue
Block a user