fix(auth): Refactor service authentication status check

This commit is contained in:
Jose Daniel G. Percy 2025-04-26 03:27:49 +08:00
parent f4409eb258
commit 749aa6f1cf

View File

@ -623,61 +623,47 @@ list_services() {
return 1 return 1
fi fi
# Show a warning if we're not creating a backup for this operation
echo -e "${BLUE}Checking services in $COMPOSE_FILE...${NC}" echo -e "${BLUE}Checking services in $COMPOSE_FILE...${NC}"
echo -e "${CYAN}SERVICE\t\tAUTH STATUS${NC}" echo -e "${CYAN}SERVICE\t\tAUTH STATUS${NC}"
echo -e "${CYAN}-------\t\t-----------${NC}" echo -e "${CYAN}-------\t\t-----------${NC}"
local service_count=0 local service_count=0
# Use yq if available for more reliable parsing # Get all router names from the labels
if command -v yq &> /dev/null; then # This pattern is specific to how your docker-compose.yml format works
# Get all services from the docker-compose.yml file local router_lines=$(grep -n "traefik.http.routers" "$COMPOSE_FILE")
local services=$(yq e '.services | keys | .[]' "$COMPOSE_FILE" 2>/dev/null)
# Process each router line to get service names
while IFS= read -r line; do
local line_num=$(echo "$line" | cut -d: -f1)
local router_config=$(echo "$line" | cut -d: -f2-)
# Extract service name from router definition
if [[ "$router_config" =~ traefik\.http\.routers\.([^.]+) ]]; then
local service="${BASH_REMATCH[1]}"
for service in $services; do
# Skip infrastructure containers # Skip infrastructure containers
if [[ "$service" == "redis" || "$service" == "authelia" || "$service" == "traefik" || "$service" == "tailscale" || "$service" == "watchtower" || "$service" == "autoheal" || "$service" == "middlewares" ]]; then if [[ "$service" == "redis" || "$service" == "authelia" || "$service" == "traefik" || "$service" == "tailscale" || "$service" == "watchtower" || "$service" == "autoheal" || "$service" == "middlewares" ]]; then
continue continue
fi fi
# Check if this service has Traefik router configured # Skip duplicate entries - only handle each service once
local has_router=$(yq e ".services.$service.labels[] | select(contains(\"traefik.http.routers.$service\"))" "$COMPOSE_FILE" 2>/dev/null) if [[ "$processed_services" == *"$service"* ]]; then
if [ -n "$has_router" ]; then
local status=$(get_auth_status "$service")
# Format the output with padding
printf "${BOLD}%-20s${NC}" "$service"
case "$status" in
"enabled")
echo -e "${GREEN}Enabled${NC}"
service_count=$((service_count + 1))
;;
"disabled")
echo -e "${YELLOW}Disabled${NC}"
service_count=$((service_count + 1))
;;
*)
echo -e "${RED}Unknown${NC}"
;;
esac
fi
done
else
# Fallback to using grep for parsing (less reliable)
# First identify all container names
local services=$(grep "container_name:" "$COMPOSE_FILE" | awk '{print $3}')
for service in $services; do
# Skip infrastructure containers
if [[ "$service" == "redis" || "$service" == "authelia" || "$service" == "traefik" || "$service" == "tailscale" || "$service" == "watchtower" || "$service" == "autoheal" || "$service" == "middlewares" ]]; then
continue continue
fi fi
processed_services="$processed_services $service"
# Look specifically for router configuration for this service # Find if this router has a middlewares configuration, with or without authelia
if grep -q "traefik.http.routers.$service" "$COMPOSE_FILE"; then local status="unknown"
local status=$(get_auth_status "$service") # Look for middlewares for this service
if grep -q "traefik.http.routers.$service.middlewares=.*authelia-auth" "$COMPOSE_FILE"; then
status="enabled"
elif grep -q "traefik.http.routers.$service.middlewares=" "$COMPOSE_FILE"; then
# Has middlewares but no authelia-auth
if ! grep -q "traefik.http.routers.$service.middlewares=.*authelia-auth" "$COMPOSE_FILE"; then
status="disabled"
fi
fi
printf "${BOLD}%-20s${NC}" "$service" printf "${BOLD}%-20s${NC}" "$service"
@ -695,13 +681,11 @@ list_services() {
;; ;;
esac esac
fi fi
done done <<< "$router_lines"
fi
if [ $service_count -eq 0 ]; then if [ $service_count -eq 0 ]; then
echo -e "${YELLOW}No services found with authentication status.${NC}" echo -e "${YELLOW}No services found with authentication status.${NC}"
echo -e "${YELLOW}This could indicate that no services are configured with Traefik routers,${NC}" echo -e "${YELLOW}This could indicate an issue with detecting middlewares in your docker-compose.yml.${NC}"
echo -e "${YELLOW}or that the compose file has an unexpected structure.${NC}"
fi fi
} }