The release of Red Hat Data Grid 8.1 offers new features for securing applications deployed on Red Hat OpenShift. Naturally, I wanted to check them out for Quarkus. Using the Quarkus Data Grid extension made that easy to do.
Data Grid is an in-memory, distributed, NoSQL datastore solution based on Infinispan. Since it manages your data, Data Grid should be as secure as possible. For this reason, it uses a default property realm that requires HTTPS and automatically enforces user authentication on remote endpoints. As an additional layer of security on OpenShift, Data Grid presents certificates signed by the OpenShift Service Signer. In practice, this means that Data Grid is as secure as possible out of the box, requiring encrypted connections and authentication from the first request. Data Grid generates a default set of credentials (which, of course, you can override), but unauthenticated access is denied.
In this article, I show you how to configure a Quarkus application with Data Grid and deploy it on OpenShift.
Getting started with Data Grid 8.1
OpenShift's Data Grid Operator makes it easy to deploy Data Grid in a variety of configurations. The Operator also manages Data Grid's security, storage, upgrade, and logging capabilities. Once you've deployed the Data Grid Operator from the OpenShift OperatorHub, it exposes custom resources (called Infinispan Cluster and Infinispan Cache), which Data Grid uses to provision the caches on an existing cluster.
In the next sections, we'll deploy a three-node Data Grid cluster, configure a sample Quarkus application to securely connect to Data Grid, then deploy and verify our application on OpenShift.
Deploy a three-node Data Grid cluster
To deploy a three-node Data Grid cluster, just add the following custom resource to any OpenShift project (note that my project name for this example is dgdemo
):
apiVersion: infinispan.org/v1 kind: Infinispan metadata: name: example-infinispan namespace: dgdemo spec: replicas: 3 expose: type: LoadBalancer
After you've added the custom resource, Data Grid deploys the nodes and sets up the networking. The setup includes a Kubernetes service that resolves to the cluster members. The Kubernetes service support HTTPS using the OpenShift Service Signer.
Using signed certificates to connect Data Grid and Quarkus
When your applications access Data Grid, they'll do so through the Kubernetes service, so they will be subject to its certificates. It's possible to use an annotated ConfigMap
to simplify the process of injecting these certificates into a pod. However, because we're using Java, we'll need the certificates in a format that Java applications can consume, so JKS or PKCS12.
We'll use the signed certificates when our Quarkus application initiates a TLS handshake with Data Grid. We need to manually extract and convert them to JKS format before mounting them in our application's running pods.
Extract the certificates
To generate the Java keystore, we first need to extract the certificates from the OpenShift Service Signer's certificate authority. We can then use the JDK's keytool
to generate the keystore file. Using the oc
, openssl
, and keytool
command-line interfaces (CLIs), enter the following:
$ oc get secrets/signing-key -n openshift-service-ca -o template='{{index .data "tls.crt"}}' | openssl base64 -d -A > /tmp/server.crt $ keytool -importcert -keystore /tmp/server.jks -storepass password -file /tmp/server.crt -trustcacerts -noprompt
This series of commands grabs the Base64-encoded certificate content, then uses the keytool
format to a file /tmp/server.jks
with the passphrase: password
.
Create a secret
Next, we need to consume the certificates in our Quarkus application namespace (dgdemo
). We'll create a secret, clientcerts
, whose contents are set to the contents of our certificate file. Here's the command to do it:
$ oc create secret generic clientcerts -n dgdemo --from-file=clientcerts=/tmp/server.jks
Configure the Quarkus application
We've created our secret named clientcerts
and set its value as the content of our server.jks
. Next, we need to configure our Quarkus application to mount the secret at a given mount point, read the contents, and use the certificates whenever it connects to Data Grid.
I created a sample application for this part of the demonstration. Open it, and you'll see that I've already installed both the infinispan-client
and openshift
extensions. This is a simple application that responds to the GET /infinispan
endpoint with the value of the Data Grid entry under the key of hello
. As you might guess, the entry is "Hello World, Infinispan is up!"
You can find the configuration in src/main/resources/application.properties
. I'll also show it here:
# The DNS name and port of the Service fronting Data Grid quarkus.infinispan-client.server-list=example-infinispan:11222 # Auth info, including server name and super secret password for the developer quarkus.infinispan-client.auth-server-name=example-infinispan quarkus.infinispan-client.auth-realm=default # DG user credentials. You'll need to change these quarkus.infinispan-client.auth-username=developer quarkus.infinispan-client.auth-password=I@TIR7awMPvYATGl quarkus.infinispan-client.sasl-mechanism=PLAIN quarkus.infinispan-client.client-intelligence=BASIC # Where the application can read the trust store from when it runs quarkus.infinispan-client.trust-store=/mnt/clientcerts # trust store password quarkus.infinispan-client.trust-store-password=password # trust store type quarkus.infinispan-client.trust-store-type=jks # which secret to mount and where to mount it quarkus.openshift.mounts.my-volume.path=/mnt quarkus.openshift.secret-volumes.my-volume.secret-name=clientcerts # instructs quarkus to build and deploy to kubernetes/openshift, and # create an ingress Route, and to trust the Kubernetes API since we're using self-signed, quarkus.container-image.build=true quarkus.kubernetes.deploy=true quarkus.openshift.expose=true quarkus.kubernetes-client.trust-certs=true quarkus.kubernetes.deployment-target=openshift
Get the secret
Our application will use the values of quarkus.infinispan-client.auth-username
and quarkus.infinispan-client.auth-password
as the credentials to talk to Data Grid. Enter the following command to discover the autogenerated username of developer
and its associated password:
$ oc get secret/example-infinispan-generated-secret -o template='{{index .data "identities.yaml"}}' | openssl base64 -d -A
Take the username and password and insert them into your theapplication.propeties
file.
With all of that in place, we're ready to deploy!
Deploy to OpenShift
All we have to do now is log into OpenShift and switch to our demo project, then build and deploy. In this case, the Quarkus OpenShift plugin does most of the work for us; we just enter:
$ oc project dgdemo $ mvn clean package
It will take a minute or so to build and deploy. After the build is done, the application should deploy and show up in the OpenShift developer console, as shown in Figure 1.
Test the application
Before we test the application, let's take a look at its log file:
$ oc logs -n dgdemo dc/infinispan-client-quickstart __ ____ __ _____ ___ __ ____ ______ --/ __ \/ / / / _ | / _ \/ //_/ / / / __/ -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \ --\___\_\____/_/ |_/_/|_/_/|_|\____/___/ 2020-10-01 22:53:32,096 INFO [org.inf.SECURITY] (HotRod-client-async-pool-1-1) ISPN000947: Using Java SSL Provider 2020-10-01 22:53:37,612 INFO [org.inf.HOTROD] (main) ISPN004021: Infinispan version: Infinispan 'Corona Extra' 11.0.3.Final 2020-10-01 22:53:37,863 INFO [InfinispanClientApp] (main) Create or get cache named mycache with the default configuration 2020-10-01 22:53:37,909 INFO [io.quarkus] (main) infinispan-client-quickstart 1.0-SNAPSHOT on JVM (powered by Quarkus 1.8.0.Final) started in 1.676s. Listening on: http://0.0.0.0:8080 2020-10-01 22:53:37,909 INFO [io.quarkus] (main) Profile prod activated. 2020-10-01 22:53:37,909 INFO [io.quarkus] (main) Installed features: [cdi, infinispan-client, kubernetes, resteasy]
That looks healthy. Now, let's test it with curl
:
$ curl http://$(oc get route -n dgdemo infinispan-client-quickstart -o jsonpath="{.spec.host}")/infinispan Hello World, Infinispan is up!
Yes, we got the expected value showing that the connection was successful.
Verify the value in the Data Grid 8.1 web console
As our final step, let's use the new Data Grid 8.1 web console to verify the value. To get to it, you can discover the URL with:
$ echo http://$(oc get service/example-infinispan-external jsonpath='{.status.loadBalancer.ingress[0].hostname}'):11222
This command outputs the URL, which you can then copy and paste into your browser. You can use the credentials that you specified earlier to log in. Then, find the mycache cache, and search for entries using the key hello, as shown in Figure 2.
Wrap up
Red Hat Data Grid delivers many features for hybrid cloud deployments on OpenShift. In this article, you've seen how to use a default configuration to securely connect your Quarkus applications to Data Grid and deploy them on OpenShift.
While the default configuration is considered to be as secure as possible, there are many other features to explore. Examples include cross-data center asynchronous replication on OpenShift and autoscaling to dynamically expand and contract clusters sizes.
Whether you choose the default configuration or need to explore additional security features, Red Hat Data Grid is a valuable solution for large datasets across large clouds.
Last updated: October 14, 2020