
All checks were successful
Test and Publish Templates / test-and-publish (push) Successful in 39s
115 lines
3.4 KiB
Bash
Executable File
115 lines
3.4 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Interactive API Key Generation Script for LogServer
|
|
# This script generates secure API keys and adds them to api-keys.yml
|
|
|
|
API_KEYS_FILE="${CONFIG_PATH:-./config}/api-keys.yml"
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Generate a secure random API key
|
|
generate_key() {
|
|
openssl rand -hex 32 2>/dev/null || cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1
|
|
}
|
|
|
|
# Initialize api-keys.yml if it doesn't exist
|
|
init_api_keys_file() {
|
|
if [ ! -f "$API_KEYS_FILE" ]; then
|
|
echo "# API Keys for LogServer Authentication" > "$API_KEYS_FILE"
|
|
echo "# Format: hostname:api_key" >> "$API_KEYS_FILE"
|
|
echo "# Generated by generate-api-key.sh" >> "$API_KEYS_FILE"
|
|
echo "" >> "$API_KEYS_FILE"
|
|
echo "api_keys:" >> "$API_KEYS_FILE"
|
|
echo -e "${GREEN}Created new api-keys.yml file${NC}"
|
|
fi
|
|
}
|
|
|
|
# Check if hostname already has a key
|
|
check_existing_key() {
|
|
local hostname=$1
|
|
if grep -q "^ ${hostname}:" "$API_KEYS_FILE" 2>/dev/null; then
|
|
return 0
|
|
fi
|
|
return 1
|
|
}
|
|
|
|
# Add key to api-keys.yml
|
|
add_key_to_file() {
|
|
local hostname=$1
|
|
local api_key=$2
|
|
echo " ${hostname}: ${api_key}" >> "$API_KEYS_FILE"
|
|
}
|
|
|
|
# Main script
|
|
echo -e "${GREEN}=== LogServer API Key Generator ===${NC}"
|
|
echo ""
|
|
|
|
# Initialize file if needed
|
|
init_api_keys_file
|
|
|
|
# Interactive mode
|
|
while true; do
|
|
echo -e "${YELLOW}Enter hostname for the client (or 'done' to finish):${NC}"
|
|
read -p "> " hostname
|
|
|
|
if [ "$hostname" = "done" ] || [ -z "$hostname" ]; then
|
|
break
|
|
fi
|
|
|
|
# Validate hostname
|
|
if [[ ! "$hostname" =~ ^[a-zA-Z0-9][a-zA-Z0-9-_.]*$ ]]; then
|
|
echo -e "${RED}Invalid hostname format. Use only letters, numbers, dots, dashes, and underscores.${NC}"
|
|
continue
|
|
fi
|
|
|
|
# Check if key already exists
|
|
if check_existing_key "$hostname"; then
|
|
echo -e "${YELLOW}Key already exists for ${hostname}${NC}"
|
|
read -p "Generate new key? (y/n): " overwrite
|
|
if [ "$overwrite" != "y" ]; then
|
|
continue
|
|
fi
|
|
# Remove old key
|
|
sed -i "/^ ${hostname}:/d" "$API_KEYS_FILE"
|
|
fi
|
|
|
|
# Generate new key
|
|
api_key=$(generate_key)
|
|
|
|
# Add to file
|
|
add_key_to_file "$hostname" "$api_key"
|
|
|
|
echo -e "${GREEN}✓ Generated API key for ${hostname}${NC}"
|
|
echo ""
|
|
echo "Configuration for ${hostname}:"
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
|
echo "Add to client's service.env:"
|
|
echo ""
|
|
echo "LOGSERVER_HOST=$(hostname -I | awk '{print $1}')"
|
|
echo "LOGSERVER_PORT=5044"
|
|
echo "API_KEY=${api_key}"
|
|
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
|
echo ""
|
|
|
|
# Option to add more
|
|
read -p "Add another client? (y/n): " add_more
|
|
if [ "$add_more" != "y" ]; then
|
|
break
|
|
fi
|
|
done
|
|
|
|
# Show summary
|
|
echo ""
|
|
echo -e "${GREEN}=== Summary ===${NC}"
|
|
echo "API keys file: $API_KEYS_FILE"
|
|
echo "Total clients configured: $(grep -c "^ " "$API_KEYS_FILE" 2>/dev/null || echo 0)"
|
|
echo ""
|
|
echo "To view all keys: cat $API_KEYS_FILE"
|
|
echo "To revoke a key: Edit $API_KEYS_FILE and remove the line"
|
|
echo ""
|
|
echo -e "${YELLOW}Remember to restart logserver after adding keys:${NC}"
|
|
echo " dropshell restart logserver" |