Skip to content

Commit

Permalink
KOGITO-3208: fix keycloak in trusty service and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kostola committed Oct 19, 2020
1 parent 89944dc commit 5631242
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@
<artifactId>awaitility</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-test-utils</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import java.util.function.BiFunction;

import org.junit.jupiter.api.Test;
import org.keycloak.representations.AccessTokenResponse;
import org.kie.kogito.testcontainers.ExplainabilityServiceMessagingContainer;
import org.kie.kogito.testcontainers.InfinispanContainer;
import org.kie.kogito.testcontainers.KogitoKeycloakContainer;
import org.kie.kogito.testcontainers.KogitoServiceContainer;
import org.kie.kogito.testcontainers.TrustyServiceContainer;
import org.kie.kogito.trusty.service.responses.ExecutionsResponse;
Expand Down Expand Up @@ -53,10 +55,36 @@ public abstract class AbstractTrustyExplainabilityEnd2EndIT {
private static final String KAFKA_ALIAS = "kafka";
private static final String KAFKA_BOOTSTRAP_SERVERS = KAFKA_ALIAS + ":9092";

private static final String KEYCLOAK_ALIAS = "keycloak";
private static final String KEYCLOAK_DB_VENDOR_VARIABLE = "DB_VENDOR";
private static final String KEYCLOAK_DB_VENDOR_VALUE = "h2";
private static final String KEYCLOAK_ACCESS_TOKEN_PATH = "/auth/realms/kogito/protocol/openid-connect/token";
private static final String KEYCLOAK_GRANT_TYPE_PARAM_NAME = "grant_type";
private static final String KEYCLOAK_GRANT_TYPE_PARAM_VALUE = "password";
private static final String KEYCLOAK_USERNAME_PARAM_NAME = "username";
private static final String KEYCLOAK_USERNAME_PARAM_VALUE = "jdoe";
private static final String KEYCLOAK_PASSWORD_PARAM_NAME = "password";
private static final String KEYCLOAK_PASSWORD_PARAM_VALUE = "jdoe";
private static final String KEYCLOAK_CLIENT_ID_PARAM_NAME = "client_id";
private static final String KEYCLOAK_CLIENT_ID_PARAM_VALUE = KogitoKeycloakContainer.CLIENT_ID;
private static final String KEYCLOAK_CLIENT_SECRET_PARAM_NAME = "client_secret";
private static final String KEYCLOAK_CLIENT_SECRET_PARAM_VALUE = KogitoKeycloakContainer.CLIENT_SECRET;

private static final String KOGITO_SERVICE_ALIAS = "kogito-service";
private static final String KOGITO_SERVICE_URL = "http://" + KOGITO_SERVICE_ALIAS + ":8080";
private static final List<String> KOGITO_SERVICE_PAYLOADS = List.of(
"{\"Driver\":{\"Age\":25,\"Points\":13},\"Violation\":{\"Type\":\"speed\",\"Actual Speed\":105,\"Speed Limit\":100}}",
"{\"Driver\":{\"Age\":37,\"Points\":20},\"Violation\":{\"Type\":\"speed\",\"Actual Speed\":135,\"Speed Limit\":100}}",
"{\"Driver\":{\"Age\":18,\"Points\": 0},\"Violation\":{\"Type\":\"speed\",\"Actual Speed\": 85,\"Speed Limit\": 70}}",
"{\"Driver\":{\"Age\":56,\"Points\":13},\"Violation\":{\"Type\":\"speed\",\"Actual Speed\": 35,\"Speed Limit\": 25}}",
"{\"Driver\":{\"Age\":40,\"Points\":13},\"Violation\":{\"Type\":\"speed\",\"Actual Speed\":215,\"Speed Limit\":120}}"
);

private static final String TRUSTY_SERVICE_ALIAS = "trusty-service";
private static final String TRUSTY_SERVICE_OIDC_AUTH_SERVER_URL_VARIABLE = "QUARKUS_OIDC_AUTH_SERVER_URL";
private static final String TRUSTY_SERVICE_OIDC_AUTH_SERVER_URL_VALUE = "http://" + KEYCLOAK_ALIAS + ":8080/auth/realms/kogito";
private static final String TRUSTY_SERVICE_OIDC_CLIENT_ID_VARIABLE = "QUARKUS_OIDC_CLIENT_ID";
private static final String TRUSTY_SERVICE_OIDC_CLIENT_ID_VALUE = "kogito-trusty-service";

private final BiFunction<String, String, KogitoServiceContainer> kogitoServiceContainerProducer;

Expand All @@ -65,7 +93,7 @@ protected AbstractTrustyExplainabilityEnd2EndIT(BiFunction<String, String, Kogit
}

@Test
public void doTest() throws Exception {
public void doTest() {
try (
final Network network = Network.newNetwork();

Expand All @@ -77,12 +105,19 @@ public void doTest() throws Exception {
.withNetwork(network)
.withNetworkAliases(KAFKA_ALIAS);

final KogitoKeycloakContainer keycloak = new KogitoKeycloakContainer()
.withEnv(KEYCLOAK_DB_VENDOR_VARIABLE, KEYCLOAK_DB_VENDOR_VALUE)
.withNetwork(network)
.withNetworkAliases(KEYCLOAK_ALIAS);

final ExplainabilityServiceMessagingContainer explService = new ExplainabilityServiceMessagingContainer(KAFKA_BOOTSTRAP_SERVERS, EXPL_SERVICE_SAMPLES)
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
.withNetwork(network)
.withNetworkAliases(EXPL_SERVICE_ALIAS);

final TrustyServiceContainer trustyService = new TrustyServiceContainer(INFINISPAN_SERVER_LIST, KAFKA_BOOTSTRAP_SERVERS, true)
.withEnv(TRUSTY_SERVICE_OIDC_AUTH_SERVER_URL_VARIABLE, TRUSTY_SERVICE_OIDC_AUTH_SERVER_URL_VALUE)
.withEnv(TRUSTY_SERVICE_OIDC_CLIENT_ID_VARIABLE, TRUSTY_SERVICE_OIDC_CLIENT_ID_VALUE)
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
.withNetwork(network)
.withNetworkAliases(TRUSTY_SERVICE_ALIAS);
Expand All @@ -98,6 +133,9 @@ public void doTest() throws Exception {
kafka.start();
assertTrue(kafka.isRunning());

keycloak.start();
assertTrue(keycloak.isRunning());

explService.start();
assertTrue(explService.isRunning());

Expand All @@ -107,16 +145,22 @@ public void doTest() throws Exception {
kogitoService.start();
assertTrue(kogitoService.isRunning());

final List<String> jsonList = List.of(
"{\"Driver\":{\"Age\":25,\"Points\":13},\"Violation\":{\"Type\":\"speed\",\"Actual Speed\":105,\"Speed Limit\":100}}",
"{\"Driver\":{\"Age\":37,\"Points\":20},\"Violation\":{\"Type\":\"speed\",\"Actual Speed\":135,\"Speed Limit\":100}}",
"{\"Driver\":{\"Age\":18,\"Points\": 0},\"Violation\":{\"Type\":\"speed\",\"Actual Speed\": 85,\"Speed Limit\": 70}}",
"{\"Driver\":{\"Age\":56,\"Points\":13},\"Violation\":{\"Type\":\"speed\",\"Actual Speed\": 35,\"Speed Limit\": 25}}",
"{\"Driver\":{\"Age\":40,\"Points\":13},\"Violation\":{\"Type\":\"speed\",\"Actual Speed\":215,\"Speed Limit\":120}}"
);
final int expectedExecutions = jsonList.size();
final String accessToken = given()
.port(keycloak.getFirstMappedPort())
.param(KEYCLOAK_GRANT_TYPE_PARAM_NAME, KEYCLOAK_GRANT_TYPE_PARAM_VALUE)
.param(KEYCLOAK_USERNAME_PARAM_NAME, KEYCLOAK_USERNAME_PARAM_VALUE)
.param(KEYCLOAK_PASSWORD_PARAM_NAME, KEYCLOAK_PASSWORD_PARAM_VALUE)
.param(KEYCLOAK_CLIENT_ID_PARAM_NAME, KEYCLOAK_CLIENT_ID_PARAM_VALUE)
.param(KEYCLOAK_CLIENT_SECRET_PARAM_NAME, KEYCLOAK_CLIENT_SECRET_PARAM_VALUE)
.when()
.post(KEYCLOAK_ACCESS_TOKEN_PATH)
.as(AccessTokenResponse.class).getToken();

assertNotNull(accessToken);

final int expectedExecutions = KOGITO_SERVICE_PAYLOADS.size();

jsonList.forEach(json ->
KOGITO_SERVICE_PAYLOADS.forEach(json ->
given()
.port(kogitoService.getFirstMappedPort())
.contentType("application/json")
Expand All @@ -132,6 +176,7 @@ public void doTest() throws Exception {
.untilAsserted(() -> {
ExecutionsResponse executionsResponse = given()
.port(trustyService.getFirstMappedPort())
.auth().oauth2(accessToken)
.when().get(String.format("/executions?limit=%d", expectedExecutions))
.then().statusCode(200)
.extract().as(ExecutionsResponse.class);
Expand All @@ -145,6 +190,7 @@ public void doTest() throws Exception {

SalienciesResponse salienciesResponse = given()
.port(trustyService.getFirstMappedPort())
.auth().oauth2(accessToken)
.when().get("/executions/decisions/" + executionId + "/explanations/saliencies")
.then().statusCode(200)
.extract().as(SalienciesResponse.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
quarkus.http.cors=true

# Quarkus OIDC
quarkus.oidc.enabled=false
quarkus.oidc.auth-server-url=none

# HTTP Security Configuration
quarkus.http.auth.permission.authenticated.paths=/*
Expand Down

0 comments on commit 5631242

Please sign in to comment.