feat(cognito): add cognito overrides for client-id and secret. some b… (#1486)

* feat(cognito): add cognito overrides for client-id and secret. some boy-scouting

* feat(cognito): update docs, clarify reserved tags and possible values. Throws exception when unknown reserved tags are used.

* feat(cognito): reverted pom changes, align errorCode, use cleaned tag value

* feat(cognito): fix region resolver initialization
This commit is contained in:
Mark authored and GitHub committed 2026-06-27 00:10:20 -05:00
1 parent 6a0efb7803
commit dcc90031c5
17 files changed
+371 -209

No files matched your search

@@ -54,19 +54,19 @@ class ReservedTagsTest {
}
@Test
void extractOverrideIdReturnsNullForNullInput() {
assertNull(ReservedTags.extractOverrideId(null));
void extractOverrideUserPoolIdReturnsNullForNullInput() {
assertNull(ReservedTags.extractOverrideUserPoolId(null));
}
@Test
void extractOverrideIdReturnsReservedOverrideOnly() {
void extractOverrideIdReturnsReservedOverrideUserPoolOnly() {
Map<String, String> tags = Map.of(
ReservedTags.OVERRIDE_ID_KEY, "my-id",
"floci:internal", "hidden",
"env", "test"
);
assertEquals("my-id", ReservedTags.extractOverrideId(tags));
assertEquals("my-id", ReservedTags.extractOverrideUserPoolId(tags));
}
@Test
@@ -83,4 +83,24 @@ class ReservedTagsTest {
assertEquals("ValidationException", exception.getErrorCode());
}
@Test
void rejectUnknownReservedTagsRejectsReservedTagsWithUserPoolTaggingException() {
AwsException exception = assertThrows(
AwsException.class,
() -> ReservedTags.rejectUnknownReservedTags(Map.of(ReservedTags.RESERVED_PREFIX + "unknown-override", "something"), "UserPoolTaggingException")
);
assertEquals("UserPoolTaggingException", exception.getErrorCode());
}
@Test
void rejectUnknownReservedTagsRejectsReservedTagsWithTagException() {
AwsException exception = assertThrows(
AwsException.class,
() -> ReservedTags.rejectUnknownReservedTags(Map.of(ReservedTags.RESERVED_PREFIX + "unknown-override", "something"), "TagException")
);
assertEquals("TagException", exception.getErrorCode());
}
}
@@ -4,19 +4,19 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import static io.restassured.RestAssured.given;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
import static org.junit.jupiter.api.Assertions.*;
/**
* Integration tests for the AdminUserGlobalSignOut API.
*
* <p>
* Reproduces issue #1395: previously issued tokens must be invalidated
* after calling AdminUserGlobalSignOut, matching AWS Cognito behavior.
*/
@@ -24,13 +24,11 @@ import static org.junit.jupiter.api.Assertions.*;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class AdminUserGlobalSignOutIntegrationTest {
private static final String CONTENT_TYPE = "application/x-amz-json-1.1";
private static final ObjectMapper MAPPER = new ObjectMapper();
private static String poolId;
private static String clientId;
private static String refreshToken;
private static String accessToken;
private static final String USERNAME = "alice";
private static final String PASSWORD = "Password123!";
@@ -42,20 +40,6 @@ class AdminUserGlobalSignOutIntegrationTest {
// ─── helpers ────────────────────────────────────────────────────────────
private static Response cognitoAction(String action, String body) {
return given()
.header("X-Amz-Target", "AWSCognitoIdentityProviderService." + action)
.contentType(CONTENT_TYPE)
.body(body)
.when()
.post("/");
}
private static JsonNode cognitoJson(String action, String body) throws Exception {
return MAPPER.readTree(
cognitoAction(action, body).then().statusCode(200).extract().asString());
}
/** POST without asserting 200 — lets tests inspect the error response. */
private static JsonNode cognitoJsonAny(String action, String body) throws Exception {
return MAPPER.readTree(cognitoAction(action, body).then().extract().asString());
@@ -103,7 +87,7 @@ class AdminUserGlobalSignOutIntegrationTest {
""".formatted(clientId, USERNAME, PASSWORD));
refreshToken = auth.path("AuthenticationResult").path("RefreshToken").asText();
accessToken = auth.path("AuthenticationResult").path("AccessToken").asText();
String accessToken = auth.path("AuthenticationResult").path("AccessToken").asText();
assertFalse(refreshToken.isBlank(), "RefreshToken must be present after sign-in");
assertFalse(accessToken.isBlank(), "AccessToken must be present after sign-in");
@@ -29,6 +29,8 @@ import java.util.UUID;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -40,14 +42,13 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class CognitoIntegrationTest {
private static final String COGNITO_CONTENT_TYPE = "application/x-amz-json-1.1";
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final Pattern SIX_DIGIT_CODE = Pattern.compile("\\b(\\d{6})\\b");
private static String poolId;
private static String clientId;
private static final String username = "alice+" + UUID.randomUUID() + "@example.com";
private static final String password = "Perm1234!";
private static final String USERNAME = "alice+" + UUID.randomUUID() + "@example.com";
private static final String PASSWORD = "Perm1234!";
@BeforeAll
static void configureRestAssured() {
@@ -93,7 +94,7 @@ class CognitoIntegrationTest {
{ "Name": "custom:department", "Value": "engineering" }
]
}
""".formatted(poolId, username, username))
""".formatted(poolId, USERNAME, USERNAME))
.then()
.statusCode(200);
@@ -104,7 +105,7 @@ class CognitoIntegrationTest {
"Password": "%s",
"Permanent": true
}
""".formatted(poolId, username, password))
""".formatted(poolId, USERNAME, PASSWORD))
.then()
.statusCode(200);
}
@@ -121,7 +122,7 @@ class CognitoIntegrationTest {
"PASSWORD": "%s"
}
}
""".formatted(clientId, username, password))
""".formatted(clientId, USERNAME, PASSWORD))
.then()
.statusCode(200)
.body("AuthenticationResult.AccessToken", org.hamcrest.Matchers.notNullValue())
@@ -141,7 +142,7 @@ class CognitoIntegrationTest {
"PASSWORD": "%s"
}
}
""".formatted(clientId, username, password));
""".formatted(clientId, USERNAME, PASSWORD));
authResponse.then().statusCode(200);
@@ -151,7 +152,7 @@ class CognitoIntegrationTest {
assertEquals("RS256", header.path("alg").asText());
assertEquals(poolId, header.path("kid").asText());
assertEquals("http://localhost:4566/" + poolId, payload.path("iss").asText());
assertEquals(username, payload.path("username").asText());
assertEquals(USERNAME, payload.path("username").asText());
assertEquals("access", payload.path("token_use").asText());
String jwksResponse = given()
@@ -582,7 +583,7 @@ class CognitoIntegrationTest {
"GroupName": "admin",
"Username": "%s"
}
""".formatted(poolId, username))
""".formatted(poolId, USERNAME))
.then()
.statusCode(200);
}
@@ -595,7 +596,7 @@ class CognitoIntegrationTest {
"UserPoolId": "%s",
"Username": "%s"
}
""".formatted(poolId, username));
""".formatted(poolId, USERNAME));
assertEquals(1, resp.path("Groups").size());
assertEquals("admin", resp.path("Groups").get(0).path("GroupName").asText());
}
@@ -612,7 +613,7 @@ class CognitoIntegrationTest {
"PASSWORD": "%s"
}
}
""".formatted(clientId, username, password));
""".formatted(clientId, USERNAME, PASSWORD));
authResponse.then().statusCode(200);
@@ -634,7 +635,7 @@ class CognitoIntegrationTest {
"GroupName": "admin",
"Username": "%s"
}
""".formatted(poolId, username))
""".formatted(poolId, USERNAME))
.then()
.statusCode(200);
}
@@ -647,7 +648,7 @@ class CognitoIntegrationTest {
"UserPoolId": "%s",
"Username": "%s"
}
""".formatted(poolId, username));
""".formatted(poolId, USERNAME));
assertEquals(0, resp.path("Groups").size());
}
@@ -730,7 +731,7 @@ class CognitoIntegrationTest {
"GroupName": "editors",
"Username": "%s"
}
""".formatted(poolId, username))
""".formatted(poolId, USERNAME))
.then().statusCode(200);
// List users in group
@@ -742,7 +743,7 @@ class CognitoIntegrationTest {
""".formatted(poolId));
assertEquals(1, resp.path("Users").size());
assertEquals(username, resp.path("Users").get(0).path("Username").asText());
assertEquals(USERNAME, resp.path("Users").get(0).path("Username").asText());
}
@Test
@@ -755,7 +756,7 @@ class CognitoIntegrationTest {
"GroupName": "editors",
"Username": "%s"
}
""".formatted(poolId, username))
""".formatted(poolId, USERNAME))
.then().statusCode(200);
JsonNode resp = cognitoJson("ListUsersInGroup", """
@@ -796,7 +797,7 @@ class CognitoIntegrationTest {
"AuthFlow": "USER_PASSWORD_AUTH",
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "%s" }
}
""".formatted(clientId, username, password));
""".formatted(clientId, USERNAME, PASSWORD));
String idToken = auth.path("AuthenticationResult").path("IdToken").asText();
JsonNode payload = decodeJwtPayload(idToken);
assertTrue(payload.path("client_id").isMissingNode(),
@@ -814,7 +815,7 @@ class CognitoIntegrationTest {
"AuthFlow": "USER_PASSWORD_AUTH",
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "%s" }
}
""".formatted(clientId, username, password));
""".formatted(clientId, USERNAME, PASSWORD));
String idToken = auth.path("AuthenticationResult").path("IdToken").asText();
JsonNode payload = decodeJwtPayload(idToken);
assertEquals(clientId, payload.path("aud").asText(),
@@ -839,7 +840,7 @@ class CognitoIntegrationTest {
"AuthFlow": "USER_PASSWORD_AUTH",
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "%s" }
}
""".formatted(clientId, username, password));
""".formatted(clientId, USERNAME, PASSWORD));
String refreshToken = authResp.path("AuthenticationResult").path("RefreshToken").asText();
JsonNode refreshed = cognitoJson("InitiateAuth", """
@@ -982,7 +983,7 @@ class CognitoIntegrationTest {
"AuthFlow": "USER_PASSWORD_AUTH",
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "%s" }
}
""".formatted(clientId, username, password));
""".formatted(clientId, USERNAME, PASSWORD));
JsonNode payload = decodeJwtPayload(
auth.path("AuthenticationResult").path("IdToken").asText());
@@ -1010,7 +1011,7 @@ class CognitoIntegrationTest {
"AuthFlow": "USER_PASSWORD_AUTH",
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "%s" }
}
""".formatted(clientId, username, password));
""".formatted(clientId, USERNAME, PASSWORD));
JsonNode payload = decodeJwtPayload(
auth.path("AuthenticationResult").path("AccessToken").asText());
@@ -1032,7 +1033,7 @@ class CognitoIntegrationTest {
"AuthFlow": "USER_PASSWORD_AUTH",
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "%s" }
}
""".formatted(clientId, username, password));
""".formatted(clientId, USERNAME, PASSWORD));
String refreshToken = authResp.path("AuthenticationResult").path("RefreshToken").asText();
JsonNode refreshed = cognitoJson("InitiateAuth", """
@@ -1063,7 +1064,7 @@ class CognitoIntegrationTest {
"AuthFlow": "USER_PASSWORD_AUTH",
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "WrongPassword!" }
}
""".formatted(clientId, username))
""".formatted(clientId, USERNAME))
.then()
.statusCode(400);
}
@@ -1075,7 +1076,7 @@ class CognitoIntegrationTest {
void adminGetUserBySubUuid() throws Exception {
JsonNode user = cognitoJson("AdminGetUser", """
{ "UserPoolId": "%s", "Username": "%s" }
""".formatted(poolId, username));
""".formatted(poolId, USERNAME));
String sub = null;
for (JsonNode attr : user.path("UserAttributes")) {
if ("sub".equals(attr.path("Name").asText())) {
@@ -1088,7 +1089,7 @@ class CognitoIntegrationTest {
JsonNode bySubUser = cognitoJson("AdminGetUser", """
{ "UserPoolId": "%s", "Username": "%s" }
""".formatted(poolId, sub));
assertEquals(username, bySubUser.path("Username").asText(),
assertEquals(USERNAME, bySubUser.path("Username").asText(),
"AdminGetUser with sub UUID should return the same user");
}
@@ -1097,8 +1098,8 @@ class CognitoIntegrationTest {
void adminGetUserByEmailAlias() throws Exception {
JsonNode byEmail = cognitoJson("AdminGetUser", """
{ "UserPoolId": "%s", "Username": "%s" }
""".formatted(poolId, username));
assertEquals(username, byEmail.path("Username").asText());
""".formatted(poolId, USERNAME));
assertEquals(USERNAME, byEmail.path("Username").asText());
}
// ── Issue #233: ListUsers Filter ─────────────────────────────────────
@@ -1111,16 +1112,16 @@ class CognitoIntegrationTest {
"UserPoolId": "%s",
"Filter": "email = \\"%s\\""
}
""".formatted(poolId, username));
""".formatted(poolId, USERNAME));
assertEquals(1, resp.path("Users").size(),
"Filter by email should return exactly one matching user");
assertEquals(username, resp.path("Users").get(0).path("Username").asText());
assertEquals(USERNAME, resp.path("Users").get(0).path("Username").asText());
}
@Test
@Order(61)
void listUsersFilterByEmailPrefixStartsWith() throws Exception {
String prefix = username.substring(0, 5);
String prefix = USERNAME.substring(0, 5);
JsonNode resp = cognitoJson("ListUsers", """
{
"UserPoolId": "%s",
@@ -1151,7 +1152,7 @@ class CognitoIntegrationTest {
"AuthFlow": "USER_PASSWORD_AUTH",
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "%s" }
}
""".formatted(clientId, username, password));
""".formatted(clientId, USERNAME, PASSWORD));
String refreshToken = authResp.path("AuthenticationResult").path("RefreshToken").asText();
assertNotNull(refreshToken);
@@ -1177,7 +1178,7 @@ class CognitoIntegrationTest {
"AuthFlow": "USER_PASSWORD_AUTH",
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "%s" }
}
""".formatted(clientId, username, password));
""".formatted(clientId, USERNAME, PASSWORD));
String refreshToken = authResp.path("AuthenticationResult").path("RefreshToken").asText();
JsonNode refreshed = cognitoJson("InitiateAuth", """
@@ -1420,14 +1421,14 @@ class CognitoIntegrationTest {
@Test
@Order(90)
void adminResetUserPasswordBlocksAuth() throws Exception {
void adminResetUserPasswordBlocksAuth() {
// 1. Reset the user's password
cognitoAction("AdminResetUserPassword", """
{
"UserPoolId": "%s",
"Username": "%s"
}
""".formatted(poolId, username))
""".formatted(poolId, USERNAME))
.then()
.statusCode(200);
@@ -1441,7 +1442,7 @@ class CognitoIntegrationTest {
"PASSWORD": "%s"
}
}
""".formatted(clientId, username, password))
""".formatted(clientId, USERNAME, PASSWORD))
.then()
.statusCode(400)
.body("__type", org.hamcrest.Matchers.containsString("PasswordResetRequiredException"));
@@ -1455,7 +1456,7 @@ class CognitoIntegrationTest {
"Password": "%s",
"Permanent": true
}
""".formatted(poolId, username, newPassword))
""".formatted(poolId, USERNAME, newPassword))
.then()
.statusCode(200);
@@ -1469,7 +1470,7 @@ class CognitoIntegrationTest {
"PASSWORD": "%s"
}
}
""".formatted(clientId, username, newPassword))
""".formatted(clientId, USERNAME, newPassword))
.then()
.statusCode(200);
}
@@ -1503,7 +1504,7 @@ class CognitoIntegrationTest {
{ "Name": "email", "Value": "%s" }
]
}
""".formatted(localClientId, testUser, password, testUser));
""".formatted(localClientId, testUser, PASSWORD, testUser));
// Get user details to verify user status is UNCONFIRMED
JsonNode userResp = cognitoJson("AdminGetUser", """
@@ -2155,23 +2156,7 @@ class CognitoIntegrationTest {
assertEquals(1200L, refreshedIdPayload.path("exp").asLong() - refreshedIdPayload.path("iat").asLong());
}
private static Response cognitoAction(String action, String body) {
return given()
.header("X-Amz-Target", "AWSCognitoIdentityProviderService." + action)
.contentType(COGNITO_CONTENT_TYPE)
.body(body)
.when()
.post("/");
}
private static JsonNode cognitoJson(String action, String body) throws Exception {
String response = cognitoAction(action, body)
.then()
.statusCode(200)
.extract()
.asString();
return OBJECT_MAPPER.readTree(response);
}
private static JsonNode decodeJwtPayload(String token) throws Exception {
return decodeJwtPart(token, 1);
@@ -2209,7 +2194,7 @@ class CognitoIntegrationTest {
"AuthFlow": "USER_PASSWORD_AUTH",
"AuthParameters": { "USERNAME": "%s", "PASSWORD": "%s" }
}
""".formatted(clientId, username, password));
""".formatted(clientId, USERNAME, PASSWORD));
return auth.path("AuthenticationResult").path("AccessToken").asText();
}
@@ -19,17 +19,16 @@ import java.security.Signature;
import java.security.spec.RSAPublicKeySpec;
import java.util.Base64;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class CognitoOAuthTokenIntegrationTest {
private static final String COGNITO_CONTENT_TYPE = "application/x-amz-json-1.1";
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static String poolId;
@@ -482,24 +481,6 @@ class CognitoOAuthTokenIntegrationTest {
assertEquals("client_secret_basic", document.path("token_endpoint_auth_methods_supported").get(0).asText());
}
private static Response cognitoAction(String action, String body) {
return given()
.header("X-Amz-Target", "AWSCognitoIdentityProviderService." + action)
.contentType(COGNITO_CONTENT_TYPE)
.body(body)
.when()
.post("/");
}
private static JsonNode cognitoJson(String action, String body) throws Exception {
String response = cognitoAction(action, body)
.then()
.statusCode(200)
.extract()
.asString();
return OBJECT_MAPPER.readTree(response);
}
private static String cognitoDescribeClientSecret(String clientId) {
return cognitoAction("DescribeUserPoolClient", """
{
@@ -0,0 +1,17 @@
package io.github.hectorvent.floci.services.cognito;
import com.fasterxml.jackson.databind.JsonNode;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.restassured.response.Response;
public class CognitoRestAssuredUtils {
private CognitoRestAssuredUtils(){}
public static Response cognitoAction(String action, String body) {
return RestAssuredJsonUtils.awsAction("AWSCognitoIdentityProviderService", action, body);
}
public static JsonNode cognitoJson(String action, String body) throws Exception {
return RestAssuredJsonUtils.awsActionJson("AWSCognitoIdentityProviderService", action, body);
}
}
@@ -13,6 +13,8 @@ import io.github.hectorvent.floci.services.cognito.verification.VerificationCode
import io.github.hectorvent.floci.services.cognito.verification.VerificationCodeService;
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 java.nio.charset.StandardCharsets;
import java.util.ArrayList;
@@ -607,7 +609,7 @@ class CognitoServiceTest {
)
);
assertEquals("ValidationException", exception.getErrorCode());
assertEquals("InvalidParameterException", exception.getErrorCode());
}
@Test
@@ -620,7 +622,7 @@ class CognitoServiceTest {
)
);
assertEquals("ValidationException", exception.getErrorCode());
assertEquals("InvalidParameterException", exception.getErrorCode());
}
@Test
@@ -632,7 +634,7 @@ class CognitoServiceTest {
"us-east-1"
)
);
assertEquals("ValidationException", questionMarkException.getErrorCode());
assertEquals("InvalidParameterException", questionMarkException.getErrorCode());
AwsException hashException = assertThrows(
AwsException.class,
@@ -641,7 +643,7 @@ class CognitoServiceTest {
"us-east-1"
)
);
assertEquals("ValidationException", hashException.getErrorCode());
assertEquals("InvalidParameterException", hashException.getErrorCode());
}
@Test
@@ -1980,4 +1982,68 @@ class CognitoServiceTest {
CognitoUser user = service.adminGetUser(pool.getId(), "carol");
assertEquals("Carolyn", user.getAttributes().get("given_name"));
}
// =========================================================================
// Cognito ClientId And Secret overrides
// =========================================================================
@ParameterizedTest
@CsvSource( {
"use-name,basic-client",
"prepend-to-name:prepended-,prepended-basic-client",
"append-to-name:-appended,basic-client-appended",
})
void createUserPoolWithOverrideForClientIdAndClientSecret(String overrideClientId, String expectedClientId) {
UserPool pool = service.createUserPool(
Map.of(
"PoolName", "ClientOverridesPool",
"UserPoolTags", Map.of(
"env", "test",
ReservedTags.OVERRIDE_COGNITO_CLIENT_ID_KEY, overrideClientId,
ReservedTags.OVERRIDE_COGNITO_CLIENT_SECRET_KEY, "secret")
),
"us-east-1"
);
assertEquals("test",pool.getUserPoolTags().get("env"));
assertFalse(pool.getUserPoolTags().containsKey(ReservedTags.OVERRIDE_COGNITO_CLIENT_ID_KEY));
assertFalse(pool.getUserPoolTags().containsKey(ReservedTags.OVERRIDE_COGNITO_CLIENT_SECRET_KEY));
UserPoolClient client = service.createUserPoolClient(
pool.getId(),
"basic-client",
true,
true,
List.of(),
List.of()
);
assertEquals("basic-client", client.getClientName());
assertEquals(expectedClientId, client.getClientId());
assertEquals("secret", client.getClientSecret());
}
@ParameterizedTest
@CsvSource( {
"prepend-to-name: prepended- ,secret",
"append-to-name: -appended ,secret",
"append-to-name:-appended,",
"something-else,secret"
})
void createUserPoolWithInvalidOverrideForClientIdAndClientSecret(String overrideClientId, String secret) {
Map<String, Object> createUserPool = new HashMap<>();
Map<String,String> userPoolTags = new HashMap<>();
userPoolTags.put(ReservedTags.OVERRIDE_COGNITO_CLIENT_ID_KEY, overrideClientId);
userPoolTags.put(ReservedTags.OVERRIDE_COGNITO_CLIENT_SECRET_KEY, secret);
createUserPool.put("PoolName", "InvalidOverridesPool");
createUserPool.put("UserPoolTags", userPoolTags);
AwsException ex = assertThrows(AwsException.class, () ->
service.createUserPool(
createUserPool,
"us-east-1"
));
assertEquals("InvalidParameterException", ex.getErrorCode());
}
}
@@ -13,25 +13,23 @@ import org.junit.jupiter.api.TestMethodOrder;
import java.util.UUID;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoAction;
import static io.github.hectorvent.floci.services.cognito.CognitoRestAssuredUtils.cognitoJson;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class CognitoUserInfoIntegrationTest {
private static final String COGNITO_CONTENT_TYPE = "application/x-amz-json-1.1";
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static String poolId;
private static String clientId;
private static String accessToken;
private static final String username = "carol+" + UUID.randomUUID() + "@example.com";
private static final String password = "Perm1234!";
private static final String USERNAME = "carol+" + UUID.randomUUID() + "@example.com";
private static final String PASSWORD = "Perm1234!";
@BeforeAll
static void configureRestAssured() {
@@ -71,7 +69,7 @@ class CognitoUserInfoIntegrationTest {
{ "Name": "custom:tenant_id", "Value": "tenant-42" }
]
}
""".formatted(poolId, username, username))
""".formatted(poolId, USERNAME, USERNAME))
.then()
.statusCode(200);
@@ -82,7 +80,7 @@ class CognitoUserInfoIntegrationTest {
"Password": "%s",
"Permanent": true
}
""".formatted(poolId, username, password))
""".formatted(poolId, USERNAME, PASSWORD))
.then()
.statusCode(200);
@@ -95,7 +93,7 @@ class CognitoUserInfoIntegrationTest {
"PASSWORD": "%s"
}
}
""".formatted(clientId, username, password));
""".formatted(clientId, USERNAME, PASSWORD));
accessToken = authResp.path("AuthenticationResult").path("AccessToken").asText();
assertNotNull(accessToken);
assertFalse(accessToken.isBlank());
@@ -116,9 +114,9 @@ class CognitoUserInfoIntegrationTest {
JsonNode body = OBJECT_MAPPER.readTree(response.asString());
assertEquals(username, body.path("username").asText());
assertEquals(USERNAME, body.path("username").asText());
assertTrue(body.hasNonNull("sub"));
assertEquals(username, body.path("email").asText());
assertEquals(USERNAME, body.path("email").asText());
// email_verified / phone_number_verified must be JSON strings, not booleans.
assertTrue(body.path("email_verified").isTextual(),
"email_verified must be a string per AWS Cognito spec");
@@ -263,7 +261,7 @@ class CognitoUserInfoIntegrationTest {
"PASSWORD": "%s"
}
}
""".formatted(clientId, username, password));
""".formatted(clientId, USERNAME, PASSWORD));
String refreshToken = initialAuth.path("AuthenticationResult").path("RefreshToken").asText();
assertNotNull(refreshToken);
@@ -286,7 +284,7 @@ class CognitoUserInfoIntegrationTest {
.extract()
.asString();
JsonNode refreshedBody = OBJECT_MAPPER.readTree(refreshedResponse);
assertEquals(username, refreshedBody.path("username").asText());
assertEquals(USERNAME, refreshedBody.path("username").asText());
assertEquals("true", refreshedBody.path("email_verified").asText());
assertEquals("tenant-42", refreshedBody.path("custom:tenant_id").asText());
}
@@ -338,24 +336,6 @@ class CognitoUserInfoIntegrationTest {
.header("WWW-Authenticate", containsString("invalid_token"));
}
private static Response cognitoAction(String action, String body) {
return given()
.header("X-Amz-Target", "AWSCognitoIdentityProviderService." + action)
.contentType(COGNITO_CONTENT_TYPE)
.body(body)
.when()
.post("/");
}
private static JsonNode cognitoJson(String action, String body) throws Exception {
String response = cognitoAction(action, body)
.then()
.statusCode(200)
.extract()
.asString();
return OBJECT_MAPPER.readTree(response);
}
private static String base64Url(String raw) {
return java.util.Base64.getUrlEncoder().withoutPadding()
.encodeToString(raw.getBytes(java.nio.charset.StandardCharsets.UTF_8));
@@ -945,7 +945,7 @@ class KmsServiceTest {
() -> kmsService.createKey("bad", null, Map.of(ReservedTags.OVERRIDE_ID_KEY, " "), REGION)
);
assertEquals("ValidationException", exception.getErrorCode());
assertEquals("TagException", exception.getErrorCode());
}
@Test
@@ -1,5 +1,7 @@
package io.github.hectorvent.floci.testing;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.restassured.RestAssured;
import io.restassured.config.EncoderConfig;
import io.restassured.filter.Filter;
@@ -9,6 +11,10 @@ import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;
import static io.github.hectorvent.floci.core.common.AwsJson11Controller.CONTENT_TYPE_AWS_JSON_1_1;
import static io.github.hectorvent.floci.core.common.AwsJsonController.CONTENT_TYPE_AWS_JSON_1_0;
import static io.restassured.RestAssured.given;
/**
* Registers a global RestAssured filter that rewrites AWS-specific JSON content types
* to standard application/json before response parsing.
@@ -22,10 +28,8 @@ import io.restassured.specification.FilterableResponseSpecification;
*/
public class RestAssuredJsonUtils {
private static final String AWS_CONTENT_TYPE_1_0 = "application/x-amz-json-1.0";
private static final String AWS_CONTENT_TYPE_1_1 = "application/x-amz-json-1.1";
private static final AwsContentTypeFilter AWS_CONTENT_TYPE_FILTER = new AwsContentTypeFilter();
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private RestAssuredJsonUtils() {
// Utility class, prevent instantiation
@@ -34,13 +38,32 @@ public class RestAssuredJsonUtils {
public static void configureAwsContentTypes() {
RestAssured.config = RestAssured.config().encoderConfig(
EncoderConfig.encoderConfig()
.encodeContentTypeAs(AWS_CONTENT_TYPE_1_0, ContentType.JSON)
.encodeContentTypeAs(AWS_CONTENT_TYPE_1_1, ContentType.JSON));
.encodeContentTypeAs(CONTENT_TYPE_AWS_JSON_1_0, ContentType.JSON)
.encodeContentTypeAs(CONTENT_TYPE_AWS_JSON_1_1, ContentType.JSON));
if (!RestAssured.filters().contains(AWS_CONTENT_TYPE_FILTER)) {
RestAssured.filters(AWS_CONTENT_TYPE_FILTER);
}
}
public static Response awsAction(String target, String action, String body) {
return given()
.header("X-Amz-Target", target + "." + action)
.contentType(CONTENT_TYPE_AWS_JSON_1_1)
.body(body)
.when()
.post("/");
}
public static JsonNode awsActionJson(String target, String action, String body) throws Exception {
String response = awsAction(target, action, body)
.then()
.statusCode(200)
.extract()
.asString();
return OBJECT_MAPPER.readTree(response);
}
}
/**