hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.81k stars 9.16k forks source link

Create least privilege IAM policy for a resource or module #9154

Open joepindar opened 5 years ago

joepindar commented 5 years ago

Community Note

Description

As a security specialist, I want my Terraform plans to run with the minimum set of IAM privileges. However, it is not clear which AWS IAM actions are required by a Terraform resource or module. The current solutions to this problem are:

This feature request is to create a process or method to generate the set of least privilege IAM actions that are required to apply the Terraform plan successfully.

New or Affected Resource(s)

All resources will be impacted. A potential initial solution would be to automatically generate documentation of the IAM actions required for each resource, and add as a section to the Terraform documentation. Future iterations would then automate the IAM policy generation.

matthiasr commented 5 years ago

I would also be interested in the least privilege policy to create a plan. We have projects shared across teams, where non-infrastructure teams "only" need enough access to modify specific resources, but they do need enough access to terraform plan.

We currently use the second approach, and in this case it is especially painful because someone with enough privileges to use a new kind of resource can inadvertently break planning for many others without noticing.

kunickiaj commented 4 years ago

Would love to hear what folks' current strategies are outside of the ones mentioned by OP as those are all pretty painful.

I've been using Cloudtrail logs from a user/role that has fairly high privileges as a bit of a flight recorder. Then querying those logs with Athena to list all of the permissions invoked for an apply+destroy.

I've found this to capture many but not all of them for some reason but does give a pretty good starting point.

Example query:

select distinct eventname, eventsource, eventtype, additionaleventdata, resources, requestparameters, useridentity.arn
from cloudtrail_logs
where 1 = 1
and account = 'xxxxxx'
and region = 'yyyyy'
and year = '2020'
and eventtime > '2020-08-18'
and useridentity.arn LIKE 'arn:aws:sts::<accountid>:assumed-role/TF-xyz/%'

Throw this into a spreadsheet and you can do some auto grouping to show you the services and actions invoked as well as some details of the resource ARNs in some cases.

I'm surprised I don't see more tools around this -- so maybe a good side project to develop a cloudtrail-based flight recorder + policy generator.

flosell commented 4 years ago

There are some tools that do things similar to what @kunickiaj describes, one of them my own (shameless plug):

Being a heavy terraform user, my trigger to start building trailscraper was specifically because I couldn't find a way to figure out which permissions were necessary from terraform itself. Having worked with many teams who were just adopting IaC practices, IAM is often their biggest struggle: they either spend countless hours figuring out a least-privilege policy by trial-and-error or give up and attach AdministratorAccess. Obviously, neither of those is great so anything terraform can do to improve this situation (even by a tiny bit) will be a huge improvement.

tomasbackman commented 3 years ago

Yes very much agreeing with the need of this! I will try out the tools linked above, while hoping that we will get resources that can show/return what services they actually correspond to in AWS and what permissions they need. So that modules could automatically add all resource needs together as some "module requirements" list or similar that easily can be checked against etc.

paultyng commented 3 years ago

Just wanted to add a pointer to this issue as the ability to know the total list of resource/data sources in a configuration could help in this regard (even though it wouldn't be able to limit based on the specifics of those resources): https://github.com/hashicorp/terraform/issues/25568

flosell commented 3 years ago

Here's an interesting approach that might help us here: iamlive uses AWS SDKs' Client Side Monitoring feature to collect API calls made and translates them into IAM policies.

This tool could be an alternative to the tools mentioned above for people who just want to record what IAM permissions are necessary for their specific terraform project. More interestingly though, we could try embedding this approach in the providers' acceptance tests to autogenerate documentation about which IAM actions are used:

It'd probably be slightly more complicated if acceptance tests need multiple different resources (e.g. to set up preconditions for the unit under test) but that might be a solvable problem.

Happy to look into this more if folks are interested.

Other resources that might be interesting: The same author is also working on a lower-level tool that maps AWS SDK actions to IAM Actions.

confuser commented 3 years ago

Just ran into this scenario and required some trial/error to figure out the right permissions. Would be great to start adding necessary permissions to the docs site (after import sections?), perhaps in an iterative fashion?