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/andrews actions #67

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
dd4af40
Adding github-download-release composite action.
andrewhaller Dec 1, 2022
c3fc59a
Adding discover-changed.
andrewhaller Dec 1, 2022
d27f23e
Adding terraform-compliance-aws.
andrewhaller Dec 1, 2022
231ffbc
Adding terragrunt-setup.
andrewhaller Dec 1, 2022
2c44c6b
Adding terragrunt-run-all.
andrewhaller Dec 1, 2022
955708f
Adding terragrunt-run-all file contents
andrewhaller Dec 1, 2022
3217f61
Adding aws-account-number.
andrewhaller Dec 1, 2022
5e2e781
Implementing some suggested changes.
andrewhaller Dec 1, 2022
bffe4ab
Commenting out token input to test passing env from parent.
andrewhaller Dec 1, 2022
a7fbc40
Implementing more generic terragrunt path input.
andrewhaller Dec 1, 2022
fc1f7d5
Using curl to download asset, instead of gh cli.
andrewhaller Dec 2, 2022
307a950
Changing output name.
andrewhaller Dec 2, 2022
a2ecc8f
Using GITHUB_API_URL.
andrewhaller Dec 6, 2022
2219fdd
Updating directory checks in terragrunt-setup action.
andrewhaller Dec 7, 2022
b2a032d
Checking for empty input in terragrunt-setup.
andrewhaller Dec 7, 2022
d1e7cdb
Using token input.
andrewhaller Dec 7, 2022
58b6219
Removing terragrunt-run-all.
andrewhaller Dec 8, 2022
dd1f4a6
Changing output name.
andrewhaller Dec 8, 2022
0763bf1
Updating description.
andrewhaller Dec 9, 2022
9b6abaa
Adding discover-filename.
andrewhaller Dec 9, 2022
6843b21
Updating terraform-compliance-aws.
andrewhaller Dec 9, 2022
a057622
Changed bash var name for consistency.
andrewhaller Dec 9, 2022
679681a
Defining referenced var.
andrewhaller Dec 9, 2022
fd8b982
Wrapping printf array value in quotes.
andrewhaller Dec 9, 2022
f27b4a1
Wrapping printf array value in quotes.
andrewhaller Dec 9, 2022
ffa9141
Adding git-config-pat.
andrewhaller Dec 9, 2022
7d2808d
Terraform compliance docker.
andrewhaller Dec 9, 2022
39a2bbc
Removing aws-account-number.
andrewhaller Dec 9, 2022
15d2084
Renaming terraform-compliance-aws to terraform-compliance-terragrunt.
andrewhaller Dec 9, 2022
adaf88d
Adding entrypoint contents.
andrewhaller Dec 9, 2022
a686e09
Fixed RANDOM issue.
andrewhaller Dec 9, 2022
f04c0b3
Updating default input.
andrewhaller Dec 9, 2022
d214063
Updating default input.
andrewhaller Dec 10, 2022
54409b0
Checking if file exists.
andrewhaller Dec 10, 2022
22b9ecd
Using mv instead of cp for performance.
andrewhaller Dec 10, 2022
723049f
Updating description.
andrewhaller Dec 10, 2022
c9936a8
Adding composite version of action.
andrewhaller Dec 10, 2022
1a50998
Action updates.
andrewhaller Dec 13, 2022
6bd29a3
Use openssl rand to get replacement key.
andrewhaller Dec 13, 2022
2ab8bd3
Updating actions.
andrewhaller Jan 6, 2023
f649fdd
Referencing action from branch instead of local.
andrewhaller Jan 17, 2023
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
3 changes: 3 additions & 0 deletions discover-changed/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Discover Changed

TODO
37 changes: 37 additions & 0 deletions discover-changed/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Discover Changed Targets
description: Discovers a list of targets
inputs:
targets:
description: A list of target folders to check. This should be a newline-delimited string of targets.
required: true
outputs:
matrix:
description: A list of targets. This will be a JSON formatted array.
value: ${{ steps.set-matrix.outputs.matrix }}
runs:
using: composite
steps:
- name: Get Changed Files
id: files
uses: tj-actions/changed-files@v34
with:
files: ${{ inputs.targets }}

