Skip to content

Lesson 2 of 9 · 26m

Configuration Management Without Secrets Leaks

Model configuration explicitly, validate it at startup, and keep environment-specific behavior out of business code.

Most production configuration failures are boring: a missing environment variable, an unvalidated URL, a timeout copied from a tutorial, or a profile activated in one deployment tool and not another. The dangerous part is that the application often starts anyway and fails six minutes later in a downstream call. Configuration is a contract. Treat it like one.

Use typed properties instead of stringly-typed lookup

@Value scattered through services turns configuration into trivia. Bind related settings into a validated object:

@ConfigurationProperties(prefix = "payments")
@Validated
public record PaymentProperties(
        @NotNull URI baseUrl,
        @DurationMin(seconds = 1) Duration connectTimeout,
        @DurationMin(seconds = 1) Duration readTimeout,
        @NotBlank String apiKey) {
}

Enable it with @ConfigurationPropertiesScan. Missing or malformed configuration then fails during startup, close to the cause, instead of during the first payment attempt.

Profiles are environment selectors, not personality disorders

A common failure is profile logic becoming a parallel application: local means mocks, qa means relaxed validation, prod means security enabled, and nobody knows which combination is live. Keep profiles small and structural:

spring:
  profiles:
    active: prod
payments:
  base-url: https://payments.internal.sudosecurity.org
  connect-timeout: 2s
  read-timeout: 5s

Do not use profiles to express customer features. Feature flags, entitlements, and tenant configuration are runtime data, not deployment profiles.

Secrets do not belong in application.yml

This is still too common:

payments:
  api-key: sk_live_4eC39HqLyjWDarjtT1zdp7dc

Use a secret manager, Kubernetes secret volume, environment-specific injection, or your platform's secret mechanism. The key rule is rotation without a rebuild. Also treat logs as hostile: startup banners, configuration dumps, exception messages, and actuator output can all leak values.

Field notes

  • Never log raw configuration at startup; even 'non-secret' hosts and usernames can help an attacker map the system.
  • spring.config.import is powerful, but make import failure behavior explicit with optional: only when absence is truly safe.
  • Do not make every timeout configurable. Configurable noise hides missing architectural decisions.
  • Property binding is relaxed; base-url, baseUrl, and BASE_URL can bind to the same field. That convenience makes typos harder to spot, so validation matters.
  • A local profile that disables authentication is a production incident waiting for a copy-paste mistake.

Was this lesson helpful?

Sign in to rate