Skip to content

Commit

Permalink
Merge pull request #1 from krixapolinario/iac
Browse files Browse the repository at this point in the history
Iac
  • Loading branch information
krixapolinario authored Nov 5, 2024
2 parents 4ee8af4 + bd96e19 commit 4ed4693
Show file tree
Hide file tree
Showing 10 changed files with 601 additions and 0 deletions.
155 changes: 155 additions & 0 deletions .github/workflows/iac.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
name: IaC

on:
push:
branches:
- main

permissions:
contents: read

jobs:
ansible-lint:
name: Ansible Lint
runs-on: ubuntu-latest
steps:
- name: Checkout source code
uses: actions/checkout@v4
- name: Run ansible-lint
uses: ansible/ansible-lint@main
with:
working_directory: ./ansible

terraform-lint:
name: Terraform Lint
runs-on: ubuntu-latest
defaults:
run:
shell: bash
working-directory: ./terraform
steps:
- name: Checkout source code
uses: actions/checkout@v4
- name: Setup TFLint
uses: terraform-linters/setup-tflint@v4
with:
tflint_version: v0.52.0
- name: Show version
run: tflint --version
- name: Init TFLint
run: tflint --init
- name: Run TFLint
run: tflint -f compact

terraform-sec:
name: Terraform Security
runs-on: ubuntu-latest
needs:
- terraform-lint
steps:
- name : Check out Git Repository
uses: actions/checkout@v4
- name: Run Tfsec
uses: aquasecurity/[email protected]

sonarcloud:
name: SonarCloud
runs-on: ubuntu-latest
needs:
- ansible-lint
- terraform-lint
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

terraform:
name: Terraform
runs-on: ubuntu-latest
environment: production
needs:
- ansible-lint
- terraform-lint
- sonarcloud
defaults:
run:
shell: bash
working-directory: ./terraform
outputs:
instance_nat_ip: ${{ steps.instance_inventory.outputs.instance_nat_ip }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup SSH
shell: bash
run: |
eval `ssh-agent -s`
mkdir -p /home/runner/.ssh/
touch /home/runner/.ssh/ecdsa_ansible.pub
echo -e "${{ secrets.ANSIBLE_SSH_PUB }}" > /home/runner/.ssh/ecdsa_ansible.pub
chmod 644 /home/runner/.ssh/ecdsa_ansible.pub
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
- name: Terraform Init
run: terraform init
env:
GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
TF_VAR_google_project_number: ${{ secrets.GOOGLE_PROJECT_NUMBER }}
- name: Terraform Validate
run: terraform validate
env:
GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
TF_VAR_google_project_number: ${{ secrets.GOOGLE_PROJECT_NUMBER }}
- name: Terraform Plan
run: terraform plan -input=false
env:
GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
TF_VAR_google_project_number: ${{ secrets.GOOGLE_PROJECT_NUMBER }}
- name: Terraform Apply
run: terraform apply -auto-approve -input=false
env:
GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
TF_VAR_google_project_number: ${{ secrets.GOOGLE_PROJECT_NUMBER }}
- name: Terraform Output
id: instance_inventory
run: echo "instance_nat_ip=$(terraform output instance_nat_ip)" >> "$GITHUB_OUTPUT"
env:
GOOGLE_CREDENTIALS: ${{ secrets.GOOGLE_CREDENTIALS }}
TF_VAR_google_project_number: ${{ secrets.GOOGLE_PROJECT_NUMBER }}

ansible:
name: Ansible
runs-on: ubuntu-latest
environment: production
needs:
- terraform
defaults:
run:
shell: bash
working-directory: ./ansible
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup SSH
shell: bash
run: |
eval `ssh-agent -s`
mkdir -p /home/runner/.ssh/
touch /home/runner/.ssh/ecdsa_ansible
echo -e "${{ secrets.ANSIBLE_SSH }}" > /home/runner/.ssh/ecdsa_ansible
chmod 700 /home/runner/.ssh/ecdsa_ansible
- name: Create Ansible Inventory
run: |
echo "lab-sast ansible_port=22 ansible_host=${INSTANCE_NAT_IP} ansible_user=ansible ansible_ssh_private_key_file=/home/runner/.ssh/ecdsa_ansible" >> ./inventory.yml
cat ./inventory.yml
env:
INSTANCE_NAT_IP: ${{ needs.terraform.outputs.instance_nat_ip }}
- name: Run Ansible Playbook
env:
ANSIBLE_HOST_KEY_CHECKING: False
run: |
ansible-playbook -i ./inventory.yml main.yml
201 changes: 201 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

credential.json
*/credential.json

# Local .terraform directories
**/.terraform*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
# *.tfvars
*.tfvars.json

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc
*/.terraform.lock.hcl
.terraform.lock.hcl
.terraform/
Loading

0 comments on commit 4ed4693

Please sign in to comment.