Light, fluffy, and always free - The AWS Local Emulator alternative
test(iam): reuse signed user fixtures in signature tests (#6675)
Andre Kuraitc0e3ffea94d5
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/SigV4HeaderSignatureIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/SigV4HeaderSignatureIntegrationTest.javaindex dc33c7e429..2fddc2494f 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/SigV4HeaderSignatureIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/SigV4HeaderSignatureIntegrationTest.java@@ -1,6 +1,7 @@package io.github.hectorvent.floci.core.common;import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;+import io.github.hectorvent.floci.testing.SignedIamUsers;import io.github.hectorvent.floci.testing.ValidateSignaturesProfile;import io.github.hectorvent.floci.testutil.AppSyncRequestSigner;import io.github.hectorvent.floci.testutil.AwsRequestSigner;@@ -54,9 +55,6 @@ class SigV4HeaderSignatureIntegrationTest {private static final String FORM = "application/x-www-form-urlencoded";private static final String CBOR_1_1 = "application/x-amz-cbor-1.1";- private static String userAccessKeyId;- private static String userSecretKey;-@Test@Order(100)void legacyS3HeadersRemainRejectedWhenGlobalSignatureValidationIsEnabled() {@@ -255,38 +253,34 @@ class SigV4HeaderSignatureIntegrationTest {@Test@Order(9)- void iamCreatedKeyVerifiesWithItsOwnSecretOnly() {- AwsRequestSigner iam = AwsRequestSigner.signedAs("test", "test", "iam");- given().filter(iam).contentType(FORM)- .body("Action=CreateUser&UserName=validate-signatures-sqs-user&Version=2010-05-08")- .when().post("/").then().statusCode(200);- XmlPath key = given().filter(iam).contentType(FORM)- .body("Action=CreateAccessKey&UserName=validate-signatures-sqs-user&Version=2010-05-08")- .when().post("/").then().statusCode(200).extract().xmlPath();- userAccessKeyId = key.getString("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId");- userSecretKey = key.getString("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.SecretAccessKey");-- AwsRequestSigner user = AwsRequestSigner.signedAs(userAccessKeyId, userSecretKey, "sqs");- given()- .filter(user)- .contentType(JSON_1_0)- .header("X-Amz-Target", "AmazonSQS.ListQueues")- .body("{}")- .when()- .post("/")- .then()- .statusCode(200);+ void iamCreatedKeyVerifiesWithItsOwnSecretOnly() throws Exception {+ URI endpoint = URI.create("http://localhost:" + RestAssured.port + "/");+ String userName;+ try (SignedIamUsers.User credentials = SignedIamUsers.createUser(endpoint, "validate-signatures-sqs-")) {+ userName = credentials.name();+ AwsRequestSigner user = credentials.signer("sqs");+ given()+ .filter(user)+ .contentType(JSON_1_0)+ .header("X-Amz-Target", "AmazonSQS.ListQueues")+ .body("{}")+ .when()+ .post("/")+ .then()+ .statusCode(200);- given()- .filter(user.withSecret("not-the-secret"))- .contentType(JSON_1_0)- .header("X-Amz-Target", "AmazonSQS.ListQueues")- .body("{}")- .when()- .post("/")- .then()- .statusCode(403)- .body("__type", equalTo("InvalidSignatureException"));+ given()+ .filter(user.withSecret("not-the-secret"))+ .contentType(JSON_1_0)+ .header("X-Amz-Target", "AmazonSQS.ListQueues")+ .body("{}")+ .when()+ .post("/")+ .then()+ .statusCode(403)+ .body("__type", equalTo("InvalidSignatureException"));+ }+ SignedIamUsers.iam(endpoint, "GetUser", Map.of("UserName", userName)).then().statusCode(404);}@ParameterizedTest@@ -510,20 +504,22 @@ class SigV4HeaderSignatureIntegrationTest {@Test@Order(19)- void queryGetAuthenticatesTheIssuedKeyBeforeDispatch() {- AwsRequestSigner signer = AwsRequestSigner.signedAs(- userAccessKeyId, userSecretKey, "elasticloadbalancing");- given().filter(signer)- .queryParam("Action", "DescribeLoadBalancers")- .queryParam("Version", "2012-06-01")- .get("/").then().statusCode(200)- .body(containsString("DescribeLoadBalancersResponse"));- given().filter(signer.withSecret("not-the-issued-secret"))- .queryParam("Action", "DescribeLoadBalancers")- .queryParam("Version", "2012-06-01")- .get("/").then().statusCode(403)- .contentType(containsString("xml"))- .body("ErrorResponse.Error.Code", equalTo("SignatureDoesNotMatch"));+ void queryGetAuthenticatesTheIssuedKeyBeforeDispatch() throws Exception {+ URI endpoint = URI.create("http://localhost:" + RestAssured.port + "/");+ try (SignedIamUsers.User user = SignedIamUsers.createUser(endpoint, "validate-signatures-query-")) {+ AwsRequestSigner signer = user.signer("elasticloadbalancing");+ given().filter(signer)+ .queryParam("Action", "DescribeLoadBalancers")+ .queryParam("Version", "2012-06-01")+ .get("/").then().statusCode(200)+ .body(containsString("DescribeLoadBalancersResponse"));+ given().filter(signer.withSecret("not-the-issued-secret"))+ .queryParam("Action", "DescribeLoadBalancers")+ .queryParam("Version", "2012-06-01")+ .get("/").then().statusCode(403)+ .contentType(containsString("xml"))+ .body("ErrorResponse.Error.Code", equalTo("SignatureDoesNotMatch"));+ }}@Test@@ -532,9 +528,8 @@ class SigV4HeaderSignatureIntegrationTest {URI endpoint = URI.create("http://localhost:" + RestAssured.port + "/");Map<String, String> query = Map.of("Action", "DescribeLoadBalancers", "Version", "2012-06-01");- AwsRequestSigner signer = AwsRequestSigner.signedAs(- userAccessKeyId, userSecretKey, "elasticloadbalancing");- try (HttpClient client = HttpClient.newHttpClient()) {+ try (SignedIamUsers.User user = SignedIamUsers.createUser(endpoint, "validate-signatures-presigned-"); HttpClient client = HttpClient.newHttpClient()) {+ AwsRequestSigner signer = user.signer("elasticloadbalancing");URI signed = signer.presignedUri("GET", endpoint, query, new byte[0]);HttpResponse<String> accepted = client.send(HttpRequest.newBuilder(signed).GET().build(),HttpResponse.BodyHandlers.ofString());diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ValidateSignaturesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ValidateSignaturesIntegrationTest.javaindex 06f3993168..d01e40ee48 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ValidateSignaturesIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ValidateSignaturesIntegrationTest.java@@ -4,13 +4,12 @@ import io.github.hectorvent.floci.core.common.Base64Url;import io.github.hectorvent.floci.core.common.Digests;import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;import io.github.hectorvent.floci.testing.MutableClock;+import io.github.hectorvent.floci.testing.SignedIamUsers;import io.github.hectorvent.floci.testing.ValidateSignaturesProfile;-import io.github.hectorvent.floci.testutil.AwsRequestSigner;import io.github.hectorvent.floci.testutil.S3RequestSigner;import io.quarkus.test.junit.QuarkusTest;import io.quarkus.test.junit.TestProfile;import io.restassured.RestAssured;-import io.restassured.path.xml.XmlPath;import io.restassured.response.Response;import jakarta.inject.Inject;import org.junit.jupiter.api.MethodOrderer;@@ -63,9 +62,6 @@ class S3ValidateSignaturesIntegrationTest {private static final String SIGNIN_REDIRECT_URI = "http://127.0.0.1:4567/oauth/callback";private static final String SIGNIN_VERIFIER = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~";- private static String userAccessKeyId;- private static String userSecretKey;-@InjectMutableClock clock;@@ -158,30 +154,35 @@ class S3ValidateSignaturesIntegrationTest {@Test@Order(10)- void verifiedCallerIsNotAuthorizedAgainstTheBucketPolicy() {- createIamUser("validate-signatures-user");- given()- .filter(LOCAL_SIGNER)- .body(DENY_ALL_POLICY)- .when()- .put("/" + BUCKET + "?policy")- .then()- .statusCode(200);- given()- .filter(LOCAL_SIGNER)- .when()- .get("/" + BUCKET + "?policy")- .then()- .statusCode(200)- .body(containsString("\"Deny\""));-- given()- .filter(S3RequestSigner.signedAs(userAccessKeyId, userSecretKey))- .when()- .get("/" + BUCKET + "/" + KEY)- .then()- .statusCode(200)- .body(equalTo("signed body"));+ void verifiedCallerIsNotAuthorizedAgainstTheBucketPolicy() throws Exception {+ URI endpoint = URI.create("http://localhost:" + RestAssured.port + "/");+ String userName;+ try (SignedIamUsers.User user = SignedIamUsers.createUser(endpoint, "validate-signatures-user-")) {+ userName = user.name();+ given()+ .filter(LOCAL_SIGNER)+ .body(DENY_ALL_POLICY)+ .when()+ .put("/" + BUCKET + "?policy")+ .then()+ .statusCode(200);+ given()+ .filter(LOCAL_SIGNER)+ .when()+ .get("/" + BUCKET + "?policy")+ .then()+ .statusCode(200)+ .body(containsString("\"Deny\""));++ given()+ .filter(S3RequestSigner.signedAs(user.accessKeyId(), user.secretAccessKey()))+ .when()+ .get("/" + BUCKET + "/" + KEY)+ .then()+ .statusCode(200)+ .body(equalTo("signed body"));+ }+ SignedIamUsers.iam(endpoint, "GetUser", Map.of("UserName", userName)).then().statusCode(404);}@Test@@ -473,32 +474,6 @@ class S3ValidateSignaturesIntegrationTest {parts -> URLDecoder.decode(parts[1], StandardCharsets.UTF_8)));}- private static void createIamUser(String userName) {- // validate-signatures verifies IAM's signatures too, so these calls carry real ones.- AwsRequestSigner iam = AwsRequestSigner.signedAs("test", "test", "iam");- given()- .filter(iam)- .contentType("application/x-www-form-urlencoded")- .body("Action=CreateUser&UserName=" + userName + "&Version=2010-05-08")- .when()- .post("/")- .then()- .statusCode(200);-- XmlPath key = given()- .filter(iam)- .contentType("application/x-www-form-urlencoded")- .body("Action=CreateAccessKey&UserName=" + userName + "&Version=2010-05-08")- .when()- .post("/")- .then()- .statusCode(200)- .extract()- .xmlPath();- userAccessKeyId = key.getString("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId");- userSecretKey = key.getString("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.SecretAccessKey");- }-private static String postPolicy(String key) {return encodePolicy("""[{"bucket": "%s"}, {"key": "%s"}]""".formatted(BUCKET, key));diff --git a/src/test/java/io/github/hectorvent/floci/testing/SignedIamUsers.java b/src/test/java/io/github/hectorvent/floci/testing/SignedIamUsers.javaindex 2feff673f1..ad40ef8969 100644--- a/src/test/java/io/github/hectorvent/floci/testing/SignedIamUsers.java+++ b/src/test/java/io/github/hectorvent/floci/testing/SignedIamUsers.java@@ -17,7 +17,7 @@ import static io.restassured.RestAssured.given;* {@link SignedIamEnforcementProfile}), where {@link EnforcementFixtures}' placeholder signatures* are refused. Every call is signed as the root {@code test} key.*- * <p>{@link #createUser} makes an IAM user with an access key and one inline policy; closing the+ * <p>{@link #createUser} makes an IAM user with an access key and an optional inline policy; closing the* returned {@link User} deletes the key, the policy and the user, so a test can hold it in a* try-with-resources block.*/@@ -37,6 +37,11 @@ public final class SignedIamUsers {.contentType("application/x-www-form-urlencoded").body(body).post(endpoint);}+ /** Creates a user with one access key and no inline policy. */+ public static User createUser(URI endpoint, String namePrefix) throws Exception {+ return createUser(endpoint, namePrefix, null, null);+ }+/*** Creates a user named {@code namePrefix} plus a random suffix, with one access key and the* inline policy {@code policyName} holding {@code policyDocument}. If a later step fails, the@@ -53,8 +58,10 @@ public final class SignedIamUsers {user = new User(endpoint, name, policyName,credentials.xmlPath().getString("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId"),credentials.xmlPath().getString("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.SecretAccessKey"));- iam(endpoint, "PutUserPolicy", Map.of("UserName", name, "PolicyName", policyName,- "PolicyDocument", policyDocument)).then().statusCode(200);+ if (policyName != null) {+ iam(endpoint, "PutUserPolicy", Map.of("UserName", name, "PolicyName", policyName,+ "PolicyDocument", policyDocument)).then().statusCode(200);+ }return user;} catch (Exception | AssertionError e) {try {@@ -81,7 +88,9 @@ public final class SignedIamUsers {iam(endpoint, "DeleteAccessKey", Map.of("UserName", name, "AccessKeyId", accessKeyId)).then().statusCode(200);}- iam(endpoint, "DeleteUserPolicy", Map.of("UserName", name, "PolicyName", policyName));+ if (policyName != null) {+ iam(endpoint, "DeleteUserPolicy", Map.of("UserName", name, "PolicyName", policyName));+ }iam(endpoint, "DeleteUser", Map.of("UserName", name)).then().statusCode(200);}}