- name: List Changed Files
id: set-matrix
shell: bash
run: |
folders=()
files="${{ steps.files.outputs.all_modified_files }}"
targets=(${{ inputs.targets }})
printf "files: %s\n" "${files}"
printf "targets: %s\n" "${targets[@]}"
if [ ! -z "$files" ]; then
for file in $files[@]; do
folders+=($(echo $file | cut -d'/' -f2))
done
fi

output="$(echo "${folders[@]}" | tr ' ' '\n' | sort -u | xargs echo -n | jq -R -s -c 'split(" ")')"
echo "matrix=${output}"
echo "matrix=${output}" >> $GITHUB_OUTPUT
3 changes: 3 additions & 0 deletions discover-filename/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Discover Filename

TODO
35 changes: 35 additions & 0 deletions discover-filename/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Discover Filenames Targets
description: Discovers a list of targets
inputs:
targets:
description: A list of target folders to check. This should be a newline-delimited string of targets.
required: true
filename:
description: The filename to discover
required: true
outputs:
matrix:
description: A list of targets. This will be a JSON formatted array.
value: ${{ steps.set-matrix.outputs.matrix }}
runs:
using: composite
steps:
- name: Set Matrix
id: set-matrix
shell: bash
run: |
folders=()
targets=(${{ inputs.targets }})
printf "filename: %s\n" ${{ inputs.filename }}
printf "targets: %s\n" "${targets[@]}"
for target in "${targets[@]}"; do
files=()
cd $(dirname $target)
for file in $(find $(basename $target) -mindepth 1 -name ${{ inputs.filename }}); do
folders+=($(dirname $file))
done
done

output="$(echo "${folders[@]}" | tr ' ' '\n' | sort -u | xargs echo -n | jq -R -s -c 'split(" ")')"
echo "matrix=${output}"
echo "matrix=${output}" >> $GITHUB_OUTPUT
3 changes: 3 additions & 0 deletions git-config-pat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Configure PAT

TODO
23 changes: 23 additions & 0 deletions git-config-pat/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Git Config With PAT
description: Git config global for use with PAT
inputs:
token:
description: "GitHub API token. Used to query for pull request commits."
required: true
outputs:
gitconfig:
description: The path to the .gitconfig file being configured
value: ${{ steps.git-config.outputs.gitconfig }}
runs:
using: composite
steps:
- name: Git Config For Private Repo
shell: bash
id: git-config
env:
GH_PAT: ${{ inputs.token }}
run: |
git config --global url."https://${GITHUB_ACTOR}:${GH_PAT}@github.com".insteadOf ssh://[email protected]

echo "gitconfig=${HOME}/.gitconfig"
echo "gitconfig=${HOME}/.gitconfig" >> $GITHUB_OUTPUT
87 changes: 87 additions & 0 deletions github-download-release/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Github Download Release
description: Downloads the specified release using curl
inputs:
token:
steve-liatrio marked this conversation as resolved.
Show resolved Hide resolved
description: "GitHub API token. Used to query for pull request commits."
required: true
owner:
description: The repo owner
required: true
repo:
description: The repo name
required: true
release:
description: The release tag or number
required: true
format-ext:
description: The type of archive
required: false
default: linux_amd64.tar.gz
file:
description: The explicit filename to use ... format-ext will be ignored.
required: false
outputs:
filename:
description: The name of the downloaded tarball
value: ${{ steps.download-asset.outputs.filename }}
runs:
using: composite
steps:

- name: CURL Download Asset
shell: bash
id: download-asset
env:
GH_PAT: ${{ inputs.token }}
GITHUB_REPO: ${{ inputs.repo }}
GITHUB_REPO_OWNER: ${{ inputs.owner }}
RELEASE_NAME: ${{ inputs.release }}
DOWNLOAD_FORMAT_EXT: ${{ inputs.format-ext }}
FILE: ${{ inputs.file }}
run: |
function gh_curl_releases() {
local base_url="https://${GITHUB_API_URL}"
curl -H "Authorization: token ${GH_PAT}" -H "Accept: application/vnd.github.v3.raw" -o releases.tmp.json ${base_url}$@
}

