adutra commented on code in PR #5107: URL: https://github.com/apache/polaris/pull/5107#discussion_r3614631288
########## tools/testcontainers/keycloak/src/main/java/org/apache/polaris/test/keycloak/KeycloakContainer.java: ########## @@ -0,0 +1,308 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.polaris.test.keycloak; + +import com.google.common.base.Preconditions; +import java.io.IOException; +import java.io.InputStream; +import java.io.UncheckedIOException; +import java.net.URI; +import java.net.URLEncoder; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import org.apache.polaris.containerspec.ContainerSpecHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.containers.wait.strategy.Wait; +import tools.jackson.databind.JsonNode; +import tools.jackson.databind.json.JsonMapper; + +public class KeycloakContainer extends GenericContainer<KeycloakContainer> + implements KeycloakAccess { + + private static final Logger LOGGER = LoggerFactory.getLogger(KeycloakContainer.class); + + private static final int KEYCLOAK_PORT = 8080; + private static final String REALM = "master"; + private static final String ADMIN_USERNAME = "admin"; + private static final String ADMIN_PASSWORD = "admin"; + + private URI issuerUrl; + private URI tokenEndpoint; + private HttpClient httpClient; + + @SuppressWarnings("resource") + public KeycloakContainer() { + super( + ContainerSpecHelper.containerSpecHelper("keycloak", KeycloakContainer.class) + .dockerImageName(null) + .asCanonicalNameString()); + withExposedPorts(KEYCLOAK_PORT); + withEnv("KEYCLOAK_ADMIN", ADMIN_USERNAME); + withEnv("KEYCLOAK_ADMIN_PASSWORD", ADMIN_PASSWORD); + withEnv("KC_LOG_LEVEL", getRootLoggerLevel() + ",org.keycloak:" + getKeycloakLoggerLevel()); + withCommand("start-dev"); + waitingFor( + Wait.forHttp("/realms/" + REALM) + .forStatusCode(200) + .withStartupTimeout(Duration.ofMinutes(2))); + withLogConsumer(new Slf4jLogConsumer(LOGGER)); + } + + @Override + public void start() { + super.start(); + httpClient = HttpClient.newHttpClient(); + String baseUrl = "http://" + getHost() + ":" + getMappedPort(KEYCLOAK_PORT); + issuerUrl = URI.create(baseUrl + "/realms/" + REALM + "/"); + tokenEndpoint = issuerUrl.resolve("protocol/openid-connect/token"); + } + + @Override + public void stop() { + super.stop(); + httpClient = null; + } + + @Override + public URI getIssuerUrl() { + return issuerUrl; + } + + @Override + public URI getTokenEndpoint() { + return tokenEndpoint; + } + + /* + * The methods below were taken from org.keycloak:keycloak-admin-client. We don't depend on that + * artifact directly to avoid bringing in RESTEasy Classic transitively. + */ + + @Override + public void createRole(String roleName) { + String token = getAdminToken(); + int status = adminPost(realmAdminUrl("/roles"), "{\"name\":\"" + roleName + "\"}", token); + Preconditions.checkState( + status == 201, "Failed to create role '%s', status: %s", roleName, status); + } Review Comment: Fixed the javadocs. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
