mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(secretsmanager): refuse an unreadable rotation function reference with InvalidParameterException (#4768)
Follow-up from the review of #4709. A RotationLambdaARN that Lambda's reference parser cannot read was refused with Lambda's own error, InvalidParameterValueException for a malformed reference or ValidationException for a name outside the FunctionName pattern. Neither is an error RotateSecret declares: its four are ResourceNotFoundException, InvalidParameterException, InternalServiceError and InvalidRequestException, so an SDK client could not type the refusal. It is now Secrets Manager's InvalidParameterException ("The parameter name or value is invalid.", HTTP 400), with Lambda's explanation kept in the message. The unit test that pinned the old code used a Region-prefixed partial ARN, which Lambda's documented FunctionName pattern accepts even though Floci's parser does not yet. It now uses references that are invalid under that pattern too: an empty function name, a character the name pattern excludes, and an empty qualifier. They cover both of Lambda's codes. An integration test checks the error type on the wire.
This commit is contained in:
1 parent
62bcfb95ed
commit
44e9b1600e
3 files changed
+57
-8
No files matched your search
+17
-4
@@ -1139,10 +1139,7 @@ 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.
|
||||
AwsArnUtils.Arn secretArn = AwsArnUtils.parse(secret.getArn());
|
||||
String functionArn = serviceManaged
|
||||
? null
|
||||
: LambdaArnUtils.functionArn(finalLambdaArn, secretArn.partition(), region, secretArn.accountId());
|
||||
String functionArn = serviceManaged ? null : rotationFunctionArn(finalLambdaArn, secret, region);
|
||||
if (!serviceManaged) {
|
||||
authorizeInvoke.accept(functionArn);
|
||||
}
|
||||
@@ -1214,6 +1211,22 @@ public class SecretsManagerService implements ResourceProvider {
|
||||
return secret;
|
||||
}
|
||||
|
||||
/**
|
||||
* The full ARN of the rotation function {@code reference} names, in the secret's partition and
|
||||
* account unless the reference names its own. A reference Lambda cannot read is refused with
|
||||
* Secrets Manager's {@code InvalidParameterException}: Lambda's codes for it,
|
||||
* {@code InvalidParameterValueException} and {@code ValidationException}, are not among the
|
||||
* errors {@code RotateSecret} declares, so an SDK client could not type them.
|
||||
*/
|
||||
private static String rotationFunctionArn(String reference, Secret secret, String region) {
|
||||
AwsArnUtils.Arn secretArn = AwsArnUtils.parse(secret.getArn());
|
||||
try {
|
||||
return LambdaArnUtils.functionArn(reference, secretArn.partition(), region, secretArn.accountId());
|
||||
} catch (AwsException e) {
|
||||
throw new AwsException("InvalidParameterException",
|
||||
"RotationLambdaARN is not a valid Lambda function reference: " + e.getMessage(), 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The outcome of {@link #cancelRotateSecret}: the secret with rotation turned off, plus the id
|
||||
|
||||
+24
@@ -12,6 +12,7 @@ import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -98,4 +99,27 @@ public class SecretsManagerRotationIntegrationTest {
|
||||
assertTrue(hasTest, "Should invoke testSecret");
|
||||
assertTrue(hasFinish, "Should invoke finishSecret");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rotationFunctionReferenceLambdaCannotReadIsAnInvalidParameter() {
|
||||
String secretName = "rotation-invalid-reference-secret";
|
||||
given()
|
||||
.header("X-Amz-Target", "secretsmanager.CreateSecret")
|
||||
.contentType(SM_CONTENT_TYPE)
|
||||
.body("{\"Name\": \"" + secretName + "\", \"SecretString\": \"initial-value\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "secretsmanager.RotateSecret")
|
||||
.contentType(SM_CONTENT_TYPE)
|
||||
.body("{\"SecretId\": \"" + secretName + "\", \"RotationLambdaARN\": \"rotator!\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterException"));
|
||||
}
|
||||
}
|
||||
+16
-4
@@ -12,6 +12,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -74,14 +75,25 @@ class SecretsManagerRotationInvokeAuthorizationTest {
|
||||
verify(lambda, timeout(5000).atLeastOnce()).invoke(eq(REGION), eq(expected), any(byte[].class), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void referenceLambdaCannotReadIsRejectedBeforeTheCheck() {
|
||||
/**
|
||||
* Each reference is invalid under Lambda's documented {@code FunctionName} pattern, not only
|
||||
* under Floci's parser: an empty name, a character the name pattern excludes, an empty qualifier.
|
||||
* RotateSecret answers with its own {@code InvalidParameterException}, not Lambda's codes.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"arn:aws:lambda:us-east-1:111122223333:function:",
|
||||
"rotator!",
|
||||
"rotator:"
|
||||
})
|
||||
void referenceLambdaCannotReadIsRejectedBeforeTheCheck(String reference) {
|
||||
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));
|
||||
reference, null, false, REGION, checked::add));
|
||||
|
||||
assertEquals("InvalidParameterValueException", e.getErrorCode());
|
||||
assertEquals("InvalidParameterException", e.getErrorCode());
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
assertEquals(List.of(), checked);
|
||||
assertNull(service.describeSecret("my-secret", REGION).getRotationLambdaArn());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user