The best secret is one that does not exist: use IAM roles wherever possible, and store the rest in a managed secret store with automatic rotation. 1. Prefer IAM roles over static credentials - Run the application with an IAM role: an ECS task role, EKS IRSA or EKS Pod Identity, or an EC2 instance profile. - The AWS SDK's default credential provider chain picks up temporary credentials automatically, so there are no access keys in configuration. - RDS and Aurora support IAM database authentication (short-lived tokens instead of passwords) for suitable workloads. 2. Store remaining secrets in AWS Secrets Manager (or SSM Parameter Store SecureString) - Database passwords, third-party API keys, OAuth client secrets, signing keys. - Encrypted with KMS; access controlled by IAM policies with least privilege (allow secretsmanager:GetSecretValue only on the specific secret ARNs); access logged in CloudTrail. 3. Load secrets into Spring Boot - Spring Cloud AWS: spring.config.import=aws-secretsmanager:/prod/orders-service (or aws-parameterstore:/prod/orders/) maps secrets into the Spring Environment. - On Kubernetes: the External Secrets Operator or the Secrets Store CSI driver syncs secrets into pods, preferably as mounted files rather than environment variables, which can leak through debugging output or crash dumps. - Never commit secrets to Git, bake them into images, or log configuration values (sanitise Actuator /env and /configprops endpoints and restrict access to them). 4. Rotation - Enable Secrets Manager automatic rotation (managed rotation for RDS, or a Lambda rotation function for custom secrets). Use the alternating-users strategy for databases so one credential is always valid during rotation. - Make the application tolerate rotation: - Refresh secrets periodically or on authentication failure. - HikariCP: connections created with the old password keep working until closed; new connections need the new password. Tools such as the AWS JDBC driver's Secrets Manager plugin handle retrieval and retries. - Spring Cloud AWS can reload configuration properties; beans that cache credentials must re-read them. - Rotate signing keys with overlap (publish the new key, keep accepting the old one until tokens expire). 5. Detection and governance - Secret scanning in repositories (GitHub secret scanning, gitleaks) and immediate rotation if anything leaks. - CloudTrail alerts on unusual GetSecretValue activity; separate secrets per environment and per service. - CI/CD uses OIDC federation to assume AWS roles, not long-lived keys stored in the pipeline.
Ravindra Wadile · · 0 likes