profiler module moved from static config to environment dpendent config
This commit is contained in:
@@ -4,49 +4,29 @@ import os
|
||||
from fastapi import Header, HTTPException, status
|
||||
from pathlib import Path
|
||||
|
||||
# Load config from the main APP directory
|
||||
CONFIG_FILE = Path(__file__).parent.parent.parent / 'APP' / 'config.ini'
|
||||
from .config import get_config_value
|
||||
|
||||
def get_secret_key():
|
||||
# Priority 1: Environment Variable
|
||||
env_secret = os.getenv('OVPMON_SECRET_KEY')
|
||||
if env_secret:
|
||||
print("[AUTH] Using SECRET_KEY from environment variable")
|
||||
return env_secret
|
||||
|
||||
# Priority 2: Config file (multiple possible locations)
|
||||
# Resolve absolute path to be sure
|
||||
base_path = Path(__file__).resolve().parent.parent
|
||||
# Use consistent OVPMON_API_SECRET_KEY as primary source
|
||||
key = get_config_value('api', 'secret_key', fallback='ovpmon-secret-change-me')
|
||||
|
||||
config_locations = [
|
||||
base_path.parent / 'APP' / 'config.ini', # Brother directory (Local/Gitea structure)
|
||||
base_path / 'APP' / 'config.ini', # Child directory
|
||||
base_path / 'config.ini', # Same directory
|
||||
Path('/opt/ovpmon/APP/config.ini'), # Common production path 1
|
||||
Path('/opt/ovpmon/config.ini'), # Common production path 2
|
||||
Path('/etc/ovpmon/config.ini'), # Standard linux config path
|
||||
Path('/opt/ovpn_python_profiler/APP/config.ini') # Path based on traceback
|
||||
]
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
for loc in config_locations:
|
||||
if loc.exists():
|
||||
try:
|
||||
config.read(loc)
|
||||
if config.has_section('api') and config.has_option('api', 'secret_key'):
|
||||
key = config.get('api', 'secret_key')
|
||||
if key:
|
||||
print(f"[AUTH] Successfully loaded SECRET_KEY from {loc}")
|
||||
return key
|
||||
except Exception as e:
|
||||
print(f"[AUTH] Error reading config at {loc}: {e}")
|
||||
continue
|
||||
|
||||
print("[AUTH] WARNING: No config found, using default fallback SECRET_KEY")
|
||||
return 'ovpmon-secret-change-me'
|
||||
if key == 'ovpmon-secret-change-me':
|
||||
print("[AUTH] WARNING: Using default fallback SECRET_KEY")
|
||||
else:
|
||||
# Check if it was from env (get_config_value prioritizes env)
|
||||
import os
|
||||
if os.getenv('OVPMON_API_SECRET_KEY'):
|
||||
print("[AUTH] Using SECRET_KEY from OVPMON_API_SECRET_KEY environment variable")
|
||||
elif os.getenv('OVPMON_SECRET_KEY'):
|
||||
print("[AUTH] Using SECRET_KEY from OVPMON_SECRET_KEY environment variable")
|
||||
else:
|
||||
print("[AUTH] SECRET_KEY loaded (config.ini or fallback)")
|
||||
|
||||
return key
|
||||
|
||||
SECRET_KEY = get_secret_key()
|
||||
|
||||
|
||||
async def verify_token(authorization: str = Header(None)):
|
||||
if not authorization or not authorization.startswith("Bearer "):
|
||||
print(f"[AUTH] Missing or invalid Authorization header: {authorization[:20] if authorization else 'None'}")
|
||||
|
||||
Reference in New Issue
Block a user