-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5ceb60
commit aa2079b
Showing
7 changed files
with
237 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add `AuditLog` table to record `ImageAuditLogOperationType` events. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/ai/backend/manager/models/alembic/versions/683ca0a32f41_add_imageauditlogrow_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Add AuditLog table | ||
Revision ID: 683ca0a32f41 | ||
Revises: 8f85e9d0bd4e | ||
Create Date: 2025-02-14 10:56:10.191119 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
from ai.backend.manager.models.base import GUID | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "683ca0a32f41" | ||
down_revision = "8f85e9d0bd4e" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
op.create_table( | ||
"audit_logs", | ||
sa.Column("id", GUID(), server_default=sa.text("uuid_generate_v4()"), nullable=False), | ||
sa.Column("entity_type", sa.String, nullable=False), | ||
sa.Column("operation", sa.String, nullable=False), | ||
sa.Column("entity_id", sa.String, nullable=False), | ||
sa.Column( | ||
"created_at", | ||
sa.DateTime(timezone=True), | ||
server_default=sa.text("now()"), | ||
nullable=True, | ||
), | ||
sa.PrimaryKeyConstraint("id", name=op.f("pk_audit_logs")), | ||
) | ||
op.create_index(op.f("ix_audit_logs_created_at"), "audit_logs", ["created_at"], unique=False) | ||
op.create_index(op.f("ix_audit_logs_entity_type"), "audit_logs", ["entity_type"], unique=False) | ||
op.create_index(op.f("ix_audit_logs_operation"), "audit_logs", ["operation"], unique=False) | ||
op.create_index(op.f("ix_audit_logs_entity_id"), "audit_logs", ["entity_id"], unique=False) | ||
|
||
|
||
def downgrade() -> None: | ||
op.drop_index(op.f("ix_audit_logs_created_at"), table_name="audit_logs") | ||
op.drop_index(op.f("ix_audit_logs_entity_type"), table_name="audit_logs") | ||
op.drop_index(op.f("ix_audit_logs_operation"), table_name="audit_logs") | ||
op.drop_index(op.f("ix_audit_logs_entity_id"), table_name="audit_logs") | ||
op.drop_table("audit_logs") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from __future__ import annotations | ||
|
||
import enum | ||
import logging | ||
import uuid | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from ai.backend.logging import BraceStyleAdapter | ||
|
||
from .base import ( | ||
Base, | ||
IDColumn, | ||
) | ||
|
||
log = BraceStyleAdapter(logging.getLogger(__spec__.name)) | ||
|
||
__all__ = ("AuditLogRow", "ImageAuditLogOperationType") | ||
|
||
|
||
class AuditLogEntityType(enum.StrEnum): | ||
IMAGE = "image" | ||
|
||
|
||
class ImageAuditLogOperationType(enum.StrEnum): | ||
SESSION_CREATE = "session_create" | ||
UPDATE = "update" # Rescan and update image metadata | ||
PULL = "pull" # Pull image from the registry | ||
|
||
|
||
AuditLogOperationType = ImageAuditLogOperationType | ||
|
||
|
||
class AuditLogRow(Base): | ||
__tablename__ = "audit_logs" | ||
|
||
id = IDColumn("id") | ||
|
||
entity_type = sa.Column("entity_type", sa.String, index=True, nullable=False) | ||
operation = sa.Column("operation", sa.String, index=True, nullable=False) | ||
|
||
entity_id = sa.Column( | ||
"entity_id", | ||
sa.String, | ||
nullable=False, | ||
index=True, | ||
) | ||
|
||
created_at = sa.Column( | ||
"created_at", | ||
sa.DateTime(timezone=True), | ||
server_default=sa.func.now(), | ||
index=True, | ||
) | ||
|
||
def __init__( | ||
self, | ||
entity_type: AuditLogEntityType, | ||
operation: AuditLogOperationType, | ||
entity_id: str | uuid.UUID, | ||
): | ||
self.entity_type = entity_type.value | ||
self.operation = operation.value | ||
self.entity_id = str(entity_id) if isinstance(entity_id, uuid.UUID) else entity_id | ||
|
||
def __str__(self) -> str: | ||
return ( | ||
f"AuditLogRow(" | ||
f"entity_type: {self.entity_type}, " | ||
f"operation: {self.operation}, " | ||
f"created_at: {self.created_at}, " | ||
f"entity_id: {self.entity_id}" | ||
f")" | ||
) | ||
|
||
def __repr__(self) -> str: | ||
return self.__str__() | ||
|
||
@classmethod | ||
async def report_image( | ||
cls, | ||
db_session: AsyncSession, | ||
entity_type: AuditLogEntityType, | ||
operation: ImageAuditLogOperationType, | ||
entity_id: str | uuid.UUID, | ||
) -> None: | ||
db_session.add(cls(entity_type, operation, entity_id)) | ||
await db_session.flush() |
Oops, something went wrong.