|
| 1 | +############################################################################## |
| 2 | +############################################################################## |
| 3 | +# Create the ECR repository to store the ECS image(s) along with a lifecycle |
| 4 | +# policy. |
| 5 | +resource "aws_ecr_repository" "this" { |
| 6 | + #checkov:skip=CKV_AWS_51:We do not currently use releases for this, but may choose to turn this on in the future |
| 7 | + #checkov:skip=CKV_AWS_136:We do not store any private information in our images, encryption is unnecessary |
| 8 | + name = "${var.repo_name}-${var.environment}" |
| 9 | + image_scanning_configuration { |
| 10 | + scan_on_push = true |
| 11 | + } |
| 12 | + tags = var.tags |
| 13 | +} |
| 14 | + |
| 15 | +resource "aws_ecr_lifecycle_policy" "this" { |
| 16 | + #checkov:skip=CKV_AWS_136:We do not store any private information in our images, encryption is unnecessary |
| 17 | + #checkov:skip=CKV_AWS_163:We do not use image scanning by AWS right now |
| 18 | + #checkov:skip=CKV_AWS_51:We do not currently use releases for this, but may choose to turn this on in the future |
| 19 | + repository = aws_ecr_repository.this.name |
| 20 | + policy = jsonencode({ |
| 21 | + rules = [{ |
| 22 | + rulePriority = 1 |
| 23 | + description = "keep last 5 images" |
| 24 | + action = { |
| 25 | + type = "expire" |
| 26 | + } |
| 27 | + selection = { |
| 28 | + tagStatus = "any" |
| 29 | + countType = "imageCountMoreThan" |
| 30 | + countNumber = 5 |
| 31 | + } |
| 32 | + }] |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +### Read-write permissions ECR repository |
| 37 | +### Since IAM Policies are "global" we ONLY create the policy statement if this |
| 38 | +### module is called with the "default" us-east-1 AWS Provider so that we only |
| 39 | +### have one IAM Policy that is good for all regions. |
| 40 | +data "aws_iam_policy_document" "rw_this" { |
| 41 | + #checkov:skip=CKV_AWS_111:This policy needs unconstrained CreateRepository privileges |
| 42 | + #checkov:skip=CKV_AWS_356:This policy should allow "*" as a resource for restrictable actions |
| 43 | + count = data.aws_region.current.name == "us-east-1" ? 1 : 0 |
| 44 | + statement { |
| 45 | + actions = [ |
| 46 | + "ecr:CreateRepository", |
| 47 | + "ecr:GetAuthorizationToken", |
| 48 | + ] |
| 49 | + resources = ["*"] |
| 50 | + } |
| 51 | + statement { |
| 52 | + actions = [ |
| 53 | + "ecr:BatchCheckLayerAvailability", |
| 54 | + "ecr:BatchGetImage", |
| 55 | + "ecr:CompleteLayerUpload", |
| 56 | + "ecr:DeleteRepository", |
| 57 | + "ecr:DescribeImages", |
| 58 | + "ecr:DescribeImageScanFindings", |
| 59 | + "ecr:DescribeRepositories", |
| 60 | + "ecr:GetDownloadUrlForLayer", |
| 61 | + "ecr:GetLifecyclePolicy", |
| 62 | + "ecr:GetLifecyclePolicyPreview", |
| 63 | + "ecr:InitiateLayerUpload", |
| 64 | + "ecr:ListImages", |
| 65 | + "ecr:ListTagsForResource", |
| 66 | + "ecr:PutImage", |
| 67 | + "ecr:TagResource", |
| 68 | + "ecr:UntagResource", |
| 69 | + "ecr:UploadLayerPart", |
| 70 | + ] |
| 71 | + resources = [ |
| 72 | + "arn:aws:ecr:*:${data.aws_caller_identity.current.account_id}:repository/${aws_ecr_repository.this.name}" |
| 73 | + ] |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +resource "aws_iam_policy" "rw_this" { |
| 78 | + count = data.aws_region.current.name == "us-east-1" ? 1 : 0 |
| 79 | + name = "${var.repo_name}-ecr-rw-${var.environment}" |
| 80 | + description = "policy to allow read/write into ${var.repo_name} ECR" |
| 81 | + policy = data.aws_iam_policy_document.rw_this[0].json |
| 82 | + |
| 83 | + tags = var.tags |
| 84 | +} |
| 85 | + |
| 86 | +# The trust policy that allows GitHub Actions OIDC-based connections from the |
| 87 | +# repository. The definition is explicitly linked to the name of the GitHub repo. |
| 88 | +data "aws_iam_policy_document" "gh_trust" { |
| 89 | + count = data.aws_region.current.name == "us-east-1" ? 1 : 0 |
| 90 | + statement { |
| 91 | + actions = ["sts:AssumeRoleWithWebIdentity"] |
| 92 | + principals { |
| 93 | + type = "Federated" |
| 94 | + identifiers = [var.oidc_arn] |
| 95 | + } |
| 96 | + condition { |
| 97 | + test = "StringLike" |
| 98 | + variable = "token.actions.githubusercontent.com:sub" |
| 99 | + values = ["repo:${var.gh_organization}/${var.repo_name}:*"] # "repo:MITLibraries//<repo-name>:ref:refs/branch/${var.environment}" eventually |
| 100 | + } |
| 101 | + condition { |
| 102 | + test = "StringLike" |
| 103 | + variable = "token.actions.githubusercontent.com:aud" |
| 104 | + values = ["sts.amazonaws.com"] |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +# Role for GitHub Action OIDC connections from the lambdas repository |
| 110 | +resource "aws_iam_role" "gha_this" { |
| 111 | + count = data.aws_region.current.name == "us-east-1" ? 1 : 0 |
| 112 | + name = "${var.repo_name}-gha-${var.environment}" |
| 113 | + assume_role_policy = data.aws_iam_policy_document.gh_trust[0].json |
| 114 | + |
| 115 | + tags = var.tags |
| 116 | +} |
| 117 | + |
| 118 | +resource "aws_iam_role_policy_attachment" "gha_ecr_rw" { |
| 119 | + count = data.aws_region.current.name == "us-east-1" ? 1 : 0 |
| 120 | + role = aws_iam_role.gha_this[0].name |
| 121 | + policy_arn = aws_iam_policy.rw_this[0].arn |
| 122 | +} |
| 123 | +resource "aws_iam_role_policy_attachment" "gha_ecr_login" { |
| 124 | + count = data.aws_region.current.name == "us-east-1" ? 1 : 0 |
| 125 | + role = aws_iam_role.gha_this[0].name |
| 126 | + policy_arn = var.login_policy_arn |
| 127 | +} |
0 commit comments