8000 Stabilizing asynchronous login failure processing by ahus1 · Pull Request #52503 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ public static void waitForModalFadeOut() {
waitUntilElementIsNotPresent(By.className("modal-backdrop"));
}

/**
* Wait for the brute force executor to finish processing all pending login failure tasks.
* Needed because {@code DefaultBlockingBruteForceProtector} processes failure counting asynchronously.
* Without this wait, a subsequent login request may race with the in-flight task and miss incrementing
* the failure counter (see <a href="https://github.com/keycloak/keycloak/issues/52428">#52428</a>).
* Once <a href="https://github.com/keycloak/keycloak/issues/52128">#52128</a> makes brute force processing synchronous, this method becomes unnecessary.
*/
public static void waitForBruteForceExecutors(KeycloakTestingClient testingClient) {
testingClient.server().run(session -> {
ExecutorsProvider provider = session.getProvider(ExecutorsProvider.class);
Expand All @@ -177,7 +184,7 @@ public static void waitForBruteForceExecutors(KeycloakTestingClient testingClien
CompletableFuture.runAsync(() -> {
do {
try {
Thread.sleep(1000);
Thread.sleep(100);
Comment thread
ahus1 marked this conversation as resolved.
Resolved
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Expand Down
< 8000 /div>
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,6 @@ public void testReadOnlyUserGetsPermanentlyLocked(){
loginInvalidPassword("johnkeycloak");
assertUserNumberOfFailures(user.getId(), failureFactor);

WaitUtils.waitForBruteForceExecutors(testingClient);

// Make sure user is now disabled
bruteForceStatus = managedRealm.admin().attackDetection().bruteForceUserStatus(user.getId());
assertTrue((boolean) bruteForceStatus.get("disabled"), "User should be disabled by brute force.");
Expand All @@ -247,6 +245,8 @@ public void loginInvalidPassword(String username) {

Assertions.assertEquals("Invalid username or password.", loginPage.getInputError());

WaitUtils.waitForBruteForceExecutors(testingClient);

events.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ public void testGrantInvalidPassword() {
AccessTokenResponse response = getTestToken(getPassword("test-user@localhost"), totpSecret);
Assertions.assertNotNull(response.getAccessToken());
Assertions.assertNull(response.getError());
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}
{
Expand All @@ -270,6 +271,7 @@ public void testGrantInvalidPassword() {
Assertions.assertNull(response.getAccessToken());
Assertions.assertEquals(response.getError(), "invalid_grant");
Assertions.assertEquals(response.getErrorDescription(), "Invalid user credentials");
WaitUtils.waitForBruteForceExecutors(testingClient);
Comment thread
ahus1 marked this conversation as resolved.
Resolved
events.clear();
}
{
Expand All @@ -278,6 +280,7 @@ public void testGrantInvalidPassword() {
Assertions.assertNull(response.getAccessToken());
Assertions.assertEquals(response.getError(), "invalid_grant");
Assertions.assertEquals(response.getErrorDescription(), "Invalid user credentials");
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}
{
Expand Down Expand Up @@ -308,20 +311,23 @@ public void testGrantInvalidOtp() {
AccessTokenResponse response = getTestToken(getPassword("test-user@localhost"), totpSecret);
Assertions.assertNotNull(response.getAccessToken());
Assertions.assertNull(response.getError());
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}
{
AccessTokenResponse response = getTestToken(getPassword("test-user@localhost"), "shite");
Assertions.assertNull(response.getAccessToken());
Assertions.assertEquals(response.getError(), "invalid_grant");
Assertions.assertEquals(response.getErrorDescription(), "Invalid user credentials");
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}
{
AccessTokenResponse response = getTestToken(getPassword("test-user@localhost"), "shite");
Assertions.assertNull(response.getAccessToken());
Assertions.assertEquals(response.getError(), "invalid_grant");
Assertions.assertEquals(response.getErrorDescription(), "Invalid user credentials");
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}
{
Expand Down Expand Up @@ -356,20 +362,23 @@ public void testGrantMissingOtp() {
AccessTokenResponse response = getTestToken(getPassword("test-user@localhost"), totpSecret);
Assertions.assertNotNull(response.getAccessToken());
Assertions.assertNull(response.getError());
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}
{
AccessTokenResponse response = getTestToken(getPassword("test-user@localhost"), null);
Assertions.assertNull(response.getAccessToken());
Assertions.assertEquals(response.getError(), "invalid_grant");
Assertions.assertEquals(response.getErrorDescription(), "Invalid user credentials");
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}
{
AccessTokenResponse response = getTestToken(getPassword("test-user@localhost"), null);
Assertions.assertNull(response.getAccessToken());
Assertions.assertEquals(response.getError(), "invalid_grant");
Assertions.assertEquals(response.getErrorDescription(), "Invalid user credentials");
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}
{
Expand Down Expand Up @@ -406,13 +415,15 @@ public void testGrantPermamentOtpAbsolut() throws Exception {
AccessTokenResponse response = getTestToken(getPassword("test-user@localhost"), totpSecret);
Assertions.assertNotNull(response.getAccessToken());
Assertions.assertNull(response.getError());
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}
for (int i = 0; i <= managedRealm.admin().toRepresentation().getMaxSecondaryAuthFailures(); i++) {
AccessTokenResponse response = getTestToken(getPassword("test-user@localhost"), null);
Assertions.assertNull(response.getAccessToken());
Assertions.assertEquals("invalid_grant", response.getError());
Assertions.assertEquals("Invalid user credentials", response.getErrorDescription());
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}
{
Expand Down Expand Up @@ -536,8 +547,6 @@ public void testFailureResetForTemporaryLockout() {
managedRealm.admin().update(realm);
loginInvalidPassword();

//Wait for brute force executor to process the login and then wait for delta time
WaitUtils.waitForBruteForceExecutors(testingClient);
timeOffSet.set(5);

loginInvalidPassword();
Expand Down Expand Up @@ -604,8 +613,6 @@ public void testCacheExpiryForTemporaryLockout() {
RealmRepresentation realm = managedRealm.admin().toRepresentation();
loginInvalidPassword();

//Wait for brute force executor to process the login and then wait for delta time
WaitUtils.waitForBruteForceExecutors(testingClient);
timeOffSet.set(realm.getMaxDeltaTimeSeconds());

String realmId = realm.getId();
Expand Down Expand Up @@ -1042,6 +1049,7 @@ public void testFailureCountResetWithPasswordGrantType() {
Assertions.assertNull(response.getAccessToken());
Assertions.assertEquals(response.getError(), "invalid_grant");
Assertions.assertEquals(response.getErrorDescription(), "Invalid user credentials");
WaitUtils.waitForBruteForceExecutors(testingClient);

UserRepresentation user = adminClient.realm("test").users().search("test-user@localhost", 0, 1).get(0);
Map<String, Object> userAttackInfo = adminClient.realm("test").attackDetection().bruteForceUserStatus(user.getId());
Expand All @@ -1050,6 +1058,7 @@ public void testFailureCountResetWithPasswordGrantType() {
response = getTestToken(getPassword("test-user@localhost"), totpSecret);
Assertions.assertNotNull(response.getAccessToken());
Assertions.assertNull(response.getError());
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();

userAttackInfo = adminClient.realm("test").attackDetection().bruteForceUserStatus(user.getId());
Expand Down Expand Up @@ -1154,9 +1163,12 @@ public void testCookieSsoReauthenticationDoesNotResetLockout() {
loginTotpPage.login(totpSecret);
Assertions.assertTrue(oauth.parseLoginResponse().isSuccess());
String sessionId = EventAssertion.expectLoginSuccess(events.poll()).getEvent().getSessionId();
WaitUtils.waitForBruteForceExecutors(testingClient);

getTestToken("wrongpass", totpSecret);
WaitUtils.waitForBruteForceExecutors(testingClient);
getTestToken("wrongpass", totpSecret);
WaitUtils.waitForBruteForceExecutors(testingClient);

AccessTokenResponse shouldBeLocked = getTestToken(getPassword("test-user@localhost"), totpSecret);
Assertions.assertNull(shouldBeLocked.getAccessToken());
Expand Down Expand Up @@ -1297,6 +1309,7 @@ public void loginSuccess(String username) {
String code = oauth.parseLoginResponse().getCode();
String idTokenHint = oauth.doAccessTokenRequest(code ).getIdToken();
oauth.logoutForm().idTokenHint(idTokenHint).withRedirect().open();
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}

Expand All @@ -1309,6 +1322,7 @@ public void loginWithTotpFailure() {
loginTotpPage.login("123456");
loginTotpPage.assertCurrent();
Assertions.assertEquals("Invalid authenticator code.", loginTotpPage.getInputError());
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}

Expand Down Expand Up @@ -1346,6 +1360,7 @@ public void continueLoginWithInvalidTotp() {

loginTotpPage.assertCurrent();
Assertions.assertEquals("Invalid authenticator code.", loginTotpPage.getInputError());
WaitUtils.waitForBruteForceExecutors(testingClient);
}

public void continueLoginWithMissingTotp() {
Expand All @@ -1355,6 +1370,7 @@ public void continueLoginWithMissingTotp() {

loginTotpPage.assertCurrent();
Assertions.assertEquals("Invalid authenticator code.", loginTotpPage.getInputError());
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}

Expand All @@ -1368,6 +1384,7 @@ public void loginWithMissingTotp() {
loginTotpPage.assertCurrent();
Assertions.assertEquals("Invalid authenticator code.", loginTotpPage.getInputError());

WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}

Expand All @@ -1387,6 +1404,8 @@ public void loginInvalidPassword(String username, boolean clearEventsQueue) {

Assertions.assertEquals("Invalid username or password.", loginPage.getInputError());

WaitUtils.waitForBruteForceExecutors(testingClient);
Comment thread
ahus1 marked this conversation as resolved.
Resolved

if (clearEventsQueue) {
events.clear();
}
Expand Down Expand Up @@ -1442,6 +1461,7 @@ private void sendInvalidPasswordPasswordGrant() {
Assertions.assertNull(response.getAccessToken());
Assertions.assertEquals(response.getError(), "invalid_grant");
Assertions.assertEquals(response.getErrorDescription(), "Invalid user credentials");
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();
}

Expand All @@ -1450,6 +1470,7 @@ private void lockUserWithPasswordGrant() {
AccessTokenResponse response = getTestToken(getPassword("test-user@localhost"), totpSecret);
Assertions.assertNotNull(response.getAccessToken());
Assertions.assertNull(response.getError());
WaitUtils.waitForBruteForceExecutors(testingClient);
events.clear();

for (int i = 0; i < failureFactor; ++i) {
Expand Down
Loading
0