mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(lambda): resolve ARN and function URL invocations in owning account (#2414)
* fix(lambda): resolve ARN and function URL invocations in owning account * test(lambda): adapt account routing fixtures to layer service * fix(lambda): migrate legacy ARN invocation targets
This commit is contained in:
1 parent
06d58aad36
commit
c22652dd15
13 files changed
+698
-15
No files matched your search
+1
-3
@@ -89,9 +89,7 @@ public class EventBridgeInvoker {
|
||||
|
||||
try {
|
||||
if (arn.contains(":lambda:") || arn.contains(":function:")) {
|
||||
String fnName = arn.substring(arn.lastIndexOf(':') + 1);
|
||||
String fnRegion = extractRegionFromArn(arn, region);
|
||||
lambdaService.invoke(fnRegion, fnName, payload.getBytes(), InvocationType.Event);
|
||||
lambdaService.invokeArn(arn, payload.getBytes(), InvocationType.Event);
|
||||
LOG.debugv("EventBridge delivered to Lambda: {0}", arn);
|
||||
} else if (arn.contains(":sqs:")) {
|
||||
String queueUrl = AwsArnUtils.arnToQueueUrl(arn, baseUrl);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaAlias;
|
||||
@@ -31,7 +33,10 @@ public class LambdaAliasStore {
|
||||
}
|
||||
|
||||
private void loadIndex() {
|
||||
backend.scan(k -> true).forEach(this::indexAlias);
|
||||
List<LambdaAlias> all = backend instanceof AccountAwareStorageBackend<LambdaAlias> aware
|
||||
? aware.scanAllAccounts()
|
||||
: backend.scan(k -> true);
|
||||
all.forEach(this::indexAlias);
|
||||
}
|
||||
|
||||
private void indexAlias(LambdaAlias alias) {
|
||||
@@ -70,6 +75,17 @@ public class LambdaAliasStore {
|
||||
return backend.get(key(region, functionName, aliasName));
|
||||
}
|
||||
|
||||
public Optional<LambdaAlias> getForAccount(
|
||||
String accountId, String region, String functionName, String aliasName) {
|
||||
if (backend instanceof AccountAwareStorageBackend<LambdaAlias> aware) {
|
||||
return aware.getForAccountMigratingLegacy(
|
||||
accountId,
|
||||
key(region, functionName, aliasName),
|
||||
alias -> accountId.equals(AwsArnUtils.accountOrDefault(alias.getAliasArn(), "")));
|
||||
}
|
||||
return backend.get(key(region, functionName, aliasName));
|
||||
}
|
||||
|
||||
public Optional<LambdaAlias> getByUrlId(String urlId) {
|
||||
return Optional.ofNullable(urlIdIndex.get(urlId));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.Resettable;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
@@ -90,10 +91,25 @@ public class LambdaFunctionStore implements Resettable {
|
||||
}
|
||||
|
||||
public Optional<LambdaFunction> getForAccount(String accountId, String region, String functionName) {
|
||||
return getForAccount(accountId, region, functionName, "$LATEST");
|
||||
}
|
||||
|
||||
public Optional<LambdaFunction> getForAccount(
|
||||
String accountId, String region, String functionName, String version) {
|
||||
if (backend instanceof AccountAwareStorageBackend<LambdaFunction> aware) {
|
||||
return aware.getForAccount(accountId, regionKey(region, functionName, "$LATEST"));
|
||||
return aware.getForAccountMigratingLegacy(
|
||||
accountId,
|
||||
regionKey(region, functionName, version),
|
||||
function -> belongsToAccount(function, accountId));
|
||||
}
|
||||
return backend.get(regionKey(region, functionName, "$LATEST"));
|
||||
return backend.get(regionKey(region, functionName, version));
|
||||
}
|
||||
|
||||
private static boolean belongsToAccount(LambdaFunction function, String accountId) {
|
||||
if (function.getAccountId() != null && !function.getAccountId().isBlank()) {
|
||||
return accountId.equals(function.getAccountId());
|
||||
}
|
||||
return accountId.equals(AwsArnUtils.accountOrDefault(function.getFunctionArn(), ""));
|
||||
}
|
||||
|
||||
public Optional<LambdaFunction> getByUrlId(String urlId) {
|
||||
|
||||
@@ -669,7 +669,24 @@ public class LambdaService {
|
||||
enforceRegion(region, ref);
|
||||
String name = ref.name();
|
||||
String qualifier = ref.qualifier();
|
||||
LambdaFunction fn = resolveInvokeTarget(region, name, qualifier);
|
||||
LambdaFunction fn;
|
||||
if (functionName.startsWith("arn:")) {
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(functionName);
|
||||
fn = resolveInvokeTargetForAccount(arn.accountId(), region, name, qualifier);
|
||||
} else {
|
||||
fn = resolveInvokeTarget(region, name, qualifier);
|
||||
}
|
||||
InvokeResult result = executorService.invoke(fn, payload, type);
|
||||
result.setExecutedVersion(fn.getVersion());
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Invokes a Lambda target ARN using the account encoded in that ARN. */
|
||||
public InvokeResult invokeArn(String functionArn, byte[] payload, InvocationType type) {
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(functionArn);
|
||||
LambdaArnUtils.ResolvedFunctionRef ref = LambdaArnUtils.resolve(functionArn);
|
||||
LambdaFunction fn = resolveInvokeTargetForAccount(
|
||||
arn.accountId(), arn.region(), ref.name(), ref.qualifier());
|
||||
InvokeResult result = executorService.invoke(fn, payload, type);
|
||||
result.setExecutedVersion(fn.getVersion());
|
||||
return result;
|
||||
@@ -697,6 +714,37 @@ public class LambdaService {
|
||||
"Function version not found: " + name + ":" + version, 404));
|
||||
}
|
||||
|
||||
private LambdaFunction resolveInvokeTargetForAccount(
|
||||
String accountId, String region, String name, String qualifier) {
|
||||
if (qualifier == null || qualifier.equals("$LATEST")) {
|
||||
return functionStore.getForAccount(accountId, region, name)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"Function not found: " + name, 404));
|
||||
}
|
||||
if (qualifier.chars().allMatch(Character::isDigit)) {
|
||||
return functionStore.getForAccount(accountId, region, name, qualifier)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"Function version not found: " + name + ":" + qualifier, 404));
|
||||
}
|
||||
LambdaAlias alias = aliasStore != null
|
||||
? aliasStore.getForAccount(accountId, region, name, qualifier)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"Alias not found: " + qualifier, 404))
|
||||
: null;
|
||||
if (alias == null) {
|
||||
throw new AwsException("ResourceNotFoundException", "Alias not found: " + qualifier, 404);
|
||||
}
|
||||
String version = pickAliasVersion(alias);
|
||||
if (version == null || version.equals("$LATEST")) {
|
||||
return functionStore.getForAccount(accountId, region, name)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"Function not found: " + name, 404));
|
||||
}
|
||||
return functionStore.getForAccount(accountId, region, name, version)
|
||||
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
|
||||
"Function version not found: " + name + ":" + version, 404));
|
||||
}
|
||||
|
||||
private String pickAliasVersion(LambdaAlias alias) {
|
||||
java.util.Map<String, Double> weights = alias.getRoutingConfig();
|
||||
if (weights == null || weights.isEmpty()) {
|
||||
@@ -1240,7 +1288,12 @@ public class LambdaService {
|
||||
urlConfig.setInvokeMode((String) request.get("InvokeMode"));
|
||||
}
|
||||
|
||||
String urlId = UUID.nameUUIDFromBytes((region + functionName + (qualifier != null ? qualifier : "")).getBytes()).toString().replace("-", "").substring(0, 32);
|
||||
String accountId = regionResolver.getAccountId();
|
||||
String urlId = UUID.nameUUIDFromBytes(
|
||||
(accountId + region + functionName + (qualifier != null ? qualifier : "")).getBytes())
|
||||
.toString()
|
||||
.replace("-", "")
|
||||
.substring(0, 32);
|
||||
String baseHost = config.effectiveBaseUrl().replaceFirst("https?://", "");
|
||||
String url = String.format("http://%s.lambda-url.%s.%s/", urlId, region, baseHost);
|
||||
urlConfig.setFunctionUrl(url);
|
||||
|
||||
+19
-4
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.lambda;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaAlias;
|
||||
@@ -49,13 +50,15 @@ public class LambdaUrlInvocationController {
|
||||
private final LambdaService lambdaService;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final RequestContext requestContext;
|
||||
|
||||
@Inject
|
||||
public LambdaUrlInvocationController(LambdaService lambdaService, RegionResolver regionResolver,
|
||||
ObjectMapper objectMapper) {
|
||||
ObjectMapper objectMapper, RequestContext requestContext) {
|
||||
this.lambdaService = lambdaService;
|
||||
this.regionResolver = regionResolver;
|
||||
this.objectMapper = objectMapper;
|
||||
this.requestContext = requestContext;
|
||||
}
|
||||
|
||||
@GET
|
||||
@@ -96,25 +99,37 @@ public class LambdaUrlInvocationController {
|
||||
private Response invoke(String method, String urlId, String proxy, HttpHeaders headers, UriInfo uriInfo, byte[] body) {
|
||||
Object target = lambdaService.getTargetByUrlId(urlId);
|
||||
String functionName;
|
||||
String functionArn;
|
||||
String region;
|
||||
String accountId;
|
||||
|
||||
if (target instanceof LambdaAlias alias) {
|
||||
functionName = alias.getFunctionName();
|
||||
region = AwsArnUtils.parse(alias.getAliasArn()).region();
|
||||
functionArn = alias.getAliasArn();
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(functionArn);
|
||||
region = arn.region();
|
||||
accountId = arn.accountId();
|
||||
} else if (target instanceof LambdaFunction fn) {
|
||||
functionName = fn.getFunctionName();
|
||||
region = AwsArnUtils.parse(fn.getFunctionArn()).region();
|
||||
functionArn = fn.getFunctionArn();
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(functionArn);
|
||||
region = arn.region();
|
||||
accountId = fn.getAccountId() != null ? fn.getAccountId() : arn.accountId();
|
||||
} else {
|
||||
return Response.status(404).entity(jsonMessage("Function URL not found")).type(MediaType.APPLICATION_JSON).build();
|
||||
}
|
||||
|
||||
requestContext.setAccountId(accountId);
|
||||
requestContext.setRegion(region);
|
||||
|
||||
String requestId = UUID.randomUUID().toString();
|
||||
String event = buildEvent(method, urlId, proxy, headers, uriInfo, body, requestId, region);
|
||||
|
||||
LOG.infov("Lambda URL invocation: {0} {1} -> {2} (region: {3})", method, urlId, functionName, region);
|
||||
|
||||
try {
|
||||
InvokeResult result = lambdaService.invoke(region, functionName, event.getBytes(), InvocationType.RequestResponse);
|
||||
InvokeResult result = lambdaService.invokeArn(
|
||||
functionArn, event.getBytes(), InvocationType.RequestResponse);
|
||||
return buildResponse(result);
|
||||
} catch (AwsException e) {
|
||||
return Response.status(e.getHttpStatus()).entity(e.getMessage()).build();
|
||||
|
||||
@@ -87,8 +87,7 @@ public class ScheduleInvoker {
|
||||
sqsService.sendMessage(queueUrl, payload, 0, messageGroupId, null, targetRegion);
|
||||
LOG.debugv("Scheduler delivered to SQS: {0}", arn);
|
||||
} else if (arn.contains(":lambda:") || arn.contains(":function:")) {
|
||||
String fnName = arn.substring(arn.lastIndexOf(':') + 1);
|
||||
lambdaService.invoke(targetRegion, fnName, payload.getBytes(), InvocationType.Event);
|
||||
lambdaService.invokeArn(arn, payload.getBytes(), InvocationType.Event);
|
||||
LOG.debugv("Scheduler delivered to Lambda: {0}", arn);
|
||||
} else if (arn.contains(":sns:")) {
|
||||
snsService.publish(arn, null, payload, "Scheduler", targetRegion);
|
||||
|
||||
Reference in new issue
Block a user