5 min read

Security and Governance in Infrastructure - Least Privilege IAM in Practice

1. The Problem: Excessive Privilege Is the Norm, Not the Exception

In the previous two articles of this series, we saw how to keep secrets from leaking in plain text and how to block insecure resources before apply with Policy as Code. We wrap up the series with a quieter problem, because it produces no errors or alerts: IAM permissions granted with more reach than necessary. An application role with s3:* when it only needs s3:GetObject on a specific bucket. A CI user with AdministratorAccess because “that fixes it right away and nobody has to ask for more permissions later”. None of this breaks anything — until the day a credential is compromised, and the attacker’s blast radius is exactly the size of the permission nobody restricted.

2. Common Excessive-Permission Mistakes

  • Wildcard actions: "Action": "s3:*" or "Action": "*" instead of listing exactly the operations needed (s3:GetObject, s3:PutObject).
  • Wildcard resources: "Resource": "*" when the real intent was to restrict access to a specific bucket or prefix.
  • Roles shared across different services: a single “backend-role” used by several Lambdas/services with different needs, accumulating everyone’s permissions over time because it’s easier to reuse than to create a new one.
  • Long-lived credentials: IAM user access keys with no rotation, instead of temporarily assumed roles (via sts:AssumeRole) or federated identities (OIDC in CI pipelines).
  • Permissions granted and never reviewed: access opened up to handle a one-off incident (“give me S3 admin access to debug this quickly”) that’s never removed afterwards.

3. Designing Minimal Policies: Methodology

Least privilege in practice is rarely “getting it right the first time” — it’s an iterative process:

  1. Start restrictive, not permissive. When creating a new role, list only the actions the code actually calls, even if that means testing and adjusting a few times until it works.
  2. Use real access logs to calibrate. Tools like IAM Access Analyzer (AWS) generate policy recommendations based on actual usage recorded in CloudTrail — instead of guessing which actions are needed, you see which ones were actually called over a period.
  3. Restrict by Resource, not just by Action. Allowing s3:GetObject on * is still excessive privilege if the application only needs one bucket. Whenever the service’s API supports it, specify the exact ARN.
  4. Add conditions (Condition) when possible. Restricting by source IP, requiring MFA or limiting by resource tag are additional layers that reduce the impact of a leaked credential.

4. Practical Example: From Permissive to Minimal

Starting policy, common in code copied from a generic tutorial:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "s3:*",
      "Resource": "*"
    }
  ]
}

Least-privilege version, after identifying that the application only reads objects from a specific bucket, under a specific prefix:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject",
        "s3:ListBucket"
      ],
      "Resource": [
        "arn:aws:s3:::app-uploads-prod",
        "arn:aws:s3:::app-uploads-prod/incoming/*"
      ],
      "Condition": {
        "StringEquals": {
          "aws:PrincipalTag/environment": "production"
        }
      }
    }
  ]
}

In Terraform, the same design, without writing the JSON by hand:

data "aws_iam_policy_document" "app_uploads_read" {
  statement {
    effect = "Allow"

    actions = [
      "s3:GetObject",
      "s3:ListBucket",
    ]

    resources = [
      aws_s3_bucket.app_uploads.arn,
      "${aws_s3_bucket.app_uploads.arn}/incoming/*",
    ]

    condition {
      test     = "StringEquals"
      variable = "aws:PrincipalTag/environment"
      values   = ["production"]
    }
  }
}

resource "aws_iam_policy" "app_uploads_read" {
  name   = "app-uploads-read-only"
  policy = data.aws_iam_policy_document.app_uploads_read.json
}

5. Automating Access Audits

Designing the minimal policy at creation time isn’t enough — permissions tend to accumulate over time, and you need a continuous process to detect it:

  • IAM Access Analyzer automatically identifies resources accessible by entities outside the account (buckets, roles, KMS keys) and also generates reports of “permissions granted versus permissions used in the last 90 days”, pointing out what can be removed:
aws accessanalyzer list-findings \
  --analyzer-arn arn:aws:access-analyzer:us-east-1:123456789012:analyzer/default \
  --filter '{"status": {"eq": ["ACTIVE"]}}'
  • cloudsplaining, an open source tool, scans the IAM policies of an entire AWS account and flags risky patterns (wildcards, privilege escalation, missing resource constraints) in an HTML report:
pip install cloudsplaining

cloudsplaining download --profile production
cloudsplaining scan --input-file default.json --output results/
  • Integrating with the IaC pipeline: the same conftest/OPA seen in the previous article of this series can evaluate IAM policy documents generated by Terraform before apply, blocking any "Resource": "*" combined with a write action, for example:
package terraform.iam

deny[msg] {
    resource := input.resource_changes[_]
    resource.type == "aws_iam_policy"

    statement := resource.change.after.policy.Statement[_]
    statement.Effect == "Allow"
    statement.Resource == "*"

    msg := sprintf(
        "policy '%s' uses Resource=\"*\" — restrict it to the specific ARN",
        [resource.address],
    )
}

6. Rotation and Temporary Identities

Besides restricting what a credential can do, reducing how long it’s valid limits the impact of a leak even further:

  • Prefer roles assumed via sts:AssumeRole over IAM users with fixed access keys whenever possible.
  • In CI pipelines, use OIDC federation (GitHub Actions and GitLab CI already support it natively) so the pipeline assumes a temporary role without any long-lived credential stored as a secret:
# GitHub Actions assuming a role via OIDC, with no fixed access key
permissions:
  id-token: write
  contents: read

steps:
  - uses: aws-actions/configure-aws-credentials@v4
    with:
      role-to-assume: arn:aws:iam::123456789012:role/github-actions-deploy
      aws-region: us-east-1

7. Series Conclusion

Properly stored secrets, resources validated by policy before apply and permissions designed with the least privilege necessary are three independent fronts that reinforce each other: a leaked secret matters less if the associated credential has limited reach; a well-designed IAM policy is itself a natural target for Policy as Code checks. No infrastructure automation is complete without treating security and governance as part of the pipeline, rather than as a manual review that may (or may not) happen before going to production.


References:

  1. AWS IAM — Grant Least Privilege
  2. AWS IAM Access Analyzer
  3. cloudsplaining — Documentation