Skip to content

Commit

Permalink
add optional policy allowing push access (#98)
Browse files Browse the repository at this point in the history
* add optional policy allowing push access

* Update main.tf

Co-authored-by: nitrocode <7775707+nitrocode@users.noreply.github.com>

* Update main.tf

* wrap override policies in distinct

* add lambda policy to push-only

* rebase

* Sync github (#104)

* Update main.tf

Co-authored-by: Matt Gowie  <matt@masterpoint.io>

* remove lambda permissions from push policy

---------

Co-authored-by: nitrocode <7775707+nitrocode@users.noreply.github.com>
Co-authored-by: Max Lobur <max_lobur@outlook.com>
Co-authored-by: Matt Gowie  <matt@masterpoint.io>
Co-authored-by: Matt Gowie <gowie.matt@gmail.com>
  • Loading branch information
5 people authored May 18, 2023
1 parent 80a016e commit d9effd7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ Available targets:
| [aws_iam_policy_document.empty](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.resource](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.resource_full_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.resource_push_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.resource_readonly_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |

Expand Down Expand Up @@ -207,6 +208,7 @@ Available targets:
| <a name="input_namespace"></a> [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no |
| <a name="input_principals_full_access"></a> [principals\_full\_access](#input\_principals\_full\_access) | Principal ARNs to provide with full access to the ECR | `list(string)` | `[]` | no |
| <a name="input_principals_lambda"></a> [principals\_lambda](#input\_principals\_lambda) | Principal account IDs of Lambdas allowed to consume ECR | `list(string)` | `[]` | no |
| <a name="input_principals_push_access"></a> [principals\_push\_access](#input\_principals\_push\_access) | Principal ARNs to provide with push access to the ECR | `list(string)` | `[]` | no |
| <a name="input_principals_readonly_access"></a> [principals\_readonly\_access](#input\_principals\_readonly\_access) | Principal ARNs to provide with readonly access to the ECR | `list(string)` | `[]` | no |
| <a name="input_protected_tags"></a> [protected\_tags](#input\_protected\_tags) | Name of image tags prefixes that should not be destroyed. Useful if you tag images with names like `dev`, `staging`, and `prod` | `set(string)` | `[]` | no |
| <a name="input_regex_replace_chars"></a> [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.<br>Characters matching the regex will be removed from the ID elements.<br>If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no |
Expand Down
2 changes: 2 additions & 0 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
| [aws_iam_policy_document.empty](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.resource](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.resource_full_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.resource_push_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.resource_readonly_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |

Expand Down Expand Up @@ -57,6 +58,7 @@
| <a name="input_namespace"></a> [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no |
| <a name="input_principals_full_access"></a> [principals\_full\_access](#input\_principals\_full\_access) | Principal ARNs to provide with full access to the ECR | `list(string)` | `[]` | no |
| <a name="input_principals_lambda"></a> [principals\_lambda](#input\_principals\_lambda) | Principal account IDs of Lambdas allowed to consume ECR | `list(string)` | `[]` | no |
| <a name="input_principals_push_access"></a> [principals\_push\_access](#input\_principals\_push\_access) | Principal ARNs to provide with push access to the ECR | `list(string)` | `[]` | no |
| <a name="input_principals_readonly_access"></a> [principals\_readonly\_access](#input\_principals\_readonly\_access) | Principal ARNs to provide with readonly access to the ECR | `list(string)` | `[]` | no |
| <a name="input_protected_tags"></a> [protected\_tags](#input\_protected\_tags) | Name of image tags prefixes that should not be destroyed. Useful if you tag images with names like `dev`, `staging`, and `prod` | `set(string)` | `[]` | no |
| <a name="input_regex_replace_chars"></a> [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.<br>Characters matching the regex will be removed from the ID elements.<br>If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no |
Expand Down
39 changes: 33 additions & 6 deletions main.tf
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
locals {
principals_readonly_access_non_empty = length(var.principals_readonly_access) > 0 ? true : false
principals_full_access_non_empty = length(var.principals_full_access) > 0 ? true : false
ecr_need_policy = length(var.principals_full_access) + length(var.principals_readonly_access) > 0 ? true : false
principals_readonly_access_non_empty = length(var.principals_readonly_access) > 0
principals_push_access_non_empty = length(var.principals_push_access) > 0
principals_full_access_non_empty = length(var.principals_full_access) > 0
ecr_need_policy = length(var.principals_full_access) + length(var.principals_readonly_access) + length(var.principals_push_access) > 0
}

locals {
Expand Down Expand Up @@ -160,7 +161,30 @@ data "aws_iam_policy_document" "resource_readonly_access" {
]
}
}
}

data "aws_iam_policy_document" "resource_push_access" {
count = module.this.enabled ? 1 : 0

statement {
sid = "PushAccess"
effect = "Allow"

principals {
type = "AWS"

identifiers = var.principals_push_access
}

actions = [
"ecr:CompleteLayerUpload",
"ecr:GetAuthorizationToken",
"ecr:UploadLayerPart",
"ecr:InitiateLayerUpload",
"ecr:BatchCheckLayerAvailability",
"ecr:PutImage",
]
}
}

data "aws_iam_policy_document" "resource_full_access" {
Expand Down Expand Up @@ -224,9 +248,12 @@ data "aws_iam_policy_document" "resource_full_access" {
}

data "aws_iam_policy_document" "resource" {
count = module.this.enabled ? 1 : 0
source_policy_documents = local.principals_readonly_access_non_empty ? [data.aws_iam_policy_document.resource_readonly_access[0].json] : [data.aws_iam_policy_document.empty[0].json]
override_policy_documents = local.principals_full_access_non_empty ? [data.aws_iam_policy_document.resource_full_access[0].json] : [data.aws_iam_policy_document.empty[0].json]
count = module.this.enabled ? 1 : 0
source_policy_documents = local.principals_readonly_access_non_empty ? [data.aws_iam_policy_document.resource_readonly_access[0].json] : [data.aws_iam_policy_document.empty[0].json]
override_policy_documents = distinct([
local.principals_push_access_non_empty ? data.aws_iam_policy_document.resource_push_access[0].json : data.aws_iam_policy_document.empty[0].json,
local.principals_full_access_non_empty ? data.aws_iam_policy_document.resource_full_access[0].json : data.aws_iam_policy_document.empty[0].json,
])
}

resource "aws_ecr_repository_policy" "name" {
Expand Down
Empty file modified outputs.tf
100755 → 100644
Empty file.
6 changes: 6 additions & 0 deletions variables.tf
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ variable "principals_full_access" {
default = []
}

variable "principals_push_access" {
type = list(string)
description = "Principal ARNs to provide with push access to the ECR"
default = []
}

variable "principals_readonly_access" {
type = list(string)
description = "Principal ARNs to provide with readonly access to the ECR"
Expand Down

0 comments on commit d9effd7

Please sign in to comment.