mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(lambda): enforce function policies on cross-account calls (#5476)
Under IAM enforcement, a caller whose own policy allowed lambda:InvokeFunction could invoke another account's function by ARN even when that function's policy granted nothing, and RotateSecret could be pointed at another account's function the same way. Three things combined: there was no resource-policy provider for Lambda, so the call was judged as same-account; the function name was cut at its first colon, so an ARN was evaluated as function:arn in the caller's account, where no Allow or Deny written for the function could match; and the invoke followed the account in the ARN. - LambdaResourcePolicyProvider supplies a function's policy statements and the account its ARN names, also when the function has no policy, so a call to another account's function needs that function's policy to allow the caller as well as the caller's own policy. It is keyed on the ARN, so RotateSecret's check on its rotation function, made under the secretsmanager scope, sees it too. - ResourceArnBuilder evaluates each call against the function it acts on. An invoke runs in the account and Region a full or partial ARN names, as the version or alias it names in the function name or in Qualifier, since Lambda authorizes a qualified call against the qualified ARN. GetFunction, GetFunctionConfiguration and ListDurableExecutionsByFunction read a qualified full ARN in the account it names, so they are evaluated there too, and a read of another account's function needs its policy as on AWS. Every other route acts on the caller's own function of that name, so it is evaluated against that function, unqualified because not all of their handlers act on the qualifier. - An invoke by partial ARN runs in the account it names, as one by full ARN already did. - RotateSecret looks its function up where the rotation runs it: in the ARN's account, and as the version or alias the ARN names.
This commit is contained in:
1 parent
23c6666a51
commit
6d7d85f0f8
13 files changed
+827
-24
No files matched your search
@@ -903,6 +903,94 @@ class ResourceArnBuilderTest {
|
||||
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function", arn);
|
||||
}
|
||||
|
||||
@Test
|
||||
void lambdaFunctionArnKeepsTheAccountItNames() {
|
||||
when(uriInfo.getPath()).thenReturn(
|
||||
"/2015-03-31/functions/arn:aws:lambda:us-east-1:111122223333:function:my-function/invocations");
|
||||
assertEquals("arn:aws:lambda:us-east-1:111122223333:function:my-function",
|
||||
builder.build("lambda", ctx, "us-east-1", "000000000000"));
|
||||
|
||||
when(uriInfo.getPath()).thenReturn("/2015-03-31/functions/111122223333:function:my-function/invocations");
|
||||
assertEquals("arn:aws:lambda:us-east-1:111122223333:function:my-function",
|
||||
builder.build("lambda", ctx, "us-east-1", "000000000000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void lambdaInvokeKeepsTheQualifierItRuns() {
|
||||
when(uriInfo.getPath()).thenReturn(
|
||||
"/2015-03-31/functions/arn:aws:lambda:us-east-1:111122223333:function:my-function:live/invocations");
|
||||
assertEquals("arn:aws:lambda:us-east-1:111122223333:function:my-function:live",
|
||||
builder.build("lambda", ctx, "us-east-1", "000000000000"));
|
||||
|
||||
when(uriInfo.getPath()).thenReturn("/2015-03-31/functions/my-function/invocations");
|
||||
queryParams.putSingle("Qualifier", "1");
|
||||
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function:1",
|
||||
builder.build("lambda", ctx, "us-east-1", "000000000000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void lambdaRoutesOtherThanInvokeAreCheckedAgainstTheCallersUnqualifiedFunction() {
|
||||
when(uriInfo.getPath()).thenReturn(
|
||||
"/2015-03-31/functions/arn:aws:lambda:us-east-1:111122223333:function:my-function:1/code");
|
||||
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function",
|
||||
builder.build("lambda", ctx, "us-east-1", "000000000000"));
|
||||
|
||||
when(uriInfo.getPath()).thenReturn("/2015-03-31/functions/111122223333:function:my-function/policy");
|
||||
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function",
|
||||
builder.build("lambda", ctx, "us-east-1", "000000000000"));
|
||||
|
||||
when(uriInfo.getPath()).thenReturn("/2015-03-31/functions/my-function");
|
||||
queryParams.putSingle("Qualifier", "1");
|
||||
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function",
|
||||
builder.build("lambda", ctx, "us-east-1", "000000000000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void lambdaQualifiedReadsByArnAreCheckedInTheAccountTheyRead() {
|
||||
when(ctx.getMethod()).thenReturn("GET");
|
||||
when(uriInfo.getPath()).thenReturn(
|
||||
"/2015-03-31/functions/arn:aws:lambda:us-east-1:111122223333:function:my-function:1");
|
||||
assertEquals("arn:aws:lambda:us-east-1:111122223333:function:my-function",
|
||||
builder.build("lambda", ctx, "us-east-1", "000000000000"));
|
||||
|
||||
when(uriInfo.getPath()).thenReturn(
|
||||
"/2015-03-31/functions/arn:aws:lambda:us-east-1:111122223333:function:my-function/configuration");
|
||||
queryParams.putSingle("Qualifier", "live");
|
||||
assertEquals("arn:aws:lambda:us-east-1:111122223333:function:my-function",
|
||||
builder.build("lambda", ctx, "us-east-1", "000000000000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void lambdaReadsThatDoNotFollowTheArnAreCheckedOnTheCallersFunction() {
|
||||
when(ctx.getMethod()).thenReturn("GET");
|
||||
when(uriInfo.getPath()).thenReturn(
|
||||
"/2015-03-31/functions/arn:aws:lambda:us-east-1:111122223333:function:my-function");
|
||||
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function",
|
||||
builder.build("lambda", ctx, "us-east-1", "000000000000"));
|
||||
|
||||
when(uriInfo.getPath()).thenReturn("/2015-03-31/functions/111122223333:function:my-function:1");
|
||||
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function",
|
||||
builder.build("lambda", ctx, "us-east-1", "000000000000"));
|
||||
|
||||
when(uriInfo.getPath()).thenReturn(
|
||||
"/2015-03-31/functions/arn:aws:lambda:us-east-1:111122223333:function:my-function:1/policy");
|
||||
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function",
|
||||
builder.build("lambda", ctx, "us-east-1", "000000000000"));
|
||||
|
||||
when(ctx.getMethod()).thenReturn("DELETE");
|
||||
when(uriInfo.getPath()).thenReturn(
|
||||
"/2015-03-31/functions/arn:aws:lambda:us-east-1:111122223333:function:my-function:1");
|
||||
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function",
|
||||
builder.build("lambda", ctx, "us-east-1", "000000000000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void lambdaReferenceLambdaRefusesIsNamedAsGiven() {
|
||||
when(uriInfo.getPath()).thenReturn("/2015-03-31/functions/not:a:function:ref/invocations");
|
||||
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:not:a:function:ref",
|
||||
builder.build("lambda", ctx, "us-east-1", "000000000000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void lambdaDurableExecutionRoutesNameTheExecution() {
|
||||
when(uriInfo.getPath()).thenReturn("/2025-12-01/durable-executions/arn:aws:lambda:us-east-1:000000000000:"
|
||||
|
||||
+441
@@ -0,0 +1,441 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentMatcher;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.timeout;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Under IAM enforcement, a call to a function in another account needs the function policy to allow
|
||||
* the caller as well as the caller's own policy (Lambda Developer Guide, function URLs: "If the
|
||||
* principal making the request is in a different account, then the principal must have both an
|
||||
* identity-based policy ... and permissions granted to them in a resource-based policy"). Lambda
|
||||
* authorizes a call against the function, version or alias ARN the call names, so a function addressed
|
||||
* by ARN is evaluated as that ARN, in its own account and with its qualifier.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class LambdaCrossAccountInvokeIntegrationTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String OWNER = "444455556666";
|
||||
private static final String CALLER = "777788889999";
|
||||
private static final String SM_CONTENT_TYPE = "application/x-amz-json-1.1";
|
||||
private static final String INVOKE_ANYTHING = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"lambda:InvokeFunction","Resource":"*"}]}""";
|
||||
private static final String ROTATE_AND_INVOKE = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"secretsmanager:*","Resource":"*"},
|
||||
{"Effect":"Allow","Action":"lambda:InvokeFunction","Resource":"*"}]}""";
|
||||
|
||||
@InjectMock
|
||||
LambdaExecutorService executor;
|
||||
|
||||
/** How to delete each thing the test created, pushed as it is created and run newest first. */
|
||||
private final Deque<Runnable> cleanup = new ArrayDeque<>();
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void stubExecutor() {
|
||||
when(executor.invoke(any(), any(), any(), anyInt(), any(), any()))
|
||||
.thenReturn(new InvokeResult(200, null, "{}".getBytes(StandardCharsets.UTF_8), null, "req"));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void deleteWhatTheTestCreated() {
|
||||
while (!cleanup.isEmpty()) {
|
||||
cleanup.pop().run();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void anotherAccountsFunctionIsRefusedWhenItsPolicyGrantsNothing() {
|
||||
String functionArn = createFunction(OWNER, "victim-" + suffix());
|
||||
String caller = createUser(CALLER, "invoker-" + suffix(), INVOKE_ANYTHING);
|
||||
|
||||
invoke(caller, functionArn)
|
||||
.statusCode(403)
|
||||
.body("__type", equalTo("AccessDeniedException"));
|
||||
|
||||
verify(executor, never()).invoke(argThat(isFunction(functionArn)), any(), any(), anyInt(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aGrantToTheCallersAccountLetsItInvoke() {
|
||||
String functionArn = createFunction(OWNER, "shared-" + suffix());
|
||||
addPermission(OWNER, functionArn, null, Map.of("Principal", CALLER));
|
||||
String caller = createUser(CALLER, "invoker-" + suffix(), INVOKE_ANYTHING);
|
||||
|
||||
invoke(caller, functionArn).statusCode(200);
|
||||
|
||||
verify(executor).invoke(argThat(isFunction(functionArn)), any(), any(), anyInt(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aGrantDoesNotStandInForTheCallersOwnPolicy() {
|
||||
String functionArn = createFunction(OWNER, "shared-" + suffix());
|
||||
addPermission(OWNER, functionArn, null, Map.of("Principal", CALLER));
|
||||
String caller = createUser(CALLER, "no-lambda-" + suffix(), """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:ListAllMyBuckets","Resource":"*"}]}""");
|
||||
|
||||
invoke(caller, functionArn).statusCode(403);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPartialArnReachesTheAccountItNames() {
|
||||
String name = "partial-" + suffix();
|
||||
String functionArn = createFunction(OWNER, name);
|
||||
String caller = createUser(CALLER, "invoker-" + suffix(), INVOKE_ANYTHING);
|
||||
|
||||
invoke(caller, OWNER + ":function:" + name).statusCode(403);
|
||||
|
||||
addPermission(OWNER, functionArn, null, Map.of("Principal", CALLER));
|
||||
invoke(caller, OWNER + ":function:" + name).statusCode(200);
|
||||
|
||||
verify(executor).invoke(argThat(isFunction(functionArn)), any(), any(), anyInt(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aGrantNamingARoleCoversThatRolesSessionsOnly() {
|
||||
String functionArn = createFunction(OWNER, "role-scoped-" + suffix());
|
||||
String grantedRole = "deploy.bot-" + suffix();
|
||||
String otherRole = "other-" + suffix();
|
||||
createRole(CALLER, grantedRole, INVOKE_ANYTHING);
|
||||
createRole(CALLER, otherRole, INVOKE_ANYTHING);
|
||||
addPermission(OWNER, functionArn, null,
|
||||
Map.of("Principal", "arn:aws:iam::" + CALLER + ":role/" + grantedRole));
|
||||
|
||||
invoke(assumeRole(CALLER, grantedRole), functionArn).statusCode(200);
|
||||
invoke(assumeRole(CALLER, otherRole), functionArn).statusCode(403);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAliasGrantCoversThatAliasOnly() {
|
||||
String name = "aliased-" + suffix();
|
||||
String functionArn = createFunction(OWNER, name);
|
||||
publishVersionAndAlias(OWNER, name, "live");
|
||||
addPermission(OWNER, functionArn, "live", Map.of("Principal", CALLER));
|
||||
String caller = createUser(CALLER, "invoker-" + suffix(), INVOKE_ANYTHING);
|
||||
|
||||
invoke(caller, functionArn + ":live").statusCode(200);
|
||||
invoke(caller, functionArn, "live").statusCode(200);
|
||||
invoke(caller, functionArn).statusCode(403);
|
||||
invoke(caller, functionArn + ":1").statusCode(403);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPublicFunctionUrlGrantDoesNotAdmitADirectInvoke() {
|
||||
String functionArn = createFunction(OWNER, "url-only-" + suffix());
|
||||
addPermission(OWNER, functionArn, null, Map.of("Principal", "*", "InvokedViaFunctionUrl", true));
|
||||
String caller = createUser(CALLER, "invoker-" + suffix(), INVOKE_ANYTHING);
|
||||
|
||||
invoke(caller, functionArn).statusCode(403);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDenyOnTheFunctionArnAppliesToAnInvokeByArn() {
|
||||
String name = "guarded-" + suffix();
|
||||
String functionArn = createFunction(OWNER, name);
|
||||
String user = createUser(OWNER, "denied-" + suffix(), """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"lambda:InvokeFunction","Resource":"*"},
|
||||
{"Effect":"Deny","Action":"lambda:InvokeFunction","Resource":"%s"}]}""".formatted(functionArn));
|
||||
|
||||
invoke(user, name).statusCode(403);
|
||||
invoke(user, functionArn).statusCode(403);
|
||||
invoke(user, OWNER + ":function:" + name).statusCode(403);
|
||||
|
||||
verify(executor, never()).invoke(argThat(isFunction(functionArn)), any(), any(), anyInt(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSameAccountFunctionPolicyGrantsOnItsOwn() {
|
||||
String functionArn = createFunction(OWNER, "own-grant-" + suffix());
|
||||
String userName = "granted-" + suffix();
|
||||
String user = createUser(OWNER, userName, """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"s3:ListAllMyBuckets","Resource":"*"}]}""");
|
||||
addPermission(OWNER, functionArn, null, Map.of("Principal", "arn:aws:iam::" + OWNER + ":user/" + userName));
|
||||
|
||||
invoke(user, functionArn).statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Operations other than invoke act on the caller's own function of the name an ARN gives, whatever
|
||||
* account it names, so they are authorized against that function: a grant from the account the
|
||||
* ARN names does not get past a Deny on the function the call actually changes.
|
||||
*/
|
||||
@Test
|
||||
void aManagementCallNamingAnotherAccountsArnIsCheckedAgainstTheFunctionItChanges() {
|
||||
String name = "managed-" + suffix();
|
||||
String foreignArn = createFunction(OWNER, name);
|
||||
String ownArn = createFunction(CALLER, name);
|
||||
addPermission(OWNER, foreignArn, null, Map.of("Principal", CALLER, "Action", "lambda:DeleteFunction"));
|
||||
String user = createUser(CALLER, "deleter-" + suffix(), """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"lambda:*","Resource":"*"},
|
||||
{"Effect":"Deny","Action":"lambda:DeleteFunction","Resource":"%s"}]}""".formatted(ownArn));
|
||||
|
||||
lambda(user).delete("/2015-03-31/functions/{name}", foreignArn).then().statusCode(403);
|
||||
|
||||
lambda(CALLER).get("/2015-03-31/functions/{name}", name).then().statusCode(200);
|
||||
lambda(OWNER).get("/2015-03-31/functions/{name}", name).then().statusCode(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code GetFunction} reads a function named by a qualified ARN in the account the ARN names, so
|
||||
* that account's function policy has to allow the caller, as for an invoke.
|
||||
*/
|
||||
@Test
|
||||
void aQualifiedReadOfAnotherAccountsFunctionNeedsItsGrant() {
|
||||
String foreignArn = createFunction(OWNER, "read-" + suffix());
|
||||
String user = createUser(CALLER, "reader-" + suffix(), """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"lambda:GetFunction","Resource":"*"}]}""");
|
||||
|
||||
lambda(user).get("/2015-03-31/functions/{name}", foreignArn + ":$LATEST").then().statusCode(403);
|
||||
lambda(user).queryParam("Qualifier", "$LATEST").get("/2015-03-31/functions/{name}", foreignArn)
|
||||
.then().statusCode(403);
|
||||
|
||||
addPermission(OWNER, foreignArn, null, Map.of("Principal", CALLER, "Action", "lambda:GetFunction"));
|
||||
lambda(user).get("/2015-03-31/functions/{name}", foreignArn + ":$LATEST")
|
||||
.then().statusCode(200).body("Configuration.FunctionArn", startsWith(foreignArn));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rotateSecretIsRefusedForAnotherAccountsFunctionThatGrantsNothing() {
|
||||
String name = "rotator-" + suffix();
|
||||
String foreignArn = createFunction(OWNER, name);
|
||||
createFunction(CALLER, name);
|
||||
String caller = createUser(CALLER, "rotator-user-" + suffix(), ROTATE_AND_INVOKE);
|
||||
String secretName = createSecret(caller, "foreign-" + suffix());
|
||||
|
||||
secretsManager(caller, "RotateSecret",
|
||||
"{\"SecretId\":\"" + secretName + "\",\"RotationLambdaARN\":\"" + foreignArn + "\"}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("AccessDeniedException"));
|
||||
|
||||
verify(executor, never()).invoke(argThat(isFunction(foreignArn)), any(), any(), anyInt(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rotateSecretRunsAnotherAccountsFunctionThatGrantsTheCaller() {
|
||||
String foreignArn = createFunction(OWNER, "rotator-" + suffix());
|
||||
addPermission(OWNER, foreignArn, null, Map.of("Principal", CALLER));
|
||||
String caller = createUser(CALLER, "rotator-user-" + suffix(), ROTATE_AND_INVOKE);
|
||||
String secretName = createSecret(caller, "granted-" + suffix());
|
||||
|
||||
secretsManager(caller, "RotateSecret",
|
||||
"{\"SecretId\":\"" + secretName + "\",\"RotationLambdaARN\":\"" + foreignArn + "\"}")
|
||||
.statusCode(200);
|
||||
|
||||
verify(executor, timeout(5000).atLeastOnce())
|
||||
.invoke(argThat(isFunction(foreignArn)), any(), any(), anyInt(), any(), any());
|
||||
awaitRotationFinished(caller, secretName);
|
||||
}
|
||||
|
||||
@Test
|
||||
void rotateSecretRefusesAnAliasTheFunctionDoesNotHave() {
|
||||
String functionArn = createFunction(OWNER, "rotator-" + suffix());
|
||||
String user = createUser(OWNER, "rotator-user-" + suffix(), ROTATE_AND_INVOKE);
|
||||
String secretName = createSecret(user, "no-alias-" + suffix());
|
||||
|
||||
secretsManager(user, "RotateSecret",
|
||||
"{\"SecretId\":\"" + secretName + "\",\"RotationLambdaARN\":\"" + functionArn + ":nosuchalias\"}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("AccessDeniedException"));
|
||||
|
||||
verify(executor, never()).invoke(any(), any(), any(), anyInt(), any(), any());
|
||||
}
|
||||
|
||||
private static String suffix() {
|
||||
return UUID.randomUUID().toString().substring(0, 8);
|
||||
}
|
||||
|
||||
private static ArgumentMatcher<LambdaFunction> isFunction(String functionArn) {
|
||||
return fn -> fn != null && functionArn.equals(fn.getFunctionArn());
|
||||
}
|
||||
|
||||
private String createFunction(String account, String name) {
|
||||
lambda(account)
|
||||
.body("""
|
||||
{"FunctionName":"%s","Runtime":"nodejs20.x","Handler":"index.handler",
|
||||
"Role":"arn:aws:iam::%s:role/lambda-role"}""".formatted(name, account))
|
||||
.when()
|
||||
.post("/2015-03-31/functions")
|
||||
.then()
|
||||
.statusCode(201);
|
||||
cleanup.push(() -> lambda(account).delete("/2015-03-31/functions/{name}", name).then().statusCode(204));
|
||||
return "arn:aws:lambda:" + REGION + ":" + account + ":function:" + name;
|
||||
}
|
||||
|
||||
private static void publishVersionAndAlias(String account, String name, String alias) {
|
||||
lambda(account).body("{}").post("/2015-03-31/functions/{name}/versions", name).then().statusCode(201);
|
||||
lambda(account)
|
||||
.body("{\"Name\":\"" + alias + "\",\"FunctionVersion\":\"1\"}")
|
||||
.post("/2015-03-31/functions/{name}/aliases", name)
|
||||
.then()
|
||||
.statusCode(201);
|
||||
}
|
||||
|
||||
/** Adds a statement granting {@code lambda:InvokeFunction} unless {@code grant} names another action. */
|
||||
private static void addPermission(String account, String functionArn, String qualifier, Map<String, Object> grant) {
|
||||
Map<String, Object> request = new LinkedHashMap<>();
|
||||
request.put("StatementId", "s-" + suffix());
|
||||
request.put("Action", "lambda:InvokeFunction");
|
||||
request.putAll(grant);
|
||||
RequestSpecification spec = lambda(account).body(request);
|
||||
if (qualifier != null) {
|
||||
spec.queryParam("Qualifier", qualifier);
|
||||
}
|
||||
spec.post("/2015-03-31/functions/{name}/policy", functionArn).then().statusCode(201);
|
||||
}
|
||||
|
||||
private static ValidatableResponse invoke(String accessKeyId, String functionName) {
|
||||
return invoke(accessKeyId, functionName, null);
|
||||
}
|
||||
|
||||
private static ValidatableResponse invoke(String accessKeyId, String functionName, String qualifier) {
|
||||
RequestSpecification spec = given()
|
||||
.header("Authorization", authorization(accessKeyId, "lambda"))
|
||||
.contentType("application/json")
|
||||
.body("{}");
|
||||
if (qualifier != null) {
|
||||
spec.queryParam("Qualifier", qualifier);
|
||||
}
|
||||
return spec.when().post("/2015-03-31/functions/{name}/invocations", functionName).then();
|
||||
}
|
||||
|
||||
private static RequestSpecification lambda(String accessKeyId) {
|
||||
return given()
|
||||
.header("Authorization", authorization(accessKeyId, "lambda"))
|
||||
.contentType("application/json");
|
||||
}
|
||||
|
||||
private String createSecret(String accessKeyId, String secretName) {
|
||||
secretsManager(accessKeyId, "CreateSecret",
|
||||
"{\"Name\":\"" + secretName + "\",\"SecretString\":\"v1\"}").statusCode(200);
|
||||
cleanup.push(() -> secretsManager(accessKeyId, "DeleteSecret",
|
||||
"{\"SecretId\":\"" + secretName + "\",\"ForceDeleteWithoutRecovery\":true}").statusCode(200));
|
||||
return secretName;
|
||||
}
|
||||
|
||||
private static ValidatableResponse secretsManager(String accessKeyId, String operation, String body) {
|
||||
return given()
|
||||
.header("Authorization", authorization(accessKeyId, "secretsmanager"))
|
||||
.header("X-Amz-Target", "secretsmanager." + operation)
|
||||
.contentType(SM_CONTENT_TYPE)
|
||||
.body(body)
|
||||
.when()
|
||||
.post("/")
|
||||
.then();
|
||||
}
|
||||
|
||||
/**
|
||||
* Lets the rotation finish before cleanup deletes the secret, which it would otherwise go on
|
||||
* trying to update.
|
||||
*/
|
||||
private static void awaitRotationFinished(String accessKeyId, String secretName) {
|
||||
await().atMost(Duration.ofSeconds(5)).until(() -> secretsManager(accessKeyId, "DescribeSecret",
|
||||
"{\"SecretId\":\"" + secretName + "\"}").statusCode(200).extract().path("LastRotatedDate") != null);
|
||||
}
|
||||
|
||||
private String createUser(String account, String userName, String policyDocument) {
|
||||
iam(account, "CreateUser", Map.of("UserName", userName)).statusCode(200);
|
||||
cleanup.push(() -> iam(account, "DeleteUser", Map.of("UserName", userName)).statusCode(200));
|
||||
iam(account, "PutUserPolicy", Map.of(
|
||||
"UserName", userName,
|
||||
"PolicyName", "inline",
|
||||
"PolicyDocument", policyDocument)).statusCode(200);
|
||||
cleanup.push(() -> iam(account, "DeleteUserPolicy",
|
||||
Map.of("UserName", userName, "PolicyName", "inline")).statusCode(200));
|
||||
String accessKeyId = iam(account, "CreateAccessKey", Map.of("UserName", userName))
|
||||
.statusCode(200)
|
||||
.extract().path("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId");
|
||||
cleanup.push(() -> iam(account, "DeleteAccessKey",
|
||||
Map.of("UserName", userName, "AccessKeyId", accessKeyId)).statusCode(200));
|
||||
return accessKeyId;
|
||||
}
|
||||
|
||||
private void createRole(String account, String roleName, String policyDocument) {
|
||||
iam(account, "CreateRole", Map.of(
|
||||
"RoleName", roleName,
|
||||
"AssumeRolePolicyDocument", """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Principal":{"AWS":"arn:aws:iam::%s:root"},"Action":"sts:AssumeRole"}]}"""
|
||||
.formatted(account))).statusCode(200);
|
||||
cleanup.push(() -> iam(account, "DeleteRole", Map.of("RoleName", roleName)).statusCode(200));
|
||||
iam(account, "PutRolePolicy", Map.of(
|
||||
"RoleName", roleName,
|
||||
"PolicyName", "inline",
|
||||
"PolicyDocument", policyDocument)).statusCode(200);
|
||||
cleanup.push(() -> iam(account, "DeleteRolePolicy",
|
||||
Map.of("RoleName", roleName, "PolicyName", "inline")).statusCode(200));
|
||||
}
|
||||
|
||||
private static String assumeRole(String account, String roleName) {
|
||||
return given()
|
||||
.header("Authorization", authorization(account, "sts"))
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "AssumeRole")
|
||||
.formParam("Version", "2011-06-15")
|
||||
.formParam("RoleArn", "arn:aws:iam::" + account + ":role/" + roleName)
|
||||
.formParam("RoleSessionName", "s1b")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId");
|
||||
}
|
||||
|
||||
private static ValidatableResponse iam(String account, String action, Map<String, String> params) {
|
||||
RequestSpecification spec = given()
|
||||
.header("Authorization", authorization(account, "iam"))
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", action)
|
||||
.formParam("Version", "2010-05-08");
|
||||
params.forEach(spec::formParam);
|
||||
return spec.when().post("/").then();
|
||||
}
|
||||
|
||||
private static String authorization(String accessKeyId, String service) {
|
||||
return "AWS4-HMAC-SHA256 Credential=" + accessKeyId + "/20261011/" + REGION + "/" + service
|
||||
+ "/aws4_request, SignedHeaders=host, Signature=abc";
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.iam.ResourcePolicyProvider.ResourcePolicy;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class LambdaResourcePolicyProviderTest {
|
||||
|
||||
private static final String OWNER = "111122223333";
|
||||
private static final String FUNCTION_ARN = "arn:aws:lambda:us-east-1:" + OWNER + ":function:my-function";
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private LambdaService lambdaService;
|
||||
private LambdaResourcePolicyProvider provider;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
lambdaService = mock(LambdaService.class);
|
||||
when(lambdaService.policyStatements(OWNER, "us-east-1", "my-function")).thenReturn(List.of());
|
||||
provider = new LambdaResourcePolicyProvider(lambdaService, objectMapper);
|
||||
}
|
||||
|
||||
@Test
|
||||
void theOwnerComesFromTheArnEvenWithoutAPolicy() {
|
||||
List<ResourcePolicy> policies = provider.getResourcePolicies("lambda", FUNCTION_ARN);
|
||||
|
||||
assertEquals(1, policies.size());
|
||||
assertNull(policies.getFirst().policyDocument());
|
||||
assertEquals(OWNER, policies.getFirst().ownerAccountId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyStatementOnTheFunctionComesBackForAQualifiedArn() throws Exception {
|
||||
Map<String, Object> functionGrant = Map.of("Sid", "fn", "Effect", "Allow",
|
||||
"Principal", Map.of("AWS", "arn:aws:iam::444455556666:root"),
|
||||
"Action", "lambda:InvokeFunction", "Resource", FUNCTION_ARN);
|
||||
Map<String, Object> aliasGrant = Map.of("Sid", "alias", "Effect", "Allow",
|
||||
"Principal", Map.of("AWS", "arn:aws:iam::444455556666:root"),
|
||||
"Action", "lambda:InvokeFunction", "Resource", FUNCTION_ARN + ":live");
|
||||
when(lambdaService.policyStatements(OWNER, "us-east-1", "my-function"))
|
||||
.thenReturn(List.of(functionGrant, aliasGrant));
|
||||
|
||||
List<ResourcePolicy> policies = provider.getResourcePolicies("lambda", FUNCTION_ARN + ":live");
|
||||
|
||||
JsonNode statements = objectMapper.readTree(policies.getFirst().policyDocument()).get("Statement");
|
||||
assertEquals(2, statements.size());
|
||||
assertEquals(FUNCTION_ARN, statements.get(0).get("Resource").asText());
|
||||
assertEquals(FUNCTION_ARN + ":live", statements.get(1).get("Resource").asText());
|
||||
assertEquals(OWNER, policies.getFirst().ownerAccountId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aFunctionCheckedUnderAnotherServicesScopeIsStillSupplied() {
|
||||
List<ResourcePolicy> policies = provider.getResourcePolicies("secretsmanager", FUNCTION_ARN);
|
||||
|
||||
assertEquals(OWNER, policies.getFirst().ownerAccountId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourcesThatAreNotFunctionsAreLeftToOtherProviders() {
|
||||
assertTrue(provider.getResourcePolicies("lambda", "*").isEmpty());
|
||||
assertTrue(provider.getResourcePolicies("lambda", "arn:aws:lambda:us-east-1:" + OWNER + ":layer:my-layer:1").isEmpty());
|
||||
assertTrue(provider.getResourcePolicies("lambda",
|
||||
"arn:aws:lambda:us-east-1:" + OWNER + ":event-source-mapping:fa123456-14a1-4fd2-9fec-83de64ad683d").isEmpty());
|
||||
assertTrue(provider.getResourcePolicies("s3", "arn:aws:s3:::bucket/key").isEmpty());
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -60,7 +60,7 @@ class SecretsManagerRotationInvokeAuthorizationIntegrationTest {
|
||||
|
||||
@BeforeEach
|
||||
void stubLambda() {
|
||||
Mockito.when(lambdaService.getFunction(anyString(), anyString())).thenReturn(new LambdaFunction());
|
||||
Mockito.when(lambdaService.getFunctionByArn(anyString())).thenReturn(new LambdaFunction());
|
||||
InvokeResult ok = new InvokeResult();
|
||||
ok.setStatusCode(200);
|
||||
Mockito.when(lambdaService.invoke(any(), any(), any(), any())).thenReturn(ok);
|
||||
|
||||
+5
-5
@@ -50,7 +50,7 @@ class SecretsManagerRotationInvokeAuthorizationTest {
|
||||
void setUp() {
|
||||
store = new InMemoryStorage<>();
|
||||
lambda = mock(LambdaService.class);
|
||||
when(lambda.getFunction(anyString(), anyString())).thenReturn(new LambdaFunction());
|
||||
when(lambda.getFunctionByArn(anyString())).thenReturn(new LambdaFunction());
|
||||
when(lambda.invoke(anyString(), anyString(), any(byte[].class), any())).thenReturn(new InvokeResult());
|
||||
service = new SecretsManagerService(store, 30, new RegionResolver(REGION, ACCOUNT), lambda, new ObjectMapper());
|
||||
service.createSecret("my-secret", "v1", null, null, null, null, REGION);
|
||||
@@ -116,7 +116,7 @@ class SecretsManagerRotationInvokeAuthorizationTest {
|
||||
|
||||
@Test
|
||||
void lambdaRefusingTheReferenceAtTheLookupIsAnInvalidParameter() {
|
||||
when(lambda.getFunction(anyString(), anyString()))
|
||||
when(lambda.getFunctionByArn(anyString()))
|
||||
.thenThrow(new AwsException("ValidationException", "refused by Lambda", 400));
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () ->
|
||||
@@ -149,7 +149,7 @@ class SecretsManagerRotationInvokeAuthorizationTest {
|
||||
|
||||
@Test
|
||||
void refusalComesBeforeTheExistenceCheck() {
|
||||
when(lambda.getFunction(anyString(), anyString()))
|
||||
when(lambda.getFunctionByArn(anyString()))
|
||||
.thenThrow(new AwsException("ResourceNotFoundException", "Function not found", 404));
|
||||
Consumer<String> refuse = functionArn -> {
|
||||
throw new AwsException("AccessDeniedException", "refused by the caller check", 400);
|
||||
@@ -163,7 +163,7 @@ class SecretsManagerRotationInvokeAuthorizationTest {
|
||||
|
||||
@Test
|
||||
void missingFunctionIsReportedAsSecretsManagerReportsIt() {
|
||||
when(lambda.getFunction(anyString(), anyString()))
|
||||
when(lambda.getFunctionByArn(anyString()))
|
||||
.thenThrow(new AwsException("ResourceNotFoundException", "Function not found", 404));
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () ->
|
||||
@@ -181,7 +181,7 @@ class SecretsManagerRotationInvokeAuthorizationTest {
|
||||
Secret secret = service.describeSecret("my-secret", REGION);
|
||||
secret.setRotationLambdaArn(FUNCTION_ARN);
|
||||
store.put(REGION + "::my-secret", secret);
|
||||
when(lambda.getFunction(anyString(), anyString()))
|
||||
when(lambda.getFunctionByArn(anyString()))
|
||||
.thenThrow(new AwsException("ResourceNotFoundException", "Function not found", 404));
|
||||
|
||||
assertThrows(AwsException.class, () -> service.rotateSecret("my-secret", TOKEN,
|
||||
|
||||
+1
-1
@@ -89,7 +89,7 @@ class SecretsManagerWriteLockTest {
|
||||
@Test
|
||||
void deletingASecretWaitsForTheFinalSaveOfARotation() throws Exception {
|
||||
LambdaService lambda = mock(LambdaService.class);
|
||||
when(lambda.getFunction(anyString(), anyString())).thenReturn(new LambdaFunction());
|
||||
when(lambda.getFunctionByArn(anyString())).thenReturn(new LambdaFunction());
|
||||
InvokeResult ok = new InvokeResult();
|
||||
ok.setStatusCode(200);
|
||||
when(lambda.invoke(anyString(), anyString(), any(byte[].class), any())).thenReturn(ok);
|
||||
|
||||
Reference in new issue
Block a user