Skip to content

feat: support CodeArtifact domain permissions policies - #12

Merged
todpunk merged 2 commits into
catalystcommunity:mainfrom
DocMarten:feat/domain_policy
Oct 26, 2026
Merged

todpunk merged 2 commits into
catalystcommunity:mainfrom
DocMarten:feat/domain_policy

Conversation

@DocMarten

Copy link
Copy Markdown
Contributor

Summary

Add optional support for managing a CodeArtifact domain permissions policy.

Callers can pass a complete JSON policy document through
codeartifact_domain_permissions_policy. When omitted, the module does not
create a policy, preserving existing behavior.

Example

module "codeartifact" {
  source = "github.com/catalystcommunity/terraform-aws-codeartifact"

  codeartifact_domain_name = "mydomain"

  codeartifact_domain_permissions_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect = "Allow"
      Principal = {
        AWS = "arn:aws:iam::123456789012:role/package-reader"
      }
      Action = [
        "codeartifact:GetAuthorizationToken",
        "codeartifact:ReadFromRepository",
      ]
      Resource = "*"
    }]
  })
}

@DocMarten
DocMarten requested a review from a team as a code owner October 14, 2026 12:25
@DocMarten

Copy link
Copy Markdown
Contributor Author

@benjyiw

@todpunk

todpunk commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Cool beans. Some notes:

Must fix:

  1. Use a bool to turn on the policy (main.tf:33). Right now the policy is created when its value is not null. If that value comes from something built in the same run, the plan can't finish. An enable_... bool fixes this and matches how the rest of the module works.
  2. Reword the README cross-account example (README.md:46). A domain policy can't give read access to repositories, so the example promises a little more than it can deliver.
  3. Fix the variable description (variables.tf:25). Setting the variable back to null deletes the policy. It doesn't just stop Terraform from managing it.
  4. Validate the input (variables.tf:24). An empty string gets through plan and only fails partway through apply. A simple JSON check fixes this.

Should fix:

  1. Recreate the policy when the domain is replaced (main.tf:35). This is rare, but replace_triggered_by covers it.
  2. Set domain_owner (main.tf:35). This only matters when the domain is in a different account.
  3. Possible follow-up (main.tf:32): cross-account reads also need a policy on each repository, which the module can't create yet.

@todpunk

todpunk commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

More verbose clanker version:

main.tf:33 — use a bool to turn on the policy

When the policy comes from something built in the same run (for example an
aws_iam_policy_document with a role ARN from the same config), it is not
known at plan time, so count can't be computed. A bool gate avoids that and
matches the other enable_* toggles in the module:

resource "aws_codeartifact_domain_permissions_policy" "domain" {
  count = var.enable_codeartifact_domain_permissions_policy ? 1 : 0

  domain          = local.domain
  domain_owner    = var.codeartifact_domain_owner
  policy_document = var.codeartifact_domain_permissions_policy

  lifecycle {
    precondition {
      condition     = var.codeartifact_domain_permissions_policy != null
      error_message = "Set var.codeartifact_domain_permissions_policy when var.enable_codeartifact_domain_permissions_policy is true."
    }

    # The domain is found by name only, so a replaced domain does not show a diff here.
    replace_triggered_by = [aws_codeartifact_domain.domain]
  }
}

This also covers two smaller things:

  • replace_triggered_by: if the domain is replaced (for example after a KMS
    key change), the policy is recreated with it. Without it, the policy goes
    away with the old domain and Terraform sees no diff.
  • domain_owner: needed when enable_codeartifact_domain = false and the
    domain is in another account. It defaults to the current account, so it does
    nothing for existing users.

Optional: move this block below the locals { domain } block, so local.domain
is defined before it is used.

variables.tf:24-25 — description and validation

Setting the variable back to null deletes the policy in AWS, so "unmanaged"
is a bit misleading. An empty string also passes plan and only fails partway
through apply. Suggest:

variable "enable_codeartifact_domain_permissions_policy" {
  description = "Whether to manage a permissions policy on the CodeArtifact domain. Changing this from true to false deletes the policy"
  type        = bool
  default     = false
}

variable "codeartifact_domain_permissions_policy" {
  description = "JSON policy document to apply to the CodeArtifact domain if var.enable_codeartifact_domain_permissions_policy is enabled"
  type        = string
  default     = null

  validation {
    condition     = var.codeartifact_domain_permissions_policy == null || can(jsondecode(var.codeartifact_domain_permissions_policy))
    error_message = "The codeartifact_domain_permissions_policy value must be a valid JSON policy document."
  }
}

variable "codeartifact_domain_owner" {
  description = "Account ID that owns the CodeArtifact domain. Defaults to the current account"
  type        = string
  default     = null
}

README.md:44-78 — cross-account example

GetRepositoryEndpoint and ReadFromRepository are repository actions, so a
domain policy alone does not let the other account read packages. That also
needs a repository permissions policy. Suggest keeping the example to what the
domain policy can do:

### Cross-account domain access

Set `enable_codeartifact_domain_permissions_policy` to `true` and give the
domain permissions policy as a JSON string. This example lets a role in a
different account get a CodeArtifact authorization token for the domain:

```terraform
module "codeartifact" {
  source = "github.com/catalystcommunity/terraform-aws-codeartifact"

  codeartifact_domain_name                      = "mydomain"
  enable_codeartifact_domain_permissions_policy = true
  codeartifact_domain_permissions_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect = "Allow"
      Principal = {
        AWS = "arn:aws:iam::123456789012:role/codeartifact-consumer"
      }
      Action   = ["codeartifact:GetAuthorizationToken"]
      Resource = "*"
    }]
  })
}
```

To read packages, the role also needs a repository permissions policy
(`aws_codeartifact_repository_permissions_policy`) on each repository. This
module does not create repository permissions policies.

The identity policy of the role must also allow the CodeArtifact actions that
the role uses. It must also allow `sts:GetServiceBearerToken` for
`codeartifact.amazonaws.com`. A domain permissions policy cannot give
`sts:GetServiceBearerToken`.

Also, after these changes, regenerate the terraform-docs table. The current one
has <br> in place of the <br/> that is on main.

@todpunk

todpunk commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

@DocMarten if you want a more immediate response from me, feel free to ping me in Discord, but I will try to remember to check this PR periodically as well. Github notifications are awful and I'll entirely miss them most of the time in the sea of noise.

Add an explicit policy enable flag, domain owner support, and JSON validation.
Recreate the policy when its domain is replaced, and clarify cross-account
access requirements in the README.
@DocMarten

Copy link
Copy Markdown
Contributor Author

@todpunk
Thanks for the thorough review.

I’ve updated the PR to:
• Add an enable flag so the policy resource count is known at plan time.
• Validate the policy JSON and fail with a clear precondition if the policy is enabled without a document.
• Add domain_owner support and recreate the policy when the domain is replaced.
• Clarify that disabling policy management deletes the policy.
• Update the README example to cover domain authorization only, and explain that package reads also need repository policies.
• Regenerate the terraform-docs tables.
I also raised the Terraform minimum to 1.3.0 to match the optional object attributes already used by the module.

@todpunk

todpunk commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

lol, old Actions stuff in the way. I'm not going to move this to Reactorcide at this activity level, the validation I ran locally is enough. Merging.

@todpunk
todpunk merged commit 10bf841 into catalystcommunity:main Oct 26, 2026
1 of 3 checks passed
@todpunk

todpunk commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Release action also failed because it's just ancient and stupid, so I manually did it. If there's actual interest in active development next time we can convert to Reactorcide. For now, this is fine, I believe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants