mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(redshift-serverless): ensure generated admin password contains uppercase, lowercase, and digit (#7200)
* fix(redshift-serverless): ensure generated admin password contains uppercase, lowercase, and digit - Dam bao mat khau ngau nhien sinh ra luon chua it nhat mot chu hoa, mot chu thuong va mot chu so, dap ung quy tac mat khau cua Redshift * test(redshift-serverless): verify password complexity retry with deterministic candidates - Bo sung test xac dinh (deterministic) kiem tra generatePassword bo qua cac candidate thieu chu hoa, chu thuong, chu so va chi tra ve khi du ca 3 dieu kien - Tach generatePassword(Supplier<String>) de inject candidate trong test va tien bien dich cac Pattern kiem tra regex
This commit is contained in:
1 parent
fd4abd365b
commit
9fe670a5de
2 files changed
+42
-3
No files matched your search
+26
-3
@@ -19,6 +19,7 @@ import java.security.SecureRandom;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Owns the PostgreSQL container and the auth proxy behind a workgroup endpoint.
|
||||
@@ -35,6 +36,9 @@ public class RedshiftServerlessRuntime {
|
||||
private static final String PASSWORD_ALPHABET =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
private static final int GENERATED_PASSWORD_LENGTH = 32;
|
||||
private static final Pattern HAS_UPPERCASE = Pattern.compile("[A-Z]");
|
||||
private static final Pattern HAS_LOWERCASE = Pattern.compile("[a-z]");
|
||||
private static final Pattern HAS_DIGIT = Pattern.compile("[0-9]");
|
||||
|
||||
/** The namespace admin pair as it stands now; read on every login so a password change applies at once. */
|
||||
public record AdminCredentials(String username, String password) {
|
||||
@@ -199,11 +203,30 @@ public class RedshiftServerlessRuntime {
|
||||
}
|
||||
|
||||
public String generatePassword() {
|
||||
StringBuilder password = new StringBuilder(GENERATED_PASSWORD_LENGTH);
|
||||
return generatePassword(this::randomPasswordCandidate);
|
||||
}
|
||||
|
||||
String generatePassword(Supplier<String> candidateSupplier) {
|
||||
String password;
|
||||
do {
|
||||
password = candidateSupplier.get();
|
||||
} while (!isValidPassword(password));
|
||||
return password;
|
||||
}
|
||||
|
||||
private String randomPasswordCandidate() {
|
||||
StringBuilder sb = new StringBuilder(GENERATED_PASSWORD_LENGTH);
|
||||
for (int i = 0; i < GENERATED_PASSWORD_LENGTH; i++) {
|
||||
password.append(PASSWORD_ALPHABET.charAt(random.nextInt(PASSWORD_ALPHABET.length())));
|
||||
sb.append(PASSWORD_ALPHABET.charAt(random.nextInt(PASSWORD_ALPHABET.length())));
|
||||
}
|
||||
return password.toString();
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
static boolean isValidPassword(String password) {
|
||||
return password != null
|
||||
&& HAS_UPPERCASE.matcher(password).find()
|
||||
&& HAS_LOWERCASE.matcher(password).find()
|
||||
&& HAS_DIGIT.matcher(password).find();
|
||||
}
|
||||
|
||||
private void rollback(String accountId, String backendId) {
|
||||
|
||||
+16
@@ -14,6 +14,7 @@ import org.mockito.InOrder;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
@@ -286,6 +287,20 @@ class RedshiftServerlessRuntimeTest {
|
||||
assertTrue(BACKEND_ID.contains("_"), "cluster identifiers cannot contain an underscore");
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatePasswordRetriesUntilAllRequiredCharacterClassesArePresent() {
|
||||
Iterator<String> candidates = List.of(
|
||||
"abcdefghijklmnopqrstuvwxyz123456",
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ123456",
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef",
|
||||
"AdminPassword123WithAllClassesOk"
|
||||
).iterator();
|
||||
|
||||
String password = runtime.generatePassword(candidates::next);
|
||||
|
||||
assertEquals("AdminPassword123WithAllClassesOk", password);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatedPasswordsAreLongAlphanumericAndDifferEachTime() {
|
||||
String first = runtime.generatePassword();
|
||||
@@ -293,6 +308,7 @@ class RedshiftServerlessRuntimeTest {
|
||||
|
||||
assertEquals(32, first.length());
|
||||
assertTrue(first.matches("[A-Za-z0-9]+"), first);
|
||||
assertTrue(first.matches(".*[A-Z].*") && first.matches(".*[a-z].*") && first.matches(".*[0-9].*"), first);
|
||||
assertNotEquals(first, second);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user