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

Adds concept of additional bucket policies #17

Merged
merged 9 commits into from
Feb 28, 2020

Conversation

asiegman
Copy link

@asiegman asiegman commented Aug 19, 2019

What

This allows a user of this module to specify additional arbitrary policies they would like to apply to the bucket.

Why

Currently, the allow_encrypted_uploads_only option uses the only attachment available for bucket policies on the s3 bucket. If a user needs to add their own policies, say to enable cross account access to the bucket, they cannot. This allows them to do that.

Example usage

data "aws_iam_policy_document" "additional_policies" {
  count = "${length(var.trusted_roles) > 0 ? 1 : 0}"

  statement {
    effect = "Allow"
    principals = {
      type = "AWS"
      identifiers = "${var.trusted_roles}"
    }

    actions = [
      "s3:Get*",
      "s3:Put*",
      "s3:List*"
    ]

    resources = [
      "${module.s3_bucket.bucket_arn}"
    ]
  }
}

module "s3_bucket" {
  source                       = "git::https://github.com/asiegman/terraform-aws-s3-bucket.git?ref=moar-bucket-policy-0.11"
  enabled                      = "true"
  versioning_enabled           = "true"
  kms_master_key_arn           = "${module.my_kms_key.key_arn}"
  sse_algorithm                = "aws:kms"
  allow_encrypted_uploads_only = "true"
  name                         = "${var.name}"
  stage                        = "${var.stage}"
  namespace                    = "${var.namespace}"
  additional_bucket_policies   = ["${data.aws_iam_policy_document.additional_policies.*.json}"]
}

Example Output:

# Cleaned up and redacted new policy output from terraform plan
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "DenyIncorrectEncryptionHeader",
      "Effect": "Deny",
      "Action": "s3:PutObject",
      "Resource": "arn:aws:s3:::example-staging-example/*",
      "Principal": "*",
      "Condition": {
        "StringNotEquals": {
          "s3:x-amz-server-side-encryption": [
            "aws:kms"
          ]
        }
      }
    },
    {
      "Sid": "DenyUnEncryptedObjectUploads",
      "Effect": "Deny",
      "Action": "s3:PutObject",
      "Resource": "arn:aws:s3:::example-staging-example/*",
      "Principal": "*",
      "Condition": {
        "Null": {
          "s3:x-amz-server-side-encryption": [
            "true"
          ]
        }
      }
    },
    {
      "Sid": "",
      "Effect": "Allow",
      "Action": [
        "s3:Put*",
        "s3:List*",
        "s3:Get*"
      ],
      "Resource": "arn:aws:s3:::example-staging-example",
      "Principal": {
        "AWS": "arn:aws:iam::331539668475:role/example-x-account-server-role"
      }
    }
  ]
}

@Nuru Nuru requested a review from aknysh August 19, 2019 15:44
variables.tf Outdated Show resolved Hide resolved
main.tf Outdated Show resolved Hide resolved
Copy link
Member

@aknysh aknysh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @asiegman
LGTM, just a few comments.

Also please rebuild README by executing these commands:

make init
make readme/deps
make readme

It will add the new variables and outputs to README.md automatically.

In general, any changes to README should be made in README.yaml (not in this case), and after that executing the commands above will rebuild README.yaml into README.md and add all new variables and outputs to README.md

thanks

asiegman and others added 4 commits August 19, 2019 10:58
@asiegman
Copy link
Author

@aknysh Thanks for the comments, agreed on both. Updated and READMEs generated.

@aknysh aknysh self-requested a review August 19, 2019 19:49
@@ -87,9 +87,14 @@ data "aws_iam_policy_document" "bucket_policy" {
}
}

module "aggregated_policy" {
source = "git::https://github.com/cloudposse/terraform-aws-iam-policy-document-aggregator.git?ref=tags/0.1.2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count = "${var.enabled == "true" ? 1 : 0}"

let's add count to this resource as well.
Because if var.enabled is set to false, all resources w/o a count=0 will still be created by terraform.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit tidier; done. No need for the state to have that intermediate resource if not needed.

bucket = "${join("", aws_s3_bucket.default.*.id)}"

policy = "${join("", data.aws_iam_policy_document.bucket_policy.*.json)}"
policy = "${module.aggregated_policy.result_document}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
policy = "${module.aggregated_policy.result_document}"
policy = "${join("", module.aggregated_policy.*.result_document)}"

because of the count, it's now a list, not a single item

aknysh
aknysh previously requested changes Aug 19, 2019
Copy link
Member

@aknysh aknysh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asiegman
just one minor addition

@asiegman
Copy link
Author

@aknysh it seems count is not valid on module statements?

https://travis-ci.org/cloudposse/terraform-aws-s3-bucket/builds/574024219#L245

Did I miss something?

@aknysh
Copy link
Member

aknysh commented Aug 20, 2019

@asiegman
sorry, I meant to add enabled to the module, but that module does not have it, so please discard my comment about the count

@maximmi maximmi dismissed aknysh’s stale review February 28, 2020 17:52

comments were addressed

@maximmi maximmi merged commit cc05728 into cloudposse:0.11/master Feb 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants