mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
+16
-1
@@ -22,12 +22,14 @@ import java.util.Map;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.AdditionalMatchers.aryEq;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class EventBridgeInvokerTest {
|
||||
|
||||
private EventBridgeInvoker invoker;
|
||||
private LambdaService lambdaService;
|
||||
private SqsService sqsService;
|
||||
private BatchService batchService;
|
||||
private FirehoseService firehoseService;
|
||||
@@ -36,7 +38,7 @@ class EventBridgeInvokerTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
LambdaService lambdaService = mock(LambdaService.class);
|
||||
lambdaService = mock(LambdaService.class);
|
||||
sqsService = mock(SqsService.class);
|
||||
SnsService snsService = mock(SnsService.class);
|
||||
batchService = mock(BatchService.class);
|
||||
@@ -59,6 +61,19 @@ class EventBridgeInvokerTest {
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeTarget_lambdaTargetPreservesArnAccount() {
|
||||
String arn = "arn:aws:lambda:ap-south-1:100000000012:function:cross-account-function";
|
||||
Target target = new Target("id1", arn, "{\"detail\":{\"job\":\"workflow-recovery\"}}", null);
|
||||
|
||||
invoker.invokeTarget(target, "{\"ignored\":true}", "ap-south-1");
|
||||
|
||||
verify(lambdaService).invokeArn(
|
||||
eq(arn),
|
||||
aryEq("{\"detail\":{\"job\":\"workflow-recovery\"}}".getBytes()),
|
||||
eq(io.github.hectorvent.floci.services.lambda.model.InvocationType.Event));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeTarget_sqsTarget_usesSuppliedRegion() {
|
||||
Target target = new Target("id1", "arn:aws:sqs:eu-west-1:000000000000:my-queue", null, null);
|
||||
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@QuarkusTest
|
||||
class LambdaArnInvocationAccountIntegrationTest {
|
||||
|
||||
private static final String CALLER_ACCOUNT = "234567890123";
|
||||
private static final String TARGET_ACCOUNT = "345678901234";
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String FUNCTION_NAME = "arn-invocation-account-routing";
|
||||
|
||||
@Test
|
||||
void fullArnInvocationUsesArnAccountInsteadOfCallerAccount() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization(TARGET_ACCOUNT))
|
||||
.body("""
|
||||
{
|
||||
"FunctionName":"%s",
|
||||
"Runtime":"nodejs20.x",
|
||||
"Role":"arn:aws:iam::%s:role/lambda-role",
|
||||
"Handler":"index.handler"
|
||||
}
|
||||
""".formatted(FUNCTION_NAME, TARGET_ACCOUNT))
|
||||
.when().post("/2015-03-31/functions")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
String functionArn = "arn:aws:lambda:" + REGION + ":" + TARGET_ACCOUNT
|
||||
+ ":function:" + FUNCTION_NAME;
|
||||
|
||||
given()
|
||||
.header("Authorization", authorization(CALLER_ACCOUNT))
|
||||
.header("X-Amz-Invocation-Type", "DryRun")
|
||||
.contentType("application/json")
|
||||
.body("{}")
|
||||
.when().post("/2015-03-31/functions/{functionArn}/invocations", functionArn)
|
||||
.then()
|
||||
.statusCode(204)
|
||||
.header("X-Amz-Executed-Version", equalTo("$LATEST"));
|
||||
}
|
||||
|
||||
private String authorization(String accountId) {
|
||||
return "AWS4-HMAC-SHA256 Credential=" + accountId
|
||||
+ "/20260812/" + REGION + "/lambda/aws4_request";
|
||||
}
|
||||
}
|
||||
+267
@@ -0,0 +1,267 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
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;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import io.github.hectorvent.floci.services.lambda.zip.CodeStore;
|
||||
import io.github.hectorvent.floci.services.lambda.zip.ZipExtractor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.AdditionalMatchers.aryEq;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class LambdaArnInvocationAccountTest {
|
||||
|
||||
@Test
|
||||
void invokeArnResolvesFunctionFromArnAccountOutsideRequestContext() {
|
||||
String defaultAccount = "000000000000";
|
||||
String targetAccount = "100000000012";
|
||||
String region = "ap-south-1";
|
||||
String functionName = "cross-account-function";
|
||||
String functionArn = "arn:aws:lambda:" + region + ":" + targetAccount
|
||||
+ ":function:" + functionName;
|
||||
|
||||
AccountAwareStorageBackend<LambdaFunction> backend = new AccountAwareStorageBackend<>(
|
||||
new InMemoryStorage<>(), null, defaultAccount);
|
||||
LambdaFunctionStore store = new LambdaFunctionStore(backend);
|
||||
LambdaFunction function = new LambdaFunction();
|
||||
function.setAccountId(targetAccount);
|
||||
function.setFunctionName(functionName);
|
||||
function.setFunctionArn(functionArn);
|
||||
function.setVersion("$LATEST");
|
||||
backend.putForAccount(targetAccount,
|
||||
"lambda::" + region + "::" + functionName + "::$LATEST", function);
|
||||
|
||||
LambdaExecutorService executor = mock(LambdaExecutorService.class);
|
||||
InvokeResult executorResult = new InvokeResult();
|
||||
when(executor.invoke(eq(function), aryEq("{}".getBytes()), eq(InvocationType.Event)))
|
||||
.thenReturn(executorResult);
|
||||
LambdaService service = new LambdaService(
|
||||
store,
|
||||
executor,
|
||||
new LambdaConcurrencyLimiter(),
|
||||
new WarmPool(),
|
||||
new CodeStore(Path.of("target/test-data/lambda-code")),
|
||||
new ZipExtractor(),
|
||||
null,
|
||||
new RegionResolver(region, defaultAccount),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
|
||||
InvokeResult result = service.invokeArn(functionArn, "{}".getBytes(), InvocationType.Event);
|
||||
|
||||
assertEquals("$LATEST", result.getExecutedVersion());
|
||||
verify(executor).invoke(eq(function), aryEq("{}".getBytes()), eq(InvocationType.Event));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeArnResolvesVersionAndAliasFromArnAccount() {
|
||||
String defaultAccount = "000000000000";
|
||||
String targetAccount = "100000000012";
|
||||
String region = "ap-south-1";
|
||||
String functionName = "versioned-account-function";
|
||||
String functionArn = "arn:aws:lambda:" + region + ":" + targetAccount
|
||||
+ ":function:" + functionName;
|
||||
|
||||
AccountAwareStorageBackend<LambdaFunction> functionBackend = new AccountAwareStorageBackend<>(
|
||||
new InMemoryStorage<>(), null, defaultAccount);
|
||||
LambdaFunctionStore functionStore = new LambdaFunctionStore(functionBackend);
|
||||
LambdaFunction version = new LambdaFunction();
|
||||
version.setAccountId(targetAccount);
|
||||
version.setFunctionName(functionName);
|
||||
version.setFunctionArn(functionArn + ":7");
|
||||
version.setVersion("7");
|
||||
functionBackend.putForAccount(targetAccount,
|
||||
"lambda::" + region + "::" + functionName + "::7", version);
|
||||
|
||||
AccountAwareStorageBackend<LambdaAlias> aliasBackend = new AccountAwareStorageBackend<>(
|
||||
new InMemoryStorage<>(), null, defaultAccount);
|
||||
LambdaAliasStore aliasStore = new LambdaAliasStore(aliasBackend);
|
||||
LambdaAlias alias = new LambdaAlias();
|
||||
alias.setName("live");
|
||||
alias.setFunctionName(functionName);
|
||||
alias.setFunctionVersion("7");
|
||||
alias.setAliasArn(functionArn + ":live");
|
||||
aliasBackend.putForAccount(targetAccount,
|
||||
"alias::" + region + "::" + functionName + "::live", alias);
|
||||
|
||||
LambdaExecutorService executor = mock(LambdaExecutorService.class);
|
||||
when(executor.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event)))
|
||||
.thenAnswer(ignored -> new InvokeResult());
|
||||
LambdaService service = new LambdaService(
|
||||
functionStore,
|
||||
executor,
|
||||
new LambdaConcurrencyLimiter(),
|
||||
new WarmPool(),
|
||||
new CodeStore(Path.of("target/test-data/lambda-code")),
|
||||
new ZipExtractor(),
|
||||
null,
|
||||
new RegionResolver(region, defaultAccount),
|
||||
null,
|
||||
aliasStore,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
|
||||
InvokeResult versionResult = service.invokeArn(
|
||||
functionArn + ":7", "{}".getBytes(), InvocationType.Event);
|
||||
InvokeResult aliasResult = service.invokeArn(
|
||||
functionArn + ":live", "{}".getBytes(), InvocationType.Event);
|
||||
|
||||
assertEquals("7", versionResult.getExecutedVersion());
|
||||
assertEquals("7", aliasResult.getExecutedVersion());
|
||||
verify(executor, org.mockito.Mockito.times(2))
|
||||
.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeArnMigratesOwnedLegacyFunctionVersionAndAlias() {
|
||||
String defaultAccount = "000000000000";
|
||||
String targetAccount = "100000000012";
|
||||
String region = "ap-south-1";
|
||||
String functionName = "legacy-account-function";
|
||||
String functionArn = "arn:aws:lambda:" + region + ":" + targetAccount
|
||||
+ ":function:" + functionName;
|
||||
String latestKey = "lambda::" + region + "::" + functionName + "::$LATEST";
|
||||
String versionKey = "lambda::" + region + "::" + functionName + "::7";
|
||||
String aliasKey = "alias::" + region + "::" + functionName + "::live";
|
||||
|
||||
InMemoryStorage<String, LambdaFunction> rawFunctions = new InMemoryStorage<>();
|
||||
LambdaFunction latest = function(functionName, functionArn, "$LATEST");
|
||||
LambdaFunction version = function(functionName, functionArn + ":7", "7");
|
||||
rawFunctions.put(latestKey, latest);
|
||||
rawFunctions.put(versionKey, version);
|
||||
LambdaFunctionStore functionStore = new LambdaFunctionStore(
|
||||
new AccountAwareStorageBackend<>(rawFunctions, null, defaultAccount));
|
||||
|
||||
InMemoryStorage<String, LambdaAlias> rawAliases = new InMemoryStorage<>();
|
||||
LambdaAlias alias = new LambdaAlias();
|
||||
alias.setName("live");
|
||||
alias.setFunctionName(functionName);
|
||||
alias.setFunctionVersion("7");
|
||||
alias.setAliasArn(functionArn + ":live");
|
||||
rawAliases.put(aliasKey, alias);
|
||||
LambdaAliasStore aliasStore = new LambdaAliasStore(
|
||||
new AccountAwareStorageBackend<>(rawAliases, null, defaultAccount));
|
||||
|
||||
LambdaExecutorService executor = mock(LambdaExecutorService.class);
|
||||
when(executor.invoke(eq(latest), aryEq("{}".getBytes()), eq(InvocationType.Event)))
|
||||
.thenAnswer(ignored -> new InvokeResult());
|
||||
when(executor.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event)))
|
||||
.thenAnswer(ignored -> new InvokeResult());
|
||||
LambdaService service = service(functionStore, aliasStore, executor, region, defaultAccount);
|
||||
|
||||
service.invokeArn(functionArn, "{}".getBytes(), InvocationType.Event);
|
||||
service.invokeArn(functionArn + ":7", "{}".getBytes(), InvocationType.Event);
|
||||
service.invokeArn(functionArn + ":live", "{}".getBytes(), InvocationType.Event);
|
||||
|
||||
assertTrue(rawFunctions.get(latestKey).isEmpty());
|
||||
assertTrue(rawFunctions.get(versionKey).isEmpty());
|
||||
assertEquals(latest, rawFunctions.get(targetAccount + "/" + latestKey).orElseThrow());
|
||||
assertEquals(version, rawFunctions.get(targetAccount + "/" + versionKey).orElseThrow());
|
||||
assertTrue(rawAliases.get(aliasKey).isEmpty());
|
||||
assertEquals(alias, rawAliases.get(targetAccount + "/" + aliasKey).orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void invokeArnDoesNotMigrateForeignLegacyFunctionOrAlias() {
|
||||
String defaultAccount = "000000000000";
|
||||
String targetAccount = "100000000012";
|
||||
String foreignAccount = "200000000034";
|
||||
String region = "ap-south-1";
|
||||
String functionName = "legacy-account-function";
|
||||
String targetArn = "arn:aws:lambda:" + region + ":" + targetAccount
|
||||
+ ":function:" + functionName;
|
||||
String functionKey = "lambda::" + region + "::" + functionName + "::$LATEST";
|
||||
|
||||
InMemoryStorage<String, LambdaFunction> rawFunctions = new InMemoryStorage<>();
|
||||
LambdaFunction foreignFunction = function(
|
||||
functionName,
|
||||
"arn:aws:lambda:" + region + ":" + foreignAccount + ":function:" + functionName,
|
||||
"$LATEST");
|
||||
rawFunctions.put(functionKey, foreignFunction);
|
||||
LambdaFunctionStore functionStore = new LambdaFunctionStore(
|
||||
new AccountAwareStorageBackend<>(rawFunctions, null, defaultAccount));
|
||||
|
||||
String aliasKey = "alias::" + region + "::" + functionName + "::live";
|
||||
InMemoryStorage<String, LambdaAlias> rawAliases = new InMemoryStorage<>();
|
||||
LambdaAlias foreignAlias = new LambdaAlias();
|
||||
foreignAlias.setName("live");
|
||||
foreignAlias.setFunctionName(functionName);
|
||||
foreignAlias.setFunctionVersion("7");
|
||||
foreignAlias.setAliasArn(
|
||||
"arn:aws:lambda:" + region + ":" + foreignAccount + ":function:" + functionName + ":live");
|
||||
rawAliases.put(aliasKey, foreignAlias);
|
||||
LambdaAliasStore aliasStore = new LambdaAliasStore(
|
||||
new AccountAwareStorageBackend<>(rawAliases, null, defaultAccount));
|
||||
LambdaService service = service(
|
||||
functionStore, aliasStore, mock(LambdaExecutorService.class), region, defaultAccount);
|
||||
|
||||
assertThrows(AwsException.class,
|
||||
() -> service.invokeArn(targetArn, "{}".getBytes(), InvocationType.Event));
|
||||
assertThrows(AwsException.class,
|
||||
() -> service.invokeArn(targetArn + ":live", "{}".getBytes(), InvocationType.Event));
|
||||
assertEquals(foreignFunction, rawFunctions.get(functionKey).orElseThrow());
|
||||
assertTrue(rawFunctions.get(targetAccount + "/" + functionKey).isEmpty());
|
||||
assertEquals(foreignAlias, rawAliases.get(aliasKey).orElseThrow());
|
||||
assertTrue(rawAliases.get(targetAccount + "/" + aliasKey).isEmpty());
|
||||
}
|
||||
|
||||
private static LambdaFunction function(String functionName, String functionArn, String version) {
|
||||
LambdaFunction function = new LambdaFunction();
|
||||
function.setFunctionName(functionName);
|
||||
function.setFunctionArn(functionArn);
|
||||
function.setVersion(version);
|
||||
return function;
|
||||
}
|
||||
|
||||
private static LambdaService service(
|
||||
LambdaFunctionStore functionStore,
|
||||
LambdaAliasStore aliasStore,
|
||||
LambdaExecutorService executor,
|
||||
String region,
|
||||
String defaultAccount) {
|
||||
return new LambdaService(
|
||||
functionStore,
|
||||
executor,
|
||||
new LambdaConcurrencyLimiter(),
|
||||
new WarmPool(),
|
||||
new CodeStore(Path.of("target/test-data/lambda-code")),
|
||||
new ZipExtractor(),
|
||||
null,
|
||||
new RegionResolver(region, defaultAccount),
|
||||
null,
|
||||
aliasStore,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
}
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
@QuarkusTest
|
||||
class LambdaFunctionUrlAccountRoutingTest {
|
||||
|
||||
private static final String ACCOUNT_A = "345678901234";
|
||||
private static final String ACCOUNT_B = "456789012345";
|
||||
private static final String FUNCTION_NAME = "function-url-multi-account";
|
||||
|
||||
@Test
|
||||
void functionUrlIdsAndInvocationsAreAccountScoped() throws Exception {
|
||||
String urlA = createFunctionUrl(ACCOUNT_A);
|
||||
String urlB = createFunctionUrl(ACCOUNT_B);
|
||||
|
||||
assertNotEquals(urlA, urlB);
|
||||
|
||||
given()
|
||||
.when().get("/lambda-url/" + urlId(urlA) + "/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("accountId", equalTo(ACCOUNT_A));
|
||||
|
||||
given()
|
||||
.when().get("/lambda-url/" + urlId(urlB) + "/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("accountId", equalTo(ACCOUNT_B));
|
||||
}
|
||||
|
||||
private String createFunctionUrl(String accountId) throws Exception {
|
||||
String authorization = authorization(accountId, "lambda");
|
||||
String zip = Base64.getEncoder().encodeToString(lambdaZip());
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization)
|
||||
.body("""
|
||||
{
|
||||
"FunctionName":"%s",
|
||||
"Runtime":"nodejs20.x",
|
||||
"Role":"arn:aws:iam::%s:role/lambda-role",
|
||||
"Handler":"index.handler",
|
||||
"Code":{"ZipFile":"%s"}
|
||||
}
|
||||
""".formatted(FUNCTION_NAME, accountId, zip))
|
||||
.when().post("/2015-03-31/functions")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
|
||||
return given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", authorization)
|
||||
.body("""
|
||||
{"AuthType":"NONE"}
|
||||
""")
|
||||
.when().post("/2021-10-31/functions/" + FUNCTION_NAME + "/url")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.extract().path("FunctionUrl");
|
||||
}
|
||||
|
||||
private byte[] lambdaZip() throws Exception {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
try (ZipOutputStream zip = new ZipOutputStream(bytes)) {
|
||||
zip.putNextEntry(new ZipEntry("index.js"));
|
||||
zip.write("""
|
||||
exports.handler = async (event) => ({
|
||||
statusCode: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ accountId: event.requestContext.accountId })
|
||||
});
|
||||
""".getBytes(StandardCharsets.UTF_8));
|
||||
zip.closeEntry();
|
||||
}
|
||||
return bytes.toByteArray();
|
||||
}
|
||||
|
||||
private String urlId(String functionUrl) {
|
||||
int scheme = functionUrl.indexOf("://");
|
||||
int firstDot = functionUrl.indexOf('.', scheme + 3);
|
||||
return functionUrl.substring(scheme + 3, firstDot);
|
||||
}
|
||||
|
||||
private String authorization(String accountId, String service) {
|
||||
return "AWS4-HMAC-SHA256 Credential=" + accountId
|
||||
+ "/20260803/us-east-1/" + service + "/aws4_request";
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
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;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class LambdaUrlInvocationControllerTest {
|
||||
|
||||
@Test
|
||||
void aliasUrlInvokesOwningAccountAliasArn() {
|
||||
String accountId = "100000000012";
|
||||
String region = "ap-south-1";
|
||||
String aliasArn = "arn:aws:lambda:" + region + ":" + accountId
|
||||
+ ":function:account-function:live";
|
||||
LambdaAlias alias = new LambdaAlias();
|
||||
alias.setFunctionName("account-function");
|
||||
alias.setName("live");
|
||||
alias.setAliasArn(aliasArn);
|
||||
|
||||
LambdaService lambdaService = mock(LambdaService.class);
|
||||
when(lambdaService.getTargetByUrlId("url-id")).thenReturn(alias);
|
||||
InvokeResult invokeResult = new InvokeResult();
|
||||
invokeResult.setStatusCode(200);
|
||||
invokeResult.setPayload("{\"statusCode\":200,\"body\":\"ok\"}"
|
||||
.getBytes(StandardCharsets.UTF_8));
|
||||
when(lambdaService.invokeArn(eq(aliasArn), any(byte[].class), eq(InvocationType.RequestResponse)))
|
||||
.thenReturn(invokeResult);
|
||||
|
||||
RegionResolver regionResolver = mock(RegionResolver.class);
|
||||
when(regionResolver.getAccountId()).thenReturn(accountId);
|
||||
RequestContext requestContext = new RequestContext();
|
||||
LambdaUrlInvocationController controller = new LambdaUrlInvocationController(
|
||||
lambdaService, regionResolver, new ObjectMapper(), requestContext);
|
||||
|
||||
HttpHeaders headers = mock(HttpHeaders.class);
|
||||
when(headers.getRequestHeaders()).thenReturn(new MultivaluedHashMap<>());
|
||||
UriInfo uriInfo = mock(UriInfo.class);
|
||||
when(uriInfo.getRequestUri()).thenReturn(URI.create("http://localhost/lambda-url/url-id/"));
|
||||
when(uriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap<>());
|
||||
|
||||
Response response = controller.handleGet("url-id", "", headers, uriInfo);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals(accountId, requestContext.getAccountId());
|
||||
assertEquals(region, requestContext.getRegion());
|
||||
ArgumentCaptor<byte[]> event = ArgumentCaptor.forClass(byte[].class);
|
||||
verify(lambdaService).invokeArn(eq(aliasArn), event.capture(), eq(InvocationType.RequestResponse));
|
||||
assertTrue(new String(event.getValue(), StandardCharsets.UTF_8)
|
||||
.contains("\"accountId\":\"" + accountId + "\""));
|
||||
}
|
||||
}
|
||||
@@ -169,6 +169,22 @@ class ScheduleInvokerTest {
|
||||
eq("group-7"), isNull(), eq("us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void lambdaTargetPreservesArnAccount() {
|
||||
String arn = "arn:aws:lambda:ap-south-1:100000000012:function:cross-account-function";
|
||||
Target target = new Target();
|
||||
target.setArn(arn);
|
||||
target.setInput("{\"detail\":{\"job\":\"workflow-recovery\"}}");
|
||||
|
||||
invoker.invoke(target, "ap-south-1");
|
||||
|
||||
verify(lambdaService).invokeArn(
|
||||
eq(arn),
|
||||
org.mockito.AdditionalMatchers.aryEq(
|
||||
"{\"detail\":{\"job\":\"workflow-recovery\"}}".getBytes()),
|
||||
eq(io.github.hectorvent.floci.services.lambda.model.InvocationType.Event));
|
||||
}
|
||||
|
||||
@Test
|
||||
void directSnsTopicArnStillDelivers() {
|
||||
Target target = new Target();
|
||||
|
||||
Reference in new issue
Block a user