Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't start Keycloak Dev Service for OIDC client and other extensions when OIDC extension is configured with a known social provider #44980

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
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jboss.logging.Logger;
import org.keycloak.representations.idm.RealmRepresentation;
Expand All @@ -24,6 +25,7 @@ public final class KeycloakDevServicesRequiredBuildItem extends MultiBuildItem {

private static final Logger LOG = Logger.getLogger(KeycloakDevServicesProcessor.class);
public static final String OIDC_AUTH_SERVER_URL_CONFIG_KEY = "quarkus.oidc.auth-server-url";
private static final String OIDC_PROVIDER_CONFIG_KEY = "quarkus.oidc.provider";

private final KeycloakDevServicesConfigurator devServicesConfigurator;
private final String authServerUrl;
Expand All @@ -39,8 +41,17 @@ String getAuthServerUrl() {
}

public static KeycloakDevServicesRequiredBuildItem of(KeycloakDevServicesConfigurator devServicesConfigurator,
String authServerUrl, String... dontStartConfigProperties) {
if (shouldStartDevService(dontStartConfigProperties, authServerUrl)) {
String authServerUrl, String... additionalDontStartConfigProperties) {
final Set<String> dontStartConfigProperties = new HashSet<>(Arrays.asList(additionalDontStartConfigProperties));
dontStartConfigProperties.add(authServerUrl);
dontStartConfigProperties.add(OIDC_AUTH_SERVER_URL_CONFIG_KEY);
dontStartConfigProperties.add(OIDC_PROVIDER_CONFIG_KEY);
return of(devServicesConfigurator, authServerUrl, dontStartConfigProperties);
}

private static KeycloakDevServicesRequiredBuildItem of(KeycloakDevServicesConfigurator devServicesConfigurator,
String authServerUrl, Set<String> dontStartConfigProperties) {
if (shouldStartDevService(dontStartConfigProperties)) {
return new KeycloakDevServicesRequiredBuildItem(devServicesConfigurator, authServerUrl);
}
return null;
Expand Down Expand Up @@ -69,10 +80,8 @@ public void customizeDefaultRealm(RealmRepresentation realmRepresentation) {
};
}

private static boolean shouldStartDevService(String[] dontStartConfigProperties, String authServerUrl) {
return Stream
.concat(Stream.of(authServerUrl), Arrays.stream(dontStartConfigProperties))
.allMatch(KeycloakDevServicesRequiredBuildItem::shouldStartDevService);
private static boolean shouldStartDevService(Set<String> dontStartConfigProperties) {
return dontStartConfigProperties.stream().allMatch(KeycloakDevServicesRequiredBuildItem::shouldStartDevService);
}

private static boolean shouldStartDevService(String dontStartConfigProperty) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.quarkus.oidc.client.registration.deployment.devservices.keycloak;

import static io.quarkus.devservices.keycloak.KeycloakDevServicesRequiredBuildItem.OIDC_AUTH_SERVER_URL_CONFIG_KEY;

import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -55,8 +53,7 @@ public void customizeDefaultRealm(RealmRepresentation realmRepresentation) {
}
};

return KeycloakDevServicesRequiredBuildItem.of(devServicesConfigurator,
OIDC_CLIENT_REG_AUTH_SERVER_URL_CONFIG_KEY, OIDC_AUTH_SERVER_URL_CONFIG_KEY);
return KeycloakDevServicesRequiredBuildItem.of(devServicesConfigurator, OIDC_CLIENT_REG_AUTH_SERVER_URL_CONFIG_KEY);
}

@BuildStep(onlyIf = IsDevelopment.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.quarkus.oidc.client.deployment.devservices.keycloak;

import static io.quarkus.devservices.keycloak.KeycloakDevServicesRequiredBuildItem.OIDC_AUTH_SERVER_URL_CONFIG_KEY;

import java.util.HashMap;

import io.quarkus.deployment.IsDevelopment;
Expand Down Expand Up @@ -35,7 +33,7 @@ KeycloakDevServicesRequiredBuildItem requireKeycloakDevService(KeycloakDevServic
configProperties.put(OIDC_CLIENT_SECRET_CONFIG_KEY, ctx.oidcClientSecret());
}
return configProperties;
}, OIDC_CLIENT_AUTH_SERVER_URL_CONFIG_KEY, OIDC_CLIENT_TOKEN_PATH_CONFIG_KEY, OIDC_AUTH_SERVER_URL_CONFIG_KEY);
}, OIDC_CLIENT_AUTH_SERVER_URL_CONFIG_KEY, OIDC_CLIENT_TOKEN_PATH_CONFIG_KEY);
}

@BuildStep(onlyIf = IsDevelopment.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.oidc.client;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

/**
* Test Keycloak Dev Service is not started when known social provider is configured
* in Quarkus OIDC extension.
*/
public class OidcClientKeycloakDevServiceStartupTest {

@RegisterExtension
static final QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot(jar -> jar
.addAsResource(new StringAsset("""
quarkus.oidc.provider=slack
quarkus.oidc.client-id=irrelevant-client-id
"""), "application.properties"))
.setLogRecordPredicate(logRecord -> logRecord != null && logRecord.getMessage() != null
&& logRecord.getMessage().contains("Dev Services for Keycloak started"))
.assertLogRecords(logRecords -> assertTrue(logRecords.isEmpty()));

@Test
public void testDevServiceNotStarted() {
// needs to be here so that log asserter runs after all tests
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class KeycloakDevServiceRequiredBuildStep {
private static final Logger LOG = Logger.getLogger(KeycloakDevServiceRequiredBuildStep.class);
private static final String CONFIG_PREFIX = "quarkus.oidc.";
private static final String TENANT_ENABLED_CONFIG_KEY = CONFIG_PREFIX + "tenant-enabled";
private static final String PROVIDER_CONFIG_KEY = CONFIG_PREFIX + "provider";
private static final String APPLICATION_TYPE_CONFIG_KEY = CONFIG_PREFIX + "application-type";
private static final String CLIENT_ID_CONFIG_KEY = CONFIG_PREFIX + "client-id";
private static final String CLIENT_SECRET_CONFIG_KEY = CONFIG_PREFIX + "credentials.secret";
Expand All @@ -42,7 +41,7 @@ KeycloakDevServicesRequiredBuildItem requireKeycloakDevService(KeycloakDevServic
configProperties.put(CLIENT_SECRET_CONFIG_KEY, ctx.oidcClientSecret());
}
return configProperties;
}, OIDC_AUTH_SERVER_URL_CONFIG_KEY, PROVIDER_CONFIG_KEY);
}, OIDC_AUTH_SERVER_URL_CONFIG_KEY);
}

private static boolean isOidcTenantEnabled() {
Expand Down
Loading