Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Fixed IAM permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
robbytaylor committed Sep 24, 2019
1 parent 846456e commit 20e5e6c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 27 deletions.
1 change: 1 addition & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module "target" {
source = "./target-account"

artifact_bucket_arn = module.pipeline.artifact_bucket_arn
artifact_kms_key_arn = module.pipeline.artifact_kms_key_arn
bucket_name = var.target_bucket_name
codepipeline_role_arn = module.pipeline.codepipeline_role_arn

Expand Down
20 changes: 3 additions & 17 deletions pipeline-account/iam_policy_document.tf
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ data "aws_iam_policy_document" "codepipeline" {
statement {
actions = [
"s3:GetObject",
"s3:PutObject"
"s3:PutObject",
"s3:ListBucket"
]

resources = [
Expand All @@ -99,21 +100,6 @@ data "aws_iam_policy_document" "codepipeline" {
resources = ["*"]
}

statement {
effect = "Allow"

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

resources = [
"arn:aws:s3:::${var.target_bucket_name}/*",
"arn:aws:s3:::${var.target_bucket_name}"
]
}

statement {
effect = "Allow"

Expand All @@ -123,7 +109,7 @@ data "aws_iam_policy_document" "codepipeline" {
]

resources = [
var.target_kms_key_arn,
aws_kms_key.artifacts.arn,
]
}
}
8 changes: 4 additions & 4 deletions pipeline-account/output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ output "artifact_bucket_arn" {
value = aws_s3_bucket.artifact-bucket.arn
}

output "codepipeline_role_arn" {
value = aws_iam_role.codepipeline.arn
output "artifact_kms_key_arn" {
value = aws_kms_key.artifacts.arn
}

output "kms_policy" {
value = data.aws_iam_policy_document.kms-usage.json
output "codepipeline_role_arn" {
value = aws_iam_role.codepipeline.arn
}

output "pipeline_arn" {
Expand Down
17 changes: 17 additions & 0 deletions pipeline-account/s3_bucket.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,21 @@ resource "aws_s3_bucket" "artifact-bucket" {
versioning {
enabled = true
}

policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowRoleInTargetAccount",
"Effect": "Allow",
"Principal": {
"AWS": "${var.target_role_arn}"
},
"Action": "s3:*",
"Resource": "arn:aws:s3:::${var.artifact_bucket_name}/*"
}
]
}
POLICY
}
30 changes: 24 additions & 6 deletions target-account/iam_policy_document.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ data "aws_iam_policy_document" "kms-usage" {

principals {
type = "AWS"
identifiers = [var.codepipeline_role_arn]
identifiers = [aws_iam_role.target.arn]
}

resources = ["*"]
Expand All @@ -47,7 +47,7 @@ data "aws_iam_policy_document" "kms-usage" {

principals {
type = "AWS"
identifiers = [var.codepipeline_role_arn]
identifiers = [aws_iam_role.target.arn]
}

resources = ["*"]
Expand All @@ -65,11 +65,29 @@ data "aws_iam_policy_document" "target" {
effect = "Allow"

actions = [
"s3:PutObject"
"s3:PutObject",
"s3:PutObjectAcl",
"s3:PutObjectVersionAcl",
"s3:GetObject",
"s3:ListBucket"
]

resources = [
"arn:aws:s3:::${aws_s3_bucket.target.arn}/*"
aws_s3_bucket.target.arn,
"${aws_s3_bucket.target.arn}/*"
]
}

statement {
effect = "Allow"

actions = [
"kms:Encrypt",
"kms:Decrypt"
]

resources = [
"${aws_kms_key.target-key.arn}"
]
}

Expand All @@ -81,6 +99,7 @@ data "aws_iam_policy_document" "target" {
]

resources = [
var.artifact_bucket_arn,
"${var.artifact_bucket_arn}/*"
]
}
Expand All @@ -89,12 +108,11 @@ data "aws_iam_policy_document" "target" {
effect = "Allow"

actions = [
"kms:Encrypt",
"kms:Decrypt"
]

resources = [
"${aws_kms_key.target-key.arn}"
var.artifact_kms_key_arn
]
}
}
4 changes: 4 additions & 0 deletions target-account/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ variable "artifact_bucket_arn" {
type = "string"
}

variable "artifact_kms_key_arn" {
type = "string"
}

variable "bucket_name" {
type = "string"
}
Expand Down

0 comments on commit 20e5e6c

Please sign in to comment.