All guides

AWS IAM least privilege: from wildcards to exact actions

IAM records when each role last used a service, and CloudTrail records its API calls. Start from those two records and cut a wildcard policy down to what the role uses.

An open laptop on a wooden table in front of bookshelves

Start from recorded activity

The IAM best practices page allows broad permissions while you explore what a workload requires, and tells you to reduce them as the use case matures. Two records show what to keep. IAM provides last accessed information for users, roles, groups and policies. CloudTrail logs the API calls, and IAM Access Analyzer can generate a policy from them.

Last accessed information

For each service that a role's policies allow, IAM reports the date and time of the last attempt to access it. Service activity is tracked for a minimum of 400 days. The period is shorter in a Region where tracking started less than 400 days ago. For the services AWS lists, it also reports the last attempt for each management action. For data plane events, IAM reports no action last accessed information at all.

Shell

aws iam generate-service-last-accessed-details \
  --arn arn:aws:iam::111122223333:role/my-app-role \
  --granularity ACTION_LEVEL

aws iam get-service-last-accessed-details --job-id <job-id>

In the report, LastAuthenticated is null when no IAM entity attempted to access the service within the tracking period. Remove those services first. The data includes all attempts to access an AWS API, including the unsuccessful ones, so an entry does not show that a call succeeded.

Policy generation from CloudTrail

IAM Access Analyzer reviews the CloudTrail events of one user or role over a period of up to 90 days and generates a policy from them. You specify the trail and a service role that gives Access Analyzer access to the trail and to last accessed information.

Shell

aws accessanalyzer start-policy-generation \
  --policy-generation-details principalArn=arn:aws:iam::111122223333:role/my-app-role \
  --cloud-trail-details file://trail.json

aws accessanalyzer get-generated-policy --job-id <job-id>

trail.json holds the trail ARN, the service role in accessRole and a startTime. In the console, the feature is on the role's Permissions tab. AWS lists these limits:

  • For data events such as Amazon S3 data events, it reports no activity at the level of individual actions.
  • iam:PassRole is not tracked by CloudTrail and is not included.
  • For some services it identifies only the service and prompts you to add the actions.
  • It reviews all CloudTrail events, including denied actions, so read every action before you keep it.

The generated template leaves resource ARNs for you to fill in. Use * only for actions that do not support resource-level permissions.

One policy before and after

Before, the policy allows every S3 action on every resource:

JSON

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

After, it follows the AWS example for read and write access to the objects in one bucket. The AWS example grants s3:*Object. This version names two object actions in its place:

JSON

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ListObjectsInBucket",
      "Effect": "Allow",
      "Action": ["s3:ListBucket"],
      "Resource": ["arn:aws:s3:::my-app-data"]
    },
    {
      "Sid": "ReadAndWriteObjects",
      "Effect": "Allow",
      "Action": ["s3:GetObject", "s3:PutObject"],
      "Resource": ["arn:aws:s3:::my-app-data/*"]
    }
  ]
}

Conditions that narrow a grant

A condition sets when a policy statement is in effect. Four global condition keys:

  • aws:SecureTransport checks whether the request was sent using TLS.
  • aws:RequestedRegion checks the Region the request called against the Region you name.
  • aws:ResourceTag/tag-key compares a tag key and value in the policy with the tag attached to the resource.
  • aws:PrincipalOrgID compares the organization of the requesting principal with the ID you specify. AWS notes that it simplifies the Principal element of a resource-based policy.

Permissions boundaries

A permissions boundary is a managed policy. It caps what an identity-based policy can grant to a user or a role. It grants no permissions on its own. The effective permissions are the intersection of the boundary and the identity-based policies.

Shell

aws iam put-role-permissions-boundary \
  --role-name my-app-role \
  --permissions-boundary arn:aws:iam::111122223333:policy/my-boundary

You can use boundaries to delegate permissions management. In the AWS example, a delegated user may create IAM users only when he sets a named policy as their permissions boundary. His policy tests the iam:PermissionsBoundary condition key.

Validation and rollout

Shell

aws accessanalyzer validate-policy \
  --policy-document file://policy.json \
  --policy-type IDENTITY_POLICY

IAM Access Analyzer provides more than 100 policy checks. The command returns findings of four types: ERROR, SECURITY_WARNING, WARNING and SUGGESTION. An error reports a part of the policy that is not functional. A security warning reports access that AWS considers overly permissive. Fix both before you attach the policy. In the console, Access Analyzer gives the same recommendations as you author or edit a policy.

Then work in this order:

  1. Remove the services that last accessed information shows as unused.
  2. Generate a policy. It contains only what the role used in the date range you chose, and that range is 90 days at most.
  3. Fill in the resource ARNs and add conditions.
  4. Validate the policy and test it before production, as AWS advises.
  5. After you attach it, remove the broad policy. CloudTrail is the authoritative record of whether each call succeeded or was denied.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides