forked from thorntail/thorntail
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SWARM-123 - Provide extra configuration for Jolokia (thorntail#134)
Motivation ---------- Particularly when running Jolokia within the context of a Keycloak-enabled application, it would be fantastic to be able to also secure the Jolokia endpoints with Keycloak. Modifications ------------- If org.wildfly.swarm:keycloak is present in a -swarm.jar, AND a property named swarm.jolokia.keycloak.role is set to non-null, then the jolokia.war is manipulated akin to: jolokiaWar.as(Secured.class).protect().withRole( THE_ROLE ); Result ------ In the event Keycloak is available, and a property/config-value is set, then Jolokia endpoints can be easily secured using the underlying Keycloak infrastructure.
- Loading branch information
1 parent
cf3e7e3
commit ac18d97
Showing
10 changed files
with
251 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.wildfly.swarm; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.wildfly.swarm.spi.api.JARArchive; | ||
import org.wildfly.swarm.spi.api.JBossDeploymentStructureAsset; | ||
import org.wildfly.swarm.spi.api.JBossDeploymentStructureContainer; | ||
|
||
/** | ||
* @author Bob McWhirter | ||
*/ | ||
public class DebugUtils { | ||
|
||
public static void dumpJBossDeploymentStructure(Archive archive) { | ||
System.err.println( "--- start jboss-deployment-structure.xml" ); | ||
JBossDeploymentStructureAsset asset = archive.as(JARArchive.class).getDescriptorAsset(); | ||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(asset.openStream()))) { | ||
reader.lines().forEach(line -> System.err.println(line)); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
System.err.println( "--- end jboss-deployment-structure.xml" ); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
org.wildfly.swarm.undertow | ||
org.jboss.as.logging | ||
|
||
*org.wildfly.swarm.keycloak |
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
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
56 changes: 56 additions & 0 deletions
56
jolokia/src/main/java/org/wildfly/swarm/jolokia/runtime/JolokiaKeycloakCustomizer.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,56 @@ | ||
package org.wildfly.swarm.jolokia.runtime; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.wildfly.swarm.jolokia.JolokiaFraction; | ||
import org.wildfly.swarm.jolokia.JolokiaProperties; | ||
import org.wildfly.swarm.keycloak.KeycloakFraction; | ||
import org.wildfly.swarm.keycloak.Secured; | ||
import org.wildfly.swarm.spi.api.Customizer; | ||
import org.wildfly.swarm.spi.runtime.annotations.ConfigurationValue; | ||
import org.wildfly.swarm.spi.runtime.annotations.Pre; | ||
|
||
/** | ||
* @author Bob McWhirter | ||
*/ | ||
@Pre | ||
@Singleton | ||
public class JolokiaKeycloakCustomizer implements Customizer { | ||
|
||
@Inject | ||
KeycloakFraction keycloak; | ||
|
||
@Inject | ||
JolokiaFraction jolokia; | ||
|
||
@Inject | ||
@ConfigurationValue(JolokiaProperties.KEYCLOAK_ROLE) | ||
String role; | ||
|
||
@Override | ||
public void customize() { | ||
if ( this.role == null ) { | ||
return; | ||
} | ||
|
||
Consumer<Archive> keycloakPreparer = (archive)->{ | ||
archive.as(Secured.class) | ||
.protect() | ||
.withRole(this.role); | ||
}; | ||
|
||
Consumer<Archive> preparer = this.jolokia.jolokiaWarPreparer(); | ||
|
||
if ( preparer == null ) { | ||
preparer = keycloakPreparer; | ||
} else { | ||
preparer = preparer.andThen( keycloakPreparer ); | ||
} | ||
|
||
this.jolokia.prepareJolokiaWar(preparer); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
jolokia/src/test/java/org/wildfly/swarm/jolokia/runtime/JolokiaKeycloakCustomizerTest.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,39 @@ | ||
package org.wildfly.swarm.jolokia.runtime; | ||
|
||
import org.junit.Test; | ||
import org.wildfly.swarm.jolokia.JolokiaFraction; | ||
|
||
import static org.fest.assertions.Assertions.assertThat; | ||
|
||
/** | ||
* @author Bob McWhirter | ||
*/ | ||
public class JolokiaKeycloakCustomizerTest { | ||
|
||
@Test | ||
public void testWithoutRole() { | ||
JolokiaFraction jolokia = new JolokiaFraction(); | ||
|
||
JolokiaKeycloakCustomizer customizer = new JolokiaKeycloakCustomizer(); | ||
|
||
customizer.jolokia = jolokia; | ||
|
||
customizer.customize(); | ||
|
||
assertThat( jolokia.jolokiaWarPreparer() ).isNull(); | ||
} | ||
|
||
@Test | ||
public void testWithRole() { | ||
JolokiaFraction jolokia = new JolokiaFraction(); | ||
|
||
JolokiaKeycloakCustomizer customizer = new JolokiaKeycloakCustomizer(); | ||
|
||
customizer.jolokia = jolokia; | ||
customizer.role = "admin"; | ||
|
||
customizer.customize(); | ||
|
||
assertThat( jolokia.jolokiaWarPreparer() ).isNotNull(); | ||
} | ||
} |
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
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
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,60 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ Copyright 2015 Red Hat, Inc. and/or its affiliates. | ||
~ | ||
~ Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.wildfly.swarm</groupId> | ||
<artifactId>testsuite</artifactId> | ||
<version>2016.10-SNAPSHOT</version> | ||
<relativePath>../</relativePath> | ||
</parent> | ||
|
||
<groupId>org.wildfly.swarm</groupId> | ||
<artifactId>testsuite-jolokia-keycloak</artifactId> | ||
|
||
<name>Test Suite: Jolokia with Keycloak</name> | ||
<description>Test Suite: Jolokia with Keycloak</description> | ||
|
||
<packaging>jar</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.wildfly.swarm</groupId> | ||
<artifactId>jolokia</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.wildfly.swarm</groupId> | ||
<artifactId>keycloak</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.wildfly.swarm</groupId> | ||
<artifactId>arquillian</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.arquillian.junit</groupId> | ||
<artifactId>arquillian-junit-container</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>4.5.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
<version>1.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
51 changes: 51 additions & 0 deletions
51
...stsuite-jolokia-keycloak/src/test/java/org/wildfly/swarm/jolokia/JolokiaKeycloakTest.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,51 @@ | ||
package org.wildfly.swarm.jolokia; | ||
|
||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpUriRequest; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.wildfly.swarm.Swarm; | ||
import org.wildfly.swarm.arquillian.CreateSwarm; | ||
import org.wildfly.swarm.spi.api.JARArchive; | ||
|
||
import static org.fest.assertions.Assertions.assertThat; | ||
|
||
/** | ||
* @author Bob McWhirter | ||
*/ | ||
@RunWith(Arquillian.class) | ||
public class JolokiaKeycloakTest { | ||
|
||
@Deployment(testable = false) | ||
public static Archive deployment() { | ||
JARArchive deployment = ShrinkWrap.create(JARArchive.class); | ||
deployment.add(EmptyAsset.INSTANCE, "nothing"); | ||
return deployment; | ||
} | ||
|
||
@CreateSwarm | ||
public static Swarm createSwarm() throws Exception { | ||
System.setProperty( JolokiaProperties.KEYCLOAK_ROLE, "admin" ); | ||
return new Swarm(); | ||
} | ||
|
||
@Test | ||
public void testJolokia() throws Exception { | ||
|
||
HttpClientBuilder builder = HttpClientBuilder.create(); | ||
CloseableHttpClient client = builder.build(); | ||
|
||
HttpUriRequest request = new HttpGet("http://localhost:8080/jolokia"); | ||
CloseableHttpResponse response = client.execute(request); | ||
|
||
assertThat( response.getStatusLine().getStatusCode() ).isEqualTo(403); | ||
} | ||
} |