mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 15:49:33 -05:00
fix(secretsmanager): require lambda:InvokeFunction for RotateSecret under IAM enforcement (#4697)
AWS requires the caller of RotateSecret to hold lambda:InvokeFunction on the rotation function as well as secretsmanager:RotateSecret (Secrets Manager API Reference, RotateSecret, "Required permissions"). Floci checked only RotateSecret and then invoked the function in-process, so under IAM enforcement a caller refused lambda:InvokeFunction could still run any function it could name by configuring it as a rotation function. RotateSecret now authorizes the invoke for the same caller the filter evaluated, against the exact function the rotation will run (the requested ARN, or the one already configured, with a bare name or partial ARN resolved in the secret's account and Region), and the rotation then invokes that same full ARN. Invoking the reference as given would let a name or partial ARN resolve in whatever account the rotation thread sees, so the function checked and the function run could differ. The check runs before the function is looked up or the secret changed, so a refused call leaves nothing configured for the scheduled sweep to invoke later. A refusal is reported as Secrets Manager reports one: AccessDeniedException with HTTP 400 (API Reference, "Common Errors"). A rotation function that does not exist is now reported the way AWS reports it too: AccessDeniedException, HTTP 400, "Secrets Manager cannot invoke the specified Lambda function. Ensure that the function policy grants access to the principal secretsmanager.amazonaws.com.", with the secret's rotation settings left as they were. It used to be ResourceNotFoundException with HTTP 404. The caller credential reaches the handler through IamEnforcementFilter.requestAuthorization, the filter's own header-or-presigned logic, so a presigned request is checked the same way. Requests with no caller credential, such as API Gateway service integrations, and rotations started by the scheduled sweep are unchanged. With IAM enforcement off the caller check does not run; the missing-function response above applies either way.
This commit is contained in:
1 parent
7ff6a7cfa7
commit
a075e25290
9 files changed
+467
-19
No files matched your search
@@ -67,6 +67,7 @@ import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
/**
|
||||
@@ -271,6 +272,7 @@ public class AwsJson11Controller {
|
||||
public Response handle(
|
||||
@HeaderParam("X-Amz-Target") String target,
|
||||
@Context HttpHeaders httpHeaders,
|
||||
@Context UriInfo uriInfo,
|
||||
String body) {
|
||||
|
||||
if (target == null) {
|
||||
@@ -313,7 +315,9 @@ public class AwsJson11Controller {
|
||||
case "wafv2" -> wafV2Handler.handle(action, request, region);
|
||||
case "memorydb" -> memoryDbHandler.handle(action, request, region);
|
||||
case "logs" -> cloudWatchLogsHandler.handle(action, request, region);
|
||||
case "secretsmanager" -> secretsManagerJsonHandler.handle(action, request, region);
|
||||
case "secretsmanager" -> secretsManagerJsonHandler.handle(action, request, region,
|
||||
IamEnforcementFilter.requestAuthorization(
|
||||
httpHeaders.getHeaderString("Authorization"), uriInfo.getQueryParameters()));
|
||||
case "kinesis" -> kinesisJsonHandler.handle(action, request, region);
|
||||
case "kinesisanalytics" -> kinesisAnalyticsV2JsonHandler.handle(action, request, region);
|
||||
case "apigatewayv2" -> apigwV2JsonHandler.handle(action, request, region);
|
||||
|
||||
@@ -24,6 +24,7 @@ import jakarta.ws.rs.container.ContainerRequestFilter;
|
||||
import jakarta.ws.rs.container.ResourceInfo;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.ext.Provider;
|
||||
import org.jboss.logging.Logger;
|
||||
@@ -255,7 +256,7 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
|
||||
String auth = ctx.getHeaderString("Authorization");
|
||||
if (auth == null) {
|
||||
auth = presignedCredentialAsAuthorization(ctx);
|
||||
auth = requestAuthorization(null, ctx.getUriInfo().getQueryParameters());
|
||||
}
|
||||
if (auth == null) {
|
||||
refuseUnsignedManagementCall(ctx);
|
||||
@@ -908,9 +909,20 @@ public class IamEnforcementFilter implements ContainerRequestFilter {
|
||||
* same way for the same reason. Synthesizing a {@code Credential=...} string from the query
|
||||
* parameter lets every downstream step here - access key extraction, credential scope,
|
||||
* action resolution, resource ARNs - run unchanged for both signing styles.
|
||||
*
|
||||
* <p>Public so that a handler authorizing a secondary resource through
|
||||
* {@link #authorizeAdditionalResource} hands it the same caller this filter evaluated, whichever
|
||||
* way the request was signed.
|
||||
*
|
||||
* @return the {@code Authorization} header, else a {@code Credential=...} string built from
|
||||
* {@code X-Amz-Credential}, else {@code null}
|
||||
*/
|
||||
private static String presignedCredentialAsAuthorization(ContainerRequestContext ctx) {
|
||||
String credential = ctx.getUriInfo().getQueryParameters().getFirst("X-Amz-Credential");
|
||||
public static String requestAuthorization(String authorizationHeader,
|
||||
MultivaluedMap<String, String> queryParameters) {
|
||||
if (authorizationHeader != null) {
|
||||
return authorizationHeader;
|
||||
}
|
||||
String credential = queryParameters == null ? null : queryParameters.getFirst("X-Amz-Credential");
|
||||
return credential == null || credential.isBlank() ? null : "Credential=" + credential;
|
||||
}
|
||||
|
||||
|
||||
+45
-8
@@ -1,14 +1,15 @@
|
||||
package io.github.hectorvent.floci.services.secretsmanager;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.IamEnforcementFilter;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.Secret;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
@@ -33,14 +34,31 @@ public class SecretsManagerJsonHandler {
|
||||
|
||||
private final SecretsManagerService service;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final IamEnforcementFilter iamEnforcementFilter;
|
||||
|
||||
@Inject
|
||||
public SecretsManagerJsonHandler(SecretsManagerService service, ObjectMapper objectMapper) {
|
||||
public SecretsManagerJsonHandler(SecretsManagerService service, ObjectMapper objectMapper,
|
||||
IamEnforcementFilter iamEnforcementFilter) {
|
||||
this.service = service;
|
||||
this.objectMapper = objectMapper;
|
||||
this.iamEnforcementFilter = iamEnforcementFilter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a request that carries no caller credential of its own, such as an API Gateway
|
||||
* service integration. Checks that authorize the caller for a second resource have no caller to
|
||||
* authorize here, so they are skipped, as {@link IamEnforcementFilter} skips unsigned requests.
|
||||
*/
|
||||
public Response handle(String action, JsonNode request, String region) {
|
||||
return handle(action, request, region, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param authorization the caller's credential as {@link IamEnforcementFilter#requestAuthorization}
|
||||
* reads it, used where an operation authorizes the caller for a second
|
||||
* resource
|
||||
*/
|
||||
public Response handle(String action, JsonNode request, String region, String authorization) {
|
||||
return switch (action) {
|
||||
case "CreateSecret" -> handleCreateSecret(request, region);
|
||||
case "GetSecretValue" -> handleGetSecretValue(request, region);
|
||||
@@ -50,7 +68,7 @@ public class SecretsManagerJsonHandler {
|
||||
case "ListSecrets" -> handleListSecrets(request, region);
|
||||
case "DeleteSecret" -> handleDeleteSecret(request, region);
|
||||
case "RestoreSecret" -> handleRestoreSecret(request, region);
|
||||
case "RotateSecret" -> handleRotateSecret(request, region);
|
||||
case "RotateSecret" -> handleRotateSecret(request, region, authorization);
|
||||
case "CancelRotateSecret" -> handleCancelRotateSecret(request, region);
|
||||
case "TagResource" -> handleTagResource(request, region);
|
||||
case "UntagResource" -> handleUntagResource(request, region);
|
||||
@@ -613,7 +631,7 @@ public class SecretsManagerJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleRotateSecret(JsonNode request, String region) {
|
||||
private Response handleRotateSecret(JsonNode request, String region, String authorization) {
|
||||
String secretId = request.path("SecretId").asText();
|
||||
String clientRequestToken = request.has("ClientRequestToken") ? request.path("ClientRequestToken").asText() : UUID.randomUUID().toString();
|
||||
|
||||
@@ -638,7 +656,8 @@ public class SecretsManagerJsonHandler {
|
||||
rotationRules = new Secret.RotationRules(automaticallyAfterDays, duration, scheduleExpression);
|
||||
}
|
||||
|
||||
Secret secret = service.rotateSecret(secretId, clientRequestToken, lambdaArn, rotationRules, rotateImmediately, region);
|
||||
Secret secret = service.rotateSecret(secretId, clientRequestToken, lambdaArn, rotationRules, rotateImmediately,
|
||||
region, functionArn -> authorizeRotationInvoke(authorization, functionArn));
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("ARN", secret.getArn());
|
||||
@@ -651,6 +670,24 @@ public class SecretsManagerJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* RotateSecret also needs {@code lambda:InvokeFunction} on the rotation function (Secrets Manager
|
||||
* API Reference, RotateSecret, "Required permissions"). The filter has already authorized
|
||||
* {@code secretsmanager:RotateSecret}; this authorizes the invoke for the same caller. The filter
|
||||
* reports a denial as S3's {@code AccessDenied}, so it is raised here the way Secrets Manager
|
||||
* reports one: {@code AccessDeniedException} with HTTP 400 (API Reference, "Common Errors").
|
||||
*/
|
||||
private void authorizeRotationInvoke(String authorization, String functionArn) {
|
||||
try {
|
||||
iamEnforcementFilter.authorizeAdditionalResource(authorization, "lambda:InvokeFunction", functionArn);
|
||||
} catch (AwsException e) {
|
||||
if (!"AccessDenied".equals(e.getErrorCode())) {
|
||||
throw e;
|
||||
}
|
||||
throw new AwsException("AccessDeniedException", e.getMessage(), 400);
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleCancelRotateSecret(JsonNode request, String region) {
|
||||
String secretId = request.path("SecretId").asText();
|
||||
// A service-managed secret is rotated by its owning service, so rotation can't be turned
|
||||
|
||||
+48
-5
@@ -43,6 +43,7 @@ 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.regex.Pattern;
|
||||
|
||||
@ApplicationScoped
|
||||
@@ -1088,8 +1089,23 @@ public class SecretsManagerService implements ResourceProvider {
|
||||
return secret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotation started by Floci itself, such as the scheduled sweep, with no caller to authorize.
|
||||
*/
|
||||
public Secret rotateSecret(String secretId, String clientRequestToken, String rotationLambdaArn, Secret.RotationRules rotationRules,
|
||||
boolean rotateImmediately, String region) {
|
||||
return rotateSecret(secretId, clientRequestToken, rotationLambdaArn, rotationRules, rotateImmediately, region,
|
||||
functionArn -> { });
|
||||
}
|
||||
|
||||
/**
|
||||
* @param authorizeInvoke called with the full ARN of the rotation function this call will
|
||||
* invoke, before the function is looked up or the secret changed, so the
|
||||
* caller can be refused {@code lambda:InvokeFunction} on exactly that
|
||||
* function
|
||||
*/
|
||||
public Secret rotateSecret(String secretId, String clientRequestToken, String rotationLambdaArn, Secret.RotationRules rotationRules,
|
||||
boolean rotateImmediately, String region, Consumer<String> authorizeInvoke) {
|
||||
Secret secret = resolveSecret(secretId, region);
|
||||
throwIfPendingDeletion(secret);
|
||||
throwIfReplica(secret);
|
||||
@@ -1118,14 +1134,24 @@ public class SecretsManagerService implements ResourceProvider {
|
||||
"You tried to enable rotation on a secret that doesn't already have a Lambda function ARN configured and you didn't include such an ARN as a parameter in this call.", 400);
|
||||
}
|
||||
|
||||
// Validate Lambda exists synchronously
|
||||
// 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);
|
||||
if (!serviceManaged) {
|
||||
authorizeInvoke.accept(functionArn);
|
||||
}
|
||||
|
||||
// A function that cannot be invoked is reported the way AWS reports it, whatever the reason:
|
||||
// AccessDeniedException with HTTP 400, and the secret left as it was.
|
||||
if (!serviceManaged && lambdaService != null) {
|
||||
try {
|
||||
lambdaService.getFunction(region, finalLambdaArn);
|
||||
lambdaService.getFunction(region, functionArn);
|
||||
} catch (AwsException e) {
|
||||
if (e.getHttpStatus() == 404) {
|
||||
throw new AwsException("ResourceNotFoundException",
|
||||
"Secrets Manager cannot find the specified Lambda function.", 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
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
@@ -1172,7 +1198,7 @@ public class SecretsManagerService implements ResourceProvider {
|
||||
|
||||
rotationExecutor.submit(() -> {
|
||||
try {
|
||||
executeRotationLifecycle(arn, finalToken, finalLambdaArn, rotateImmediately, isExistingVersion, region);
|
||||
executeRotationLifecycle(arn, finalToken, functionArn, rotateImmediately, isExistingVersion, region);
|
||||
} catch (Exception e) {
|
||||
LOG.errorv(e, "Rotation lifecycle failed for secret {0}", arn);
|
||||
}
|
||||
@@ -1183,6 +1209,23 @@ 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
|
||||
|
||||
Reference in new issue
Block a user