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

Get keycloak working, but test still fails #1053

Draft
wants to merge 2 commits into
base: Issue-1024-1045-Keycloak18
Choose a base branch
from
Draft
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 @@ -55,6 +55,7 @@ class OpenIdAuthorizationCodeSpec extends GebEmbeddedServerSpecification {

@IgnoreIf({ System.getProperty(Keycloak.SYS_TESTCONTAINERS) != null && !Boolean.valueOf(System.getProperty(Keycloak.SYS_TESTCONTAINERS)) })
void "test a full login"() {

when:
browser.go "/oauth/login/keycloak"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
/*
* Copyright 2017-2021 original authors
*
* Licensed 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
*
* https://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 io.micronaut.security.testutils

import dasniko.testcontainers.keycloak.KeycloakContainer
import org.testcontainers.Testcontainers
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy

import java.time.Duration

class Keycloak {
static final String SYS_TESTCONTAINERS = "testcontainers"
Expand Down Expand Up @@ -48,33 +30,57 @@ class Keycloak {
Integer.valueOf(issuer.substring(issuer.indexOf("localhost:") + "localhost:".length(), issuer.indexOf("/realms")))
}

private static exec(String... parts) {
println "Running command: " + parts.join(" ")
def result = keycloak.execInContainer(parts)
println "OUT: $result.stdout"
println "ERR: $result.stderr"
println "EXIT: $result.exitCode"
assert result.exitCode == 0
}

static void init() {
if (keycloak == null) {

keycloak = new KeycloakContainer()
.withAdminUsername("admin")
.withAdminPassword("admin")
.withExposedPorts(8080)
.withEnv(Map.of(
"KEYCLOAK_USER", "user",
"KEYCLOAK_PASSWORD", "password",
"DB_VENDOR", "H2")
)
.waitingFor(new LogMessageWaitStrategy().withRegEx(".*powered by Quarkus.*").withStartupTimeout(Duration.ofMinutes(5)))
.withEnv(DB_VENDOR: "H2")

keycloak.start()
keycloak.execInContainer("/opt/keycloak/bin/kcreg.sh config credentials " +
"--server http://localhost:8080/auth " +
"--realm master --user user --password password"
.split(" "))
keycloak.execInContainer("/opt/keycloak/bin/kcreg.sh " +
"create -s clientId=$CLIENT_ID " +
"-s redirectUris=[\"http://${TestContainersUtils.host}*\"] " +
"-s secret=$clientSecret"
.split(" "))
// Login
exec("/opt/keycloak/bin/kcadm.sh",
"config", "credentials",
"--server", "http://localhost:8080",
"--realm", "master",
"--user", "admin",
"--password", "admin")
// Add user
exec("/opt/keycloak/bin/kcadm.sh",
"create", "users",
"-s", "username=user",
"-s", "enabled=true",
"-o",
"--fields", "id,username")
// Set user password
exec("/opt/keycloak/bin/kcadm.sh",
"set-password",
"--username", "user",
"--new-password", "password")
// Add client
exec("/opt/keycloak/bin/kcreg.sh",
"create",
"--server", "http://localhost:8080",
"--realm", "master",
"--user", "admin",
"--password", "admin",
"-s", "clientId=$CLIENT_ID",
"-s", "redirectUris=[\"http://${redirectUriHost}*\"]",
"-s", "secret=$clientSecret")
int port = keycloak.getMappedPort(8080)
Testcontainers.exposeHostPorts(port)
issuer = "http://" + getHost() + ":" + port + "/realms/master"
issuer = "http://$host:$port/realms/master"
}
}

Expand Down