function gh_curl_asset() {
local base_url="https://${GH_PAT}:@${GITHUB_API_URL}"
curl -sL -H "Authorization: token ${GH_PAT}" -H "Accept: application/octet-stream" ${base_url}$@
}

export RELEASE_TAG=$(echo "${RELEASE_NAME}" | sed s/^v//g)
export REPO="${GITHUB_REPO_OWNER}/${GITHUB_REPO}"
export FILE_DEFAULT="${GITHUB_REPO}_${RELEASE_TAG}_${DOWNLOAD_FORMAT_EXT}"
[ -z "$FILE" -o "${FILE}x" = "x" ] && export FILE="$FILE_DEFAULT"

if [ "$RELEASE_NAME" = "latest" ]; then
# Github should return the latest release first.
JQ_PARSER_ASSET_ID=".[0].assets | map(select(.name == \"$FILE\"))[0].id"
else
JQ_PARSER_ASSET_ID=". | map(select(.tag_name == \"$RELEASE_NAME\"))[0].assets | map(select(.name == \"$FILE\"))[0].id"
fi

printf "Getting asset id from '%s' ...\n" "https://${GITHUB_API_URL}/repos/${REPO}/releases"
gh_curl_releases "/repos/${REPO}/releases"
ASSET_ID=$(cat releases.tmp.json | jq "$JQ_PARSER_ASSET_ID")
if [ -z "$ASSET_ID" -o "$ASSET_ID" = "null" ]; then
echo "ERROR: version not found $RELEASE_NAME" >&2
exit 1
fi

printf "Getting asset '%s' ...\n" "$ASSET_ID"
URL_BASE_PATH="/repos/${REPO}/releases"
gh_curl_asset "/repos/${REPO}/releases/assets/${ASSET_ID}" > $FILE

ls -lah $FILE

echo "filename=${FILE}"
echo "filename=${FILE}" >> $GITHUB_OUTPUT

ASSET_URL="https://${GITHUB_API_URL}/repos/${REPO}/releases/assets/${ASSET_ID}"
echo "asset=${ASSET_URL}"
echo "asset=${ASSET_URL}" >> $GITHUB_OUTPUT

WEB_URL="https://github.com/${REPO}/releases/download/${RELEASE_NAME}/${FILE}"
echo "web=${WEB_URL}"
echo "web=${WEB_URL}" >> $GITHUB_OUTPUT
38 changes: 38 additions & 0 deletions replace-secret-and-base64-encode/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Replace Secret and Base64 Encode
description: Replaces the secret token and base64 encodes to string
inputs:
token:
description: The secret token to replace
required: true
file:
description: The name of the specific account to use
required: true
replace-key-entropy:
description: openssl rand -hex <entropy>
required: false
default: "8"
outputs:
base64-encoded:
description: The base64 encoded file with pat replaced
value: ${{ steps.encode.outputs.base64-encoded }}
replacement-key:
description: The pat replacement key
value: ${{ steps.encode.outputs.replacement-key }}

runs:
using: composite
steps:
- name: Base64 Encode File
shell: bash
env:
FILE_PATH: ${{ inputs.file }}
id: encode
run: |
big_rand="$(openssl rand -hex ${{ inputs.replace-key-entropy }})" || { print "There was a problem openssl. Exiting ...\n" >&2; exit 1; }
encoded=$(echo $(cat "${FILE_PATH}" | sed "s/${{ inputs.token }}/${big_rand}/g" | base64))

echo "base64-encoded=$(echo ${encoded})"
echo "base64-encoded=$(echo ${encoded})" >> $GITHUB_OUTPUT

echo "replacement-key=${big_rand}"
echo "replacement-key=${big_rand}" >> $GITHUB_OUTPUT
3 changes: 3 additions & 0 deletions terraform-compliance-terragrunt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Terraform Compliance with Terragrunt

TODO
77 changes: 77 additions & 0 deletions terraform-compliance-terragrunt/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: AWS Terraform Compliance
description: Checks terraform complicance with aws
inputs:
target-folder:
description: The target folder
required: true
subfolders:
description: A space delimited list of sub-folders with terragrunt.hcl files
required: false
default: ""
planfile:
description: Name of plan file
required: true
features:
description: Features dir or repo
required: false
default: git:https://github.com/terraform-compliance/user-friendly-features.git
options:
description: Optional parameters (https://terraform-compliance.com/pages/usage/)
required: false
default: |
-n
runs:
using: composite
steps:
- name: Install Terraform Compliance
shell: bash
run: pip install terraform-compliance

- name: Terraform Compliance
id: compliance
shell: bash
env:
TARGET_FOLDER: ${{ inputs.target-folder }}
SUBFOLDERS: ${{ inputs.subfolders }}
COMPLIANCE_COMMAND_OPTS: ${{ inputs.options }}
PLAN_FILE: ${{ inputs.planfile }}
COMPLIANCE_FEATURES: ${{ inputs.features }}
run: |
export TARGET_FOLDER="${TARGET_FOLDER:-.}"
export SUBFOLDERS="${SUBFOLDERS:-.}"
export SUBFOLDERS=($SUBFOLDERS)

# Use $RANDOM to avoid naming collisions
export RANDOM_PLAN_FILE="${PLAN_FILE}.${RANDOM}"
export RANDOM_PLAN_FILE_JSON="${RANDOM_PLAN_FILE}.json"
command_opts=(
-p "${RANDOM_PLAN_FILE_JSON}"
-f "${COMPLIANCE_FEATURES}"
)
for param in "${COMPLIANCE_COMMAND_OPTS[@]}"; do command_opts+=($param); done
export COMPLIANCE_COMMAND_OPTS="${command_opts[@]}"
export COMPLIANCE_COMMAND=terraform-compliance

cd $TARGET_FOLDER

printf "Current Target: %s\n" "$(pwd)"

for folder in "${SUBFOLDERS[@]}"; do
fulldir="${TARGET_FOLDER}/${folder}"
echo "##################################################################################################################"
echo "TERRAFORM COMPLIANCE PATH ${fulldir}"
echo "##################################################################################################################"
pushd $folder
printf "Current Subfolder: %s\n" "$(pwd)"
ls -l
[ -f "${PLAN_FILE}" ] || terragrunt plan -lock=false --out "${PLAN_FILE}"
printf "Renaming '%s' to '%s' ...\n" "${PLAN_FILE}" "${RANDOM_PLAN_FILE}"
mv "${PLAN_FILE}" "${RANDOM_PLAN_FILE}"
printf "Creating json planfile '%s' ...\n" "${RANDOM_PLAN_FILE_JSON}"
terragrunt show -json "${RANDOM_PLAN_FILE}" > "${RANDOM_PLAN_FILE_JSON}"
printf "Running '%s' ...\n" "${COMPLIANCE_COMMAND} ${COMPLIANCE_COMMAND_OPTS[@]}"
${COMPLIANCE_COMMAND} ${COMPLIANCE_COMMAND_OPTS[@]}
echo "##################################################################################################################"
rm "${RANDOM_PLAN_FILE_JSON}"
popd
done
19 changes: 19 additions & 0 deletions terraform-compliance-terragrunt/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ARG TERRAFORM_COMPLIANCE_VERSION=latest

ARG TERRAGRUNT_IMAGE_VERSION=latest
FROM alpine/terragrunt:$TERRAGRUNT_IMAGE_VERSION as builder
RUN which terragrunt >> /tmp/alpine_terragrunt_binaries && \
which terraform >> /tmp/alpine_terragrunt_binaries && \
mkdir -p /custom && \
for file in $(echo $(cat /tmp/alpine_terragrunt_binaries)); \
do \
mv $file /custom/; \
done

FROM eerkunt/terraform-compliance:$TERRAFORM_COMPLIANCE_VERSION
COPY --from=builder /custom/* /opt/bin/
ENV PATH="/opt/bin:${PATH}"

COPY entrypoint.sh /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
Loading