Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_s3_bucket - Fix policy normalization to match AWS API #4278

Conversation

vancluever
Copy link
Contributor

There are a couple of situations I have encountered where a S3 bucket policy that is technically correct gets changed by AWS:

  • When a policy statement has no Sid specified, it will create a Sid entry with blank data.
  • When a policy statement only has one action, but is specified in an array, it will truncate the array and use a single element string action instead. (ie: "Action": [ "s3:ListBucket" ] becomes "Action": "s3:ListBucket")
  • Multiple actions will be sorted alphabetically.

This PR expands on the current normalizeJson functionality that exists as a StateFunc hook and adds features to correct this:

  • sortJSONArray will recursively search for JSON arrays in the policy and sort them
  • normalizePolicyStatements sets the empty Sid when one does not exist and also truncates single-element Action entries.

Related to #3634, #3124, #4245, and possibly others, basically there seems to be a lot of other situations where this is an issue, each with possibly different quirks.

@catsby
Copy link
Contributor

catsby commented Jan 15, 2016

Hey @vancluever sorry this has taken so long to get to, this sounds really promising. Curious, do you think this applies to other resources (like most IAM things) that could benefit from this as well? If so, maybe we could put this in structure.go you think?

@catsby catsby added the waiting-response An issue/pull request is waiting for a response from the community label Jan 15, 2016
@vancluever
Copy link
Contributor Author

Hey @catsby, definitely - if the logic for normalization is common across the board for all policy documents then this definitely should go in there. I'm not too sure if I saw that file or not, but possibly I refrained from putting it in there because I was not too sure of the scope at the time.

I can probably do the work to move this later today or early this weekend. Should I keep the existing StateFunc as normalizeJson local to the resource file or encapsulate everything in the new function (so JSON in, JSON out)? I just briefly glanced over the other functions so I haven't had time to fully assess a pattern.

@vancluever vancluever force-pushed the paybyphone_s3_bucket_policy_normalize branch from 30b11d1 to d7397ca Compare January 20, 2016 09:51
@vancluever
Copy link
Contributor Author

@catsby, check out the code I just re-pushed/squashed. This code is a mix if the sorting and "flattening" that I was doing before, and some of the stuff that was hashed out by @radeksimko in #3124, namely the struct for the policy layout. I didn't use the full depth of the struct layout as things seem to get murky when you are dealing with elements that could have either a string value or an array value, or sub-elements when the entire parent element could be missing (ie: NotPrincipal, for example). So I instead decided to handle that part in code.

This should cover a lot of the cases and gets us to almost all of the way there. As I'm typing this I'm thinking that principals might be an issue if they are sorted on the AWS side, we might not be able to get away with just having them in a map like they are right now and may have to stage them in a struct that is separate from the main data structure. However I have tested it on a slightly more complex S3 policy than what I was using before, with things like both actions and conditions and things seem to get sorted and flattened properly, so with a little more work and maybe some test cases we might be good to merge this.

Also, I have swapped out normalizeJson in the ES resource, I intend to test this soon, maybe tomorrow, to see if it corrects the policy shown in #3634.

@vancluever vancluever force-pushed the paybyphone_s3_bucket_policy_normalize branch 2 times, most recently from 2f529ce to f3a12e8 Compare January 22, 2016 01:20
@vancluever
Copy link
Contributor Author

More updates:

  • Principal stuff now supported, added in @radeksimko's struct for this as well and am parsing it using mapstructure. So things are a bit more explicit now and multiple principals in a single statement should be ordered.
  • Also - tests!

@leeprovoost
Copy link
Contributor

@vancluever I noticed another issue with the Principal (tested against 0.6.12). When you're creating an S3 bucket policy for ELB logs and follow the AWS documentation (http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/enable-access-logs.html), it asks you to use the ELB Region ID like this:

      "Principal": {
        "AWS": [
          "797873946194"
        ]
      }

