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

fix: DBTP-1789 - Remove required pipelines config #338

Merged
merged 21 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d3b8ea3
Move checks to start of deploy script
JohnStainsby Feb 6, 2025
66f3a25
Move tools install into build phase
JohnStainsby Feb 6, 2025
cbe75a1
Clean up build log
JohnStainsby Feb 6, 2025
00e8d16
Add additional sleep after initiating deployment
JohnStainsby Feb 6, 2025
41e1b22
Merge branch 'main' of github.com:uktrade/terraform-platform-modules
JohnStainsby Feb 7, 2025
49b4a2c
Merge branch 'main' of github.com:uktrade/terraform-platform-modules
JohnStainsby Feb 7, 2025
116a694
Merge branch 'main' of github.com:uktrade/terraform-platform-modules
JohnStainsby Feb 11, 2025
318d047
Merge branch 'main' of github.com:uktrade/terraform-platform-modules
JohnStainsby Feb 11, 2025
ad11cbd
Fix conditional resources
JohnStainsby Feb 14, 2025
a5a29ca
Do image check regardless of ECR type; Remove unnecessary parameters
JohnStainsby Feb 14, 2025
c93750a
Formatting
JohnStainsby Feb 14, 2025
0905903
Merge branch 'main' of github.com:uktrade/terraform-platform-modules
JohnStainsby Feb 14, 2025
3f421b7
Merge branch 'main' into DBTP-1789-remove-required-pipelines-config
JohnStainsby Feb 14, 2025
d468293
Fix format for additional_ecr_repo for public and private repositories
JohnStainsby Feb 17, 2025
acbb634
Tighten artifact store IAM access
JohnStainsby Feb 18, 2025
d0e76e9
Refactor env var tests to test individual values
JohnStainsby Feb 18, 2025
2447e55
Duplicate test should be manual release pipeline test
JohnStainsby Feb 18, 2025
f5c3984
Refactor codebuild env var tests; Add missing resource tests
JohnStainsby Feb 18, 2025
aa584d6
Formatting
JohnStainsby Feb 19, 2025
56523e8
Merge branch 'main' of github.com:uktrade/terraform-platform-modules
JohnStainsby Feb 19, 2025
2b31cf1
Merge branch 'main' into DBTP-1789-remove-required-pipelines-config
JohnStainsby Feb 19, 2025
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
13 changes: 9 additions & 4 deletions codebase-pipelines/artifactstore.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ data "aws_iam_policy_document" "artifact_store_bucket_policy" {
statement {
effect = "Allow"
principals {
type = "AWS"
identifiers = [
for env in local.pipeline_environments :
"arn:aws:iam::${env.account}:role/${var.application}-${env.name}-codebase-pipeline-deploy"
type = "AWS"
identifiers = [for id in local.deploy_account_ids : "arn:aws:iam::${id}:root"]
}
condition {
test = "ArnLike"
values = [
for id in local.deploy_account_ids :
"arn:aws:iam::${id}:role/${var.application}-*-codebase-pipeline-deploy"
]
variable = "aws:PrincipalArn"
}
actions = [
"s3:*"
Expand Down
8 changes: 3 additions & 5 deletions codebase-pipelines/buildspec-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ phases:
- set -e

# Check if the specified image tag exists
- aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
- |
if ! aws ecr describe-images --repository-name "${REPOSITORY_NAME}" --image-ids "imageTag=${IMAGE_TAG}" > /dev/null 2>&1; then
echo "Error: image tag ${IMAGE_TAG} not found in repository ${REPOSITORY_NAME}"
if ! docker manifest inspect "${REPOSITORY_URL}:${IMAGE_TAG}" > /dev/null 2>&1; then
echo "Error: image tag ${IMAGE_TAG} not found in repository ${REPOSITORY_URL}"
exit 1
fi

Expand All @@ -31,9 +32,6 @@ phases:
exit 1
fi

# Extract timestamp from image config and check if it exists
- aws ecr get-login-password --region ${AWS_REGION} | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com

# Construct Slack message env vars
- SLACK_REF=$(regctl image config "${REPOSITORY_URL}:${IMAGE_TAG}" | jq -r '.config.Labels."uk.gov.trade.digital.build.timestamp"')
- |
Expand Down
4 changes: 0 additions & 4 deletions codebase-pipelines/codepipeline.tf
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ resource "aws_codepipeline" "codebase_pipeline" {
{ name : "ENVIRONMENT", value : stage.value.name },
{ name : "IMAGE_TAG", value : "#{variables.IMAGE_TAG}" },
{ name : "PIPELINE_EXECUTION_ID", value : "#{codepipeline.PipelineExecutionId}" },
{ name : "PREFIXED_REPOSITORY_NAME", value : local.prefixed_repository_name },
{ name : "REPOSITORY_URL", value : local.repository_url },
{ name : "REPOSITORY_NAME", value : local.ecr_name },
{ name : "SERVICE", value : action.value.name },
{ name : "SLACK_CHANNEL_ID", value : var.slack_channel, type : "PARAMETER_STORE" },
])
Expand Down Expand Up @@ -168,9 +166,7 @@ resource "aws_codepipeline" "manual_release_pipeline" {
{ name : "ENVIRONMENT", value : "#{variables.ENVIRONMENT}" },
{ name : "IMAGE_TAG", value : "#{variables.IMAGE_TAG}" },
{ name : "PIPELINE_EXECUTION_ID", value : "#{codepipeline.PipelineExecutionId}" },
{ name : "PREFIXED_REPOSITORY_NAME", value : local.prefixed_repository_name },
{ name : "REPOSITORY_URL", value : local.repository_url },
{ name : "REPOSITORY_NAME", value : local.ecr_name },
{ name : "SERVICE", value : action.value.name },
{ name : "SLACK_CHANNEL_ID", value : var.slack_channel, type : "PARAMETER_STORE" },
])
Expand Down
10 changes: 6 additions & 4 deletions codebase-pipelines/eventbridge.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ resource "aws_cloudwatch_event_target" "codepipeline" {
for_each = local.pipeline_map
rule = aws_cloudwatch_event_rule.ecr_image_publish[each.key].name
arn = aws_codepipeline.codebase_pipeline[each.key].arn
role_arn = aws_iam_role.event_bridge_pipeline_trigger.arn
role_arn = aws_iam_role.event_bridge_pipeline_trigger[""].arn
}

resource "aws_iam_role" "event_bridge_pipeline_trigger" {
for_each = toset(length(local.pipeline_map) > 0 ? [""] : [])
name = "${var.application}-${var.codebase}-event-bridge-pipeline-trigger"
assume_role_policy = data.aws_iam_policy_document.assume_event_bridge_policy.json
tags = local.tags
}

resource "aws_iam_role_policy" "event_bridge_pipeline_trigger" {
name = "event-bridge-access"
role = aws_iam_role.event_bridge_pipeline_trigger.name
policy = data.aws_iam_policy_document.event_bridge_pipeline_trigger.json
for_each = toset(length(local.pipeline_map) > 0 ? [""] : [])
name = "event-bridge-access"
role = aws_iam_role.event_bridge_pipeline_trigger[""].name
policy = data.aws_iam_policy_document.event_bridge_pipeline_trigger.json
}

data "aws_iam_policy_document" "assume_event_bridge_policy" {
Expand Down
70 changes: 38 additions & 32 deletions codebase-pipelines/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -73,32 +73,6 @@ data "aws_iam_policy_document" "ecr_access_for_codebuild_images" {
]
}

statement {
effect = "Allow"
actions = [
"ecr-public:DescribeImageScanFindings",
"ecr-public:GetLifecyclePolicyPreview",
"ecr-public:GetDownloadUrlForLayer",
"ecr-public:BatchGetImage",
"ecr-public:DescribeImages",
"ecr-public:ListTagsForResource",
"ecr-public:BatchCheckLayerAvailability",
"ecr-public:GetLifecyclePolicy",
"ecr-public:GetRepositoryPolicy",
"ecr-public:PutImage",
"ecr-public:InitiateLayerUpload",
"ecr-public:UploadLayerPart",
"ecr-public:CompleteLayerUpload",
"ecr-public:BatchDeleteImage",
"ecr-public:DescribeRepositories",
"ecr-public:ListImages"
]
resources = [
# We have to wildcard the repository name because we currently expect the repository URL and it's not possible to get the ARN from that
"arn:aws:ecr-public::${data.aws_caller_identity.current.account_id}:repository/*"
]
}

statement {
effect = "Allow"
actions = [
Expand All @@ -119,9 +93,40 @@ data "aws_iam_policy_document" "ecr_access_for_codebuild_images" {
"ecr:DescribeRepositories",
"ecr:ListImages"
]
resources = [
aws_ecr_repository.this.arn
]
resources = compact([
"arn:aws:ecr:${local.account_region}:repository/${local.ecr_name}",
local.additional_private_repo_arn
])
}

dynamic "statement" {
for_each = toset(local.is_additional_repo_public ? [""] : [])

content {
effect = "Allow"
actions = [
"ecr-public:DescribeImageScanFindings",
"ecr-public:GetLifecyclePolicyPreview",
"ecr-public:GetDownloadUrlForLayer",
"ecr-public:BatchGetImage",
"ecr-public:DescribeImages",
"ecr-public:ListTagsForResource",
"ecr-public:BatchCheckLayerAvailability",
"ecr-public:GetLifecyclePolicy",
"ecr-public:GetRepositoryPolicy",
"ecr-public:PutImage",
"ecr-public:InitiateLayerUpload",
"ecr-public:UploadLayerPart",
"ecr-public:CompleteLayerUpload",
"ecr-public:BatchDeleteImage",
"ecr-public:DescribeRepositories",
"ecr-public:ListImages"
]
resources = [
# We have to wildcard the repository name because we currently expect the repository URL and it's not possible to get the ARN from that
"arn:aws:ecr-public::${data.aws_caller_identity.current.account_id}:repository/*"
]
}
}
}

Expand Down Expand Up @@ -184,9 +189,10 @@ data "aws_iam_policy_document" "ecr_access_for_codebase_pipeline" {
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer"
]
resources = [
aws_ecr_repository.this.arn
]
resources = compact([
"arn:aws:ecr:${local.account_region}:repository/${local.ecr_name}",
local.additional_private_repo_arn
])
}
statement {
effect = "Allow"
Expand Down
13 changes: 6 additions & 7 deletions codebase-pipelines/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ locals {

account_region = "${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}"

ecr_name = "${var.application}/${var.codebase}"
prefixed_repository_name = "uktrade/${var.application}"
repository_url = "${data.aws_caller_identity.current.account_id}.dkr.ecr.${data.aws_region.current.name}.amazonaws.com/${local.ecr_name}"
ecr_name = "${var.application}/${var.codebase}"
private_repo_url = "${data.aws_caller_identity.current.account_id}.dkr.ecr.${data.aws_region.current.name}.amazonaws.com"
is_additional_repo_public = var.additional_ecr_repository != null ? strcontains(var.additional_ecr_repository, "public.ecr.aws") : false
additional_ecr_url = var.additional_ecr_repository != null ? local.is_additional_repo_public ? var.additional_ecr_repository : "${local.private_repo_url}/${var.additional_ecr_repository}" : null
repository_url = coalesce(local.additional_ecr_url, "${local.private_repo_url}/${local.ecr_name}")
additional_private_repo_arn = var.additional_ecr_repository != null && !local.is_additional_repo_public ? "arn:aws:ecr:${local.account_region}:repository/${var.additional_ecr_repository}" : ""

pipeline_branches = distinct([
for pipeline in var.pipelines : pipeline.branch if lookup(pipeline, "branch", null) != null
Expand All @@ -33,10 +36,6 @@ locals {
})
}

pipeline_environments = distinct(flatten([
for pipeline in local.pipeline_map : [for env in pipeline.environments : env]
]))

services = sort(flatten([
for run_group in var.services : [for service in flatten(values(run_group)) : service]
]))
Expand Down
Loading