Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cognito): stop defaulting unset password requirements to enabled (#4250)

Abhigya4e42f477400a
diff --git a/compatibility-tests/sdk-test-go/tests/cognito_test.go b/compatibility-tests/sdk-test-go/tests/cognito_test.go
index 448641eb8c..b261929f6d 100644
--- a/compatibility-tests/sdk-test-go/tests/cognito_test.go
+++ b/compatibility-tests/sdk-test-go/tests/cognito_test.go
@@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider"
+ "github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -60,3 +61,48 @@ func TestCognitoDescribeUserPoolStandardAttributes(t *testing.T) {
}
}
}
+
+// The four Require* members of PasswordPolicyType are unboxed booleans in the Cognito model, so
+// this SDK cannot put them on the wire when they are false: the generated serializer emits each
+// one under `if v.RequireLowercase != false`. The request below therefore carries nothing but
+// MinimumLength, which is exactly what terraform-provider-aws sends for a password_policy block
+// with require_* = false. A pool that answered those members as true made the first plan after
+// apply report drift on all four.
+func TestCognitoCreateUserPoolLeavesUnsetPasswordRequirementsOff(t *testing.T) {
+ ctx := context.Background()
+ svc := testutil.CognitoClient()
+
+ created, err := svc.CreateUserPool(ctx, &cognitoidentityprovider.CreateUserPoolInput{
+ PoolName: aws.String("go-test-cognito-password-policy"),
+ Policies: &types.UserPoolPolicyType{
+ PasswordPolicy: &types.PasswordPolicyType{
+ MinimumLength: aws.Int32(7),
+ RequireLowercase: false,
+ RequireNumbers: false,
+ RequireSymbols: false,
+ RequireUppercase: false,
+ },
+ },
+ })
+ require.NoError(t, err)
+ poolID := created.UserPool.Id
+
+ t.Cleanup(func() {
+ svc.DeleteUserPool(ctx, &cognitoidentityprovider.DeleteUserPoolInput{
+ UserPoolId: poolID,
+ })
+ })
+
+ described, err := svc.DescribeUserPool(ctx, &cognitoidentityprovider.DescribeUserPoolInput{
+ UserPoolId: poolID,
+ })
+ require.NoError(t, err)
+
+ policy := described.UserPool.Policies.PasswordPolicy
+ require.NotNil(t, policy)
+ assert.Equal(t, int32(7), aws.ToInt32(policy.MinimumLength), "MinimumLength must survive the round trip")
+ assert.False(t, policy.RequireLowercase, "an unset RequireLowercase must not come back enabled")
+ assert.False(t, policy.RequireNumbers, "an unset RequireNumbers must not come back enabled")
+ assert.False(t, policy.RequireSymbols, "an unset RequireSymbols must not come back enabled")
+ assert.False(t, policy.RequireUppercase, "an unset RequireUppercase must not come back enabled")
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java
index 0c3766b69b..e1721b1381 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoService.java
@@ -331,11 +331,22 @@ public class CognitoService implements ResourceProvider {
/**
* Fills in AWS's per-field password policy defaults for a pool created with a
- * {@code PasswordPolicy} that has some fields unset: MinimumLength 8 (AWS's documented
- * complex-password recommendation; the field itself only documents a minimum of 6), the four
- * character-class requirements enabled, and TemporaryPasswordValidityDays 7 (the one default
- * the API reference states explicitly). "If you don't provide a value for an attribute,
- * Amazon Cognito sets it to its default value" (CreateUserPool).
+ * {@code PasswordPolicy} that has some fields unset: TemporaryPasswordValidityDays 7 (the one
+ * default the API reference states explicitly) and MinimumLength 8 (AWS's documented
+ * complex-password recommendation; the field itself only documents a minimum of 6).
+ * "If you don't provide a value for an attribute, Amazon Cognito sets it to its default
+ * value" (CreateUserPool).
+ *
+ * <p>RequireUppercase, RequireLowercase, RequireNumbers and RequireSymbols are deliberately
+ * left alone. The API reference documents no default for any of them, and they are unboxed
+ * booleans in the Cognito model, so a client that wants one off cannot say so on the wire:
+ * aws-sdk-go-v2 emits each under {@code if v.RequireLowercase != false}. Absence therefore
+ * means "not required", which is what the live service reports back - the Terraform provider
+ * asserts require_numbers and require_uppercase read as false immediately after a create
+ * that set them to false. Defaulting them to enabled made every Terraform plan after a
+ * create show spurious drift, and over-enforced the policy on SignUp and
+ * AdminSetUserPassword. The all-enabled policy is the console's "Cognito defaults" mode,
+ * not an API default.
*
* <p>Deliberately does not fabricate a {@code PasswordPolicy} for a pool that supplies none
* at all — every other test and fixture in this codebase creates pools that way, relying on
@@ -355,10 +366,6 @@ public class CognitoService implements ResourceProvider {
Map<String, Object> normalized = new HashMap<>(policies);
Map<String, Object> passwordPolicy = new HashMap<>((Map<String, Object>) raw);
passwordPolicy.putIfAbsent("MinimumLength", 8);
- passwordPolicy.putIfAbsent("RequireUppercase", true);
- passwordPolicy.putIfAbsent("RequireLowercase", true);
- passwordPolicy.putIfAbsent("RequireNumbers", true);
- passwordPolicy.putIfAbsent("RequireSymbols", true);
passwordPolicy.putIfAbsent("TemporaryPasswordValidityDays", 7);
normalized.put("PasswordPolicy", passwordPolicy);
pool.setPolicies(normalized);
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java
index 60dd7be806..e23ab98b5d 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoIntegrationTest.java
@@ -479,6 +479,66 @@ class CognitoIntegrationTest {
.statusCode(200);
}
+ @Test
+ void createUserPoolDoesNotEnablePasswordRequirementsTheRequestOmitted() throws Exception {
+ // RequireUppercase and its three siblings are unboxed booleans in the Cognito model, so a
+ // client that wants them off omits them: this body is what aws-sdk-go-v2 puts on the wire
+ // for a policy with every require_* set to false. Answering it with the members enabled
+ // made Terraform report drift on all four in the first plan after a create.
+ JsonNode created = cognitoJson("CreateUserPool", """
+ {
+ "PoolName": "NoPasswordRequirementsPool",
+ "Policies": {
+ "PasswordPolicy": {
+ "MinimumLength": 7
+ }
+ }
+ }
+ """);
+ String pool = created.path("UserPool").path("Id").asText();
+
+ JsonNode described = cognitoJson("DescribeUserPool", """
+ {
+ "UserPoolId": "%s"
+ }
+ """.formatted(pool));
+
+ JsonNode policy = described.path("UserPool").path("Policies").path("PasswordPolicy");
+ assertEquals(7, policy.path("MinimumLength").asInt());
+ assertEquals(7, policy.path("TemporaryPasswordValidityDays").asInt());
+ assertFalse(policy.has("RequireUppercase"), "RequireUppercase must not be defaulted on");
+ assertFalse(policy.has("RequireLowercase"), "RequireLowercase must not be defaulted on");
+ assertFalse(policy.has("RequireNumbers"), "RequireNumbers must not be defaulted on");
+ assertFalse(policy.has("RequireSymbols"), "RequireSymbols must not be defaulted on");
+ }
+
+ @Test
+ void createUserPoolKeepsPasswordRequirementsTheRequestDisabledExplicitly() throws Exception {
+ // The Java SDK boxes these members, so it can say false where the Go SDK cannot. That
+ // false must be stored and echoed rather than replaced.
+ JsonNode created = cognitoJson("CreateUserPool", """
+ {
+ "PoolName": "ExplicitlyDisabledRequirementsPool",
+ "Policies": {
+ "PasswordPolicy": {
+ "MinimumLength": 10,
+ "RequireUppercase": false,
+ "RequireLowercase": true,
+ "RequireNumbers": false,
+ "RequireSymbols": false
+ }
+ }
+ }
+ """);
+
+ JsonNode policy = created.path("UserPool").path("Policies").path("PasswordPolicy");
+ assertEquals(10, policy.path("MinimumLength").asInt());
+ assertFalse(policy.path("RequireUppercase").asBoolean());
+ assertTrue(policy.path("RequireLowercase").asBoolean());
+ assertFalse(policy.path("RequireNumbers").asBoolean());
+ assertFalse(policy.path("RequireSymbols").asBoolean());
+ }
+
@Test
@Order(7)
void confirmSignUpRequiresValidConfirmationCode() throws Exception {
diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java
index 268e90e917..142712eb80 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoServiceTest.java
@@ -136,15 +136,14 @@ class CognitoServiceTest {
List.of(Map.of("Name", "custom:my-attr", "AttributeDataType", "String")),
pool.getSchemaAttributes(),
"a non-standard Schema attribute is stored under the custom: namespace");
- // A supplied PasswordPolicy is normalized with AWS's defaults for the fields left unset
- // (MinimumLength here was explicit; the rest were not), matching what DescribeUserPool
- // returns on real Cognito for a policy submitted this way.
+ // A supplied PasswordPolicy only picks up the two defaults the API reference documents,
+ // TemporaryPasswordValidityDays 7 and MinimumLength (explicit here). The four Require*
+ // members carry no documented default and must stay unset: they are unboxed booleans in
+ // the Cognito model, so an SDK asked for false omits them from the wire entirely, and
+ // filling them in with true is what made a pool created with require_symbols = false
+ // read back as true.
Map<String, Object> expectedPasswordPolicy = new HashMap<>();
expectedPasswordPolicy.put("MinimumLength", 12);
- expectedPasswordPolicy.put("RequireUppercase", true);
- expectedPasswordPolicy.put("RequireLowercase", true);
- expectedPasswordPolicy.put("RequireNumbers", true);
- expectedPasswordPolicy.put("RequireSymbols", true);
expectedPasswordPolicy.put("TemporaryPasswordValidityDays", 7);
assertEquals(Map.of("PasswordPolicy", expectedPasswordPolicy), pool.getPolicies());
assertEquals(List.of("email"), pool.getUsernameAttributes());
@@ -499,8 +498,9 @@ class CognitoServiceTest {
@Test
void createUserPoolDefaultsAnUnsetMinimumLengthToEight() {
// A policy present but silent on MinimumLength gets AWS's default (8), not policyInt's
- // fallback of 0 for an absent key — and the unset RequireUppercase/Lowercase/Numbers
- // default to enabled too, the same "Cognito defaults" a console-created pool gets.
+ // fallback of 0 for an absent key. The character classes the policy did not ask for are
+ // not enforced: only RequireSymbols was set, so a password with no uppercase and no
+ // digit is accepted once it is long enough.
UserPool pool = service.createUserPool(Map.of(
"PoolName", "SymbolsOnlyPool",
"Policies", Map.of("PasswordPolicy", Map.of("RequireSymbols", true))
@@ -514,10 +514,59 @@ class CognitoServiceTest {
assertEquals("InvalidPasswordException", exception.getErrorCode());
assertDoesNotThrow(() -> service.signUp(
- client.getClientId(), "[email protected]", "Eightplus1!", Map.of(
+ client.getClientId(), "[email protected]", "lowercase!", Map.of(
"email", "[email protected]", "phone_number", "+4915112345679")));
}
+ @Test
+ void createUserPoolLeavesPasswordRequirementsTheRequestOmittedUnset() {
+ // The four Require* members are unboxed booleans in the Cognito model, so an SDK told to
+ // require nothing sends a PasswordPolicy carrying only the members it can express -
+ // aws-sdk-go-v2 emits `if v.RequireLowercase != false`. Treating their absence as
+ // "enabled" turned Terraform's require_lowercase = false into permanent drift on the
+ // first plan after create, because UpdateUserPool (which does not normalize) reported
+ // them absent while CreateUserPool reported them true.
+ UserPool pool = service.createUserPool(Map.of(
+ "PoolName", "NoRequirementsPool",
+ "Policies", Map.of("PasswordPolicy", Map.of("MinimumLength", 7))
+ ), "us-east-1");
+
+ Map<String, Object> expectedPasswordPolicy = new HashMap<>();
+ expectedPasswordPolicy.put("MinimumLength", 7);
+ expectedPasswordPolicy.put("TemporaryPasswordValidityDays", 7);
+ assertEquals(Map.of("PasswordPolicy", expectedPasswordPolicy), pool.getPolicies());
+
+ UserPoolClient client = service.createUserPoolClient(
+ pool.getId(), "no-requirements-client", false, false, List.of(), List.of());
+ assertDoesNotThrow(() -> service.signUp(
+ client.getClientId(), "[email protected]", "abcdefg", Map.of(
+ "email", "[email protected]", "phone_number", "+4915112345678")));
+ }
+
+ @Test
+ void createUserPoolKeepsPasswordRequirementsTheRequestDisabledExplicitly() {
+ // A caller that can express false - the Java SDK boxes these members, and so does a
+ // hand-written JSON 1.1 request - must have it stored as false, not overwritten.
+ UserPool pool = service.createUserPool(Map.of(
+ "PoolName", "ExplicitFalsePool",
+ "Policies", Map.of("PasswordPolicy", Map.of(
+ "MinimumLength", 10,
+ "RequireUppercase", false,
+ "RequireLowercase", true,
+ "RequireNumbers", false,
+ "RequireSymbols", false))
+ ), "us-east-1");
+
+ Map<String, Object> expectedPasswordPolicy = new HashMap<>();
+ expectedPasswordPolicy.put("MinimumLength", 10);
+ expectedPasswordPolicy.put("RequireUppercase", false);
+ expectedPasswordPolicy.put("RequireLowercase", true);
+ expectedPasswordPolicy.put("RequireNumbers", false);
+ expectedPasswordPolicy.put("RequireSymbols", false);
+ expectedPasswordPolicy.put("TemporaryPasswordValidityDays", 7);
+ assertEquals(Map.of("PasswordPolicy", expectedPasswordPolicy), pool.getPolicies());
+ }
+
@Test
void createUserPoolNamespacesNonStandardSchemaAttributes() {
// The Schema property carries custom attributes unprefixed - it is what CloudFormation