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

feat(SSPROD-37154): Add EKS Onboarding Terraform module #52

Merged
merged 31 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
35a8cea
feat: Add EKS Onboarding Terraform module
alexeyovriakh Feb 7, 2024
a51ff0b
chore: Remove aws_eks_cluster resource block
alexeyovriakh Feb 7, 2024
4635274
chore: Fix Linter issues
alexeyovriakh Feb 8, 2024
1fe6ae3
chore: Remove debug output
alexeyovriakh Feb 8, 2024
674a818
docs: Update variable descriptions
alexeyovriakh Feb 8, 2024
9d2adad
fix: Make clusters var optional
alexeyovriakh Feb 8, 2024
50b8d65
docs: Add README
alexeyovriakh Feb 8, 2024
35252c7
refactor: Remove Sysdig CIDR check
alexeyovriakh Feb 8, 2024
90a7cb2
fix: Remove providers file
alexeyovriakh Feb 8, 2024
99a0b6e
feat: Onboard opted-in clusters
alexeyovriakh Feb 12, 2024
bbd4089
chore: Update formatting
alexeyovriakh Feb 12, 2024
9b14257
fix: Use role_name to create access entry
alexeyovriakh Feb 14, 2024
2e3afa8
chore: Update README to add role_name attribute info
alexeyovriakh Feb 19, 2024
e444dbe
chore: Remove outputs.tf
alexeyovriakh Feb 19, 2024
b5645e5
refactor: Rename main.tf to eks.tf
alexeyovriakh Feb 28, 2024
5dcf592
feat: Add ECR-related variables
alexeyovriakh Feb 28, 2024
bc7dbd7
feat: Add an ECR role to allow pulling images
alexeyovriakh Feb 28, 2024
d3e6472
docs: Update README
alexeyovriakh Feb 28, 2024
858878a
fix: Update the README title
alexeyovriakh Feb 28, 2024
4ffa1a0
docs: Minor update to the README
alexeyovriakh Feb 28, 2024
6c8c038
refactor: Add validation for deploy_global_resources variable
alexeyovriakh Feb 29, 2024
929f783
docs: Update README to clarify variables purpose
alexeyovriakh Feb 29, 2024
2bffcfc
chore: Update required flags in README
alexeyovriakh Feb 29, 2024
4250008
fix: Remove automatic onboarding for agentless-tagged clusters
alexeyovriakh Mar 6, 2024
1c9cade
fix: Remove awscc provider
alexeyovriakh Mar 7, 2024
f67af25
fix: Remove aws_eks_cluster data source
alexeyovriakh Mar 7, 2024
122da6c
chore: Fix eks.tf formatting
alexeyovriakh Mar 7, 2024
2c296f8
chore: Fix README anchor
alexeyovriakh Mar 7, 2024
789fae5
chore: Update the intro section in README
alexeyovriakh Mar 7, 2024
4b9a068
refactor: Remove count from data sources
alexeyovriakh Mar 12, 2024
4f69256
chore: Update ecr_role_name error message
alexeyovriakh Mar 12, 2024
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
61 changes: 61 additions & 0 deletions modules/services/eks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# AWS EKS Module

This module will grant Sysdig view-only access to the AWS EKS clusters specified in the `clusters` variable.

