Skip to content

Commit

Permalink
Make roles unique when creating users in Keycloak dev services
Browse files Browse the repository at this point in the history
Signed-off-by:Phillip Kruger <[email protected]>

(cherry picked from commit 9674490)
  • Loading branch information
phillip-kruger authored and gsmet committed Aug 18, 2021
1 parent 90a64f5 commit ecfd91e
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.stream.Collectors;

import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.logging.Logger;
Expand Down Expand Up @@ -44,6 +45,7 @@
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpHeaders;
import io.vertx.mutiny.core.buffer.Buffer;
import io.vertx.mutiny.ext.web.client.HttpResponse;
import io.vertx.mutiny.ext.web.client.WebClient;

public class KeycloakDevServicesProcessor {
Expand Down Expand Up @@ -342,11 +344,16 @@ private void createRealm(String keycloakUrl, Map<String, String> users, String o
keycloakUrl + "/realms/master/protocol/openid-connect/token",
"admin-cli", null, "admin", "admin", capturedDevServicesConfiguration.webClienTimeout);

client.postAbs(keycloakUrl + "/admin/realms")
HttpResponse<Buffer> response = client.postAbs(keycloakUrl + "/admin/realms")
.putHeader(HttpHeaders.CONTENT_TYPE.toString(), "application/json")
.putHeader(HttpHeaders.AUTHORIZATION.toString(), "Bearer " + token)
.sendBuffer(Buffer.buffer().appendString(JsonSerialization.writeValueAsString(realm)))
.await().atMost(capturedDevServicesConfiguration.webClienTimeout);

if (response.statusCode() > 299) {
LOG.errorf("Realm %s can not be created %d - %s ", realm.getRealm(), response.statusCode(),
response.statusMessage());
}
} catch (Throwable t) {
LOG.errorf("Realm %s can not be created: %s", realm.getRealm(), t.getMessage());
} finally {
Expand Down Expand Up @@ -389,7 +396,9 @@ private RealmRepresentation createRealmRep() {
realm.getRoles().getRealm().add(new RoleRepresentation("user", null, false));
realm.getRoles().getRealm().add(new RoleRepresentation("admin", null, false));
} else {
for (String role : capturedDevServicesConfiguration.roles.values()) {
List<String> distinctRoles = capturedDevServicesConfiguration.roles.values().stream().distinct()
.collect(Collectors.toList());
for (String role : distinctRoles) {
realm.getRoles().getRealm().add(new RoleRepresentation(role, null, false));
}
}
Expand Down

0 comments on commit ecfd91e

Please sign in to comment.