Auth only when needed
This commit is contained in:
@@ -2,23 +2,23 @@
|
|||||||
|
|
||||||
# Function to print usage information
|
# Function to print usage information
|
||||||
usage() {
|
usage() {
|
||||||
echo "Usage: $0 <image> <start_tag> <end_tag> <username> <token>"
|
echo "Usage: $0 <image> <start_tag> <end_tag>"
|
||||||
echo "Example: $0 tabledevil/sep 230101 230916 username dckr_pat_8FEgaA5ovvL1V-UEWfV5T3jVABC"
|
echo "Example: $0 tabledevil/sep 230101 230916"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check if the correct number of arguments is provided
|
# Function to get today's date in YYMMDD format
|
||||||
if [ "$#" -ne 5 ]; then
|
get_today() {
|
||||||
usage
|
date +%y%m%d
|
||||||
fi
|
}
|
||||||
|
|
||||||
# Assign arguments to variables
|
# Assign arguments to variables or use defaults
|
||||||
image="$1"
|
image="${1:-tabledevil/sep}"
|
||||||
start_tag="$2"
|
start_tag="$2"
|
||||||
end_tag="$3"
|
end_tag="${3:-$(get_today)}"
|
||||||
DOCKER_USERNAME="$4"
|
IMAGE_REPOSITORY=$(echo "$image" | cut -d'/' -f1)
|
||||||
DOCKER_PAT="$5"
|
IMAGE_NAME=$(echo "$image" | cut -d'/' -f2)
|
||||||
REPOSITORY=$(echo "$image" | cut -d'/' -f2)
|
|
||||||
PATTERN="Threat Found!"
|
PATTERN="Threat Found!"
|
||||||
|
|
||||||
# Validate that start_tag and end_tag are in the correct format
|
# 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
|
# Function to get Docker Hub token using PAT
|
||||||
get_token() {
|
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)
|
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
|
if [ "$TOKEN" == "null" ]; then
|
||||||
echo "Failed to get token. Please check your credentials."
|
echo "Failed to get token. Please check your credentials."
|
||||||
@@ -43,7 +50,11 @@ get_tags() {
|
|||||||
TAGS=()
|
TAGS=()
|
||||||
|
|
||||||
while true; do
|
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_PAGE=$(echo $RESPONSE | jq -r '.results[].name')
|
||||||
TAGS+=($TAGS_PAGE)
|
TAGS+=($TAGS_PAGE)
|
||||||
|
|
||||||
@@ -128,11 +139,22 @@ binary_search() {
|
|||||||
|
|
||||||
|
|
||||||
# Main script execution
|
# 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..."
|
echo "Retrieving all tags for $image from Docker Hub..."
|
||||||
all_tags=($(get_tags))
|
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
|
# Filter tags to include only those within the date range
|
||||||
echo "Filtering tags from $start_tag to $end_tag..."
|
echo "Filtering tags from $start_tag to $end_tag..."
|
||||||
|
|||||||
Reference in New Issue
Block a user