This gets marked as changed all the time. It's only when you use the following format that it is not marked anymore:

      "Principal": {
        "AWS": "arn:aws:iam::797873946194:root"
      }

So you need to normalise it, as well as use the full ARN.

@coen-hyde
Copy link

@leeprovoost I'm not sure it's a good idea to normalize data, as opposed to structure. In the scenario you describe I think the user should update the principle to be the arn.

@vancluever
Copy link
Contributor Author

Guys,

I can see points in both. I actually never knew the bareword account ID was legit as an ARN. That's quite silly IMO, but if that's what happens and it's documented like that by Amazon then it might be something that might need to be done. On the other hand, I think being as explicit as possible is optimal here.

I'm torn, but if I revisit this soon then I'll try and get an opinion from Amazon on it, via ticket or whatever. Maybe they can give some insight on what else we can expect too.

Otherwise, not too sure what the status of this is, it's been open for a while - paging @radeksimko @catsby

@coen-hyde
Copy link

I think this should be shipped now, and re-evaluate the data normalization after. Getting the structure normalized will provide a lot of value to people who use Terraform. If it is decided that data normalization is a good idea, then it can be added. In the mean time we have 80% of silly policy conflicts covered.

@vancluever vancluever force-pushed the paybyphone_s3_bucket_policy_normalize branch from 74b3889 to f5181e5 Compare April 7, 2016 19:46
@vancluever
Copy link
Contributor Author

Yeah, it would be nice to see it merged. I'm still using it in our own custom fork. On that note, I've given this branch a rebase so that it's easier to merge. Would love to see it upstream!

@leeprovoost
Copy link
Contributor

I don't feel very strongly about one or the other. Maybe we can resolve it by adding it in the Terraform documentation to avoid that future users are bumping into the same problem?

@apparentlymart apparentlymart removed the waiting-response An issue/pull request is waiting for a response from the community label Apr 17, 2016
@vancluever vancluever force-pushed the paybyphone_s3_bucket_policy_normalize branch 2 times, most recently from 75b9849 to c2b293f Compare April 20, 2016 17:03
@vancluever vancluever force-pushed the paybyphone_s3_bucket_policy_normalize branch from c2b293f to 6b4fb5d Compare April 29, 2016 16:18
 * normalizePolicyDocument function designed to be used as a
   StateFunc function that can be used to match IAM policy with
   what AWS sets.
@vancluever vancluever force-pushed the paybyphone_s3_bucket_policy_normalize branch from 6b4fb5d to ccf7dc5 Compare May 16, 2016 20:36
@vancluever
Copy link
Contributor Author

Everyone - seeing as #6881 is nearing merge, I'm thinking about closing this (would be one less branch I'd need to maintain). Before I do that and drop it though is there anything that we need out of it? One thing I did take a quick look at and noticed is that there does not seem to be any logic for handling single-element actions or principals (things that AWS will convert to strings versus keeping them as single element arrays).

@stack72
Copy link
Contributor

stack72 commented Jun 23, 2016

@jen20 thoughts on the questions @vancluever has above? It would be good to be able to close this PR

@stack72 stack72 added the waiting-response An issue/pull request is waiting for a response from the community label Jun 23, 2016
@vancluever
Copy link
Contributor Author

@stack72 - Just noticed this, wow!

Anyway, if any PR should be merged, I'd like it to be #6956, since this fixes it in a Terraform-ish way, and is easier to manage (in comparison, this PR is kind of hacky and we might be playing catch-up with cases that we have not 100% caught).

@stack72
Copy link
Contributor

stack72 commented Jun 30, 2016

ok @vancluever

thanks! shall we close this one out then?

@vancluever
Copy link
Contributor Author

@stack72 Sure! I'll do the honors ;)

@ghost
Copy link

ghost commented Apr 24, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug provider/aws waiting-response An issue/pull request is waiting for a response from the community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants