DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Auto-Scaling a Spring Boot Native App With Nomad
  • Manage Microservices With Docker Compose
  • An Introduction to BentoML: A Unified AI Application Framework
  • 7 Ways of Containerizing Your Node.js Application

Trending

  • Driving DevOps With Smart, Scalable Testing
  • Ensuring Configuration Consistency Across Global Data Centers
  • Navigating the LLM Landscape: A Comparative Analysis of Leading Large Language Models
  • Building AI-Driven Intelligent Applications: A Hands-On Development Guide for Integrating GenAI Into Your Applications
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Build a Quarkus Reactive Application Using Kubernetes Secrets

Build a Quarkus Reactive Application Using Kubernetes Secrets

In this post, learn how to follow security policies while developing applications for the cloud by using Kubernetes Secrets.

By 
Daniel Oh user avatar
Daniel Oh
DZone Core CORE ·
Jun. 06, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
5.3K Views

Join the DZone community and get the full member experience.

Join For Free

Many organizations have security policies in place that dictate how to store sensitive information. When you're developing applications for the cloud, you're probably expected to follow those policies, and to do that, you often have to externalize your data storage. Kubernetes has a built-in system to access external secrets, and learning to use that is key to a safe cloud-native app.

In this article, I'm going to demonstrate how to build a Quarkus reactive application with externalized sensitive information—for instance, a password or token—using Kubernetes Secrets. A secret is a good example of how cloud platforms can secure applications by removing sensitive data from your static code. Note that you can find a solution to this tutorial in this GitHub repository.

1. Scaffold a New Reactive Quarkus Project

Use the Quarkus command-line interface (CLI) to scaffold a new project. If you haven't already installed the Quarkus CLI, follow these instructions according to your operating system.

Run the following Quarkus CLI in your project directory to add kubernetes-config, resteasy-reactive, and openshift extensions:

$ quarkus create app quarkus-secret-example \
-x resteasy-reactive,kubernetes-config,openshift


The output should look like this:

Looking for the newly published extensions in registry.quarkus.io
selected extensions:
- io.quarkus:quarkus-kubernetes-config
- io.quarkus:quarkus-resteasy-reactive
- io.quarkus:quarkus-openshift


applying codestarts...
java
maven
quarkus
config-properties
dockerfiles
maven-wrapper
resteasy-reactive-codestart

-----------

[SUCCESS] ✅  quarkus project has been successfully generated in:
--> /tmp/quarkus-secret-example
-----------
Navigate into this directory and get started: quarkus dev


2. Create a Secret in Kubernetes

To manage the Kubernetes Secrets, you have three options:

  • Using kubectl
  • Using Configuration File
  • Node Kustomize

Use the kubectl command to create a new database credential (a username and password). Run the following command:

$ kubectl create secret generic db-credentials \                                                                      
 --from-literal=username=admin \
 --from-literal=password=secret


If you haven't already installed a Kubernetes cluster locally, or you have no remote cluster, you can sign in to the developer sandbox, a no-cost sandbox environment for Red Hat OpenShift and CodeReady Workspaces.

You can confirm that the Secret is created properly by using the following command:

$ kubectl get secret/db-credentials -o yaml


The output should look like this:

apiVersion: v1
data:
  password: c2VjcmV0
  username: YWRtaW4=
kind: Secret
metadata:
  creationTimestamp: "2022-05-02T13:46:18Z"
  name: db-credentials
  namespace: doh-dev
  resourceVersion: "1190920736"
  uid: 936abd44-1097-4c1f-a9d8-8008a01c0add
type: Opaque


The username and password are encoded by default.

3. Create a New RESTful API to Access the Secret

Now you can add a new RESTful (for Representational State Transfer) API to print out the username and password stored in the Kubernetes Secret. Quarkus enables developers to refer to the secret as a normal configuration using a @ConfigureProperty annotation.

Open a GreetingResource.java file in src/main/java/org/acme. Then, add the following method and configurations:

@ConfigProperty(name = "username")
    String username;

    @ConfigProperty(name = "password")
    String password;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @Path("/securty")
    public Map securty() {
        HashMap<String, String> map = new HashMap<>();
        map.put("db.username", username);
        map.put("db.password", password);
        return map;
    }


Save the file.

4. Set the Configurations for Kubernetes Deployment

Open the application.properties file in the src/main/resources directory. Add the following configuration for the Kubernetes deployment. In the tutorial, I'll demonstrate using the developer sandbox, so the configurations tie to the OpenShift cluster.

If you want to deploy it to the Kubernetes cluster, you can package the application via Docker container directly. Then, you need to push the container image to an external container registry (for example, Docker Hub, quay.io, or Google container registry).

# Kubernetes Deployment
quarkus.kubernetes.deploy=true
quarkus.kubernetes.deployment-target=openshift
openshift.expose=true
quarkus.openshift.build-strategy=docker
quarkus.kubernetes-client.trust-certs=true

# Kubernetes Secret
quarkus.kubernetes-config.secrets.enabled=true
quarkus.kubernetes-config.secrets=db-credentials


Save the file.

5. Build and Deploy the Application to Kubernetes

To build and deploy the reactive application, you can also use the following Quarkus CLI:

$ quarkus build


This command triggers the application build to generate a fast-jar file. Then the application Jar file is containerized using a Dockerfile, which was already generated in the src/main/docker directory when you created the project. Finally, the application image is pushed into the integrated container registry inside the OpenShift cluster.

The output should end with a BUILD SUCCESS message.

When you deploy the application to the developer sandbox or normal OpenShift cluster, you can find the application in the Topology view in the Developer perspective, as shown in the figure below.

A screenshot of Red Hat OpenShift Dedicated. In the left sidebar menu Topology is highlighted, and in the main screen there is a Quarkus icon

6. Verify the Sensitive Information

To verify that your Quarkus application can refer to the sensitive information from the Kubernetes Secret, get the route URL using the following kubectl command:

$ kubectl get route


The output is similar to this:

NAME                     HOST/PORT                                                               PATH   SERVICES                 PORT   TERMINATION   WILDCARD
quarkus-secret-example   quarkus-secret-example-doh-dev.apps.sandbox.x8i5.p1.openshiftapps.com          quarkus-secret-example   8080                 None


Use the curl command to access the RESTful API:

$ curl http://YOUR_ROUTE_URL/hello/security


The output:

{db.password=secret, db.username=admin}


Awesome! The above username and password are the same as those you stored in the db-credentials secret.

Where To Learn More

This guide has shown how Quarkus enables developers to externalize sensitive information using Kubernetes Secrets. Find additional resources to develop cloud-native microservices using Quarkus on Kubernetes here:

  • 7 guides for developing applications on the cloud with Quarkus
  • Extend Kubernetes service discovery with Stork and Quarkus
  • Deploy Quarkus applications to Kubernetes using a Helm chart

Video Demo

For learning cloud-native application development with Kubernetes, you can also watch the following demo video from my YouTube channel on how each step works.


Command-line interface Kubernetes Quarkus application Build (game engine) Docker (software)

Published at DZone with permission of . See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Auto-Scaling a Spring Boot Native App With Nomad
  • Manage Microservices With Docker Compose
  • An Introduction to BentoML: A Unified AI Application Framework
  • 7 Ways of Containerizing Your Node.js Application

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!