Skip to content

Commit

Permalink
fixing creation of brokers
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Igor <[email protected]>
  • Loading branch information
pedroigor committed Jul 1, 2024
1 parent ea2ec4d commit 5103a9d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
Expand Down Expand Up @@ -86,19 +87,10 @@ protected void addIdentityProviders(KeycloakSession session, String orgId, Count
OrganizationModel organization = provider.getById(orgId);
int startIndex = getLastIndex(session, organization.getName());
RealmModel realm = session.getContext().getRealm();
ClientModel client = realm.getClientByClientId("org-broker-client");

if (client == null) {
client = realm.addClient("org-broker-client");
client.setSecret("secret");
client.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
client.setPublicClient(false);
client.addRedirectUri("http://localhost:8180/realms/" + realm.getName() + "/broker/*");
}

long count = latch.getCount();

for (int i = startIndex; i < count; i++) {
for (int i = startIndex; i < startIndex + count; i++) {
String idpAlias = "idp-" + organization.getName() + "-" + i;

IdentityProviderModel identityProvider = realm.getIdentityProviderByAlias(idpAlias);
Expand Down Expand Up @@ -129,6 +121,8 @@ protected void addIdentityProviders(KeycloakSession session, String orgId, Count
idpConfig.put("clientSecret", "secret");
idpConfig.put("clientAuthMethod", "client_secret_post");
realm.addIdentityProvider(identityProvider);
AtomicInteger lastIndex = (AtomicInteger) session.getAttribute("idpLastIndex");
lastIndex.incrementAndGet();
}

if (provider.addIdentityProvider(organization, identityProvider)) {
Expand All @@ -138,7 +132,14 @@ protected void addIdentityProviders(KeycloakSession session, String orgId, Count
}

private int getLastIndex(KeycloakSession session, String orgName) {
RealmModel realm = session.getContext().getRealm();
return ConfigUtil.findFreeEntityIndex(index -> realm.getIdentityProvidersStream().anyMatch(idp -> idp.getAlias().equals("idp-" + orgName + "-" + index)));
AtomicInteger lastIndex = (AtomicInteger) session.getAttribute("idpLastIndex");

if (lastIndex == null) {
RealmModel realm = session.getContext().getRealm();
lastIndex = new AtomicInteger(ConfigUtil.findFreeEntityIndex(index -> realm.getIdentityProvidersStream().anyMatch(idp -> idp.getAlias().equals("idp-" + orgName + "-" + index))));
session.setAttribute("idpLastIndex", lastIndex);
}

return lastIndex.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
import org.keycloak.benchmark.dataset.TaskResponse;
import org.keycloak.benchmark.dataset.config.ConfigUtil;
import org.keycloak.benchmark.dataset.config.DatasetConfig;
import org.keycloak.models.ClientModel;
import org.keycloak.models.IdentityProviderModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.OrganizationDomainModel;
import org.keycloak.models.OrganizationModel;
import org.keycloak.models.RealmModel;
import org.keycloak.organization.OrganizationProvider;
import org.keycloak.protocol.oidc.OIDCLoginProtocol;
import org.keycloak.utils.StringUtil;

public class OrganizationProvisioner extends AbstractOrganizationProvisioner {
Expand Down Expand Up @@ -109,6 +111,17 @@ private Runnable createOrganizations() {
Integer count = config.getCount();

executor.addTask(() -> runJobInTransactionWithTimeout(session -> {
RealmModel realm = getRealm(session);
ClientModel client = realm.getClientByClientId("org-broker-client");

if (client == null) {
client = realm.addClient("org-broker-client");
client.setSecret("secret");
client.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
client.setPublicClient(false);
client.addRedirectUri("http://localhost:8180/realms/" + realm.getName() + "/broker/*");
}

int startIndex = getLastIndex(session);

if (isBlank(config.getName())) {
Expand Down

0 comments on commit 5103a9d

Please sign in to comment.