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

[CES-155] Updated static_analysis.yaml with new action and pre-commit updated #1225

Merged
merged 26 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4f0ef00
feat: added test_user module and configured
mamu0 Oct 2, 2024
c719558
fix: minor
mamu0 Oct 2, 2024
8624e3e
Merge branch 'main' into add-test-users
mamu0 Oct 2, 2024
20e4806
fix: back to old test_users.tf
mamu0 Oct 2, 2024
4d83b38
Merge branch 'main' into add-test-users
mamu0 Oct 2, 2024
44c8ea9
Merge branch 'main' into add-test-users
mamu0 Oct 3, 2024
1e729f3
feat: updated static_analysis.yaml with new action
mamu0 Oct 3, 2024
e5e86af
fix: test new action
mamu0 Oct 3, 2024
325f55a
fix: minor
mamu0 Oct 3, 2024
4dfd371
Merge branch 'main' into CES-155-update-static-analysis-action
mamu0 Oct 3, 2024
e8155f6
Merge branch 'main' into add-test-users
mamu0 Oct 3, 2024
4f21239
fix: minor
mamu0 Oct 3, 2024
c849b48
chore: test changed lock files for each domain
mamu0 Oct 7, 2024
ae4f453
Merge branch 'main' into CES-155-update-static-analysis-action
mamu0 Oct 7, 2024
2db0ac5
Merge branch 'main' into add-test-users
mamu0 Oct 7, 2024
dde3c8e
chore: updated all lock files
mamu0 Oct 7, 2024
033f43c
chore: updated pre-commit
mamu0 Oct 7, 2024
aa22de2
fix: minor
mamu0 Oct 7, 2024
2fc911a
fix: minor
mamu0 Oct 7, 2024
4198160
feat: removed script and called hook
mamu0 Oct 7, 2024
cd68c72
fix: updated gitignore
mamu0 Oct 8, 2024
bbd02ca
fix: pre commit doc not in modules
mamu0 Oct 8, 2024
06c4c9b
fix: runned pre commit regenerated readme
mamu0 Oct 8, 2024
2ee19ae
fix: updated trivy ignore
mamu0 Oct 8, 2024
de54d4d
fix: added some tflint ignore
mamu0 Oct 8, 2024
897914d
fix: added some tflint ignore
mamu0 Oct 8, 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
97 changes: 7 additions & 90 deletions .github/workflows/static_analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,93 +14,10 @@ on:

jobs:
static_analysis:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: init_terraform_folders
run: |
pids=()

# map value with path to domain root and path to config entry point
declare -A newmap

newmap[src/aks-platform]="."
newmap[src/domains/cgn]="./prod"
newmap[src/domains/selfcare]="./prod/westeurope"
newmap[src/domains/citizen-auth-app]="."
newmap[src/domains/citizen-auth-common]="."
newmap[src/domains/ioweb-app]="."
newmap[src/domains/ioweb-common]="."
newmap[src/domains/elk]="."
newmap[src/domains/messages-app]="."
newmap[src/domains/messages-common]="."
newmap[src/domains/payments-app]="."
newmap[src/domains/payments-common]="."
newmap[src/domains/profile-app]="."
newmap[src/domains/profile-common]="."
newmap[src/domains/functions]="."
newmap[src/github-runner]="."
newmap[src/packer]="."

TAG=$(cat .terraform-version)

docker pull hashicorp/terraform:$TAG

for f in "${!newmap[@]}"; do
pushd "$f"
# get the folder name of the current module
module_path=$(basename "$f")

# replace '.' with empty string
relativePath="${newmap[$f]//./}"

if [[ -f "99_main.tf" ]]; then
sed -i -e 's/ backend "azurerm" {}//g' 99_main.tf # use local backend
elif [[ -f "main.tf" ]]; then
sed -i -e 's/ backend "azurerm" {}//g' main.tf # use local backend
elif [[ -f "$(pwd)/$relativePath/main.tf" ]]; then
sed -i -e '/backend "azurerm" {/,/}/d' $(pwd)/$relativePath/main.tf # use local backend

# explaination: https://github.com/pagopa/io-infra/pull/906

# pushd "_modules"
# modules=$(find . -type d)

# for module in modules; do
# folder_name=$(basename "$folder")
# echo "DEBUG - run docker in folder: $folder_name"
# docker run -v $(pwd):/tmp -w /tmp hashicorp/terraform:$TAG -chdir="./$folder_name" init &
# pids+=($!)
# done
# popd
fi

# initialize the current module (eg selfcare) from the root level of the module (eg src/domains/)
# this allows the import of modules present at the same level (eg tests)
docker run -v $(dirname $(pwd)):/tmp -w /tmp hashicorp/terraform:$TAG -chdir="$module_path/${newmap[$f]}" init &
pids+=($!)

