Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a39a092206 | |||
| f20d19cf4b | |||
| 3a5f893b2a |
@@ -0,0 +1,109 @@
|
||||
-- Migration 0047: Enhanced Audit Logging for Authentication Events
|
||||
-- AEG-AUTH-001: Comprehensive audit trail for security compliance
|
||||
-- Created: 2026-08-18
|
||||
-- Status: READY FOR DEPLOYMENT
|
||||
|
||||
BEGIN;
|
||||
|
||||
-- 1. CREATE ENHANCED AUDIT LOG TABLE
|
||||
CREATE TABLE IF NOT EXISTS public.auth_audit_log (
|
||||
audit_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
|
||||
-- Event Classification
|
||||
event_type VARCHAR(50) NOT NULL
|
||||
CHECK (event_type IN ('LOGIN', 'LOGOUT', 'MFA_SETUP', 'MFA_VERIFY', 'TOKEN_REFRESH', 'PERMISSION_DENIED', 'INVALID_TOKEN')),
|
||||
|
||||
-- User Information
|
||||
identity_id UUID REFERENCES public.identity(identity_id) ON DELETE SET NULL,
|
||||
username VARCHAR(255),
|
||||
role VARCHAR(100),
|
||||
|
||||
-- Request Context (for forensics)
|
||||
ip_address INET,
|
||||
user_agent TEXT,
|
||||
endpoint VARCHAR(255),
|
||||
http_method VARCHAR(10),
|
||||
|
||||
-- Result Status
|
||||
status VARCHAR(20) NOT NULL
|
||||
CHECK (status IN ('SUCCESS', 'FAILURE', 'BLOCKED')),
|
||||
|
||||
-- Error Details
|
||||
error_code VARCHAR(50),
|
||||
error_message TEXT,
|
||||
|
||||
-- Security Details
|
||||
token_claims JSONB, -- For token analysis
|
||||
authentication_method VARCHAR(50), -- JWT, Header, MFA, etc.
|
||||
|
||||
-- Lifecycle (immutable append-only)
|
||||
occurred_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
correlation_id UUID NOT NULL DEFAULT gen_random_uuid(),
|
||||
|
||||
-- Indexing
|
||||
INDEX_created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- 2. INDEXES FOR PERFORMANCE & FORENSICS
|
||||
CREATE INDEX idx_auth_audit_identity ON public.auth_audit_log(identity_id);
|
||||
CREATE INDEX idx_auth_audit_occurred_at ON public.auth_audit_log(occurred_at DESC);
|
||||
CREATE INDEX idx_auth_audit_event_type ON public.auth_audit_log(event_type);
|
||||
CREATE INDEX idx_auth_audit_correlation ON public.auth_audit_log(correlation_id);
|
||||
CREATE INDEX idx_auth_audit_ip_address ON public.auth_audit_log(ip_address);
|
||||
CREATE INDEX idx_auth_audit_status ON public.auth_audit_log(status);
|
||||
CREATE INDEX idx_auth_audit_username ON public.auth_audit_log(username);
|
||||
|
||||
-- 3. MONTHLY PARTITIONING (for large deployments)
|
||||
-- CREATE TABLE auth_audit_log_2026_08 PARTITION OF public.auth_audit_log
|
||||
-- FOR VALUES FROM ('2026-08-01') TO ('2026-09-01');
|
||||
|
||||
-- 4. IMMUTABILITY TRIGGER
|
||||
CREATE OR REPLACE FUNCTION prevent_audit_log_modification()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
IF TG_OP = 'UPDATE' OR TG_OP = 'DELETE' THEN
|
||||
RAISE EXCEPTION 'Audit logs are immutable. Operation % not allowed.', TG_OP;
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE TRIGGER auth_audit_log_immutable
|
||||
BEFORE UPDATE OR DELETE ON public.auth_audit_log
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION prevent_audit_log_modification();
|
||||
|
||||
-- 5. VIEW FOR COMPLIANCE REPORTING
|
||||
CREATE OR REPLACE VIEW public.v_auth_audit_summary AS
|
||||
SELECT
|
||||
DATE_TRUNC('hour', occurred_at) AS hour,
|
||||
event_type,
|
||||
status,
|
||||
COUNT(*) AS count,
|
||||
COUNT(DISTINCT identity_id) AS unique_users,
|
||||
COUNT(DISTINCT ip_address) AS unique_ips
|
||||
FROM public.auth_audit_log
|
||||
WHERE occurred_at > NOW() - INTERVAL '30 days'
|
||||
GROUP BY DATE_TRUNC('hour', occurred_at), event_type, status
|
||||
ORDER BY hour DESC, event_type;
|
||||
|
||||
-- 6. VIEW FOR FAILURE ANALYSIS
|
||||
CREATE OR REPLACE VIEW public.v_auth_failures AS
|
||||
SELECT
|
||||
identity_id,
|
||||
username,
|
||||
ip_address,
|
||||
event_type,
|
||||
error_code,
|
||||
error_message,
|
||||
occurred_at,
|
||||
COUNT(*) OVER (
|
||||
PARTITION BY ip_address, DATE_TRUNC('minute', occurred_at)
|
||||
ORDER BY occurred_at
|
||||
) AS attempts_per_minute
|
||||
FROM public.auth_audit_log
|
||||
WHERE status IN ('FAILURE', 'BLOCKED')
|
||||
AND occurred_at > NOW() - INTERVAL '24 hours'
|
||||
ORDER BY occurred_at DESC;
|
||||
|
||||
COMMIT;
|
||||
@@ -0,0 +1,131 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||
import { useModelListLogic, formatDate, formatPercentage } from '../useModelListLogic'
|
||||
|
||||
describe('useModelListLogic', () => {
|
||||
let logic: ReturnType<typeof useModelListLogic>
|
||||
|
||||
beforeEach(() => {
|
||||
logic = useModelListLogic()
|
||||
})
|
||||
|
||||
describe('initialization', () => {
|
||||
it('should initialize with default state', () => {
|
||||
expect(logic.models.value).toHaveLength(3)
|
||||
expect(logic.selectedModelId.value).toBe('1')
|
||||
expect(logic.filters.search).toBe('')
|
||||
expect(logic.filters.phase).toBe('')
|
||||
})
|
||||
|
||||
it('should set screen state to LOADING on mount', () => {
|
||||
expect(logic.screenState.value).toBe('LOADING')
|
||||
})
|
||||
})
|
||||
|
||||
describe('filtering', () => {
|
||||
it('should filter models by search term', () => {
|
||||
logic.filters.search = 'Alpha'
|
||||
expect(logic.filteredModels.value).toHaveLength(1)
|
||||
expect(logic.filteredModels.value[0].name).toBe('Hawkeye-Alpha')
|
||||
})
|
||||
|
||||
it('should filter models by phase', () => {
|
||||
logic.filters.phase = 'Mature'
|
||||
expect(logic.filteredModels.value).toHaveLength(1)
|
||||
expect(logic.filteredModels.value[0].phase).toBe('Mature')
|
||||
})
|
||||
|
||||
it('should filter by both search and phase', () => {
|
||||
logic.filters.search = 'Hawk'
|
||||
logic.filters.phase = 'Validate'
|
||||
expect(logic.filteredModels.value).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('should return all models when filters are empty', () => {
|
||||
expect(logic.filteredModels.value).toHaveLength(3)
|
||||
})
|
||||
|
||||
it('should be case-insensitive for search', () => {
|
||||
logic.filters.search = 'alpha'
|
||||
expect(logic.filteredModels.value).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('model selection', () => {
|
||||
it('should select model by id', () => {
|
||||
logic.selectModel('2')
|
||||
expect(logic.selectedModelId.value).toBe('2')
|
||||
})
|
||||
|
||||
it('should return selected model', () => {
|
||||
logic.selectModel('3')
|
||||
expect(logic.selectedModel.value?.name).toBe('Gamma Arbitrage')
|
||||
})
|
||||
|
||||
it('should return undefined for invalid model id', () => {
|
||||
logic.selectModel('invalid')
|
||||
expect(logic.selectedModel.value).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('search handling', () => {
|
||||
it('should set isSearching flag', async () => {
|
||||
expect(logic.isSearching.value).toBe(false)
|
||||
const searchPromise = logic.handleSearch()
|
||||
expect(logic.isSearching.value).toBe(true)
|
||||
await searchPromise
|
||||
expect(logic.isSearching.value).toBe(false)
|
||||
})
|
||||
|
||||
it('should set screen state to LOADING during search', async () => {
|
||||
expect(logic.screenState.value).not.toBe('LOADING')
|
||||
const searchPromise = logic.handleSearch()
|
||||
expect(logic.screenState.value).toBe('LOADING')
|
||||
await searchPromise
|
||||
expect(logic.screenState.value).toBe('READY')
|
||||
})
|
||||
})
|
||||
|
||||
describe('retry handling', () => {
|
||||
it('should set screen state to LOADING on retry', async () => {
|
||||
logic.screenState.value = 'ERROR'
|
||||
const retryPromise = logic.handleRetry()
|
||||
expect(logic.screenState.value).toBe('LOADING')
|
||||
await retryPromise
|
||||
expect(logic.screenState.value).toBe('READY')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('formatters', () => {
|
||||
describe('formatDate', () => {
|
||||
it('should format date to ko-KR locale', () => {
|
||||
const date = '2026-06-15'
|
||||
const result = formatDate(date)
|
||||
expect(result).toMatch(/2026.*06.*15/)
|
||||
})
|
||||
|
||||
it('should handle ISO date strings', () => {
|
||||
const date = '2026-06-15T12:30:00Z'
|
||||
const result = formatDate(date)
|
||||
expect(result).toMatch(/2026.*06.*15/)
|
||||
})
|
||||
})
|
||||
|
||||
describe('formatPercentage', () => {
|
||||
it('should format number as percentage', () => {
|
||||
expect(formatPercentage(15.2)).toBe('15.20%')
|
||||
})
|
||||
|
||||
it('should handle zero', () => {
|
||||
expect(formatPercentage(0)).toBe('0.00%')
|
||||
})
|
||||
|
||||
it('should handle decimal values', () => {
|
||||
expect(formatPercentage(98.123)).toBe('98.12%')
|
||||
})
|
||||
|
||||
it('should handle undefined/null as 0', () => {
|
||||
expect(formatPercentage(null as any)).toBe('0.00%')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,170 @@
|
||||
import { computed, reactive, ref, onMounted } from 'vue'
|
||||
import type { StandardScreenState } from '../../../shared/ui/contracts/screenContract'
|
||||
|
||||
export interface Model {
|
||||
modelId: string
|
||||
name: string
|
||||
phase: string
|
||||
active: boolean
|
||||
pbo: number
|
||||
dsr: number
|
||||
returnMtd: number
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
export interface ModelFilters {
|
||||
search: string
|
||||
phase: string
|
||||
}
|
||||
|
||||
/**
|
||||
* useModelListLogic - Encapsulates all business logic for ModelList
|
||||
* Extracted from God Component for testability and reusability
|
||||
*/
|
||||
export function useModelListLogic() {
|
||||
// Screen state management
|
||||
const screenState = ref<StandardScreenState>('READY')
|
||||
const evidence = reactive({
|
||||
asOf: new Date().toISOString(),
|
||||
version: 'v60-T04-Contract',
|
||||
})
|
||||
|
||||
// Mock data (replace with API call)
|
||||
const mockModels: Model[] = [
|
||||
{
|
||||
modelId: '1',
|
||||
name: 'Hawkeye-Alpha',
|
||||
phase: 'Validate',
|
||||
active: false,
|
||||
pbo: 15.2,
|
||||
dsr: 96.5,
|
||||
returnMtd: 12.5,
|
||||
createdAt: '2026-06-15',
|
||||
},
|
||||
{
|
||||
modelId: '2',
|
||||
name: 'Falcon-Beta',
|
||||
phase: 'Review',
|
||||
active: false,
|
||||
pbo: 18.3,
|
||||
dsr: 94.2,
|
||||
returnMtd: 8.3,
|
||||
createdAt: '2026-07-01',
|
||||
},
|
||||
{
|
||||
modelId: '3',
|
||||
name: 'Gamma Arbitrage',
|
||||
phase: 'Mature',
|
||||
active: true,
|
||||
pbo: 8.5,
|
||||
dsr: 98.1,
|
||||
returnMtd: 18.7,
|
||||
createdAt: '2026-05-10',
|
||||
},
|
||||
]
|
||||
|
||||
// Models data
|
||||
const models = ref<Model[]>(mockModels)
|
||||
const selectedModelId = ref<string>(mockModels[0]?.modelId ?? '')
|
||||
|
||||
// Filters
|
||||
const filters = reactive<ModelFilters>({
|
||||
search: '',
|
||||
phase: '',
|
||||
})
|
||||
|
||||
// UI state
|
||||
const isSearching = ref(false)
|
||||
|
||||
// Computed properties
|
||||
const filteredModels = computed(() => {
|
||||
return models.value.filter(m => {
|
||||
const matchesSearch = m.name.toLowerCase().includes(filters.search.toLowerCase())
|
||||
const matchesPhase = !filters.phase || m.phase === filters.phase
|
||||
return matchesSearch && matchesPhase
|
||||
})
|
||||
})
|
||||
|
||||
const selectedModel = computed(() =>
|
||||
models.value.find(m => m.modelId === selectedModelId.value)
|
||||
)
|
||||
|
||||
// Methods
|
||||
const selectModel = (id: string) => {
|
||||
selectedModelId.value = id
|
||||
}
|
||||
|
||||
const handleSearch = async () => {
|
||||
isSearching.value = true
|
||||
screenState.value = 'LOADING'
|
||||
try {
|
||||
// Simulate API call delay
|
||||
await new Promise(resolve => setTimeout(resolve, 300))
|
||||
screenState.value = 'READY'
|
||||
} finally {
|
||||
isSearching.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const handleRetry = async () => {
|
||||
screenState.value = 'LOADING'
|
||||
try {
|
||||
// Simulate retry delay
|
||||
await new Promise(resolve => setTimeout(resolve, 400))
|
||||
screenState.value = 'READY'
|
||||
} catch {
|
||||
screenState.value = 'ERROR'
|
||||
}
|
||||
}
|
||||
|
||||
const handleKeyDown = (e: KeyboardEvent) => {
|
||||
if (e.key === 'F3') {
|
||||
e.preventDefault()
|
||||
handleSearch()
|
||||
}
|
||||
}
|
||||
|
||||
// Initialization
|
||||
onMounted(() => {
|
||||
screenState.value = 'LOADING'
|
||||
setTimeout(() => {
|
||||
screenState.value = 'READY'
|
||||
}, 500)
|
||||
window.addEventListener('keydown', handleKeyDown)
|
||||
})
|
||||
|
||||
// Return public API
|
||||
return {
|
||||
// State
|
||||
screenState,
|
||||
evidence,
|
||||
models,
|
||||
selectedModelId,
|
||||
filters,
|
||||
isSearching,
|
||||
|
||||
// Computed
|
||||
filteredModels,
|
||||
selectedModel,
|
||||
|
||||
// Methods
|
||||
selectModel,
|
||||
handleSearch,
|
||||
handleRetry,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Format utilities (can be extracted to separate formatter.ts)
|
||||
*/
|
||||
export function formatDate(dateString: string): string {
|
||||
return new Date(dateString).toLocaleDateString('ko-KR', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
})
|
||||
}
|
||||
|
||||
export function formatPercentage(value: number): string {
|
||||
return (value || 0).toFixed(2) + '%'
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
using Dapper;
|
||||
using System.Data;
|
||||
using NpgsqlTypes;
|
||||
using KArtSell.BuildingBlocks.Data;
|
||||
|
||||
namespace KArtSell.Host.Features.Audit;
|
||||
|
||||
public interface IAuthAuditSql
|
||||
{
|
||||
Task LogAuthEventAsync(AuthAuditLogEntry entry, CancellationToken ct = default);
|
||||
Task<IEnumerable<AuthAuditLogEntry>> GetAuditLogsAsync(
|
||||
DateTime? startDate, DateTime? endDate, string? eventType, string? username,
|
||||
int limit = 100, int offset = 0, CancellationToken ct = default);
|
||||
Task<int> GetAuditLogsCountAsync(
|
||||
DateTime? startDate, DateTime? endDate, string? eventType, string? username,
|
||||
CancellationToken ct = default);
|
||||
}
|
||||
|
||||
public sealed class AuthAuditSql : IAuthAuditSql
|
||||
{
|
||||
private readonly IDbConnectionFactory _connectionFactory;
|
||||
|
||||
public AuthAuditSql(IDbConnectionFactory connectionFactory)
|
||||
{
|
||||
_connectionFactory = connectionFactory;
|
||||
}
|
||||
|
||||
public async Task LogAuthEventAsync(AuthAuditLogEntry entry, CancellationToken ct = default)
|
||||
{
|
||||
const string sql = @"
|
||||
INSERT INTO public.auth_audit_log (
|
||||
event_type, identity_id, username, role,
|
||||
ip_address, user_agent, endpoint, http_method,
|
||||
status, error_code, error_message,
|
||||
authentication_method, correlation_id
|
||||
) VALUES (
|
||||
@EventType, @IdentityId, @Username, @Role,
|
||||
@IpAddress, @UserAgent, @Endpoint, @HttpMethod,
|
||||
@Status, @ErrorCode, @ErrorMessage,
|
||||
@AuthenticationMethod, @CorrelationId
|
||||
)";
|
||||
|
||||
using var conn = await _connectionFactory.OpenAsync(ct);
|
||||
await conn.ExecuteAsync(sql, new
|
||||
{
|
||||
entry.EventType,
|
||||
entry.IdentityId,
|
||||
entry.Username,
|
||||
entry.Role,
|
||||
IpAddress = entry.IpAddress != null ? new NpgsqlInet(entry.IpAddress) : (NpgsqlInet?)null,
|
||||
entry.UserAgent,
|
||||
entry.Endpoint,
|
||||
entry.HttpMethod,
|
||||
entry.Status,
|
||||
entry.ErrorCode,
|
||||
entry.ErrorMessage,
|
||||
entry.AuthenticationMethod,
|
||||
entry.CorrelationId
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<AuthAuditLogEntry>> GetAuditLogsAsync(
|
||||
DateTime? startDate, DateTime? endDate, string? eventType, string? username,
|
||||
int limit = 100, int offset = 0, CancellationToken ct = default)
|
||||
{
|
||||
const string sql = @"
|
||||
SELECT
|
||||
audit_id AS AuditId,
|
||||
event_type AS EventType,
|
||||
identity_id AS IdentityId,
|
||||
username AS Username,
|
||||
role AS Role,
|
||||
ip_address::text AS IpAddress,
|
||||
user_agent AS UserAgent,
|
||||
endpoint AS Endpoint,
|
||||
http_method AS HttpMethod,
|
||||
status AS Status,
|
||||
error_code AS ErrorCode,
|
||||
error_message AS ErrorMessage,
|
||||
authentication_method AS AuthenticationMethod,
|
||||
occurred_at AS OccurredAt,
|
||||
correlation_id AS CorrelationId
|
||||
FROM public.auth_audit_log
|
||||
WHERE 1=1
|
||||
AND (@StartDate::timestamp IS NULL OR occurred_at >= @StartDate)
|
||||
AND (@EndDate::timestamp IS NULL OR occurred_at <= @EndDate)
|
||||
AND (@EventType IS NULL OR event_type = @EventType)
|
||||
AND (@Username IS NULL OR username ILIKE @Username)
|
||||
ORDER BY occurred_at DESC
|
||||
LIMIT @Limit OFFSET @Offset";
|
||||
|
||||
using var conn = await _connectionFactory.OpenAsync(ct);
|
||||
return await conn.QueryAsync<AuthAuditLogEntry>(sql, new
|
||||
{
|
||||
StartDate = startDate,
|
||||
EndDate = endDate,
|
||||
EventType = eventType,
|
||||
Username = username,
|
||||
Limit = limit,
|
||||
Offset = offset
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<int> GetAuditLogsCountAsync(
|
||||
DateTime? startDate, DateTime? endDate, string? eventType, string? username,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
const string sql = @"
|
||||
SELECT COUNT(*)
|
||||
FROM public.auth_audit_log
|
||||
WHERE 1=1
|
||||
AND (@StartDate::timestamp IS NULL OR occurred_at >= @StartDate)
|
||||
AND (@EndDate::timestamp IS NULL OR occurred_at <= @EndDate)
|
||||
AND (@EventType IS NULL OR event_type = @EventType)
|
||||
AND (@Username IS NULL OR username ILIKE @Username)";
|
||||
|
||||
using var conn = await _connectionFactory.OpenAsync(ct);
|
||||
return await conn.ExecuteScalarAsync<int>(sql, new
|
||||
{
|
||||
StartDate = startDate,
|
||||
EndDate = endDate,
|
||||
EventType = eventType,
|
||||
Username = username
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Audit log entry for authentication events
|
||||
/// </summary>
|
||||
public class AuthAuditLogEntry
|
||||
{
|
||||
public Guid AuditId { get; set; }
|
||||
public required string EventType { get; set; } // LOGIN, LOGOUT, MFA_SETUP, MFA_VERIFY, TOKEN_REFRESH, PERMISSION_DENIED, INVALID_TOKEN
|
||||
public Guid? IdentityId { get; set; }
|
||||
public string? Username { get; set; }
|
||||
public string? Role { get; set; }
|
||||
public string? IpAddress { get; set; }
|
||||
public string? UserAgent { get; set; }
|
||||
public string? Endpoint { get; set; }
|
||||
public string? HttpMethod { get; set; }
|
||||
public required string Status { get; set; } // SUCCESS, FAILURE, BLOCKED
|
||||
public string? ErrorCode { get; set; }
|
||||
public string? ErrorMessage { get; set; }
|
||||
public string? AuthenticationMethod { get; set; } // JWT, Header, MFA, etc.
|
||||
public DateTime OccurredAt { get; set; }
|
||||
public Guid CorrelationId { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
using FastEndpoints;
|
||||
using KArtSell.Host.Features.Audit;
|
||||
|
||||
namespace KArtSell.Host.Endpoints.Audit;
|
||||
|
||||
/// <summary>
|
||||
/// GET /api/admin/audit-logs - Retrieve authentication audit logs
|
||||
/// Requires Admin or SecurityOfficer role
|
||||
/// </summary>
|
||||
public sealed class GetAuditLogsEndpoint : Endpoint<GetAuditLogsRequest, GetAuditLogsResponse>
|
||||
{
|
||||
private readonly IAuthAuditSql _auditSql;
|
||||
|
||||
public GetAuditLogsEndpoint(IAuthAuditSql auditSql)
|
||||
{
|
||||
_auditSql = auditSql;
|
||||
}
|
||||
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/audit-logs");
|
||||
Roles("Admin", "SecurityOfficer");
|
||||
Description(x => x
|
||||
.WithName("Get Audit Logs")
|
||||
.WithDescription("Retrieve authentication audit logs for compliance reporting")
|
||||
.Accepts<GetAuditLogsRequest>("application/json")
|
||||
.Produces<GetAuditLogsResponse>(200, "application/json"));
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetAuditLogsRequest req, CancellationToken ct)
|
||||
{
|
||||
var logs = await _auditSql.GetAuditLogsAsync(
|
||||
startDate: req.StartDate,
|
||||
endDate: req.EndDate,
|
||||
eventType: req.EventType,
|
||||
username: req.Username,
|
||||
limit: req.PageSize > 1000 ? 1000 : req.PageSize, // Cap at 1000
|
||||
offset: (req.Page - 1) * req.PageSize,
|
||||
ct: ct
|
||||
);
|
||||
|
||||
var total = await _auditSql.GetAuditLogsCountAsync(
|
||||
startDate: req.StartDate,
|
||||
endDate: req.EndDate,
|
||||
eventType: req.EventType,
|
||||
username: req.Username,
|
||||
ct: ct
|
||||
);
|
||||
|
||||
var items = logs.Select(entry => new AuditLogItem
|
||||
{
|
||||
AuditId = entry.AuditId,
|
||||
EventType = entry.EventType,
|
||||
IdentityId = entry.IdentityId,
|
||||
Username = entry.Username,
|
||||
Role = entry.Role,
|
||||
IpAddress = entry.IpAddress,
|
||||
Endpoint = entry.Endpoint,
|
||||
HttpMethod = entry.HttpMethod,
|
||||
Status = entry.Status,
|
||||
ErrorCode = entry.ErrorCode,
|
||||
ErrorMessage = entry.ErrorMessage,
|
||||
OccurredAt = entry.OccurredAt
|
||||
}).ToList();
|
||||
|
||||
await Send.OkAsync(new GetAuditLogsResponse
|
||||
{
|
||||
Items = items,
|
||||
Total = total,
|
||||
Page = req.Page,
|
||||
PageSize = req.PageSize
|
||||
}, ct);
|
||||
}
|
||||
}
|
||||
|
||||
public class GetAuditLogsRequest
|
||||
{
|
||||
public DateTime? StartDate { get; set; }
|
||||
public DateTime? EndDate { get; set; }
|
||||
public string? EventType { get; set; } // LOGIN, LOGOUT, MFA_SETUP, etc.
|
||||
public string? Username { get; set; }
|
||||
public int Page { get; set; } = 1;
|
||||
public int PageSize { get; set; } = 50;
|
||||
}
|
||||
|
||||
public class GetAuditLogsResponse
|
||||
{
|
||||
public required List<AuditLogItem> Items { get; set; }
|
||||
public int Total { get; set; }
|
||||
public int Page { get; set; }
|
||||
public int PageSize { get; set; }
|
||||
}
|
||||
|
||||
public class AuditLogItem
|
||||
{
|
||||
public Guid AuditId { get; set; }
|
||||
public required string EventType { get; set; }
|
||||
public Guid? IdentityId { get; set; }
|
||||
public string? Username { get; set; }
|
||||
public string? Role { get; set; }
|
||||
public string? IpAddress { get; set; }
|
||||
public string? Endpoint { get; set; }
|
||||
public string? HttpMethod { get; set; }
|
||||
public required string Status { get; set; }
|
||||
public string? ErrorCode { get; set; }
|
||||
public string? ErrorMessage { get; set; }
|
||||
public DateTime OccurredAt { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
using System.Security.Claims;
|
||||
using KArtSell.Host.Features.Audit;
|
||||
|
||||
namespace KArtSell.Host.Middleware;
|
||||
|
||||
/// <summary>
|
||||
/// Middleware to audit authentication-related events
|
||||
/// Logs all requests to /api/auth/* endpoints
|
||||
/// </summary>
|
||||
public sealed class AuthAuditMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly ILogger<AuthAuditMiddleware> _logger;
|
||||
|
||||
public AuthAuditMiddleware(RequestDelegate next, ILogger<AuthAuditMiddleware> logger)
|
||||
{
|
||||
_next = next;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(HttpContext context, IAuthAuditSql auditSql)
|
||||
{
|
||||
var originalResponseBody = context.Response.Body;
|
||||
|
||||
try
|
||||
{
|
||||
await _next(context);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Log authentication events (async, non-blocking)
|
||||
if (IsAuthEndpoint(context.Request.Path))
|
||||
{
|
||||
_ = LogAuthEventAsync(context, auditSql);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsAuthEndpoint(PathString path)
|
||||
{
|
||||
return path.StartsWithSegments("/api/auth") ||
|
||||
path.StartsWithSegments("/api/admin/audit-logs");
|
||||
}
|
||||
|
||||
private async Task LogAuthEventAsync(HttpContext context, IAuthAuditSql auditSql)
|
||||
{
|
||||
try
|
||||
{
|
||||
var eventType = GetEventType(context.Request.Path, context.Request.Method);
|
||||
var status = GetStatus(context.Response.StatusCode);
|
||||
var identity = context.User.FindFirst(ClaimTypes.NameIdentifier);
|
||||
var role = context.User.FindFirst(ClaimTypes.Role);
|
||||
|
||||
var entry = new AuthAuditLogEntry
|
||||
{
|
||||
EventType = eventType,
|
||||
IdentityId = identity?.Value != null ? Guid.Parse(identity.Value) : null,
|
||||
Username = context.User.FindFirst(ClaimTypes.Name)?.Value,
|
||||
Role = role?.Value,
|
||||
IpAddress = context.Connection.RemoteIpAddress?.ToString(),
|
||||
UserAgent = context.Request.Headers["User-Agent"].ToString(),
|
||||
Endpoint = context.Request.Path,
|
||||
HttpMethod = context.Request.Method,
|
||||
Status = status,
|
||||
AuthenticationMethod = context.User.FindFirst("auth_mode")?.Value ?? "Unknown",
|
||||
CorrelationId = context.TraceIdentifier != null ? Guid.Parse(context.TraceIdentifier) : Guid.NewGuid()
|
||||
};
|
||||
|
||||
// Capture error details from response
|
||||
if (context.Response.StatusCode >= 400)
|
||||
{
|
||||
entry.ErrorCode = context.Response.StatusCode.ToString();
|
||||
entry.ErrorMessage = GetErrorMessage(context.Response.StatusCode);
|
||||
}
|
||||
|
||||
await auditSql.LogAuthEventAsync(entry);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to log authentication event");
|
||||
// Don't throw - audit logging failures shouldn't break request handling
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetEventType(PathString path, string method)
|
||||
{
|
||||
return path.Value switch
|
||||
{
|
||||
"/api/auth/login" => "LOGIN",
|
||||
"/api/auth/logout" => "LOGOUT",
|
||||
"/api/auth/mfa-setup" => "MFA_SETUP",
|
||||
"/api/auth/verify-mfa" => "MFA_VERIFY",
|
||||
"/api/auth/refresh" => "TOKEN_REFRESH",
|
||||
"/api/admin/audit-logs" => method == "GET" ? "AUDIT_READ" : "AUDIT_WRITE",
|
||||
_ => "AUTH_REQUEST"
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetStatus(int statusCode)
|
||||
{
|
||||
return statusCode switch
|
||||
{
|
||||
200 or 201 or 202 or 204 => "SUCCESS",
|
||||
401 or 403 => "BLOCKED",
|
||||
400 or 404 or 500 => "FAILURE",
|
||||
_ => "FAILURE"
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetErrorMessage(int statusCode)
|
||||
{
|
||||
return statusCode switch
|
||||
{
|
||||
400 => "Bad Request",
|
||||
401 => "Unauthorized",
|
||||
403 => "Forbidden",
|
||||
404 => "Not Found",
|
||||
500 => "Internal Server Error",
|
||||
_ => $"HTTP {statusCode}"
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@ using KArtSell.Host.Infrastructure;
|
||||
using KArtSell.Host.OpenApi;
|
||||
using KArtSell.Host.Observability;
|
||||
using KArtSell.Host.Features.Observability;
|
||||
using KArtSell.Host.Features.Audit;
|
||||
using KArtSell.Host.Middleware;
|
||||
using KArtSell.BuildingBlocks.Data;
|
||||
using KArtSell.BuildingBlocks.Reliability;
|
||||
using KArtSell.BuildingBlocks.Time;
|
||||
@@ -233,6 +235,9 @@ builder.Services.AddScoped<KArtSell.Modules.ModelOperations.Compliance.LogAuditE
|
||||
builder.Services.AddScoped<KArtSell.Modules.ModelOperations.Compliance.ProcessGdprRequestHandler>();
|
||||
builder.Services.AddScoped<KArtSell.Modules.ModelOperations.Compliance.GdprRedactionJob>();
|
||||
|
||||
// Authentication Audit Logging (AEG-AUTH-001)
|
||||
builder.Services.AddScoped<IAuthAuditSql, AuthAuditSql>();
|
||||
|
||||
builder.Services.AddProblemDetails();
|
||||
|
||||
const string authenticationScheme = "KArtSell";
|
||||
@@ -342,6 +347,7 @@ if (app.Environment.IsDevelopment())
|
||||
}
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseMiddleware<AuthAuditMiddleware>();
|
||||
app.UseAuthorization();
|
||||
app.UseFastEndpoints(config => config.Endpoints.RoutePrefix = "api");
|
||||
app.MapHub<KArtSell.Host.Consumers.ShadowRunHub>("/api/hubs/shadow-run");
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
namespace KArtSell.Host.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Role-Based Access Control (RBAC) constants
|
||||
/// Used in JWT claims and endpoint authorization
|
||||
/// </summary>
|
||||
public static class RoleConstants
|
||||
{
|
||||
/// <summary>
|
||||
/// System administrator - full access to all operations
|
||||
/// </summary>
|
||||
public const string Admin = "Admin";
|
||||
|
||||
/// <summary>
|
||||
/// Security officer - access to security/audit operations
|
||||
/// </summary>
|
||||
public const string SecurityOfficer = "SecurityOfficer";
|
||||
|
||||
/// <summary>
|
||||
/// Standard user - access to general operations
|
||||
/// </summary>
|
||||
public const string User = "User";
|
||||
|
||||
/// <summary>
|
||||
/// Read-only access - view operations only
|
||||
/// </summary>
|
||||
public const string Viewer = "Viewer";
|
||||
|
||||
/// <summary>
|
||||
/// All valid roles array (for validation/seeding)
|
||||
/// </summary>
|
||||
public static readonly string[] AllRoles = [Admin, SecurityOfficer, User, Viewer];
|
||||
|
||||
/// <summary>
|
||||
/// Roles with audit log access
|
||||
/// </summary>
|
||||
public static readonly string[] AuditAccessRoles = [Admin, SecurityOfficer];
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using KArtSell.Host.Security;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Xunit;
|
||||
|
||||
namespace KArtSell.Host.UnitTests.Security;
|
||||
|
||||
public sealed class RoleBasedAccessControlTests
|
||||
{
|
||||
private const string TestJwtKey = "test-secret-key-with-minimum-256-bits-length-requirement";
|
||||
private const string TestIssuer = "test-issuer";
|
||||
private const string TestAudience = "test-audience";
|
||||
|
||||
[Theory]
|
||||
[InlineData(RoleConstants.Admin)]
|
||||
[InlineData(RoleConstants.SecurityOfficer)]
|
||||
[InlineData(RoleConstants.User)]
|
||||
[InlineData(RoleConstants.Viewer)]
|
||||
public void GenerateToken_WithValidRole_CreatesClaim(string role)
|
||||
{
|
||||
// Arrange
|
||||
var username = "testuser";
|
||||
|
||||
// Act
|
||||
var token = GenerateTestToken(username, role);
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var jwtToken = handler.ReadToken(token) as JwtSecurityToken;
|
||||
|
||||
// Assert
|
||||
var roleClaim = jwtToken?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Role);
|
||||
Assert.NotNull(roleClaim);
|
||||
Assert.Equal(role, roleClaim!.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RoleConstants_ContainsAllExpectedRoles()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.Contains(RoleConstants.Admin, RoleConstants.AllRoles);
|
||||
Assert.Contains(RoleConstants.SecurityOfficer, RoleConstants.AllRoles);
|
||||
Assert.Contains(RoleConstants.User, RoleConstants.AllRoles);
|
||||
Assert.Contains(RoleConstants.Viewer, RoleConstants.AllRoles);
|
||||
Assert.Equal(4, RoleConstants.AllRoles.Length);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AuditAccessRoles_IncludesAdminAndSecurityOfficer()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.Contains(RoleConstants.Admin, RoleConstants.AuditAccessRoles);
|
||||
Assert.Contains(RoleConstants.SecurityOfficer, RoleConstants.AuditAccessRoles);
|
||||
Assert.Equal(2, RoleConstants.AuditAccessRoles.Length);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(RoleConstants.Admin, true)]
|
||||
[InlineData(RoleConstants.SecurityOfficer, true)]
|
||||
[InlineData(RoleConstants.User, false)]
|
||||
[InlineData(RoleConstants.Viewer, false)]
|
||||
public void IsAuditAccessAllowed_ChecksRoleMembership(string role, bool expectedAccess)
|
||||
{
|
||||
// Act
|
||||
var hasAccess = RoleConstants.AuditAccessRoles.Contains(role);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedAccess, hasAccess);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExtractRoleFromToken_ReturnsCorrectRole()
|
||||
{
|
||||
// Arrange
|
||||
var username = "testuser";
|
||||
var expectedRole = RoleConstants.Admin;
|
||||
var token = GenerateTestToken(username, expectedRole);
|
||||
|
||||
// Act
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var jwtToken = handler.ReadToken(token) as JwtSecurityToken;
|
||||
var actualRole = jwtToken?.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Role)?.Value;
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedRole, actualRole);
|
||||
}
|
||||
|
||||
private static string GenerateTestToken(string username, string role)
|
||||
{
|
||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(TestJwtKey));
|
||||
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||
|
||||
var claims = new[]
|
||||
{
|
||||
new Claim(ClaimTypes.NameIdentifier, username),
|
||||
new Claim(ClaimTypes.Name, username),
|
||||
new Claim(ClaimTypes.Role, role),
|
||||
new Claim("auth_mode", "jwt")
|
||||
};
|
||||
|
||||
var token = new JwtSecurityToken(
|
||||
issuer: TestIssuer,
|
||||
audience: TestAudience,
|
||||
claims: claims,
|
||||
expires: DateTime.UtcNow.AddMinutes(60),
|
||||
signingCredentials: credentials);
|
||||
|
||||
return new JwtSecurityTokenHandler().WriteToken(token);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user