Auth only when needed
This commit is contained in:
@@ -2,23 +2,23 @@
|
||||
|
||||
# Function to print usage information
|
||||
usage() {
|
||||
echo "Usage: $0 <image> <start_tag> <end_tag> <username> <token>"
|
||||
echo "Example: $0 tabledevil/sep 230101 230916 username dckr_pat_8FEgaA5ovvL1V-UEWfV5T3jVABC"
|
||||
echo "Usage: $0 <image> <start_tag> <end_tag>"
|
||||
echo "Example: $0 tabledevil/sep 230101 230916"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if the correct number of arguments is provided
|
||||
if [ "$#" -ne 5 ]; then
|
||||
usage
|
||||
fi
|
||||
# Function to get today's date in YYMMDD format
|
||||
get_today() {
|
||||
date +%y%m%d
|
||||
}
|
||||
|
||||
# Assign arguments to variables
|
||||
image="$1"
|
||||
# Assign arguments to variables or use defaults
|
||||
image="${1:-tabledevil/sep}"
|
||||
start_tag="$2"
|
||||
end_tag="$3"
|
||||
DOCKER_USERNAME="$4"
|
||||
DOCKER_PAT="$5"
|
||||
REPOSITORY=$(echo "$image" | cut -d'/' -f2)
|
||||
end_tag="${3:-$(get_today)}"
|
||||
IMAGE_REPOSITORY=$(echo "$image" | cut -d'/' -f1)
|
||||
IMAGE_NAME=$(echo "$image" | cut -d'/' -f2)
|
||||
|
||||
PATTERN="Threat Found!"
|
||||
|
||||
# Validate that start_tag and end_tag are in the correct format
|
||||
@@ -29,6 +29,13 @@ fi
|
||||
|
||||
# Function to get Docker Hub token using PAT
|
||||
get_token() {
|
||||
if [ -z "$DOCKER_USERNAME" ]; then
|
||||
read -p "Enter Docker Hub username: " DOCKER_USERNAME
|
||||
fi
|
||||
if [ -z "$DOCKER_PAT" ]; then
|
||||
read -sp "Enter Docker Hub token: " DOCKER_PAT
|
||||
echo
|
||||
fi
|
||||
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${DOCKER_USERNAME}'", "password": "'${DOCKER_PAT}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
|
||||
if [ "$TOKEN" == "null" ]; then
|
||||
echo "Failed to get token. Please check your credentials."
|
||||
@@ -43,7 +50,11 @@ get_tags() {
|
||||
TAGS=()
|
||||
|
||||
while true; do
|
||||
RESPONSE=$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${DOCKER_USERNAME}/${REPOSITORY}/tags/?page_size=${PAGE_SIZE}&page=${PAGE}")
|
||||
if [ -n "$TOKEN" ]; then
|
||||
RESPONSE=$(curl -s -H "Authorization: JWT ${TOKEN}" "https://hub.docker.com/v2/repositories/${IMAGE_REPOSITORY}/${IMAGE_NAME}/tags/?page_size=${PAGE_SIZE}&page=${PAGE}")
|
||||
else
|
||||
RESPONSE=$(curl -s "https://hub.docker.com/v2/repositories/${IMAGE_REPOSITORY}/${IMAGE_NAME}/tags/?page_size=${PAGE_SIZE}&page=${PAGE}")
|
||||
fi
|
||||
TAGS_PAGE=$(echo $RESPONSE | jq -r '.results[].name')
|
||||
TAGS+=($TAGS_PAGE)
|
||||
|
||||
@@ -128,11 +139,22 @@ binary_search() {
|
||||
|
||||
|
||||
# Main script execution
|
||||
get_token
|
||||
|
||||
# Retrieve all tags from Docker Hub
|
||||
# Try to retrieve all tags without authentication
|
||||
echo "Retrieving all tags for $image from Docker Hub..."
|
||||
all_tags=($(get_tags))
|
||||
echo $all_tags
|
||||
|
||||
# If tags retrieval failed, prompt for credentials and retry
|
||||
if [ ${#all_tags[@]} -eq 0 ]; then
|
||||
echo "Failed to retrieve tags without authentication. Trying with credentials..."
|
||||
get_token
|
||||
all_tags=($(get_tags))
|
||||
if [ ${#all_tags[@]} -eq 0 ]; then
|
||||
echo "Failed to retrieve tags even with authentication."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Filter tags to include only those within the date range
|
||||
echo "Filtering tags from $start_tag to $end_tag..."
|
||||
|
||||
Reference in New Issue
Block a user