Secrets and environment config
What you will learn
Environment configuration separates release bytes from deployment settings. Secrets require restricted storage and rotation. Workload identity federation exchanges a trusted short-lived identity for scoped access instead of embedding permanent cloud keys in CI or the application.
- Validate explicit runtime configuration.
- Explain issuer, audience, subject and least privilege in federation.
- Exclude sensitive configuration from logs.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Standalone Java 21 functional examples. These are offline Java 21 models, not cloud or hosted CI execution. Companion local pipeline evidence is separate.
Validate environment settings
public class Main { static int port(java.util.Map<String,String> env) { int p=Integer.parseInt(env.getOrDefault("PORT","8080")); if(p<1024 || p>65535) throw new IllegalArgumentException("PORT"); return p; } public static void main(String[] args) { System.out.println(port(java.util.Map.of("PORT","9090"))); System.out.println(port(java.util.Map.of())); }}Expected output:
90908080Use a documented default for non-secret settings only; malformed ports fail startup.
Identity and secret ownership
Use separate build, deploy and runtime identities. In an approved hosted workflow, OIDC federation exchanges a signed token for short-lived cloud credentials after the provider checks issuer, audience, repository/environment claims, expiry and trust policy. Restrict the deploy role to the intended target; restrict the application role to its own dependencies. Azure managed identities, AWS IAM roles and Google service accounts/workload identity federation are provider-specific implementations, not interchangeable tokens. Do not create access keys for this lab. Secret managers and rotation policies are distinct from ordinary configuration. Runtime secret delivery through environment variables or mounted files depends on platform; avoid logging them, and handle refresh/restart safely.
Fail closed at startup and trust boundaries
Validate non-secret settings before readiness, restrict network destinations and enforce TLS. Use allowlisted fields in logs; even allowlisted values need validation and data classification in real applications. The following claim matcher assumes a verified identity from a trusted issuer; it does not parse or verify tokens, authenticate a caller, check expiry or replace an IAM policy. CI trust conditions should also restrict environment/branch and approvals as applicable.
Model a restricted claim policy
public class Main { record Identity(String issuer,String audience,String subject) {} static boolean allowed(Identity i) { return "ci.example.invalid".equals(i.issuer()) && "deploy-demo".equals(i.audience()) && "repo:demo/catalog:environment:staging".equals(i.subject()); } public static void main(String[] args) { System.out.println(allowed(new Identity("ci.example.invalid","deploy-demo","repo:demo/catalog:environment:staging"))); System.out.println(allowed(new Identity("ci.example.invalid","deploy-demo","repo:other/catalog:environment:staging"))); }}Expected output:
truefalseThe second synthetic identity has an unapproved subject. This is not real OIDC verification.
Log only selected non-secret fields
public class Main { static java.util.Map<String,String> safeLog(java.util.Map<String,String> config) { var safe=new java.util.TreeMap<String,String>(); for(String k:java.util.List.of("REGION","APP_ENV")) if(config.containsKey(k)) safe.put(k,config.get(k)); return safe; } public static void main(String[] args) { System.out.println(safeLog(java.util.Map.of("REGION","demo-region","APP_ENV","stage","DB_PASSWORD","SYNTHETIC"))); }}Expected output:
{APP_ENV=stage, REGION=demo-region}The password key never appears; use validated structured fields, not a complete environment dump.
Common mistakes
Do not print tokens or environment maps to diagnose configuration. Do not authorize a request by decoding unsigned JWT claims or granting a wildcard deploy role. A short-lived token still needs narrowly scoped permissions and secure handling.
Practice
Check a synthetic identity with the correct issuer and subject but audience other-service. Print false.
Expected output:
falseCompare with the manual review criteria.
Further reading: OIDC · Azure identity · CI security. Independent editorial review remains pending.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Secrets and environment config — knowledge check
Cloud deployment and CI/CD reasoning.
What is the main benefit of federation?
What must precede claim-based authorization?
Which identity should read application data?
How should configuration logs be built?
What should happen on invalid required config?