Skip to content

Commit

Permalink
Add policy pack - Enforce AWS ECR Repositories Are Private. Closes #876
Browse files Browse the repository at this point in the history
  • Loading branch information
vkumbha committed Oct 15, 2024
1 parent f736d81 commit 2c2ee29
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
102 changes: 102 additions & 0 deletions policy_packs/aws/ecr/enforce_private_repositories/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
categories: ["security"]
primary_category: "security"
---

# Enforce AWS ECR Repositories Are Private

Ensuring that Amazon Elastic Container Registry (ECR) repositories are not publicly accessible is crucial for maintaining the security of your container images. This policy pack enforces that all ECR repositories are private, preventing unauthorized access and potential security risks.

This [policy pack](https://turbot.com/guardrails/docs/concepts/policy-packs) helps you configure the following settings for AWS ECR:

- **Check** if any ECR repositories are publicly accessible.
- **Enforce** that new unapproved public repositories are deleted automatically.

**[Review policy settings →](https://hub.guardrails.turbot.com/policy-packs/aws_ecr_enforce_private_repositories/settings)**

## Getting Started

### Requirements

- [Terraform](https://developer.hashicorp.com/terraform/downloads)
- The following Guardrails mod must be installed:
- [@turbot/aws-ecr](https://hub.guardrails.turbot.com/mods/aws/mods/aws-ecr)

### Credentials

To create a policy pack through Terraform:

- Ensure you have `Turbot/Admin` permissions (or higher) in Guardrails
- [Create access keys](https://turbot.com/guardrails/docs/guides/iam/access-keys#generate-a-new-guardrails-api-access-key) in Guardrails

And then set your credentials:

```sh
export TURBOT_WORKSPACE=myworkspace.acme.com
export TURBOT_ACCESS_KEY=acce6ac5-access-key-here
export TURBOT_SECRET_KEY=a8af61ec-secret-key-here
```

Please see [Turbot Guardrails Provider authentication](https://registry.terraform.io/providers/turbot/turbot/latest/docs#authentication) for additional authentication methods.

## Usage

### Install Policy Pack

> [!NOTE]
> By default, installed policy packs are not attached to any resources.
>
> Policy packs must be attached to resources in order for their policy settings to take effect.
Clone:

```sh
git clone https://github.com/turbot/guardrails-samples.git
cd guardrails-samples/policy_packs/aws/ecr/enforce_private_repositories
```

Run the Terraform to create the policy pack in your workspace:

```sh
terraform init
terraform plan
```

Then apply the changes:

```sh
terraform apply
```

### Apply Policy Pack

Log into your Guardrails workspace and [attach the policy pack to a resource](https://turbot.com/guardrails/docs/guides/policy-packs#attach-a-policy-pack-to-a-resource).

If this policy pack is attached to a Guardrails folder, its policies will be applied to all accounts and resources in that folder. The policy pack can also be attached to multiple resources.

For more information, please see [Policy Packs](https://turbot.com/guardrails/docs/concepts/policy-packs).

### Enable Enforcement

> [!TIP]
> You can also update the policy settings in this policy pack directly in the Guardrails console.
>
> Please note your Terraform state file will then become out of sync and the policy settings should then only be managed in the console.
By default, the policies are set to `Check` in the pack's policy settings. To enable automated enforcements, you can switch these policies settings by adding a comment to the `Check` setting and removing the comment from one of the listed enforcement options:

```hcl
resource "turbot_policy_setting" "aws_ecr_public_repository_approved" {
resource = turbot_policy_pack.main.id
type = "tmod:@turbot/aws-ecr#/policy/types/publicRepositoryApproved"
# value = "Check: Approved"
value = "Enforce: Delete unapproved if new"
}
```

Then re-apply the changes:

```sh
terraform plan
terraform apply
```
5 changes: 5 additions & 0 deletions policy_packs/aws/ecr/enforce_private_repositories/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "turbot_policy_pack" "main" {
title = "Enforce AWS ECR Repositories Are Private"
description = "Ensures that AWS ECR repositories are not publicly accessible."
akas = ["aws_ecr_enforce_private_repositories"]
}
14 changes: 14 additions & 0 deletions policy_packs/aws/ecr/enforce_private_repositories/policies.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# AWS > ECR > Public Repository > Approved
resource "turbot_policy_setting" "aws_ecr_public_repository_approved" {
resource = turbot_policy_pack.main.id
type = "tmod:@turbot/aws-ecr#/policy/types/publicRepositoryApproved"
value = "Check: Approved"
# value = "Enforce: Delete unapproved if new"
}

# AWS > ECR > Public Repository > Approved > Usage
resource "turbot_policy_setting" "aws_ecr_public_repository_approved_usage" {
resource = turbot_policy_pack.main.id
type = "tmod:@turbot/aws-ecr#/policy/types/publicRepositoryApprovedUsage"
value = "Not approved"
}
11 changes: 11 additions & 0 deletions policy_packs/aws/ecr/enforce_private_repositories/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
terraform {
required_providers {
turbot = {
source = "turbot/turbot"
version = ">= 1.11.0"
}
}
}

provider "turbot" {
}

0 comments on commit 2c2ee29

Please sign in to comment.