INF-26Recommended
Give services the identity of their machine
Let each running service get cloud credentials from the platform's identity, such as an instance role, a Kubernetes service account or a managed identity. Put no cloud key in its settings.
How to do it
Attach a role to the server, container task or function. The cloud SDK's default credential chain finds it without any code. Give each service its own role. Outside a cloud, use workload identity federation where the platform offers it, before reaching for a key file. It leaves no key to leak, the risk Keep secrets out of code describes.
Examples
TypeScript example
// The SDK finds the role attached to the machine. No key in code or settings.
import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
const s3 = new S3Client({});
const invoice = await s3.send(new GetObjectCommand({ Bucket: 'example-shop-invoices', Key: 'A-1042.pdf' }));
Python example
import boto3
s3 = boto3.client("s3")
invoice = s3.get_object(Bucket="example-shop-invoices", Key="A-1042.pdf")
Shell example
# Fails when a service carries a static cloud key
! tr '\0' '\n' < "/proc/$(systemctl show -p MainPID --value shop)/environ" | grep -E '^AWS_SECRET_ACCESS_KEY='
How to check
The running service's environment holds no AWS_SECRET_ACCESS_KEY, and no GOOGLE_APPLICATION_CREDENTIALS that points to a key file. Its calls appear in the audit log under its role.
Sources
Related rules
- INF-17 Keep no long-lived access keys in Infrastructure and environments
- APS-60 Keep secrets out of code in Application security
- DEN-30 Give each pipeline and BI tool its own narrow account in Data engineering
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/infrastructure/configuration-and-secrets/workload-identity">Agent Wranglers infrastructure and environments playbook</a>. In code you write, add a comment next to it with the same link.