The following resource will be created in each EKS cluster:
- EKS access entry that assigns `AmazonEKSViewPolicy` to Sysdig's IAM principal
- IAM role that grants Sysdig permissions to pull ECR images

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.7 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.0 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_eks_access_entry](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eks_access_entry) | resource |
| [aws_eks_access_policy_association](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eks_access_policy_association) | resource |
| [aws_iam_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy_attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy_attachment) | resource |
| [aws_iam_policy_document](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="var_eks_role_name"></a> [eks_role_name](#var\_eks\_role\_name) | (Required) IAM role that Sysdig will assume to access the EKS clusters. Prerequisite: Before this module can be invoked, Sysdig's CSPM Terraform module needs to create this role. | `string` | | Yes |
| <a name="var_clusters"></a> [clusters](#var\_clusters) | (Required) List the clusters that Sysdig will scan. Please note that only clusters with authentication mode set to API or API_AND_CONFIG_MAP will be onboarded. | `set(string)` | | Yes |
| <a name="var_deploy_global_resources"></a> [deploy\_global\_resources](#var\_deploy\_global\_resources) | (Optional) Setting this field to 'true' creates an IAM role that allows Sysdig to pull ECR images in order to scan them. | `bool` | `false` | No |
| <a name="var_trusted_identity"></a> [trusted\_identity](#var\_trusted\_identity) | (Optional) This value should be provided by Sysdig. The field refers to Sysdig's IAM role that will be authorized to pull ECR images | `string` | | No |
| <a name="var_ecr_role_name"></a> [ecr_role_name](#var\_ecr\_role\_name) | (Optional) This value should be provided by Sysdig. The field refers to an installation name, which will also be used to name the IAM role that grants access to pull ECR images | `string` | | No |
| <a name="var_external_id"></a> [external\_id](#var\_external\_id) | (Optional) This value should be provided by Sysdig. External ID is optional information that you can use in an IAM role trust policy to designate who in Sysdig can assume the role | `string` | | No |
| <a name="var_tags"></a> [tags](#var\_tags) | (Optional) This value should be provided by Sysdig. Tags that will be associated with the IAM role. | `map(string)` | <pre>{ "product": "sysdig-secure-for-cloud" }</pre> | No |

## Outputs

No outputs.
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

## Authors

Module is maintained by [Sysdig](https://sysdig.com).

## License

Apache 2 Licensed. See LICENSE for full details.

1 change: 1 addition & 0 deletions modules/services/eks/datasources.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data "aws_caller_identity" "current" {}
16 changes: 16 additions & 0 deletions modules/services/eks/eks.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
resource "aws_eks_access_entry" "viewer" {
for_each = var.clusters
cluster_name = each.value
principal_arn = local.principal_arn // TODO: Use data source
type = "STANDARD"
}

resource "aws_eks_access_policy_association" "viewer" {
for_each = var.clusters
cluster_name = each.value
policy_arn = local.policy_arn
principal_arn = local.principal_arn // TODO: Use data source
access_scope {
type = "cluster"
}
}
68 changes: 68 additions & 0 deletions modules/services/eks/iam.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// This is a Single Account installation. The resources are created globally (instead of regionally).
data "aws_iam_policy_document" "ecr_pull_image" {
statement {
sid = "SysdigEcrPullImagePermissions"

effect = "Allow"

actions = [
"ecr:GetDownloadUrlForLayer",
"ecr:BatchGetImage",
"ecr:BatchCheckLayerAvailability",
"ecr:ListImages",
"ecr:GetAuthorizationToken",
]

resources = [
"*",
]
}
}

resource "aws_iam_policy" "ecr_pull_image" {
count = local.n

name = var.ecr_role_name
description = "Allows Sysdig Secure to pull ECR images"
policy = data.aws_iam_policy_document.ecr_pull_image[0].json
tags = var.tags
}

data "aws_iam_policy_document" "ecr_assume_role" {
statement {
sid = "SysdigEcrAssumeRole"

actions = [
"sts:AssumeRole"
]

principals {
type = "AWS"
identifiers = [
var.trusted_identity,
]
}

condition {
test = "StringEquals"
variable = "sts:ExternalId"
values = [var.external_id]
}
}
}

resource "aws_iam_role" "ecr" {
count = local.n

name = var.ecr_role_name
tags = var.tags
assume_role_policy = data.aws_iam_policy_document.ecr_assume_role[0].json
}

resource "aws_iam_policy_attachment" "ecr" {
count = local.n

name = var.ecr_role_name
roles = [aws_iam_role.ecr[0].name]
policy_arn = aws_iam_policy.ecr_pull_image[0].arn
}
7 changes: 7 additions & 0 deletions modules/services/eks/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
locals {
account_id = data.aws_caller_identity.current.account_id
principal_arn = "arn:aws:iam::${local.account_id}:role/${var.eks_role_name}"
policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSViewPolicy"

n = var.deploy_global_resources ? 1 : 0
}
59 changes: 59 additions & 0 deletions modules/services/eks/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Values required to create access entries
variable "eks_role_name" {
description = "(Required) IAM role that Sysdig will assume to access the EKS clusters. Prerequisite: Before this module can be invoked, Sysdig's CSPM Terraform module needs to create this role."
type = string
}

variable "clusters" {
description = "(Optional) List the clusters that Sysdig will scan. Please note that only clusters with authentication mode set to API or API_AND_CONFIG_MAP will be onboarded."
type = set(string)
}

// Values required to create the ECR role
variable "deploy_global_resources" {
description = "(Optional) Setting this field to 'true' creates an IAM role that allows Sysdig to pull ECR images in order to scan them."
type = bool
default = false
}

variable "trusted_identity" {
type = string
description = "(Optional) This value should be provided by Sysdig. The field refers to Sysdig's IAM role that will be authorized to pull ECR images."
default = null
}

variable "ecr_role_name" {
description = "(Optional) This value should be provided by Sysdig. The field refers to an installation name, which will also be used to name the IAM role that grants access to pull ECR images."
type = string
default = null
}

variable "external_id" {
description = "(Optional) This value should be provided by Sysdig. External ID is optional information that you can use in an IAM role trust policy to designate who in Sysdig can assume the role."
type = string
default = null
}

variable "tags" {
type = map(string)
description = "(Optional) This value should be provided by Sysdig. Tags that will be associated with the IAM role."
default = {
"product" = "sysdig-secure-for-cloud"
}
}

output "validate_deploy_global_resources" {
value = null
precondition {
condition = (var.deploy_global_resources && var.external_id != null)
error_message = "Please provide external_id or set deploy_global_resources to false."
}
precondition {
condition = (var.deploy_global_resources && var.ecr_role_name != null)
error_message = "Please provide ecr_role_name or set deploy_global_resources set to false."
}
precondition {
condition = (var.deploy_global_resources && var.trusted_identity != null)
error_message = "Please provide trusted_identity or set deploy_global_resources to false."
}
}
10 changes: 10 additions & 0 deletions modules/services/eks/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = "~> 1.7"

required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
Loading