Last active
August 13, 2024 10:30
-
-
Save actionjack/38d9904e4817a689894c039501c382f9 to your computer and use it in GitHub Desktop.
trivy-db-check.sh
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
#!/bin/bash | |
# GitHub Container Registry API URL for trivy-db container package | |
API_URL="https://api.github.com/orgs/aquasecurity/packages/container/trivy-db/versions" | |
# Function to run jq using Docker | |
docker_jq() { | |
docker run --rm -i alpine:latest sh -c "apk add --no-cache jq > /dev/null 2>&1 && jq $*" | |
} | |
# Function to check for recent updates | |
check_recent_update() { | |
if [ -z "$GITHUB_TOKEN" ]; then | |
echo "Error: GITHUB_TOKEN is not set. Please set it with your GitHub Personal Access Token." | |
echo "Example: export GITHUB_TOKEN=ghp_your_token_here" | |
return 1 | |
fi | |
echo "Fetching trivy-db container information..." | |
# Fetch the container package information | |
response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
"$API_URL?per_page=10") | |
echo "Attempting to extract information..." | |
# Extract the latest version information using jq | |
latest_version=$(echo "$response" | docker_jq -r '.[0]') | |
if [ "$latest_version" == "null" ] || [ -z "$latest_version" ]; then | |
echo "Error: Unable to extract version information from the API response." | |
echo "Full API response:" | |
echo "$response" | docker_jq . | |
return 1 | |
fi | |
tag=$(echo "$latest_version" | docker_jq -r '.metadata.container.tags[0]') | |
last_updated=$(echo "$latest_version" | docker_jq -r '.updated_at') | |
echo "Debug: Latest tag: $tag" | |
echo "Debug: Last updated: $last_updated" | |
# Convert the timestamp to Unix epoch time | |
if [[ "$OSTYPE" == "darwin"* ]]; then | |
# macOS | |
updated_at_epoch=$(date -j -f "%Y-%m-%dT%H:%M:%SZ" "$last_updated" "+%s") | |
else | |
# Linux | |
updated_at_epoch=$(date -d "$last_updated" +%s) | |
fi | |
# Get the current time in Unix epoch time | |
current_time=$(date +%s) | |
# Calculate the time difference in seconds | |
time_diff=$((current_time - updated_at_epoch)) | |
# 6 hours in seconds | |
six_hours=$((6 * 3600)) | |
if [ $time_diff -le $six_hours ]; then | |
hours=$((time_diff / 3600)) | |
minutes=$(( (time_diff % 3600) / 60 )) | |
seconds=$((time_diff % 60)) | |
echo "A recent update was made ${hours} hours ${minutes} minutes ${seconds} seconds ago!" | |
echo "Latest trivy-db container version: $tag" | |
echo "Updated at: $last_updated" | |
else | |
echo "No recent updates in the last 6 hours." | |
echo "Latest trivy-db container version: $tag" | |
echo "Last update was at: $last_updated" | |
fi | |
} | |
# Check if Docker is installed and running | |
if ! command -v docker &> /dev/null || ! docker info &> /dev/null; then | |
echo "Error: Docker is not installed or not running. Please install Docker and ensure it's running." | |
exit 1 | |
fi | |
# Run the function | |
check_recent_update |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment