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

[#227] Migrate from tfsec to Trivy #262

Merged
merged 15 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
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
1 change: 0 additions & 1 deletion .github/wiki/Testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ npm run lint // to check linting

npm run lint:fix // to fix linting
```

11 changes: 11 additions & 0 deletions .github/wiki/Trivy-local-running.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
This project is using Trivy as a vulnerability scanner to replace the role of `tfsec` with some extra benefits:
1. Access to more languages and features in the same tool.
2. Access to more integrations with tools and services through the rich ecosystem around Trivy.

## Trivy Local Scan
```bash
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```bash
```bash

Minor markdown lint suggestion

# Project root directory
trivy config .
```

For more information, please refer to the [Trivy documentation](https://github.com/aquasecurity/trivy)
1 change: 1 addition & 0 deletions .github/wiki/_Sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
- [[Testing]]
- [[Modify the Infrastructure Diagram | Modify infra diagram]]
- [[Publishing]]
- [[Trivy Local Running]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- [[Trivy Local Running]]
- [[Trivy Local Run]]

or Running Trivy Locally? :)

15 changes: 5 additions & 10 deletions .github/workflows/test-generated-project.yml
Nihisil marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,12 @@ jobs:
- name: Generate project
run: . ./scripts/generateAdvancedAWS.sh

- name: Install Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: ${{ env.TERRAFORM_VERSION }}
- name: Install dependencies in .tool-versions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- name: Install dependencies in .tool-versions
- name: Install dependencies from .tool-versions

(we don't install the dependencies in the .tool-versions file, but we use the file to install the dependencies).

uses: asdf-vm/actions/install@v2

- name: Run Terraform format
run: terraform fmt -recursive -check

- name: Run tfsec linter
id: tfsec
uses: aquasecurity/[email protected]
with:
version: ${{ env.TFSEC_VERSION }}

- name: Run trivy linter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- name: Run trivy linter
- name: Run trivy scanner

According to the doc, Trivy is more referred to as a scanner, rather than a linter 💭

working-directory: aws-advanced-test
run: trivy config .
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ tsconfig.tsbuildinfo

# Emacs
.dir-locals.el

# Trivy
trivy-output.json
6 changes: 3 additions & 3 deletions src/generators/addons/aws/modules/alb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const albSGMainContent = dedent`
}
}

# tfsec:ignore:aws-ec2-no-public-ingress-sgr
# trivy:ignore:AVD-AWS-0107
resource "aws_security_group_rule" "alb_ingress_https" {
type = "ingress"
security_group_id = aws_security_group.alb.id
Expand All @@ -69,7 +69,7 @@ const albSGMainContent = dedent`
description = "From HTTPS to ALB"
}

# tfsec:ignore:aws-ec2-no-public-ingress-sgr
# trivy:ignore:AVD-AWS-0107
resource "aws_security_group_rule" "alb_ingress_http" {
type = "ingress"
security_group_id = aws_security_group.alb.id
Expand All @@ -80,7 +80,7 @@ const albSGMainContent = dedent`
description = "From HTTP to ALB"
}

