-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐️ terraform actions for plan and state files (#34)
Signed-off-by: Christoph Hartmann <[email protected]> Signed-off-by: Christoph Hartmann <[email protected]>
- Loading branch information
1 parent
5c3e909
commit 860172c
Showing
18 changed files
with
375 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.terraform/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.terraform/* | ||
plan | ||
plan.json | ||
.terraform.lock.hcl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
terraform { | ||
required_providers { | ||
github = { | ||
source = "integrations/github" | ||
version = "~> 4.0" | ||
} | ||
} | ||
} | ||
|
||
# Configure the GitHub Provider | ||
provider "github" {} | ||
|
||
|
||
resource "github_repository" "example" { | ||
name = "example" | ||
description = "My awesome codebase" | ||
|
||
visibility = "public" | ||
|
||
template { | ||
owner = "github" | ||
repository = "terraform-module-template" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
policies: | ||
- uid: terraform-plan-sample-policy | ||
name: Terraform Plan - GitHub | ||
version: "1.1.0" | ||
authors: | ||
- name: Mondoo, Inc. | ||
email: [email protected] | ||
specs: | ||
- title: GitHub Test | ||
asset_filter: | ||
query: | | ||
platform.name == "terraform-plan" | ||
terraform.plan.resourceChanges.any( providerName == "registry.terraform.io/integrations/github") | ||
scoring_queries: | ||
terraform-github-repo-visibility: null | ||
queries: | ||
- uid: terraform-github-repo-visibility | ||
title: Check GitHub repository visibility | ||
query: | | ||
terraform.plan.resourceChanges.where( type == "github_repository" && mode == "managed") { | ||
name == "example" | ||
change.after["visibility"] = "public" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.terraform/* | ||
.terraform.lock.hcl | ||
plan | ||
plan.json | ||
state.json | ||
terraform.tfstate | ||
terraform.tfstate.backup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/sh | ||
terraform init | ||
terraform plan -out plan | ||
terraform apply plan | ||
terraform show -json plan > plan.json | ||
terraform show -json terraform.tfstate > state.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
resource "null_resource" "ls" { | ||
provisioner "local-exec" { | ||
command = "ls" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
policies: | ||
- uid: terraform-state-sample-policy | ||
name: Terraform State - GitHub | ||
version: "1.1.0" | ||
authors: | ||
- name: Mondoo, Inc. | ||
email: [email protected] | ||
specs: | ||
- title: GitHub Test | ||
asset_filter: | ||
query: | | ||
platform.name == "terraform-state" | ||
scoring_queries: | ||
terraform-state-check-null-provider: null | ||
queries: | ||
- uid: terraform-state-check-null-provider | ||
title: Check null provider | ||
query: | | ||
terraform.state.rootModule.resources { providerName == "registry.terraform.io/hashicorp/null" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
name: Terraform Plan Scanning Tests | ||
on: | ||
pull_request: | ||
push: | ||
paths: | ||
- "action.yaml" | ||
- "terraform-plan/*" | ||
- "terraform-state/*" | ||
- ".github/test_files/**" | ||
branches: | ||
- "main" | ||
tags: ["v*.*.*"] | ||
|
||
jobs: | ||
terraform-tests: | ||
runs-on: ubuntu-latest | ||
name: Test Terraform scanning | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
# run terraform plan checks | ||
- uses: hashicorp/setup-terraform@v2 | ||
with: | ||
# super important setting, otherwise you cannot pipe terraform show -json | ||
# see https://stackoverflow.com/questions/66496105/how-can-i-remove-all-the-extraneous-output-from-redirected-output-in-github-acti | ||
terraform_wrapper: false | ||
- run: terraform init | ||
working-directory: ./.github/test_files/tfplan | ||
- name: Write terraform plan | ||
run: terraform plan -out plan | ||
working-directory: ./.github/test_files/tfplan | ||
- name: Export terraform plan to json | ||
shell: bash | ||
run: terraform show -json plan > plan.json | ||
working-directory: ./.github/test_files/tfplan | ||
- name: Upload terraform plan | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: terraform-plan.json | ||
path: .github/test_files/tfplan/plan.json | ||
- name: Scan Terraform Plan | ||
uses: ./terraform-plan | ||
with: | ||
service-account-credentials: ${{ secrets.MONDOO_SERVICE_ACCOUNT }} | ||
path: ".github/test_files/tfplan/plan.json" | ||
args: "--policy-bundle .github/test_files/tfplan/policy/policy.mql.yaml" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
name: Terraform State Scanning Tests | ||
on: | ||
pull_request: | ||
push: | ||
paths: | ||
- "action.yaml" | ||
- "terraform-plan/*" | ||
- "terraform-state/*" | ||
- ".github/test_files/**" | ||
branches: | ||
- "main" | ||
tags: ["v*.*.*"] | ||
|
||
jobs: | ||
terraform-tests: | ||
runs-on: ubuntu-latest | ||
name: Test Terraform scanning | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
# run terraform plan checks | ||
- uses: hashicorp/setup-terraform@v2 | ||
with: | ||
# super important setting, otherwise you cannot pipe terraform show -json | ||
# see https://stackoverflow.com/questions/66496105/how-can-i-remove-all-the-extraneous-output-from-redirected-output-in-github-acti | ||
terraform_wrapper: false | ||
- run: terraform init | ||
working-directory: ./.github/test_files/tfstate | ||
- name: Write terraform plan | ||
run: terraform plan -out plan | ||
working-directory: ./.github/test_files/tfstate | ||
- name: Export terraform plan to json | ||
shell: bash | ||
run: terraform show -json plan > plan.json | ||
working-directory: ./.github/test_files/tfstate | ||
- name: Export terraform state to json | ||
shell: bash | ||
run: | | ||
terraform apply plan | ||
terraform show -json terraform.tfstate > state.json | ||
working-directory: ./.github/test_files/tfstate | ||
- name: Upload terraform plan | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: terraform | ||
path: | | ||
.github/test_files/tfstate/plan.json | ||
.github/test_files/tfstate/state.json | ||
- name: Scan Terraform State | ||
uses: ./terraform-state | ||
with: | ||
service-account-credentials: ${{ secrets.MONDOO_SERVICE_ACCOUNT }} | ||
path: ".github/test_files/tfstate/state.json" | ||
args: "--policy-bundle .github/test_files/tfstate/policy/policy.mql.yaml" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Mondoo Terraform HCL Action | ||
|
||
A [GitHub Action](https://github.com/features/actions) for testing [HashiCorp Terraform](https://terraform.io) [HCL code]](https://www.terraform.io/language/syntax/configuration) for security misconfigurations. | ||
|
||
## Properties | ||
|
||
The Terraform Action has properties which are passed to the underlying image. These are passed to the action using `with`. | ||
|
||
| Property | Required | Default | Description | | ||
| ----------------------------- | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `args` | false | | Additional arguments to pass to Mondoo Client. | | ||
| `log-level` | false | info | Sets the log level: error, warn, info, debug, trace (default "info") | | ||
| `output` | false | compact | Set the output format for scan results: compact, yaml, json, junit, csv, summary, full, report (default "compact") | | ||
| `path` | true | | Path to the Terraform working directory. | | ||
| `score-threshold` | false | 0 | Sets the score threshold for scans. Scores that fall below the threshold will exit 1. (default "0" - job continues regardless of the score returned by a scan). | | ||
| `service-account-credentials` | true | | Base64 encoded [service account credentials](https://mondoo.com/docs/platform/service_accounts/#creating-service-accounts) used to authenticate with Mondoo Platform | | ||
|
||
## Scan Terraform working directory | ||
|
||
You can use the Action as follows: | ||
|
||
```yaml | ||
name: Mondoo Terraform scan | ||
on: | ||
push: | ||
paths: | ||
- "terraform/main.tf" | ||
jobs: | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: mondoohq/actions/terraform@main | ||
with: | ||
service-account-credentials: ${{ secrets.MONDOO_SERVICE_ACCOUNT }} | ||
path: terraform | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Mondoo Terraform Plan Action | ||
|
||
A [GitHub Action](https://github.com/features/actions) for testing [HashiCorp Terraform](https://terraform.io) code for security misconfigurations. Mondoo policies will verity [Terraform's HCL syntax]](https://www.terraform.io/language/syntax/configuration). | ||
|
||
## Properties | ||
|
||
The Terraform Action has properties which are passed to the underlying image. These are passed to the action using `with`. | ||
|
||
| Property | Required | Default | Description | | ||
| ----------------------------- | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `args` | false | | Additional arguments to pass to Mondoo Client. | | ||
| `log-level` | false | info | Sets the log level: error, warn, info, debug, trace (default "info") | | ||
| `output` | false | compact | Set the output format for scan results: compact, yaml, json, junit, csv, summary, full, report (default "compact") | | ||
| `path` | true | | Path to the Terraform working directory. | | ||
| `score-threshold` | false | 0 | Sets the score threshold for scans. Scores that fall below the threshold will exit 1. (default "0" - job continues regardless of the score returned by a scan). | | ||
| `service-account-credentials` | true | | Base64 encoded [service account credentials](https://mondoo.com/docs/platform/service_accounts/#creating-service-accounts) used to authenticate with Mondoo Platform | | ||
|
||
## Scan Terraform working directory | ||
|
||
You can use the Action as follows: | ||
|
||
```yaml | ||
name: Mondoo Terraform Plan scan | ||
on: | ||
push: | ||
paths: | ||
- "terraform/main.tf" | ||
jobs: | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: mondoohq/actions/terraform@main | ||
with: | ||
service-account-credentials: ${{ secrets.MONDOO_SERVICE_ACCOUNT }} | ||
path: terraform | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
name: "Mondoo Terraform Plan GitHub Action" | ||
description: "Scan HashiCorp Terraform Plan for misconfigurations with Mondoo" | ||
branding: | ||
icon: "shield" | ||
color: "purple" | ||
inputs: | ||
args: | ||
description: >- | ||
Additional arguments to pass to Mondoo Client. | ||
required: false | ||
log-level: | ||
description: >- | ||
Sets the log level: error, warn, info, debug, trace (default "info") | ||
default: info | ||
required: false | ||
output: | ||
description: >- | ||
Set the output format for scan results: compact, yaml, json, junit, csv, summary, full, report (default "compact") | ||
default: compact | ||
required: false | ||
path: | ||
description: Path to the Terraform working directory. | ||
required: true | ||
score-threshold: | ||
description: >- | ||
Sets the score threshold for scans. Scores that fall below the threshold will exit 1. (default "0" - job continues regardless of the score returned by a scan). | ||
default: "0" | ||
required: false | ||
service-account-credentials: | ||
description: "Base64 encoded service account credentials used to authenticate with Mondoo Platform" | ||
required: true | ||
runs: | ||
using: "composite" | ||
steps: | ||
- name: Install Mondoo Client | ||
shell: bash | ||
run: | | ||
echo Installing Mondoo Client... | ||
echo ${{ inputs.service-account-credentials }} | base64 -d > mondoo.json | ||
curl -sSL https://mondoo.com/install.sh | bash | ||
- name: Mondoo status | ||
shell: bash | ||
run: mondoo status --config mondoo.json | ||
- name: Scan Terraform | ||
shell: bash | ||
run: > | ||
mondoo scan terraform plan "${{ inputs.path }}" | ||
--log-level "${{ inputs.log-level }}" | ||
--output "${{ inputs.output }}" | ||
--score-threshold "${{ inputs.score-threshold }}" | ||
--config mondoo.json | ||
${{ inputs.args }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.