From 646dbcfbac8a89cbc1a2fa2ae00a11474f89bec1 Mon Sep 17 00:00:00 2001 From: "ngo.quoc.tuan" Date: Wed, 27 Nov 2024 20:47:17 +0700 Subject: [PATCH 1/5] Create list key value function --- check-connections.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) mode change 100644 => 100755 check-connections.sh diff --git a/check-connections.sh b/check-connections.sh old mode 100644 new mode 100755 index 3da3ec3..217d717 --- a/check-connections.sh +++ b/check-connections.sh @@ -72,7 +72,47 @@ check_s3_permissions() { print_separator } +list_key_value() { + print_separator + + # Environment variables from the pod containing key=value pairs + env_vars=$(printenv) + + # Default ports + DEFAULT_TCP_PORT=80 + DEFAULT_HTTP_PORT=80 + DEFAULT_HTTPS_PORT=443 + + # Process the values + while IFS='=' read -r key value; do + # Check if the value is a URL with a protocol + if [[ "$value" =~ ^(tcp|http|https)://([^:/]+)(:([0-9]+))?$ ]]; then + protocol=${BASH_REMATCH[1]} + host=${BASH_REMATCH[2]} + port=${BASH_REMATCH[3]} + + # Assign the default port if no port is specified + case "$protocol" in + tcp) + port=${port:-$DEFAULT_TCP_PORT} + ;; + http) + port=${port:-$DEFAULT_HTTP_PORT} + ;; + https) + port=${port:-$DEFAULT_HTTPS_PORT} + ;; + esac + + # Print the key=value with the determined port + echo "$key=$protocol://$host:$port" + fi + done <<< "$env_vars" + print_separator +} + # Run the checks +list_key_value check_s3_permissions check_rds_connection check_redis_connection From 44d1b770cb4041696c3f88407cbe78cf248d0008 Mon Sep 17 00:00:00 2001 From: jonmfv92 Date: Thu, 28 Nov 2024 16:19:29 +0700 Subject: [PATCH 2/5] update --- check-connections.sh | 74 +++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/check-connections.sh b/check-connections.sh index 217d717..063e54b 100755 --- a/check-connections.sh +++ b/check-connections.sh @@ -75,44 +75,48 @@ check_s3_permissions() { list_key_value() { print_separator - # Environment variables from the pod containing key=value pairs - env_vars=$(printenv) - - # Default ports - DEFAULT_TCP_PORT=80 - DEFAULT_HTTP_PORT=80 - DEFAULT_HTTPS_PORT=443 - - # Process the values - while IFS='=' read -r key value; do - # Check if the value is a URL with a protocol - if [[ "$value" =~ ^(tcp|http|https)://([^:/]+)(:([0-9]+))?$ ]]; then - protocol=${BASH_REMATCH[1]} - host=${BASH_REMATCH[2]} - port=${BASH_REMATCH[3]} - - # Assign the default port if no port is specified - case "$protocol" in - tcp) - port=${port:-$DEFAULT_TCP_PORT} - ;; - http) - port=${port:-$DEFAULT_HTTP_PORT} - ;; - https) - port=${port:-$DEFAULT_HTTPS_PORT} - ;; - esac - - # Print the key=value with the determined port - echo "$key=$protocol://$host:$port" + port_custom=$1 + + for url in $(printenv); do + + if [[ $url =~ ^([^=]+)=(.*) ]]; then + key=${BASH_REMATCH[1]} + value=${BASH_REMATCH[2]} + fi + + if [[ $value =~ ^https://([^:/]+)(:([0-9]+))?$ ]]; then + host=${BASH_REMATCH[1]} + port=${BASH_REMATCH[3]:-443} + protocol="https" + elif [[ $value =~ ^http://([^:/]+)(:([0-9]+))?$ ]]; then + host=${BASH_REMATCH[1]} + port=${BASH_REMATCH[3]:-80} + protocol="http" + elif [[ $value =~ ^tcp://([^:/]+)(:([0-9]+))?$ ]]; then + host=${BASH_REMATCH[1]} + port=${BASH_REMATCH[3]:-80} + protocol="tcp" + elif [[ $value =~ ([0-9]{1,3}\.){3}[0-9]{1,3}(:([0-9]+))?$ ]]; then + host=${BASH_REMATCH[0]%%:*} + port=${BASH_REMATCH[3]:-${port_custom:-0}} + protocol="" + elif [[ $value =~ ^([a-zA-Z0-9.-]+):([0-9]+)$ ]]; then + host=${BASH_REMATCH[1]} + port=${BASH_REMATCH[2]:-${port_custom:-0}} + protocol="" + else + continue fi - done <<< "$env_vars" + + [[ -z $protocol ]] && echo "${key}=${host}:${port}" || echo "${key}=${protocol}://${host}:${port}" + + + done print_separator } # Run the checks list_key_value -check_s3_permissions -check_rds_connection -check_redis_connection +# check_s3_permissions +# check_rds_connection +# check_redis_connection From 326cc7d1bb732d769611981b64e347d168d267c9 Mon Sep 17 00:00:00 2001 From: jonmfv92 Date: Fri, 29 Nov 2024 17:35:43 +0700 Subject: [PATCH 3/5] Change and fix function --- check-connections.sh | 80 ++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 25 deletions(-) diff --git a/check-connections.sh b/check-connections.sh index 063e54b..0e4b9e1 100755 --- a/check-connections.sh +++ b/check-connections.sh @@ -73,45 +73,75 @@ check_s3_permissions() { } list_key_value() { - print_separator + print_separator + port_custom=$1 + declare -A seen_values # Associative array to store already processed values for url in $(printenv); do - + # Check for key-value pairs from environment variables if [[ $url =~ ^([^=]+)=(.*) ]]; then key=${BASH_REMATCH[1]} value=${BASH_REMATCH[2]} + else + continue fi - if [[ $value =~ ^https://([^:/]+)(:([0-9]+))?$ ]]; then - host=${BASH_REMATCH[1]} - port=${BASH_REMATCH[3]:-443} - protocol="https" - elif [[ $value =~ ^http://([^:/]+)(:([0-9]+))?$ ]]; then - host=${BASH_REMATCH[1]} - port=${BASH_REMATCH[3]:-80} - protocol="http" - elif [[ $value =~ ^tcp://([^:/]+)(:([0-9]+))?$ ]]; then + # Case: URL contains amazonaws.com + if [[ $value =~ amazonaws\.com ]] && [[ ! $value =~ [^/]+/amazonaws\.com ]] && [[ ! $value =~ amazonaws\.com/[^/]+ ]]; then + + # If the value contains @, only take the part after @ + if [[ $value =~ @ ]]; then + # Ensure the string after @ contains amazonaws.com + tmp_value=${value##*@} + if [[ $tmp_value =~ amazonaws\.com ]]; then + value=$tmp_value + else + continue # Skip if the part after @ is invalid + fi + fi + + # Skip if the value is too long + if [[ ${#value} -gt 1000 ]]; then + continue + fi + + # Handle adding default ports for specific domains + if [[ $value =~ rds\.amazonaws\.com$ ]] && [[ ! $value =~ :[0-9]+$ ]]; then + value="${value}:3306" + elif [[ $value =~ cache\.amazonaws\.com$ ]] && [[ ! $value =~ :[0-9]+$ ]]; then + value="${value}:6379" + fi + + # Skip if the value has already been processed + if [[ -n "${seen_values[$value]}" ]]; then + continue + fi + + # Mark as processed and output the value + seen_values[$value]=1 + echo "$value" + continue + fi + + # Handle URLs that do not contain amazonaws.com + if [[ $value =~ ^https?://([^:/]+)(:([0-9]+))?(/.*)?$ ]]; then host=${BASH_REMATCH[1]} - port=${BASH_REMATCH[3]:-80} - protocol="tcp" - elif [[ $value =~ ([0-9]{1,3}\.){3}[0-9]{1,3}(:([0-9]+))?$ ]]; then - host=${BASH_REMATCH[0]%%:*} - port=${BASH_REMATCH[3]:-${port_custom:-0}} - protocol="" - elif [[ $value =~ ^([a-zA-Z0-9.-]+):([0-9]+)$ ]]; then + if [[ ${value} =~ ^https:// ]]; then + port=${BASH_REMATCH[3]:-443} + elif [[ ${value} =~ ^http:// ]]; then + port=${BASH_REMATCH[3]:-80} + fi + echo "${host}:${port}" + + elif [[ $value =~ ^([a-zA-Z0-9.-]+):([0-9]+)$ ]]; then host=${BASH_REMATCH[1]} port=${BASH_REMATCH[2]:-${port_custom:-0}} - protocol="" - else - continue + echo "${host}:${port}" fi - - [[ -z $protocol ]] && echo "${key}=${host}:${port}" || echo "${key}=${protocol}://${host}:${port}" - - done + print_separator } From f633d8deae2296b38e3ec9bb711ae14e2230fc9b Mon Sep 17 00:00:00 2001 From: jonmfv92 Date: Sat, 30 Nov 2024 09:11:20 +0700 Subject: [PATCH 4/5] Minor fix --- check-connections.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/check-connections.sh b/check-connections.sh index 0e4b9e1..380747f 100755 --- a/check-connections.sh +++ b/check-connections.sh @@ -141,12 +141,12 @@ list_key_value() { echo "${host}:${port}" fi done - + print_separator } # Run the checks list_key_value -# check_s3_permissions -# check_rds_connection -# check_redis_connection +check_s3_permissions +check_rds_connection +check_redis_connection From 98e93bec3784f6b250e9e393bfa51f808450f932 Mon Sep 17 00:00:00 2001 From: quangnhnhut123 Date: Mon, 2 Dec 2024 13:32:36 +0700 Subject: [PATCH 5/5] Update script --- .gitignore | 1 + check-connections.sh | 85 ++++++++++++++++++-------------------------- 2 files changed, 36 insertions(+), 50 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d5d3403 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +test* diff --git a/check-connections.sh b/check-connections.sh index 380747f..c960ee0 100755 --- a/check-connections.sh +++ b/check-connections.sh @@ -1,12 +1,5 @@ #!/bin/bash -# Define your RDS and Redis endpoint details -# RDS_ENDPOINT="your-rds-endpoint.rds.amazonaws.com" -RDS_PORT=3306 # Change this to your RDS port, e.g., 5432 for PostgreSQL or 3306 for MySQL - -# REDIS_ENDPOINT="your-redis-endpoint.cache.amazonaws.com" -REDIS_PORT=6379 # Default Redis port - # Function to print a separator print_separator() { echo "========================================" @@ -17,30 +10,6 @@ print_timestamp() { echo "$(date '+%Y-%m-%d %H:%M:%S')" } -# Function to check RDS network connectivity -check_rds_connection() { - print_separator - echo "$(print_timestamp) - Checking RDS network connectivity..." - if nc -zv "$RDS_ENDPOINT" "$RDS_PORT" >/dev/null 2>&1; then - echo "$(print_timestamp) - Successfully connected to RDS at $RDS_ENDPOINT on port $RDS_PORT." - else - echo "$(print_timestamp) - Failed to connect to RDS at $RDS_ENDPOINT on port $RDS_PORT." - fi - print_separator -} - -# Function to check Redis network connectivity -check_redis_connection() { - print_separator - echo "$(print_timestamp) - Checking Redis network connectivity..." - if nc -zv "$REDIS_ENDPOINT" "$REDIS_PORT" >/dev/null 2>&1; then - echo "$(print_timestamp) - Successfully connected to Redis at $REDIS_ENDPOINT on port $REDIS_PORT." - else - echo "$(print_timestamp) - Failed to connect to Redis at $REDIS_ENDPOINT on port $REDIS_PORT." - fi - print_separator -} - # Function to check specific S3 bucket permissions check_s3_permissions() { print_separator @@ -73,24 +42,20 @@ check_s3_permissions() { } list_key_value() { - - print_separator - - port_custom=$1 declare -A seen_values # Associative array to store already processed values for url in $(printenv); do # Check for key-value pairs from environment variables if [[ $url =~ ^([^=]+)=(.*) ]]; then - key=${BASH_REMATCH[1]} - value=${BASH_REMATCH[2]} + key=${BASH_REMATCH[1]} + value=${BASH_REMATCH[2]} else continue fi # Case: URL contains amazonaws.com if [[ $value =~ amazonaws\.com ]] && [[ ! $value =~ [^/]+/amazonaws\.com ]] && [[ ! $value =~ amazonaws\.com/[^/]+ ]]; then - + # If the value contains @, only take the part after @ if [[ $value =~ @ ]]; then # Ensure the string after @ contains amazonaws.com @@ -128,25 +93,45 @@ list_key_value() { # Handle URLs that do not contain amazonaws.com if [[ $value =~ ^https?://([^:/]+)(:([0-9]+))?(/.*)?$ ]]; then host=${BASH_REMATCH[1]} - if [[ ${value} =~ ^https:// ]]; then - port=${BASH_REMATCH[3]:-443} - elif [[ ${value} =~ ^http:// ]]; then - port=${BASH_REMATCH[3]:-80} - fi - echo "${host}:${port}" + port=${BASH_REMATCH[3]} + if [[ -z $port ]]; then + if [[ ${value} =~ ^https:// ]]; then + port=443 + elif [[ ${value} =~ ^http:// ]]; then + port=80 + fi + fi - elif [[ $value =~ ^([a-zA-Z0-9.-]+):([0-9]+)$ ]]; then - host=${BASH_REMATCH[1]} - port=${BASH_REMATCH[2]:-${port_custom:-0}} + # Skip if the value has already been processed + if [[ -n "${seen_values[${host}:${port}]}" ]]; then + continue + fi + + # Mark as processed and output the value + seen_values[${host}:${port}]=1 echo "${host}:${port}" fi done +} + +check_endpoints() { + print_separator + echo "$(print_timestamp) - Checking endpoints..." + + while IFS= read -r endpoint; do + echo "$(print_timestamp) - Checking $endpoint..." + host=$(echo "$endpoint" | cut -d':' -f1) + port=$(echo "$endpoint" | cut -d':' -f2) + if nc -zv "$host" "$port" >/dev/null 2>&1; then + echo "$(print_timestamp) - Successfully connected to $endpoint." + else + echo "$(print_timestamp) - Failed to connect to $endpoint." + fi + done < <(list_key_value) print_separator } # Run the checks -list_key_value +check_endpoints check_s3_permissions -check_rds_connection -check_redis_connection