Skip to content
Merged
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b0d3a05
feat: Update IAM policy document to limit ECR permissions (DBTP-3170)
BenFielder Jul 7, 2026
551a9f4
Add role names for github actions and paketo
BenFielder Jul 9, 2026
2031ea4
Merge branch 'main' into rewrite-ecr-permissions
BenFielder Jul 9, 2026
00b0f0b
allow image build role to delete
DeveloperConnor Aug 18, 2026
ab3102f
merge main
DeveloperConnor Aug 18, 2026
7621040
Update policy to have conditional sections based on pipeline_mode
BenFielder Aug 19, 2026
415a71c
Change name to match original file
BenFielder Aug 19, 2026
e83b4f0
Merge branch 'main' into rewrite-ecr-permissions
BenFielder Aug 19, 2026
e188d4a
Updates based on PR feedback
BenFielder Aug 19, 2026
f5d0aa9
Run tf fmt
BenFielder Aug 19, 2026
6a7feaf
Update terraform/codebase-pipelines/ecr.tf
dragos441 Aug 19, 2026
35dc9da
Separate out pull permissions
BenFielder Aug 19, 2026
f480a3c
Move policy statements into no condition area
BenFielder Aug 19, 2026
cddda65
TF fmt
BenFielder Aug 19, 2026
67c3a0e
Update tests for new ECR policy
BenFielder Aug 19, 2026
26166eb
Change test condition star expected
BenFielder Aug 19, 2026
7421106
Update tests to read correct part of values
BenFielder Aug 19, 2026
8c05632
Fix issue with tests
BenFielder Aug 19, 2026
9090f33
tf fmt tests
BenFielder Aug 19, 2026
9a48388
fmt and fix test
BenFielder Aug 19, 2026
3db1cb3
try to recreate local var
BenFielder Aug 20, 2026
94fc9e3
fmt and add new test cases
BenFielder Aug 20, 2026
e1b23b3
Add spacing
BenFielder Aug 20, 2026
74da15f
Add override data to fix uneeded part on iam file
BenFielder Aug 20, 2026
963f1f9
Merge branch 'main' into rewrite-ecr-permissions
BenFielder Aug 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 123 additions & 29 deletions terraform/codebase-pipelines/ecr.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,13 @@ resource "aws_ecr_repository_policy" "ecr_policy" {
}

data "aws_iam_policy_document" "ecr_policy" {
statement {
effect = "Allow"

actions = [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:CompleteLayerUpload",
"ecr:GetDownloadUrlForLayer",
"ecr:InitiateLayerUpload",
"ecr:PutImage",
"ecr:UploadLayerPart"
]

principals {
type = "AWS"
identifiers = [
for id in local.deploy_account_ids :
"arn:aws:iam::${id}:root"
]
}
}

statement {
sid = "PreventRepoDelete"
effect = "Deny"

actions = [
"ecr:DeleteRepository"
]

principals {
type = "AWS"
identifiers = ["*"]
Expand All @@ -58,23 +35,140 @@ data "aws_iam_policy_document" "ecr_policy" {
statement {
sid = "PreventImageDelete"
effect = "Deny"

actions = [
"ecr:BatchDeleteImage"
]

principals {
type = "*"
identifiers = ["*"]
}

condition {
test = "ArnNotLike"
variable = "aws:PrincipalArn"
values = [

values = compact([
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/ecr-housekeeping-role",
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/github-oidc-${var.application}-repo-role"
contains(["dual_codepipeline_github", "github_actions"], var.pipeline_mode)
? "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/github-oidc-${var.application}-platform-image-build"
: ""
])
}
}

Comment thread
dragos441 marked this conversation as resolved.
dynamic "statement" {
for_each = var.pipeline_mode == "aws_codepipeline" ? [1] : []

content {
effect = "Allow"
actions = [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:CompleteLayerUpload",
"ecr:GetDownloadUrlForLayer",
"ecr:InitiateLayerUpload",
"ecr:PutImage",
"ecr:UploadLayerPart"
]
principals {
type = "AWS"
identifiers = [
for id in local.deploy_account_ids :
"arn:aws:iam::${id}:root"
]
}
}
}
}

# ==========================================
# Github Actions & Paketo/BYOD policy
# ==========================================

dynamic "statement" {
for_each = contains(["dual_codepipeline_github"], var.pipeline_mode) ? [1] : []

content {
sid = "BasicECRAccess"
effect = "Deny"
actions = [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:CompleteLayerUpload",
"ecr:GetDownloadUrlForLayer",
"ecr:InitiateLayerUpload",
"ecr:PutImage",
"ecr:UploadLayerPart"
]
principals {
type = "AWS"
identifiers = [
for id in local.deploy_account_ids : "arn:aws:iam::${id}:root"
]
}
condition {
test = "ArnNotLike"
variable = "aws:PrincipalArn"
values = flatten([
for id in local.deploy_account_ids : [
"arn:aws:iam::${id}:role/aws-reserved/sso.amazonaws.com/eu-west-2/AWSReservedSSO_AdministratorAccess_*",
"arn:aws:iam::${id}:role/github-oidc-${var.application}-platform-image-build",
"arn:aws:iam::${id}:role/${var.application}-${var.codebase}-codebase-image-build"
]
])
}
}
}

# ==========================================
# Github Actions & BYOD policy
# ==========================================

dynamic "statement" {
for_each = (var.pipeline_mode == "github_actions" && var.requires_image_build == false) ? [1] : []

content {
sid = "EnhancedRootPull"
effect = "Allow"
actions = [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer",
]
principals {
type = "AWS"
identifiers = [
for id in local.deploy_account_ids : "arn:aws:iam::${id}:root"
]
}
}
}

dynamic "statement" {
for_each = (var.pipeline_mode == "github_actions" && var.requires_image_build == false) ? [1] : []

content {
sid = "PushActions"
effect = "Deny"
actions = [
"ecr:CompleteLayerUpload",
"ecr:InitiateLayerUpload",
"ecr:PutImage",
"ecr:UploadLayerPart",
"ecr:BatchCheckLayerAvailability"
]
principals {
type = "AWS"
identifiers = ["*"]
}
condition {
test = "ArnNotLike"
variable = "aws:PrincipalArn"
values = flatten([
for id in local.deploy_account_ids : [
"arn:aws:iam::${id}:role/aws-reserved/sso.amazonaws.com/eu-west-2/AWSReservedSSO_AdministratorAccess_*",
"arn:aws:iam::${id}:role/github-oidc-${var.application}-repo-role"
Comment thread
dragos441 marked this conversation as resolved.
Outdated
]
])
}
}
}
}
Loading