Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 38 ยท Identity and Access

Roles, Temporary Credentials and Secrets

Replace long-lived access keys with roles and short-lived tokens, manage secrets safely, rotate them, and detect leaked keys in code with a runnable secret scanner.

Leaked credentials are among the most common ways attackers get into cloud accounts: an access key pushed to a public repository, a password in a configuration file, a token printed in a log. This lesson shows how to avoid long-lived credentials altogether where you can, how to store and rotate the secrets you cannot avoid, and how to scan your own code to catch leaks before an attacker does.

Prefer identity over secrets

The best secret is the one that does not exist. Modern clouds let workloads prove who they are without any stored password:

  • Workload roles / managed identities. A virtual machine, container or function is assigned a role. The platform hands it short-lived credentials automatically, which the SDK picks up without you writing them anywhere.
  • Workload identity federation. A pipeline or an external system presents a signed token from its own provider (for example your code host), and the cloud exchanges it for temporary credentials. No long-lived key sits in your pipeline settings.
  • Assume role for humans. People sign in through single sign-on and assume a role for a limited session instead of owning permanent keys.

Temporary credentials have an expiry (often minutes to hours). If one leaks, the damage window is small.

flowchart LR
    W["Workload<br/>(VM, container, function)"] -->|"proves identity to platform"| P["Platform identity service"]
    P -->|"short-lived token"| W
    W -->|"token"| S["Cloud API"]

No password or key is stored in the workload; the platform issues a token that expires on its own.

When you still need secrets

Databases passwords, third-party API keys, signing keys and certificates sometimes cannot be replaced by roles. For those:

  1. Store them in a secrets manager, not in code, images, environment files committed to a repository, or chat messages.
  2. Fetch at runtime with a role that may read only that secret.
  3. Encrypt them with a key you control, and log every access.
  4. Rotate regularly and immediately after exposure. Automated rotation, where the manager changes the password on both sides, is best.
  5. Separate by environment. Production secrets are never reachable from development.
  6. Avoid printing secrets in logs, error messages and command lines (which show in process lists).

Secret handling rules

Never in code Or in images or chats
Use a vault Fetched at runtime
Rotate On schedule and on leak
Audit Who read what

Rotate first, investigate second when a secret leaks.

1 / 2

What to do when a key leaks

  1. Revoke or deactivate the key immediately. Deleting a commit does not help; assume it was copied.
  2. Review the audit log for what the key did since it was exposed.
  3. Rotate anything else it could reach and issue a replacement the safe way.
  4. Find the cause (a missing scanner, a template, a habit) and fix it.
  5. Record the incident and what you changed.

Runnable example: scan code for secrets

Secret scanners look for known key formats and high-entropy strings. The program below scans sample files with a few patterns. The keys in the sample are fake (obviously invalid values built for this lesson) and the patterns are simplified.

Python Find fake secrets in sample files

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 input()), it gets the next line from this box.

The scanner flags the hard-coded password, the access key ID, the bearer token and the long random-looking string, but not the line that reads the token from os.environ. Real scanners such as those built into code hosts also check git history and verify candidate keys with the provider.

Safe loading of secrets

The next example shows the pattern to follow: read a secret from the environment (or a secrets manager client in real code), fail clearly if it is missing, and never log the value.

Python Load a secret without leaking it

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 input()), it gets the next line from this box.

Prevention in the pipeline

  • Enable secret scanning and push protection in your code host so commits with known key formats are blocked.
  • Add a pre-commit hook that runs a scanner locally.
  • Keep .env files out of version control with .gitignore and provide an example file with fake values.
  • Run scanners over container images and build logs too.

Deleting is not revoking. Once a secret has reached a public repository, treat it as compromised within minutes. Always revoke and rotate; do not rely on removing it from history.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main advantage of a workload role over a stored access key?

    Temporary credentials issued automatically reduce what can leak and how long it stays useful.
  2. 2. A key was committed to a public repository and then removed. What should you do?

    Assume it was copied; only revocation makes it useless.
  3. 3. Where should a database password be kept?

    A secrets manager provides encryption, access control, auditing and rotation.
  4. 4. What does a high-entropy string in code often indicate?

    Random-looking long strings often are keys or tokens, which is why scanners check entropy.

Summary

  • Prefer roles, managed identities and federation so workloads need no stored secrets and credentials expire quickly.
  • Store unavoidable secrets in a secrets manager, fetch at runtime with narrow access, rotate and audit them.
  • On a leak, revoke and rotate first, then investigate with logs and fix the cause.
  • Scan code, history, images and logs for secrets, and block them at commit time.

Next: Privileged Access and IAM Auditing