Add JWT authentication with user/password login, role-based access control, and Bearer token support while maintaining legacy API key compatibility for transition; update README with comprehensive JWT security guidelines, installation instructions for Python/SSH, development startup scripts (.env, run_dev.sh/ps1), and migrate all API examples to JWT authentication; update Ansible inventory SSH key paths from Docker container paths to local user paths
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
"""Add users table for authentication
|
||||
|
||||
Revision ID: 0004_add_users
|
||||
Revises: 0003_add_notification_type
|
||||
Create Date: 2025-12-09
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "0004_add_users"
|
||||
down_revision = "0003_add_notification_type"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"users",
|
||||
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column("username", sa.String(50), nullable=False, unique=True),
|
||||
sa.Column("email", sa.String(255), nullable=True, unique=True),
|
||||
sa.Column("hashed_password", sa.String(255), nullable=False),
|
||||
sa.Column("role", sa.String(20), nullable=False, server_default=sa.text("'admin'")),
|
||||
sa.Column("is_active", sa.Boolean(), nullable=False, server_default=sa.text("1")),
|
||||
sa.Column("is_superuser", sa.Boolean(), nullable=False, server_default=sa.text("0")),
|
||||
sa.Column("display_name", sa.String(100), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.Column("last_login", sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column("password_changed_at", sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
|
||||
)
|
||||
|
||||
# Create index on username for fast lookups
|
||||
op.create_index("idx_users_username", "users", ["username"])
|
||||
op.create_index("idx_users_email", "users", ["email"])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index("idx_users_email", table_name="users")
|
||||
op.drop_index("idx_users_username", table_name="users")
|
||||
op.drop_table("users")
|
||||
@@ -0,0 +1,86 @@
|
||||
"""Add host_metrics table for builtin playbooks data collection
|
||||
|
||||
Revision ID: 0005
|
||||
Revises: 0004_add_users_table
|
||||
Create Date: 2024-12-11
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision: str = '0005_add_host_metrics'
|
||||
down_revision: Union[str, None] = '0004_add_users'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
'host_metrics',
|
||||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column('host_id', sa.String(), nullable=False),
|
||||
sa.Column('metric_type', sa.String(50), nullable=False),
|
||||
|
||||
# CPU metrics
|
||||
sa.Column('cpu_count', sa.Integer(), nullable=True),
|
||||
sa.Column('cpu_model', sa.String(200), nullable=True),
|
||||
sa.Column('cpu_load_1m', sa.Float(), nullable=True),
|
||||
sa.Column('cpu_load_5m', sa.Float(), nullable=True),
|
||||
sa.Column('cpu_load_15m', sa.Float(), nullable=True),
|
||||
sa.Column('cpu_usage_percent', sa.Float(), nullable=True),
|
||||
sa.Column('cpu_temperature', sa.Float(), nullable=True),
|
||||
|
||||
# Memory metrics
|
||||
sa.Column('memory_total_mb', sa.Integer(), nullable=True),
|
||||
sa.Column('memory_used_mb', sa.Integer(), nullable=True),
|
||||
sa.Column('memory_free_mb', sa.Integer(), nullable=True),
|
||||
sa.Column('memory_usage_percent', sa.Float(), nullable=True),
|
||||
sa.Column('swap_total_mb', sa.Integer(), nullable=True),
|
||||
sa.Column('swap_used_mb', sa.Integer(), nullable=True),
|
||||
sa.Column('swap_usage_percent', sa.Float(), nullable=True),
|
||||
|
||||
# Disk metrics
|
||||
sa.Column('disk_info', sa.JSON(), nullable=True),
|
||||
sa.Column('disk_root_total_gb', sa.Float(), nullable=True),
|
||||
sa.Column('disk_root_used_gb', sa.Float(), nullable=True),
|
||||
sa.Column('disk_root_usage_percent', sa.Float(), nullable=True),
|
||||
|
||||
# System info
|
||||
sa.Column('os_name', sa.String(100), nullable=True),
|
||||
sa.Column('os_version', sa.String(100), nullable=True),
|
||||
sa.Column('kernel_version', sa.String(100), nullable=True),
|
||||
sa.Column('hostname', sa.String(200), nullable=True),
|
||||
sa.Column('uptime_seconds', sa.Integer(), nullable=True),
|
||||
sa.Column('uptime_human', sa.String(100), nullable=True),
|
||||
|
||||
# Network info
|
||||
sa.Column('network_info', sa.JSON(), nullable=True),
|
||||
|
||||
# Raw data and metadata
|
||||
sa.Column('raw_data', sa.JSON(), nullable=True),
|
||||
sa.Column('collection_source', sa.String(100), nullable=True),
|
||||
sa.Column('collection_duration_ms', sa.Integer(), nullable=True),
|
||||
sa.Column('error_message', sa.Text(), nullable=True),
|
||||
|
||||
# Timestamps
|
||||
sa.Column('collected_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
|
||||
sa.ForeignKeyConstraint(['host_id'], ['hosts.id'], ondelete='CASCADE'),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
# Create indexes
|
||||
op.create_index('idx_host_metrics_host_id', 'host_metrics', ['host_id'])
|
||||
op.create_index('idx_host_metrics_collected_at', 'host_metrics', ['collected_at'])
|
||||
op.create_index('idx_host_metrics_metric_type', 'host_metrics', ['metric_type'])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index('idx_host_metrics_metric_type', 'host_metrics')
|
||||
op.drop_index('idx_host_metrics_collected_at', 'host_metrics')
|
||||
op.drop_index('idx_host_metrics_host_id', 'host_metrics')
|
||||
op.drop_table('host_metrics')
|
||||
@@ -0,0 +1,42 @@
|
||||
"""Add detailed CPU/disk fields to host_metrics
|
||||
|
||||
Revision ID: 0006_add_host_metrics_details
|
||||
Revises: 0005_add_host_metrics
|
||||
Create Date: 2025-12-12
|
||||
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision: str = '0006_add_host_metrics_details'
|
||||
down_revision: Union[str, None] = '0005_add_host_metrics'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column('host_metrics', sa.Column('cpu_cores', sa.Integer(), nullable=True))
|
||||
op.add_column('host_metrics', sa.Column('cpu_threads', sa.Integer(), nullable=True))
|
||||
op.add_column('host_metrics', sa.Column('cpu_threads_per_core', sa.Integer(), nullable=True))
|
||||
op.add_column('host_metrics', sa.Column('cpu_sockets', sa.Integer(), nullable=True))
|
||||
op.add_column('host_metrics', sa.Column('cpu_mhz', sa.Float(), nullable=True))
|
||||
op.add_column('host_metrics', sa.Column('cpu_max_mhz', sa.Float(), nullable=True))
|
||||
op.add_column('host_metrics', sa.Column('cpu_min_mhz', sa.Float(), nullable=True))
|
||||
|
||||
op.add_column('host_metrics', sa.Column('disk_devices', sa.JSON(), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column('host_metrics', 'disk_devices')
|
||||
|
||||
op.drop_column('host_metrics', 'cpu_min_mhz')
|
||||
op.drop_column('host_metrics', 'cpu_max_mhz')
|
||||
op.drop_column('host_metrics', 'cpu_mhz')
|
||||
op.drop_column('host_metrics', 'cpu_sockets')
|
||||
op.drop_column('host_metrics', 'cpu_threads_per_core')
|
||||
op.drop_column('host_metrics', 'cpu_threads')
|
||||
op.drop_column('host_metrics', 'cpu_cores')
|
||||
@@ -0,0 +1,47 @@
|
||||
"""Add alerts table
|
||||
|
||||
Revision ID: 0007_add_alerts_table
|
||||
Revises: 0006_add_host_metrics_details
|
||||
Create Date: 2025-12-12
|
||||
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision: str = '0007_add_alerts_table'
|
||||
down_revision: Union[str, None] = '0006_add_host_metrics_details'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
'alerts',
|
||||
sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True),
|
||||
sa.Column('user_id', sa.Integer(), sa.ForeignKey('users.id', ondelete='SET NULL'), nullable=True),
|
||||
sa.Column('category', sa.String(length=50), nullable=False),
|
||||
sa.Column('level', sa.String(length=20), nullable=True),
|
||||
sa.Column('title', sa.String(length=255), nullable=True),
|
||||
sa.Column('message', sa.Text(), nullable=False),
|
||||
sa.Column('source', sa.String(length=50), nullable=True),
|
||||
sa.Column('details', sa.JSON(), nullable=True),
|
||||
sa.Column('read_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=False),
|
||||
)
|
||||
|
||||
op.create_index('idx_alerts_created_at', 'alerts', ['created_at'])
|
||||
op.create_index('idx_alerts_user_id', 'alerts', ['user_id'])
|
||||
op.create_index('idx_alerts_category', 'alerts', ['category'])
|
||||
op.create_index('idx_alerts_read_at', 'alerts', ['read_at'])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index('idx_alerts_read_at', table_name='alerts')
|
||||
op.drop_index('idx_alerts_category', table_name='alerts')
|
||||
op.drop_index('idx_alerts_user_id', table_name='alerts')
|
||||
op.drop_index('idx_alerts_created_at', table_name='alerts')
|
||||
op.drop_table('alerts')
|
||||
@@ -0,0 +1,28 @@
|
||||
"""Add LVM/ZFS metrics fields to host_metrics
|
||||
|
||||
Revision ID: 0008_add_lvm_zfs_metrics
|
||||
Revises: 0007_add_alerts_table
|
||||
Create Date: 2025-12-12
|
||||
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision: str = '0008_add_lvm_zfs_metrics'
|
||||
down_revision: Union[str, None] = '0007_add_alerts_table'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.add_column('host_metrics', sa.Column('lvm_info', sa.JSON(), nullable=True))
|
||||
op.add_column('host_metrics', sa.Column('zfs_info', sa.JSON(), nullable=True))
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_column('host_metrics', 'zfs_info')
|
||||
op.drop_column('host_metrics', 'lvm_info')
|
||||
@@ -0,0 +1,34 @@
|
||||
"""Add app_settings table
|
||||
|
||||
Revision ID: 0009_add_app_settings_table
|
||||
Revises: 0008_add_lvm_zfs_metrics
|
||||
Create Date: 2025-12-13
|
||||
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision: str = '0009_add_app_settings_table'
|
||||
down_revision: Union[str, None] = '0008_add_lvm_zfs_metrics'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
'app_settings',
|
||||
sa.Column('key', sa.String(length=100), primary_key=True, nullable=False),
|
||||
sa.Column('value', sa.Text(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('CURRENT_TIMESTAMP')),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('CURRENT_TIMESTAMP')),
|
||||
)
|
||||
op.create_index('idx_app_settings_updated_at', 'app_settings', ['updated_at'], unique=False)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index('idx_app_settings_updated_at', table_name='app_settings')
|
||||
op.drop_table('app_settings')
|
||||
@@ -0,0 +1,61 @@
|
||||
"""Remove foreign key constraints from logs table.
|
||||
|
||||
Revision ID: 0010
|
||||
Revises: 0009_add_app_settings_table
|
||||
Create Date: 2025-12-14
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '0010_remove_logs_foreign_keys'
|
||||
down_revision = '0009_add_app_settings_table'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
"""Remove FK constraints from logs table by recreating it."""
|
||||
# SQLite doesn't support ALTER TABLE DROP CONSTRAINT
|
||||
# We need to recreate the table without the FK constraints
|
||||
|
||||
# Create new table without FK constraints
|
||||
op.create_table(
|
||||
'logs_new',
|
||||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column('level', sa.String(), nullable=False),
|
||||
sa.Column('source', sa.String(), nullable=True),
|
||||
sa.Column('message', sa.Text(), nullable=False),
|
||||
sa.Column('details', sa.JSON(), nullable=True),
|
||||
sa.Column('host_id', sa.String(), nullable=True),
|
||||
sa.Column('task_id', sa.String(), nullable=True),
|
||||
sa.Column('schedule_id', sa.String(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
# Copy data from old table
|
||||
op.execute('''
|
||||
INSERT INTO logs_new (id, level, source, message, details, host_id, task_id, schedule_id, created_at)
|
||||
SELECT id, level, source, message, details, host_id, task_id, schedule_id, created_at FROM logs
|
||||
''')
|
||||
|
||||
# Drop old table
|
||||
op.drop_table('logs')
|
||||
|
||||
# Rename new table
|
||||
op.rename_table('logs_new', 'logs')
|
||||
|
||||
# Recreate indexes
|
||||
op.create_index('idx_logs_created_at', 'logs', ['created_at'])
|
||||
op.create_index('idx_logs_level', 'logs', ['level'])
|
||||
op.create_index('idx_logs_source', 'logs', ['source'])
|
||||
|
||||
|
||||
def downgrade():
|
||||
"""Restore FK constraints (not recommended)."""
|
||||
# This would require recreating with FK constraints
|
||||
# For simplicity, we don't implement downgrade
|
||||
pass
|
||||
Reference in New Issue
Block a user