-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* introduce new truststore configuration, also used by mTLS client auth * separate truststore and https-client-auth configuration
- Loading branch information
Showing
3 changed files
with
172 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/test/java/dasniko/testcontainers/keycloak/KeycloakContainerHttpsLegacyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package dasniko.testcontainers.keycloak; | ||
|
||
import io.restassured.RestAssured; | ||
import io.restassured.config.SSLConfig; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.net.ssl.SSLHandshakeException; | ||
import java.time.Duration; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.startsWith; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
|
||
public class KeycloakContainerHttpsLegacyTest { | ||
|
||
@BeforeEach | ||
public void setup() { | ||
RestAssured.reset(); | ||
} | ||
|
||
@Test | ||
public void shouldStartKeycloakWithMutualTlsRequestNoMutualTls() { | ||
try (KeycloakContainer keycloak = new KeycloakContainer() | ||
.useTlsKeystore("keycloak.jks", "keycloak") | ||
.useMutualTls("keycloak.jks", "keycloak", HttpsClientAuth.REQUEST)) { | ||
keycloak.start(); | ||
checkTls(keycloak, "keycloak.jks", "keycloak"); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldStartKeycloakWithMutualTlsRequestWithMutualTls() { | ||
try (KeycloakContainer keycloak = new KeycloakContainer() | ||
.useTlsKeystore("keycloak.jks", "keycloak") | ||
.useMutualTls("keycloak.jks", "keycloak", HttpsClientAuth.REQUEST)) { | ||
keycloak.start(); | ||
checkMutualTls(keycloak, "keycloak.jks", "keycloak", "keycloak.jks", "keycloak"); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldStartKeycloakWithMutualTlsRequiredWithMutualTls() { | ||
try (KeycloakContainer keycloak = new KeycloakContainer() | ||
.useTlsKeystore("keycloak.jks", "keycloak") | ||
.useMutualTls("keycloak.jks", "keycloak", HttpsClientAuth.REQUIRED) | ||
.waitingFor(KeycloakContainer.LOG_WAIT_STRATEGY.withStartupTimeout(Duration.ofMinutes(2))) // this is hopefully only a workaround until mgmt port does not require mutual tls | ||
) { | ||
keycloak.start(); | ||
checkMutualTls(keycloak, "keycloak.jks", "keycloak", "keycloak.jks", "keycloak"); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldStartKeycloakWithMutualTlsRequiredWithoutMutualTls() { | ||
try (KeycloakContainer keycloak = new KeycloakContainer() | ||
.useTlsKeystore("keycloak.jks", "keycloak") | ||
.useMutualTls("keycloak.jks", "keycloak", HttpsClientAuth.REQUIRED) | ||
.waitingFor(KeycloakContainer.LOG_WAIT_STRATEGY.withStartupTimeout(Duration.ofMinutes(2))) // this is hopefully only a workaround until mgmt port does not require mutual tls | ||
) { | ||
keycloak.start(); | ||
assertThrows(SSLHandshakeException.class, () -> checkTls(keycloak, "keycloak.jks", "keycloak")); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldThrowNullPointerExceptionUponNullTlsTruststoreFilename() { | ||
assertThrows(NullPointerException.class, () -> new KeycloakContainer().useMutualTls(null, null, HttpsClientAuth.NONE)); | ||
} | ||
|
||
@Test | ||
public void shouldThrowNullPointerExceptionUponNullHttpsClientAuth() { | ||
assertThrows(NullPointerException.class, () -> new KeycloakContainer().useMutualTls("keycloak.jks", null, null)); | ||
} | ||
|
||
private void checkTls(KeycloakContainer keycloak, String pathToTruststore, String truststorePassword) { | ||
RestAssured.config = RestAssured.config().sslConfig( | ||
SSLConfig.sslConfig().trustStore(pathToTruststore, truststorePassword) | ||
); | ||
|
||
assertThat(keycloak.getAuthServerUrl(), startsWith("https://")); | ||
|
||
given() | ||
.when().get(keycloak.getAuthServerUrl()) | ||
.then().statusCode(200); | ||
} | ||
|
||
private void checkMutualTls(KeycloakContainer keycloak, String pathToTruststore, String truststorePassword, String pathToKeystore, | ||
String keystorePassword) { | ||
RestAssured.config = RestAssured.config().sslConfig( | ||
SSLConfig.sslConfig() | ||
.trustStore(pathToTruststore, truststorePassword) | ||
.keyStore(pathToKeystore, keystorePassword) | ||
); | ||
|
||
assertThat(keycloak.getAuthServerUrl(), startsWith("https://")); | ||
|
||
given() | ||
.when().get(keycloak.getAuthServerUrl()) | ||
.then().statusCode(200); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters