Skip to content

Commit

Permalink
Trying to make sure there is no active tasks and introduce a timeout
Browse files Browse the repository at this point in the history
Closes keycloak#34432

Signed-off-by: Pedro Igor <[email protected]>
  • Loading branch information
pedroigor committed Oct 30, 2024
1 parent abb7c41 commit 12a08ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import static org.jboss.arquillian.graphene.Graphene.waitGui;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.keycloak.testsuite.util.DroneUtils.getCurrentDriver;
import static org.openqa.selenium.support.ui.ExpectedConditions.javaScriptThrowsNoExceptions;
import static org.openqa.selenium.support.ui.ExpectedConditions.not;
Expand Down Expand Up @@ -162,28 +165,28 @@ public static void waitForModalFadeOut() {
waitUntilElementIsNotPresent(By.className("modal-backdrop"));
}

public static long getNumExecutors(KeycloakTestingClient testingClient) {
String numExecutors = testingClient.server().fetchString(session -> {
ExecutorsProvider provider = session.getProvider(ExecutorsProvider.class);
ExecutorService executor = provider.getExecutor("bruteforce");
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
return threadPoolExecutor.getCompletedTaskCount();
});
return Long.valueOf(numExecutors);
}

public static void waitForExecutors(KeycloakTestingClient testingClient, long numExecutors) {
public static void waitForBruteForceExecutors(KeycloakTestingClient testingClient) {
testingClient.server().run(session -> {
ExecutorsProvider provider = session.getProvider(ExecutorsProvider.class);
ExecutorService executor = provider.getExecutor("bruteforce");
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
do {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
} while (!threadPoolExecutor.getQueue().isEmpty());
assertEquals(numExecutors, threadPoolExecutor.getCompletedTaskCount());
try {
CompletableFuture.runAsync(() -> {
do {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} while (!threadPoolExecutor.getQueue().isEmpty() || threadPoolExecutor.getActiveCount() > 0);
}).get(30, TimeUnit.SECONDS);
} catch (java.util.concurrent.TimeoutException te) {
fail("Timeout while waiting for brute force executors!");
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected error while waiting for brute force executors!");
}
assertEquals(0, threadPoolExecutor.getActiveCount());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -567,14 +567,13 @@ public void testPostBrokerLoginFlowWithOTP_bruteForceEnabled() {

Map<String, Object> bruteForceStatus = realm.attackDetection().bruteForceUserStatus(user.getId());
assertFalse("User should not be disabled by brute force.", (boolean) bruteForceStatus.get("disabled"));
long numExecutors = WaitUtils.getNumExecutors(testingClient);

// Login for 2 times with incorrect TOTP. This should temporarily disable the user
loginTotpPage.login("bad-totp");
Assert.assertEquals("Invalid authenticator code.", loginTotpPage.getInputError());
loginTotpPage.login("bad-totp");
Assert.assertEquals("Invalid authenticator code.", loginTotpPage.getInputError());
WaitUtils.waitForExecutors(testingClient, numExecutors+2);
WaitUtils.waitForBruteForceExecutors(testingClient);

bruteForceStatus = realm.attackDetection().bruteForceUserStatus(user.getId());
assertTrue("User should be disabled by brute force.", (boolean) bruteForceStatus.get("disabled"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,10 @@ public void testFailureResetForTemporaryLockout() throws Exception {
try {
realm.setMaxDeltaTimeSeconds(5);
testRealm().update(realm);
long numExecutors = WaitUtils.getNumExecutors(testingClient);
loginInvalidPassword();

//Wait for brute force executor to process the login and then wait for delta time
WaitUtils.waitForExecutors(testingClient, numExecutors + 1);
WaitUtils.waitForBruteForceExecutors(testingClient);
testingClient.testing().setTimeOffset(Collections.singletonMap("offset", String.valueOf(5)));

loginInvalidPassword();
Expand All @@ -438,11 +437,10 @@ public void testNoFailureResetForPermanentLockout() throws Exception {
realm.setMaxDeltaTimeSeconds(5);
realm.setPermanentLockout(true);
testRealm().update(realm);
long numExecutors = WaitUtils.getNumExecutors(testingClient);
loginInvalidPassword();

//Wait for brute force executor to process the login and then wait for delta time
WaitUtils.waitForExecutors(testingClient, numExecutors + 1);
WaitUtils.waitForBruteForceExecutors(testingClient);
testingClient.testing().setTimeOffset(Collections.singletonMap("offset", String.valueOf(5)));

loginInvalidPassword();
Expand Down

0 comments on commit 12a08ff

Please sign in to comment.