Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

refactor(secretsmanager): build the rotation function ARN with Lambda's own helper (#4709)

Priamo Germosenf752946acf7b
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
index b351f9d247..075429d4dd 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/IamEnforcementFilter.java
@@ -704,9 +704,13 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
boolean accountRootPrincipal = false;
CallerContext caller = iamService.resolveCallerContext(akid);
if (caller == null) {
- // No unknown-key rejection here, unlike filter(): this path runs only for a
- // presigned POST, whose form signature S3PostPolicySigner has already verified
- // against the key's secret, so an unknown key never reaches it.
+ // No unknown-key rejection here, unlike filter(), and none is needed. For a second
+ // resource of a request filter() has seen (CopyObject's source, RotateSecret's
+ // rotation function, the secrets behind SSM GetParameter) the credential is the one
+ // filter() evaluated, and filter() refuses an unknown key before the handler runs. A
+ // presigned POST carries its credential only in the form body, which filter() never
+ // sees: an unknown key there is refused by S3's presigned-POST signature check when
+ // S3 auth enforcement is on, and otherwise uploads as an unsigned request would.
if (scpLevels == null || !akid.equals(accountId)) {
return;
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaArnUtils.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaArnUtils.java
index fc474dee64..05ea35c069 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaArnUtils.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaArnUtils.java
@@ -47,6 +47,25 @@ public final class LambdaArnUtils {
return arn.resource().substring(EVENT_SOURCE_MAPPING_PREFIX.length());
}
+ /**
+ * The full function ARN a {@code FunctionName} reference names, the way an IAM policy's
+ * {@code Resource} names it. The reference is read as {@link #resolve} reads it, so a malformed
+ * one is refused here. A full ARN comes back as given; a name or partial ARN is completed with
+ * {@code partition} and {@code region}, and with {@code defaultAccount} unless it names its own
+ * account. A qualifier is kept, since Lambda authorizes a qualified call against the qualified ARN.
+ */
+ public static String functionArn(String reference, String partition, String region, String defaultAccount) {
+ ResolvedFunctionRef ref = resolve(reference);
+ if (reference.startsWith("arn:")) {
+ return reference;
+ }
+ String resource = ref.qualifier() == null
+ ? "function:" + ref.name()
+ : "function:" + ref.name() + ":" + ref.qualifier();
+ String account = ref.account() != null ? ref.account() : defaultAccount;
+ return new AwsArnUtils.Arn(partition, "lambda", region, account, resource).toString();
+ }
+
/**
* Resolved components of a Lambda function reference.
*
@@ -54,8 +73,9 @@ public final class LambdaArnUtils {
* @param qualifier version or alias, or null if absent
* @param region region extracted from a full ARN, or null for bare
* name / partial ARN inputs
+ * @param account account named by a full or partial ARN, or null for a bare name
*/
- public record ResolvedFunctionRef(String name, String qualifier, String region) {}
+ public record ResolvedFunctionRef(String name, String qualifier, String region, String account) {}
/**
* Parses a {@code FunctionName} path parameter. Throws {@link AwsException}, HTTP 400,
@@ -98,7 +118,7 @@ public final class LambdaArnUtils {
if (effective != null && !QUALIFIER_PATTERN.matcher(effective).matches()) {
throw invalid("Invalid qualifier: " + effective);
}
- return new ResolvedFunctionRef(ref.name(), effective, ref.region());
+ return new ResolvedFunctionRef(ref.name(), effective, ref.region(), ref.account());
}
private static ResolvedFunctionRef parseFullArn(String input) {
@@ -133,7 +153,7 @@ public final class LambdaArnUtils {
if (qualifier != null) {
validateQualifier(qualifier);
}
- return new ResolvedFunctionRef(name, qualifier, base.region());
+ return new ResolvedFunctionRef(name, qualifier, base.region(), base.accountId());
}
private static ResolvedFunctionRef parsePartialArn(String input) {
@@ -155,7 +175,7 @@ public final class LambdaArnUtils {
if (qualifier != null) {
validateQualifier(qualifier);
}
- return new ResolvedFunctionRef(name, qualifier, null);
+ return new ResolvedFunctionRef(name, qualifier, null, account);
}
private static ResolvedFunctionRef parseNameWithOptionalQualifier(String input) {
@@ -170,7 +190,7 @@ public final class LambdaArnUtils {
if (qualifier != null) {
validateQualifier(qualifier);
}
- return new ResolvedFunctionRef(name, qualifier, null);
+ return new ResolvedFunctionRef(name, qualifier, null, null);
}
private static void validateNamePattern(String name) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java b/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java
index 388ab12090..e6e57c8745 100644
--- a/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerService.java
@@ -1,23 +1,28 @@
package io.github.hectorvent.floci.services.secretsmanager;
import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+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.RegionResolver;
+import io.github.hectorvent.floci.core.common.ServicePrincipals;
+import io.github.hectorvent.floci.core.resource.ExplorerResource;
+import io.github.hectorvent.floci.core.resource.ResourceProvider;
+import io.github.hectorvent.floci.core.resource.SupportedResourceType;
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.secretsmanager.model.Secret;
-import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import io.github.hectorvent.floci.services.lambda.LambdaService;
-import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.kms.KmsService;
import io.github.hectorvent.floci.services.kms.model.KmsKey;
+import io.github.hectorvent.floci.services.lambda.LambdaArnUtils;
+import io.github.hectorvent.floci.services.lambda.LambdaService;
+import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
import io.github.hectorvent.floci.services.scheduler.SchedulerExpressionParser;
+import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
+import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
import jakarta.annotation.PreDestroy;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -28,22 +33,19 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
+import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
-import java.util.function.Predicate;
-import io.github.hectorvent.floci.core.resource.ExplorerResource;
-import io.github.hectorvent.floci.core.resource.ResourceProvider;
-import io.github.hectorvent.floci.core.resource.SupportedResourceType;
-import java.util.LinkedHashMap;
-import java.util.Set;
import java.util.function.Consumer;
+import java.util.function.Predicate;
import java.util.regex.Pattern;
@ApplicationScoped
@@ -1137,7 +1139,10 @@ public class SecretsManagerService implements ResourceProvider {
// One full ARN for the permission check, the lookup and the invoke, so all three reach the same
// function: a name or partial ARN would otherwise be invoked in whatever account the rotation
// thread resolves, which is not necessarily the one that was checked.
- String functionArn = serviceManaged ? null : rotationFunctionArn(finalLambdaArn, secret, region);
+ AwsArnUtils.Arn secretArn = AwsArnUtils.parse(secret.getArn());
+ String functionArn = serviceManaged
+ ? null
+ : LambdaArnUtils.functionArn(finalLambdaArn, secretArn.partition(), region, secretArn.accountId());
if (!serviceManaged) {
authorizeInvoke.accept(functionArn);
}
@@ -1151,7 +1156,8 @@ public class SecretsManagerService implements ResourceProvider {
if (e.getHttpStatus() == 404) {
throw new AwsException("AccessDeniedException",
"Secrets Manager cannot invoke the specified Lambda function. Ensure that the function "
- + "policy grants access to the principal secretsmanager.amazonaws.com.", 400); // partition-literal: AWS's error text verbatim; names the service principal, which the Secrets Manager guide gives with no partition variant
+ + "policy grants access to the principal " + ServicePrincipals.of("secretsmanager")
+ + ".", 400);
}
throw e;
}
@@ -1209,23 +1215,6 @@ public class SecretsManagerService implements ResourceProvider {
}
- /**
- * The rotation function as the full ARN an IAM policy's {@code Resource} names. A function name
- * or partial ARN resolves in the secret's own account and the request's Region, as Lambda
- * resolves one.
- */
- static String rotationFunctionArn(String functionRef, Secret secret, String region) {
- if (functionRef.startsWith("arn:")) {
- return functionRef;
- }
- AwsArnUtils.Arn secretArn = AwsArnUtils.parse(secret.getArn());
- if (functionRef.contains(":function:")) {
- return "arn:" + secretArn.partition() + ":lambda:" + region + ":" + functionRef;
- }
- return new AwsArnUtils.Arn(secretArn.partition(), "lambda", region, secretArn.accountId(),
- "function:" + functionRef).toString();
- }
-
/**
* The outcome of {@link #cancelRotateSecret}: the secret with rotation turned off, plus the id
* of the version a cancelled rotation left behind, or {@code null} when no rotation was in
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaArnUtilsTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaArnUtilsTest.java
index 9e110a374a..cfd8531893 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaArnUtilsTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaArnUtilsTest.java
@@ -12,24 +12,26 @@ class LambdaArnUtilsTest {
@ParameterizedTest
@CsvSource({
- // input, expectedName, expectedQualifier, expectedRegion
- "my-fn, my-fn, , ",
- "my-fn:prod, my-fn, prod, ",
- "my-fn:$LATEST, my-fn, $LATEST, ",
- "my-fn:42, my-fn, 42, ",
- "000000000000:function:my-fn, my-fn, , ",
- "000000000000:function:my-fn:prod, my-fn, prod, ",
- "arn:aws:lambda:us-east-1:000000000000:function:my-fn, my-fn, , us-east-1",
- "arn:aws:lambda:eu-west-1:123456789012:function:my-fn, my-fn, , eu-west-1",
- "arn:aws:lambda:us-east-1:000000000000:function:my-fn:prod, my-fn, prod, us-east-1",
- "arn:aws:lambda:us-east-1:000000000000:function:my-fn:$LATEST, my-fn, $LATEST, us-east-1",
- "arn:aws:lambda:us-east-1:000000000000:function:my_fn-1, my_fn-1, , us-east-1",
+ // input, expectedName, expectedQualifier, expectedRegion, expectedAccount
+ "my-fn, my-fn, , , ",
+ "my-fn:prod, my-fn, prod, , ",
+ "my-fn:$LATEST, my-fn, $LATEST, , ",
+ "my-fn:42, my-fn, 42, , ",
+ "000000000000:function:my-fn, my-fn, , , 000000000000",
+ "111122223333:function:my-fn:prod, my-fn, prod, , 111122223333",
+ "arn:aws:lambda:us-east-1:000000000000:function:my-fn, my-fn, , us-east-1, 000000000000",
+ "arn:aws:lambda:eu-west-1:123456789012:function:my-fn, my-fn, , eu-west-1, 123456789012",
+ "arn:aws:lambda:us-east-1:000000000000:function:my-fn:prod, my-fn, prod, us-east-1, 000000000000",
+ "arn:aws:lambda:us-east-1:000000000000:function:my-fn:$LATEST, my-fn, $LATEST, us-east-1, 000000000000",
+ "arn:aws:lambda:us-east-1:000000000000:function:my_fn-1, my_fn-1, , us-east-1, 000000000000",
})
- void resolveAcceptsValidForms(String input, String expectedName, String expectedQualifier, String expectedRegion) {
+ void resolveAcceptsValidForms(String input, String expectedName, String expectedQualifier, String expectedRegion,
+ String expectedAccount) {
LambdaArnUtils.ResolvedFunctionRef ref = LambdaArnUtils.resolve(input);
assertEquals(expectedName, ref.name());
assertEquals(emptyToNull(expectedQualifier), ref.qualifier());
assertEquals(emptyToNull(expectedRegion), ref.region());
+ assertEquals(emptyToNull(expectedAccount), ref.account());
}
@ParameterizedTest
@@ -236,4 +238,34 @@ class LambdaArnUtilsTest {
assertEquals("myFn", ref.name());
assertEquals("PROD", ref.qualifier());
}
+
+ @ParameterizedTest
+ @CsvSource({
+ // reference, expected full ARN (partition aws, region us-east-1, default account 000000000000)
+ "rotator, arn:aws:lambda:us-east-1:000000000000:function:rotator",
+ "rotator:live, arn:aws:lambda:us-east-1:000000000000:function:rotator:live",
+ "111122223333:function:rotator, arn:aws:lambda:us-east-1:111122223333:function:rotator",
+ "111122223333:function:rotator:7, arn:aws:lambda:us-east-1:111122223333:function:rotator:7",
+ "arn:aws:lambda:eu-west-1:111122223333:function:rotator, arn:aws:lambda:eu-west-1:111122223333:function:rotator",
+ })
+ void functionArnCompletesAReferenceTheWayLambdaReadsIt(String reference, String expected) {
+ assertEquals(expected, LambdaArnUtils.functionArn(reference, "aws", "us-east-1", "000000000000"));
+ }
+
+ @Test
+ void functionArnUsesThePartitionItIsGiven() {
+ assertEquals("arn:aws-cn:lambda:cn-north-1:000000000000:function:rotator",
+ LambdaArnUtils.functionArn("rotator", "aws-cn", "cn-north-1", "000000000000"));
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {
+ "us-east-1:111122223333:function:rotator",
+ "arn:aws:lambda:us-east-1:abc:function:rotator",
+ })
+ void functionArnRefusesAReferenceLambdaCannotRead(String reference) {
+ AwsException ex = assertThrows(AwsException.class,
+ () -> LambdaArnUtils.functionArn(reference, "aws", "us-east-1", "000000000000"));
+ assertEquals("InvalidParameterValueException", ex.getErrorCode());
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationInvokeAuthorizationTest.java b/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationInvokeAuthorizationTest.java
index 89dcc31ff1..3b3ae80765 100644
--- a/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationInvokeAuthorizationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/secretsmanager/SecretsManagerRotationInvokeAuthorizationTest.java
@@ -60,6 +60,7 @@ class SecretsManagerRotationInvokeAuthorizationTest {
"rotator, arn:aws:lambda:us-east-1:000000000000:function:rotator",
"rotator:live, arn:aws:lambda:us-east-1:000000000000:function:rotator:live",
"111122223333:function:rotator, arn:aws:lambda:us-east-1:111122223333:function:rotator",
+ "111122223333:function:rotator:live, arn:aws:lambda:us-east-1:111122223333:function:rotator:live",
"arn:aws:lambda:us-east-1:111122223333:function:rotator, arn:aws:lambda:us-east-1:111122223333:function:rotator"
})
void checkAndInvokeUseTheSameFullArn(String given, String expected) {
@@ -73,6 +74,18 @@ class SecretsManagerRotationInvokeAuthorizationTest {
verify(lambda, timeout(5000).atLeastOnce()).invoke(eq(REGION), eq(expected), any(byte[].class), any());
}
+ @Test
+ void referenceLambdaCannotReadIsRejectedBeforeTheCheck() {
+ List<String> checked = new ArrayList<>();
+
+ AwsException e = assertThrows(AwsException.class, () -> service.rotateSecret("my-secret", TOKEN,
+ "us-east-1:111122223333:function:rotator", null, false, REGION, checked::add));
+
+ assertEquals("InvalidParameterValueException", e.getErrorCode());
+ assertEquals(List.of(), checked);
+ assertNull(service.describeSecret("my-secret", REGION).getRotationLambdaArn());
+ }
+
@Test
void refusedCallerLeavesTheSecretUntouchedAndNothingRuns() {
Consumer<String> refuse = functionArn -> {