0

I'm trying to get some user data out of Keycloack using the admin-client SDK. I've built the client like so:

Keycloak kc = KeycloakBuilder.builder() //
            .serverUrl("some_url")
            .realm("some-realm")
            .username("admin") //
            .password("password") //
            .clientId("curl")
            .resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).connectionCheckoutTimeout(10, TimeUnit.SECONDS).build()) //
            .build();

    System.out.println("built");
    UsersResource baz = kc.realm(keycloakConfiguration.getRealm()).users();
    System.out.println(baz.count());

What seems to happen is that my program hangs indefinitely when attempting to fetch baz - my debugger never hits it. I'm not quite sure what's going on - my credentials are correct. What is the correct way to cause the builder to either 1. fail after a certain time period, or 2.verify that my credentials are correct? It's maddeninly frustrating to debug.

1 Answers1

1

You could create a custom method to check if you client is "online". This method could look like:

public boolean isKeycloakClientValid(Keycloak keycloakClient) {
    try {
        tryToPingKeycloak(keycloakClient);
    } catch (Exception e) {
        logger.error("Error while pinging the keycloak server", e);
        return false;
    }
    return true;
}

With the help method:

private void tryToPingKeycloak(KeycloakClient keycloakClient) {
    keycloakClient.serverInfo().getInfo();
}

Now you could check your client before using it:

if (isKeycloakClientValid(kc)) {
    UsersResource baz = kc.realm(keycloakConfiguration.getRealm()).users();
}
David Klassen
  • 296
  • 2
  • 6