# tfsec:ignore:aws-ec2-no-public-egress-sgr
# trivy:ignore:AVD-AWS-0104
resource "aws_security_group_rule" "alb_egress" {
type = "egress"
security_group_id = aws_security_group.alb.id
Expand Down
2 changes: 1 addition & 1 deletion src/generators/addons/aws/modules/ecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const ecsSGMainContent = dedent`
description = "From internal VPC to app"
}

# tfsec:ignore:aws-ec2-no-public-egress-sgr
# trivy:ignore:AVD-AWS-0104
resource "aws_security_group_rule" "ecs_fargate_egress_anywhere" {
type = "egress"
security_group_id = aws_security_group.ecs_fargate.id
Expand Down
1 change: 1 addition & 0 deletions src/generators/terraform/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('Core codebase', () => {
const expectedFiles = [
'.gitignore',
'.tool-versions',
'trivy.yaml',
'core/main.tf',
'core/outputs.tf',
'core/variables.tf',
Expand Down
4 changes: 2 additions & 2 deletions templates/addons/aws/modules/alb/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ locals {
enable_stickiness = false
}

# tfsec:ignore:aws-elb-alb-not-public
# trivy:ignore:AVD-AWS-0053
resource "aws_lb" "main" {
name = "${var.env_namespace}-alb"
internal = false
Expand Down Expand Up @@ -48,7 +48,7 @@ resource "aws_lb_target_group" "target_group" {
}
}

# tfsec:ignore:aws-elb-http-not-used
# trivy:ignore:AVD-AWS-0054
resource "aws_lb_listener" "app_http" {
load_balancer_arn = aws_lb.main.arn
port = "80"
Expand Down
2 changes: 1 addition & 1 deletion templates/addons/aws/modules/bastion/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# tfsec:ignore:aws-ec2-no-public-ip
# trivy:ignore:AVD-AWS-0009
resource "aws_launch_configuration" "bastion_instance" {
name_prefix = "${var.env_namespace}-bastion-"
image_id = var.image_id
Expand Down
2 changes: 1 addition & 1 deletion templates/addons/aws/modules/cloudwatch/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# tfsec:ignore:aws-cloudwatch-log-group-customer-key
# trivy:ignore:AVD-AWS-0017
resource "aws_cloudwatch_log_group" "main" {
name = "awslogs-${var.env_namespace}-log-group"
retention_in_days = var.log_retention_in_days
Expand Down
2 changes: 1 addition & 1 deletion templates/addons/aws/modules/ecr/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# tfsec:ignore:aws-ecr-enforce-immutable-repository tfsec:ignore:aws-ecr-repository-customer-key
# trivy:ignore:AVD-AWS-0031 trivy:ignore:AVD-AWS-0033
resource "aws_ecr_repository" "main" {
name = var.env_namespace

Expand Down
2 changes: 1 addition & 1 deletion templates/addons/aws/modules/ecs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ resource "aws_iam_policy" "ecs_task_execution_ssm" {
policy = local.ecs_task_execution_ssm_policy
}

# tfsec:ignore:aws-iam-no-policy-wildcards
# trivy:ignore:AVD-AWS-0057
resource "aws_iam_policy" "ecs_task_excution_service_scaling" {
name = "${var.env_namespace}-ECSAutoScalingPolicy"
policy = local.ecs_service_scaling_policy
Expand Down
8 changes: 4 additions & 4 deletions templates/addons/aws/modules/iam_groups/main.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#tfsec:ignore:aws-iam-enforce-group-mfa
# trivy:ignore:AVD-AWS-0123
resource "aws_iam_group" "admin" {
name = "${var.project_name}-admin-group"
}

#tfsec:ignore:aws-iam-enforce-group-mfa
# trivy:ignore:AVD-AWS-0123
resource "aws_iam_group" "infra-service-account" {
name = "${var.project_name}-infra-service-account-group"
}

#tfsec:ignore:aws-iam-enforce-group-mfa
# trivy:ignore:AVD-AWS-0123
resource "aws_iam_group" "developer" {
name = "${var.project_name}-developer-group"
}
Expand All @@ -19,7 +19,7 @@ resource "aws_iam_group_policy_attachment" "admin_access" {
}

# Policy from https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_aws_my-sec-creds-self-manage.html
# tfsec:ignore:aws-iam-no-policy-wildcards
nvminhtue marked this conversation as resolved.
Show resolved Hide resolved
# trivy:ignore:AVD-AWS-0057
resource "aws_iam_group_policy" "developer_allow_manage_own_credentials" {
group = aws_iam_group.developer.name
policy = local.allow_manage_own_credentials
Expand Down
2 changes: 1 addition & 1 deletion templates/addons/aws/modules/s3/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
data "aws_elb_service_account" "elb_service_account" {}

# tfsec:ignore:aws-s3-enable-versioning tfsec:ignore:aws-s3-enable-bucket-logging tfsec:ignore:aws-s3-encryption-customer-key tfsec:ignore:aws-s3-enable-bucket-encryption
# trivy:ignore:AVD-AWS-0089 trivy:ignore:AVD-AWS-0132 trivy:ignore:AVD-AWS-0088 trivy:ignore:AVD-AWS-0090
resource "aws_s3_bucket" "alb_log" {
bucket = "${var.env_namespace}-alb-log"
force_destroy = true
Expand Down
2 changes: 1 addition & 1 deletion templates/addons/aws/modules/vpc/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
data "aws_availability_zones" "available" {}

# tfsec:ignore:aws-ec2-require-vpc-flow-logs-for-all-vpcs tfsec:ignore:aws-ec2-no-public-ip-subnet
# trivy:ignore:AVD-AWS-0178 trivy:ignore:AVD-AWS-0164
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ name: Lint
on:
push:

env:
TERRAFORM_VERSION: "1.5.5"
TFSEC_VERSION: "v1.28.1"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
Expand All @@ -25,16 +21,11 @@ jobs:
with:
ref: ${{ github.head_ref }}

- name: Install Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: ${{ env.TERRAFORM_VERSION }}
- name: Install dependencies in .tool-versions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- name: Install dependencies in .tool-versions
- name: Install dependencies from .tool-versions

uses: asdf-vm/actions/install@v2

- name: Run Terraform format
run: terraform fmt -recursive -check

- name: Run tfsec linter
id: tfsec
uses: aquasecurity/[email protected]
with:
version: ${{ env.TFSEC_VERSION }}
- name: Run trivy linter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- name: Run trivy linter
- name: Run trivy scanner

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update them all in a518570

run: trivy config .
2 changes: 1 addition & 1 deletion templates/terraform/.tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
terraform 1.5.5
tfsec 1.28.1
trivy 0.47.0
3 changes: 3 additions & 0 deletions templates/terraform/gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ terraform.rc

# Emacs
.dir-locals.el

# Trivy
trivy-output.json
23 changes: 23 additions & 0 deletions templates/terraform/trivy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
timeout: 10m
dependency-tree: true
list-all-pkgs: true
exit-code: 1
# All severity levels
severity:
- HIGH
- CRITICAL
scan:
skip-dirs:
- .github/
- core/.terraform/
- shared/.terraform/

scanners:
- vuln
- secret

vulnerability:
type:
- os
- library
ignore-unfixed: true
Loading