Sunday, December 27. 2020
Testing bootable JAR in wildfly
A few weeks ago wildfly added the bootable jar feature in order to offer a fat JAR packaging distribution. A big JAR file can now be generated which is directly the executable server with the application deployed inside it. The resulting file contains the jboss-modules to startup the server and all the needed layers that the specific app requires. More or less this feature pretends to achieve something similar to what I showed in the old Spring Boot series. For today's post I decided to accommodate the same application used in those entries but using the wildfly bootable JAR instead of Spring Boot.
The application is just a jaxrs hello world. It incorporates swagger to annotate the endpoint and keycloak to protect its access. Although using the bootable JAR feature, a common JavaEE server will be booted up, so the application is just the same WAR file that would be used in a standalone wildfly. It contains a restful application class and the endpoint, it is annotated with swagger, and, this time, keycloak will be added normally (just the OIDC adapter installed with a layer). The first thing in order to use the bootable JAR is adding its maven plugin, following the guide the server will be slimmed to the minimum layers: base-server, logging, jaxrs and keycloak-client-oidc.
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-jar-maven-plugin</artifactId>
<version>2.0.2.Final</version>
<configuration>
<feature-packs>
<feature-pack>
<location>wildfly@maven(org.jboss.universe:community-universe)#21.0.2.Final</location>
</feature-pack>
<feature-pack>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-adapter-galleon-pack</artifactId>
<version>12.0.1</version>
</feature-pack>
</feature-packs>
<layers>
<layer>base-server</layer>
<layer>logging</layer>
<layer>jaxrs</layer>
<layer>keycloak-client-oidc</layer>
</layers>
</configuration>
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
The feature packs are the normal community-universe wildfy repository and the keycloak galleon pack, which is added as an extra. The swagger part is included inside the WAR application. So, this time, keycloak will added using a layer but swagger libraries are in the WEB-INF/lib folder. For the SSO part a public client should be defined in the usual way at server level. This entry will not cover the keycloak server configuration (it is very simple anyway). Inside the application the client is used twice, javascript is used to perform the login (public app) and the jaxrs endpoint is configured as a bearer only app. So the sample application contains code in the index.html page to perform the login which uses a keycloak.json configuration located in root folder, and the bearer java part is configured inside the web.xml file using another keycloak.json file in the WEB-INF directory. The same trick was used in the Spring Boot post.
If you remember in the old entry a jaxrs filter was developed to control the access to the endpoint via RBAC (Role-Based Access Control) . I commented that an EJB can be used instead for it if using a normal wildfly server. Using the bootable JAR now we can add EJB to the application. Besides it is a good way of complicating and testing this new feature. In order to transform the endpoint in a Stateless EJB two more layers are needed: ejb-lite and bean-validation. But the final JAR did not start. The problem is that the security domain used in the web part (jaxrs) is different to the one used in the EJB. In general each layer does not only include the needed modules and libraries to the final file, it also adds some configuration to the server. In the case of keycloak it creates all the security stuff needed for the web part and assigns it to the default other security domain. But nothing does the same for the EJB part. Hopefully the bootable JAR also lets you configure the server with CLI commands during the packaging, so we can change the default security domain for the EJB layer just using the following CLI line.
/subsystem=ejb3/application-security-domain=other:write-attribute(name=security-domain, value=KeycloakDomain)
A file with the line can also be included in the maven wildfly plugin configuration, and executing the CLI, the security domain for the EJB system will be the same one used in the web/undertow subsystem (I needed to check the files inside that keycloak layer distribution to get the correct name for the domain).
<cli-sessions>
<cli-session>
<script-files>
<script>src/scripts/ejb3.cli</script>
</script-files>
<resolve-expressions>false</resolve-expressions>
</cli-session>
</cli-sessions>
And this way it finally worked. But adding more layers means more modules, and, therefore, more size for the resulting JAR file. In this case it went from 62 to 95MB when adding the EJB stuff. It was a good exercise to play with the new wildfly plugin but I prefer to maintain the filter and save those 30GB. So I commented out the EJB part and went back to the original filter solution. This is the video that shows exactly the same steps that were presented in the Spring entry. First the keycloak server is started. Then the project is packaged (at this point the server is bundled in a JAR file) and started. Using the browser the swagger info is first requested, the hello endpoint is correctly protected and, after login into the keycloak server, the endpoint works as expected.
Today's entry is a little summary of the new bootable JAR. The same application that was previously used with Spring Boot was transformed to use this new wildfly feature. For one side there is the benefit (at least for me) of using a plain JavaEE application (no Spring at all) but the resulting JAR is not as slim as before. The first entry of the previous Spring Boot series generates a file of 31GB while the bootable JAR size is 62GB, but, considering that the full wildlfy distribution is around 200GB in size, it is not a small step. The project for the sample application can be download from here. During the entry different packs were used (wildfly packs and the keycloak one) and even the CLI integration was tested to configure keycloak and EJB at the same time (although I discarded it in the end, in the project this part is just commented out). I am quite happy with this new feature. I think that it is something useful to expand the wildfly adoption with containers.
Best regards!
Comments