#!/bin/bash # Test script to verify API key authentication is working # This sends test log messages with and without API keys to verify authentication source "${AGENT_PATH}/common.sh" _check_required_env_vars "CONTAINER_NAME" "LOGSTASH_BEATS_PORT" echo "=== Testing Logstash API Key Authentication ===" echo "" # Check if service is running if ! bash ./status.sh | grep -q "Running"; then echo "Error: LogServer is not running. Please start it first." exit 1 fi # Get server IP SERVER_IP=$(hostname -I | awk '{print $1}') echo "Testing authentication on ${SERVER_IP}:${LOGSTASH_BEATS_PORT}" echo "" # Test 1: Send without API key (should be tagged or dropped based on config) echo "Test 1: Sending log without API key..." echo '{"message":"Test log without API key","timestamp":"'$(date -Iseconds)'"}' | \ nc -w 2 ${SERVER_IP} ${LOGSTASH_BEATS_PORT} 2>/dev/null || true # Test 2: Send with invalid API key (should be dropped) echo "Test 2: Sending log with invalid API key..." echo '{"message":"Test log with invalid key","fields":{"api_key":"invalid-key-12345"},"timestamp":"'$(date -Iseconds)'"}' | \ nc -w 2 ${SERVER_IP} ${LOGSTASH_BEATS_PORT} 2>/dev/null || true # Test 3: Send with valid API key (if one exists) if [ -f "${CONFIG_PATH}/api-keys.yml" ]; then # Extract first API key from the file API_KEY=$(grep -E "^ [^:]+: " "${CONFIG_PATH}/api-keys.yml" | head -1 | awk '{print $2}') if [ -n "$API_KEY" ]; then echo "Test 3: Sending log with valid API key..." echo '{"message":"Test log with valid key","fields":{"api_key":"'${API_KEY}'"},"timestamp":"'$(date -Iseconds)'"}' | \ nc -w 2 ${SERVER_IP} ${LOGSTASH_BEATS_PORT} 2>/dev/null || true else echo "Test 3: Skipped - no API keys found" fi else echo "Test 3: Skipped - api-keys.yml not found" fi echo "" echo "Tests sent. Check Logstash logs to verify authentication:" echo " docker logs ${CONTAINER_NAME}_logstash --tail 20" echo "" echo "Valid logs should appear in Elasticsearch." echo "Invalid logs should be dropped."