feat(cognito): include all standard attributes in DescribeUserPool Schema (#642)

* feat(cognito): include all standard attributes in DescribeUserPool SchemaAttributes

AWS Cognito always returns all 20 standard OIDC attributes (sub, name,
email, phone_number, etc.) in SchemaAttributes when describing a user
pool, even if none were explicitly configured. Floci was returning only
what was passed in Schema during creation.

Adds CognitoStandardAttributes with the full default set and a merge()
helper that overlays explicit schema overrides and appends custom:
attributes after the standard ones.

* test(cognito): add compat tests for standard SchemaAttributes across all SDKs

Adds DescribeUserPool SchemaAttributes coverage to all compatibility test
suites (AWS CLI/bats, Go, Java, Node, Python, Rust), verifying that all
20 standard OIDC attributes are returned and that sub is required and
immutable.
This commit is contained in:
Yorí authored and GitHub committed 2026-04-29 00:01:48 -05:00
1 parent 87eda4bcfc
commit 1cccf43f7b
12 files changed
+620 -3

No files matched your search

@@ -24,11 +24,15 @@ import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.RSAPublicKeySpec;
import java.util.Base64;
import java.util.List;
import java.util.Spliterators;
import java.util.UUID;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static io.restassured.RestAssured.given;
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;
@@ -176,6 +180,36 @@ class CognitoIntegrationTest {
assertEquals("RS256", document.path("id_token_signing_alg_values_supported").get(0).asText());
}
@Test
@Order(5)
void describeUserPoolReturnsAllTwentyStandardAttributes() throws Exception {
JsonNode body = cognitoJson("DescribeUserPool", """
{ "UserPoolId": "%s" }
""".formatted(poolId));
JsonNode schema = body.path("UserPool").path("SchemaAttributes");
assertEquals(20, schema.size(), "DescribeUserPool must include all 20 Cognito standard attributes");
java.util.Set<String> names = new java.util.HashSet<>();
schema.forEach(attr -> names.add(attr.path("Name").asText()));
for (String expected : List.of("sub", "name", "given_name", "family_name", "middle_name",
"nickname", "preferred_username", "profile", "picture", "website",
"email", "email_verified", "gender", "birthdate", "zoneinfo",
"locale", "phone_number", "phone_number_verified", "address", "updated_at")) {
assertTrue(names.contains(expected), "Missing standard attribute in DescribeUserPool response: " + expected);
}
// spot-check: sub must be required and immutable
JsonNode sub = StreamSupport.stream(
Spliterators.spliteratorUnknownSize(schema.elements(), 0), false)
.filter(n -> "sub".equals(n.path("Name").asText()))
.findFirst()
.orElseThrow();
assertTrue(sub.path("Required").asBoolean(), "sub must be Required");
assertFalse(sub.path("Mutable").asBoolean(), "sub must not be Mutable");
}
// ── Groups ────────────────────────────────────────────────────────
@Test
@@ -15,6 +15,10 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import static org.junit.jupiter.api.Assertions.*;
@@ -93,8 +97,9 @@ class CognitoJsonHandlerTest {
// Check mandatory blocks for Terraform
assertNotNull(pool.get("SchemaAttributes"));
assertEquals(1, pool.get("SchemaAttributes").size());
assertEquals("email", pool.get("SchemaAttributes").get(0).get("Name").asText());
assertEquals(20, pool.get("SchemaAttributes").size(),
"DescribeUserPool must always return all 20 Cognito standard attributes");
assertTrue(schemaNames(pool).contains("email"));
assertNotNull(pool.get("Policies"));
assertNotNull(pool.get("LambdaConfig"));
@@ -190,6 +195,80 @@ class CognitoJsonHandlerTest {
assertFalse(afterUntag.has("team"));
}
@Test
void describeUserPoolWithNoSchemaReturnsAllTwentyStandardAttributes() {
ObjectNode create = mapper.createObjectNode();
create.put("PoolName", "no-schema-pool");
JsonNode created = (JsonNode) handler.handle("CreateUserPool", create, "us-east-1").getEntity();
String poolId = created.get("UserPool").get("Id").asText();
ObjectNode describe = mapper.createObjectNode();
describe.put("UserPoolId", poolId);
JsonNode body = (JsonNode) handler.handle("DescribeUserPool", describe, "us-east-1").getEntity();
JsonNode schema = body.get("UserPool").get("SchemaAttributes");
assertEquals(20, schema.size());
Set<String> names = schemaNames(body.get("UserPool"));
List.of("sub", "name", "given_name", "family_name", "middle_name", "nickname",
"preferred_username", "profile", "picture", "website", "email",
"email_verified", "gender", "birthdate", "zoneinfo", "locale",
"phone_number", "phone_number_verified", "address", "updated_at")
.forEach(n -> assertTrue(names.contains(n), "missing standard attribute: " + n));
}
@Test
void describeUserPoolMergesCustomAttributeAfterStandardOnes() {
ObjectNode create = mapper.createObjectNode();
create.put("PoolName", "custom-attr-pool");
ArrayNode schema = create.putArray("Schema");
ObjectNode custom = schema.addObject();
custom.put("Name", "custom:tenant_id");
custom.put("AttributeDataType", "String");
JsonNode created = (JsonNode) handler.handle("CreateUserPool", create, "us-east-1").getEntity();
String poolId = created.get("UserPool").get("Id").asText();
ObjectNode describe = mapper.createObjectNode();
describe.put("UserPoolId", poolId);
JsonNode body = (JsonNode) handler.handle("DescribeUserPool", describe, "us-east-1").getEntity();
JsonNode schemaNode = body.get("UserPool").get("SchemaAttributes");
assertEquals(21, schemaNode.size(), "20 standard + 1 custom");
Set<String> names = schemaNames(body.get("UserPool"));
assertTrue(names.contains("custom:tenant_id"));
assertTrue(names.contains("sub"));
assertTrue(names.contains("email"));
// custom attribute must be last (after all standard ones)
assertEquals("custom:tenant_id", schemaNode.get(20).get("Name").asText());
}
@Test
void describeUserPoolExplicitStandardAttributeOverridesDefault() {
ObjectNode create = mapper.createObjectNode();
create.put("PoolName", "override-attr-pool");
ArrayNode schema = create.putArray("Schema");
ObjectNode emailOverride = schema.addObject();
emailOverride.put("Name", "email");
emailOverride.put("AttributeDataType", "String");
emailOverride.put("Required", true);
JsonNode created = (JsonNode) handler.handle("CreateUserPool", create, "us-east-1").getEntity();
String poolId = created.get("UserPool").get("Id").asText();
ObjectNode describe = mapper.createObjectNode();
describe.put("UserPoolId", poolId);
JsonNode body = (JsonNode) handler.handle("DescribeUserPool", describe, "us-east-1").getEntity();
JsonNode schemaNode = body.get("UserPool").get("SchemaAttributes");
assertEquals(20, schemaNode.size(), "override should not add a duplicate entry");
JsonNode emailAttr = StreamSupport.stream(Spliterators.spliteratorUnknownSize(
schemaNode.elements(), 0), false)
.filter(n -> "email".equals(n.get("Name").asText()))
.findFirst()
.orElseThrow();
assertTrue(emailAttr.get("Required").asBoolean(), "email must be required per the override");
}
@Test
void tagResourceRejectsReservedKey() {
ObjectNode createRequest = mapper.createObjectNode();
@@ -207,4 +286,11 @@ class CognitoJsonHandlerTest {
);
assertEquals("ValidationException", exception.getErrorCode());
}
private Set<String> schemaNames(JsonNode pool) {
return StreamSupport.stream(
Spliterators.spliteratorUnknownSize(pool.get("SchemaAttributes").elements(), 0), false)
.map(n -> n.get("Name").asText())
.collect(Collectors.toSet());
}
}
@@ -0,0 +1,168 @@
package io.github.hectorvent.floci.services.cognito;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
class CognitoStandardAttributesTest {
private static final Set<String> EXPECTED_NAMES = Set.of(
"sub", "name", "given_name", "family_name", "middle_name", "nickname",
"preferred_username", "profile", "picture", "website", "email",
"email_verified", "gender", "birthdate", "zoneinfo", "locale",
"phone_number", "phone_number_verified", "address", "updated_at");
@Test
void defaultsContainsAllTwentyStandardAttributes() {
assertEquals(20, CognitoStandardAttributes.DEFAULTS.size());
Set<String> names = names(CognitoStandardAttributes.DEFAULTS);
assertEquals(EXPECTED_NAMES, names);
}
@Test
void subIsRequiredAndImmutable() {
Map<String, Object> sub = findByName(CognitoStandardAttributes.DEFAULTS, "sub");
assertEquals("String", sub.get("AttributeDataType"));
assertEquals(Boolean.TRUE, sub.get("Required"));
assertEquals(Boolean.FALSE, sub.get("Mutable"));
@SuppressWarnings("unchecked")
Map<String, String> constraints = (Map<String, String>) sub.get("StringAttributeConstraints");
assertEquals("1", constraints.get("MinLength"));
assertEquals("2048", constraints.get("MaxLength"));
}
@Test
void emailHasStringConstraints() {
Map<String, Object> email = findByName(CognitoStandardAttributes.DEFAULTS, "email");
assertEquals("String", email.get("AttributeDataType"));
assertEquals(Boolean.FALSE, email.get("Required"));
assertEquals(Boolean.TRUE, email.get("Mutable"));
@SuppressWarnings("unchecked")
Map<String, String> constraints = (Map<String, String>) email.get("StringAttributeConstraints");
assertEquals("0", constraints.get("MinLength"));
assertEquals("2048", constraints.get("MaxLength"));
}
@Test
void birthdateHasTenCharacterConstraint() {
Map<String, Object> birthdate = findByName(CognitoStandardAttributes.DEFAULTS, "birthdate");
@SuppressWarnings("unchecked")
Map<String, String> constraints = (Map<String, String>) birthdate.get("StringAttributeConstraints");
assertEquals("10", constraints.get("MinLength"));
assertEquals("10", constraints.get("MaxLength"));
}
@Test
void updatedAtIsNumberType() {
Map<String, Object> updatedAt = findByName(CognitoStandardAttributes.DEFAULTS, "updated_at");
assertEquals("Number", updatedAt.get("AttributeDataType"));
@SuppressWarnings("unchecked")
Map<String, String> constraints = (Map<String, String>) updatedAt.get("NumberAttributeConstraints");
assertNotNull(constraints);
assertEquals("0", constraints.get("MinValue"));
}
@Test
void emailVerifiedAndPhoneVerifiedAreBooleanType() {
for (String name : List.of("email_verified", "phone_number_verified")) {
Map<String, Object> attr = findByName(CognitoStandardAttributes.DEFAULTS, name);
assertEquals("Boolean", attr.get("AttributeDataType"), name + " should be Boolean");
assertFalse(attr.containsKey("StringAttributeConstraints"), name + " should have no string constraints");
}
}
@Test
void noDeveloperOnlyAttributeInStandardAttrs() {
for (Map<String, Object> attr : CognitoStandardAttributes.DEFAULTS) {
assertEquals(Boolean.FALSE, attr.get("DeveloperOnlyAttribute"),
attr.get("Name") + " should not be developer-only");
}
}
// ── merge() ──────────────────────────────────────────────────────────────
@Test
void mergeWithNullReturnsAllDefaults() {
List<Map<String, Object>> result = CognitoStandardAttributes.merge(null);
assertEquals(20, result.size());
assertEquals(EXPECTED_NAMES, names(result));
}
@Test
void mergeWithEmptyListReturnsAllDefaults() {
List<Map<String, Object>> result = CognitoStandardAttributes.merge(List.of());
assertEquals(20, result.size());
assertEquals(EXPECTED_NAMES, names(result));
}
@Test
void mergeAppendsCustomAttributeAfterStandardOnes() {
List<Map<String, Object>> schema = List.of(
Map.of("Name", "custom:department", "AttributeDataType", "String"));
List<Map<String, Object>> result = CognitoStandardAttributes.merge(schema);
assertEquals(21, result.size());
assertTrue(names(result).contains("custom:department"));
assertTrue(names(result).containsAll(EXPECTED_NAMES));
assertEquals("custom:department", result.get(20).get("Name"),
"custom attribute must come after all standard ones");
}
@Test
void mergeWithMultipleCustomAttributesAppendsAllInOrder() {
List<Map<String, Object>> schema = List.of(
Map.of("Name", "custom:tenant_id", "AttributeDataType", "String"),
Map.of("Name", "custom:role", "AttributeDataType", "String"));
List<Map<String, Object>> result = CognitoStandardAttributes.merge(schema);
assertEquals(22, result.size());
assertEquals("custom:tenant_id", result.get(20).get("Name"));
assertEquals("custom:role", result.get(21).get("Name"));
}
@Test
void mergeWithExplicitStandardAttributeOverridesDefault() {
Map<String, Object> override = Map.of(
"Name", "email",
"AttributeDataType", "String",
"Required", Boolean.TRUE,
"Mutable", Boolean.TRUE);
List<Map<String, Object>> result = CognitoStandardAttributes.merge(List.of(override));
assertEquals(20, result.size(), "override must not create a duplicate");
Map<String, Object> emailAttr = findByName(result, "email");
assertEquals(Boolean.TRUE, emailAttr.get("Required"),
"email Required should reflect the override");
}
@Test
void mergePreservesStandardAttributeOrder() {
List<Map<String, Object>> result = CognitoStandardAttributes.merge(null);
// sub must be first
assertEquals("sub", result.get(0).get("Name"));
// updated_at must be last among standard attrs (index 19)
assertEquals("updated_at", result.get(19).get("Name"));
}
// ── helpers ──────────────────────────────────────────────────────────────
private static Set<String> names(List<Map<String, Object>> attrs) {
return attrs.stream().map(a -> (String) a.get("Name")).collect(Collectors.toSet());
}
private static Map<String, Object> findByName(List<Map<String, Object>> attrs, String name) {
return attrs.stream()
.filter(a -> name.equals(a.get("Name")))
.findFirst()
.orElseThrow(() -> new AssertionError("attribute not found: " + name));
}
}