Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 36 ยท Config and Profiles

Secrets, Custom Sources and Config in Production

Keep passwords out of Git: use environment variables, .env files, Kubernetes secrets and config maps, custom ConfigSource classes and secret-safe logging in Quarkus.

A configuration system is only half done when it can read values. The other half is deciding where production values live and making sure passwords never end up in Git, in a log file or in a container image. This lesson covers the usual answers in Quarkus: environment variables and .env, mounted secrets, Kubernetes ConfigMaps and Secrets, a custom ConfigSource, and how to stop secrets from being printed.

You already know the order of sources from properties and profiles and typed access from @ConfigMapping. Real Quarkus configuration is shown read-only; the TryIts model the ideas in plain Java.

The rule: images are the same everywhere

The same container image should run in test and in production. Only the configuration differs, and it arrives from outside the image. Therefore:

  • application.properties inside the jar holds defaults and non-secret structure: ports, feature switches, %dev values.
  • Anything that differs per environment, or is secret, comes from the environment.
PROPERTIES
# application.properties (committed to Git)
quarkus.datasource.db-kind=postgresql
%dev.quarkus.datasource.username=dev
%dev.quarkus.datasource.password=dev
%dev.quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/shop
# no %prod values here: they come from the environment
Terminal
# set by the platform at deploy time
QUARKUS_DATASOURCE_JDBC_URL=jdbc:postgresql://db.internal:5432/shop
QUARKUS_DATASOURCE_USERNAME=shop
QUARKUS_DATASOURCE_PASSWORD=...secret...

The development password dev is harmless. A production password in a %prod. line would be a leak the moment the repository is shared.

.env files

In dev mode and in tests Quarkus also reads a .env file from the working directory (ordinal 295, just below real environment variables). It is a convenient place for personal values:

Terminal
# .env  (listed in .gitignore!)
GITHUB_TOKEN=ghp_example
QUARKUS_MAILER_PASSWORD=local-only

Add .env to .gitignore, and commit a .env.example with fake values so a new teammate knows what to fill in.

Kubernetes ConfigMaps and Secrets

On Kubernetes there are two common patterns.

1. Inject as environment variables, which needs no Quarkus extension, only the deployment manifest:

YAML
env:
  - name: QUARKUS_DATASOURCE_PASSWORD
    valueFrom:
      secretKeyRef:
        name: shop-db
        key: password
envFrom:
  - configMapRef:
      name: shop-settings

2. Read them through the API with the quarkus-kubernetes-config extension:

PROPERTIES
quarkus.kubernetes-config.enabled=true
quarkus.kubernetes-config.config-maps=shop-settings
quarkus.kubernetes-config.secrets.enabled=true
quarkus.kubernetes-config.secrets=shop-db

The extension needs permission (a Role and RoleBinding) to read those objects, and the values appear as config sources, so @ConfigProperty works unchanged. The first approach is simpler; the second lets configuration change without restarting the deployment manifest.

A third option is a mounted file. A Kubernetes secret mounted at /mnt/secrets/db-password can be read with the quarkus.config.locations setting for a directory of properties, or by the property quarkus.datasource.password expanding from a file with a custom source (below).

Where does a value live

In the jar Defaults. Never secrets.
Environment Per-environment, set by the platform.
Secret store Vault, Kubernetes, cloud KMS.
Mounted file A secret as a file.

Same image everywhere. Only the outside changes.

1 / 2

A custom ConfigSource

When your values live somewhere Quarkus does not know, such as a company vault, a database table or a file with an unusual format, you add your own source by implementing ConfigSource:

Java
import org.eclipse.microprofile.config.spi.ConfigSource;
import java.util.Map;
import java.util.Set;

public class VaultConfigSource implements ConfigSource {

    private final Map<String, String> cache = VaultClient.readAll("secret/shop");

    @Override public Map<String, String> getProperties() { return cache; }
    @Override public Set<String> getPropertyNames() { return cache.keySet(); }
    @Override public String getValue(String name) { return cache.get(name); }
    @Override public String getName() { return "vault"; }
    @Override public int getOrdinal() { return 350; }   // beats env vars (300)
}

You register it by listing the class name in META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource. Quarkus also ships ready-made integrations, notably quarkus-vault for HashiCorp Vault, so write your own only when nothing fits.

Java Stand-in: a source chain with a vault and a mounted file

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The mounted secret (ordinal 320) overrides the placeholder changeme in the file. If the platform forgets to mount it, the application would silently run with changeme, which is why production files should not contain placeholder passwords at all: leave the key out and let startup fail.

Keep secrets out of logs

Quarkus does not log property values by default, but your code might. A few habits:

  • Never log a whole mapping object. A Java record or an interface proxy prints everything, passwords included.
  • Mask on output. Show ab**** instead of the real value.
  • Mark settings with names that scream: ...password, ...secret, ...token. SmallRye Config can hide values whose names match patterns in tooling, and reviewers spot them faster.
  • Avoid putting secrets into URLs (jdbc:...?password=), because URLs travel into log lines.
Java Stand-in: a safe config printer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Reloading and changing values

Configuration is read when the application starts, so changing an environment variable needs a restart (on Kubernetes, a rolling restart of the pods). Quarkus does support re-reading some sources, and you can inject Config and call getValue at runtime, but values that went into build-time properties or into a @ConfigMapping already created cannot change. Plan for restart-based configuration, and use a feature-flag service when you really need switches that flip without a deploy.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should a production database password live?

    Secrets should come from outside the image and outside Git. Env variables, Kubernetes Secrets and vaults all do that.
  2. 2. Why is .env added to .gitignore?

    The file is for local values; commit a .env.example with fake data instead.
  3. 3. A custom ConfigSource returns ordinal 350. Which sources does it override?

    The highest ordinal wins, so 350 beats 300 and 250 but loses against 400.
  4. 4. Why should you avoid logging a whole @ConfigMapping object?

    The generated implementation prints every value. Print selected, masked fields instead.

Summary

  • Ship one image; supply per-environment and secret values from the outside.
  • Environment variables, .env (local only), Kubernetes Secrets/ConfigMaps and Vault are the usual sources.
  • A custom ConfigSource with an ordinal plugs in any other store.
  • Do not commit, log or bake secrets; leave placeholders out so a missing secret stops startup.

Next: Mutiny: Uni and Multi