Skip to content

Commit

Permalink
fix: [#5] Create empty trivyignore if absent. (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
030 authored Jan 1, 2024
1 parent 57db063 commit 5abbd7b
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 31 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@ jobs:
runs-on: ubuntu-20.04
steps:
- uses: actions/[email protected]
- uses: 030/[email protected]
- uses: 030/[email protected]
```

## unit tests

```bash
docker run -it -v "${PWD}:/code" --entrypoint=bash bats/bats:v1.10.0
apk add --no-cache coreutils
bats --tap test --print-output-on-failure
```
31 changes: 1 addition & 30 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,5 @@ description: trivyignore-validator
runs:
using: 'composite'
steps:
- run: |
filename=.trivyignore
if test -f ${filename}; then
echo "found a ${filename} file...";
while read -r line; do
if echo ${line} | grep -qE "^CVE\-"; then
echo "found a 'CVE-' entry in the ${filename}...";
echo "checking whether an expiry has been attached..."
if ! echo ${line} | grep -qE "^CVE\-.*exp:[0-9]{4}(\-[0-9]{2}){2}$"; then
echo "no expiry associated to: '${line}'. Add it by adding: 'exp:yyyy-mm-dd'"
exit 1
fi
echo "checking whether the expiry will take place in one month..."
current=$(echo "${line}" | sed -e "s|CVE.*exp:\(.*\)|\1|g")
max=$(date +"%F" --date="$(date +%F) next month")
if [[ "${current}" > "${max}" ]]; then
echo "current date: '${current}' in line: '${line}' exceeds"
echo "the maximum date of one month. Choose a new date that is"
echo "before: ${max}"
exit 1
fi
fi
done < "${filename}"
exit 0
fi
echo "no ${filename} file found"
- run: ${GITHUB_ACTION_PATH}/src/action.sh
shell: bash
54 changes: 54 additions & 0 deletions src/action.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash

readonly filename=".trivyignore"

createEmptyDotTrivyignoreIfAbsent() {
if test -f "${filename}"; then
echo "found a ${filename} file...";
return
fi

echo "no ${filename} file found. Creating empty one..."
touch "${filename}"
exit 0
}

inspectCveExpiry() {
echo "checking whether an expiry has been attached..."

if ! echo ${1} | grep -qE "^CVE\-.*exp:[0-9]{4}(\-[0-9]{2}){2}$"; then
echo "no expiry associated to: '${1}'. Add it by adding: 'exp:yyyy-mm-dd'"
exit 1
fi
}

inspectCveExpiryMaxOneMonth() {
echo "checking whether the expiry will take place in one month..."
current=$(echo "${1}" | sed -e "s|CVE\-.*exp:\(.*\)|\1|g")
max=$(date +"%F" --date="$(date +%F) next month")

if [[ "${current}" > "${max}" ]]; then
echo "current date: '${current}' in line: '${1}' exceeds"
echo "the maximum date of one month. Choose a new date that is"
echo "before: ${max}"
exit 1
fi
}

inspectCveExpiryAndMaxOneMonth() {
while read -r line; do
if echo "${line}" | grep -qE "^CVE\-"; then
echo "found a 'CVE-' entry in the ${filename}...";

inspectCveExpiry "${line}"
inspectCveExpiryMaxOneMonth "${line}"
fi
done < "${filename}"
}

main() {
createEmptyDotTrivyignoreIfAbsent
inspectCveExpiryAndMaxOneMonth
}

main
15 changes: 15 additions & 0 deletions test/create-empty-dot-trivyignore-if-absent.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
setup() {
filename=".trivyignore"
}

teardown() {
echo "found filename: '${filename}'. Removing it..."
rm "${filename}"
}

@test "create empty dot trivyignore if absent" {
run ./src/action.sh
[ "$status" -eq 0 ]
regex=".*no ${filename} file found. Creating empty one.*"
[[ "$output" =~ $regex ]]
}
17 changes: 17 additions & 0 deletions test/inspect-cve-expiry-max-one-month.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
setup() {
filename=".trivyignore"

echo -en "CVE-123 exp:2124-02-15\nCVE-456 exp:2124-02-16" > "${filename}"
}

teardown() {
echo "found filename: '${filename}'. Removing it..."
rm "${filename}"
}

@test "inspect cve expiry max one month" {
run ./src/action.sh
[ "$status" -eq 1 ]
regex=".*current date: '2124-02-15' in line: 'CVE-123 exp:2124-02-15' exceeds.*the maximum date of one month. Choose a new date that is.*before: .*"
[[ "$output" =~ $regex ]]
}
17 changes: 17 additions & 0 deletions test/inspect-cve-expiry.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
setup() {
filename=".trivyignore"

echo -en "CVE-123\nCVE-456 exp:2124-02-16" > "${filename}"
}

teardown() {
echo "found filename: '${filename}'. Removing it..."
rm "${filename}"
}

@test "inspect cve expiry" {
run ./src/action.sh
[ "$status" -eq 1 ]
regex=".*no expiry associated to: 'CVE-123'. Add it by adding: 'exp:yyyy-mm-dd'.*"
[[ "$output" =~ $regex ]]
}

0 comments on commit 5abbd7b

Please sign in to comment.