popd

done

# Wait for each specific process to terminate.
# Instead of this loop, a single call to 'wait' would wait for all the jobs
# to terminate, but it would not give us their exit status.
#
for pid in "${pids[@]}"; do
#
# Waiting on a specific PID makes the wait command return with the exit
# status of that process. Because of the 'set -e' setting, any exit status
# other than zero causes the current shell to terminate with that exit
# status as well.
#
wait "$pid"
done

- name: run_pre_commit_terraform
run: |
TAG="v1.96.1@sha256:9aea677ac51d67eb96b3bbb4cf93b16afdde5476f984e75e87888850d18146c9"
docker run -v $(pwd):/lint -w /lint ghcr.io/antonbabenko/pre-commit-terraform:$TAG run -a
uses: pagopa/dx/.github/workflows/static_analysis.yaml@main
name: Terraform Validation
secrets: inherit
with:
terraform_version: "1.7.5"
mamu0 marked this conversation as resolved.
Show resolved Hide resolved
pre_commit_tf_tag: "v1.96.1@sha256:9aea677ac51d67eb96b3bbb4cf93b16afdde5476f984e75e87888850d18146c9"
enable_modified_files_detection: false
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ __TMP
.metals/
__azurite_*
/.idea

mamu0 marked this conversation as resolved.
Show resolved Hide resolved
# **/modules/**/.terraform.lock.hcl
# **/_modules/**/.terraform.lock.hcl
26 changes: 20 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
repos:
- repo: local
hooks:
- id: terraform-providers-lock-staged
name: Terraform Providers Lock (on staged .terraform.lock.hcl files)
entry: ./.utils/terraform_lock_precommit.sh
language: script
pass_filenames: false
verbose: true

- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.83.0
rev: v1.96.1
hooks:
- id: terraform_tflint
args:
- --args=--disable-rule terraform_required_version
- --args=--config=__GIT_WORKING_DIR__/.tflint.hcl
- id: terraform_fmt
- id: terraform_docs
name: terraform_docs on modules
args:
# - --hook-config=--create-file-if-not-exist=true
mamu0 marked this conversation as resolved.
Show resolved Hide resolved
- --args=--hide providers
- id: terraform_tfsec
args:
- --args=--exclude-downloaded-modules
- id: terraform_validate
exclude: '(\/_?modules\/.*)'
args:
- --tf-init-args=-lockfile=readonly
- --args=-json
- --args=-no-color
- --hook-config=--retry-once-with-cleanup=true
- id: terraform_trivy
args:
- --args=--skip-dirs="**/.terraform"
- --args=--ignorefile=__GIT_WORKING_DIR__/.trivyignore
18 changes: 18 additions & 0 deletions .tflint.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
config {
format = "default"
call_module_type = "local"
force = false
disabled_by_default = false
}

plugin "terraform" {
enabled = true
preset = "recommended"
}

# install the plugin by running 'tflint --init'
plugin "azurerm" {
enabled = true
version = "0.27.0"
source = "github.com/terraform-linters/tflint-ruleset-azurerm"
}
10 changes: 10 additions & 0 deletions .trivyignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# https://avd.aquasec.com/misconfig/azure/

# Github repository shouldn’t be public.
AVD-GIT-0001

# GitHub branch protection does not require signed commits.
AVD-GIT-0004

# The minimum TLS version for Storage Accounts should be TLS1_2
AVD-AZU-0011
29 changes: 29 additions & 0 deletions .utils/terraform_lock_precommit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# Find all .terraform.lock.hcl files that have been modified or added to the staging
staged_lock_files=$(git status --porcelain | grep '[AM][ ]*s.*.terraform.lock.hcl$' | awk '{print $2}')

if [[ -z "$staged_lock_files" ]]; then
echo "No .terraform.lock.hcl files to process."
exit 0
fi

# Run terraform providers lock in each directory containing a .terraform.lock.hcl file
for lockfile in $staged_lock_files; do
dir=$(dirname "$lockfile")

echo "Run terraform providers lock in: $dir"

# Go to the directory
cd "$dir" || exit

# Run terraform providers lock
terraform providers lock \
-platform=windows_amd64 \
-platform=darwin_amd64 \
-platform=darwin_arm64 \
-platform=linux_amd64

# Return to the previous directory
cd - > /dev/null
done
mamu0 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion src/domains/cgn/prod/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/domains/citizen-auth-common/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/domains/functions/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/domains/ioweb-app/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/domains/ioweb-common/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/domains/payments-app/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/github-runner/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/legacy-apim/prod/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading