Skip to content

Add audit logging support to Redis, Drizzle, and Prisma storage adapters #4

@izadoesdev

Description

@izadoesdev

Problem

Audit logging functionality is currently only implemented in the MemoryStore (src/storage/memory.ts). The Redis, Drizzle, and Prisma storage adapters don't support audit logging operations, meaning users who want to use audit logging are limited to the in-memory storage.

Current State

The Storage interface defines optional audit logging methods (src/types/storage-types.ts):

  • saveLog?(log: AuditLog): Promise<void>
  • findLogs?(query: AuditLogQuery): Promise<AuditLog[]>
  • countLogs?(query: AuditLogQuery): Promise<number>
  • deleteLogs?(query: AuditLogQuery): Promise<number>
  • getLogStats?(ownerId: string): Promise<AuditLogStats>

Implemented:

  • MemoryStore - All 5 methods implemented (lines 76-209)

Not Implemented:

  • RedisStore - No audit logging methods
  • DrizzleStore - No audit logging methods
  • PrismaStore - No audit logging methods

Expected Behavior

Users should be able to enable audit logging with any storage adapter:

// Should work with any storage adapter
const keys = createKeys({
  storage: new RedisStore({ client: redis }),
  auditLogs: true, // Currently only works with MemoryStore
})

await keys.create({ ownerId: 'user_123' }, { userId: 'admin_456' })

Proposed Implementation

RedisStore

Use Redis Sorted Sets or Lists to store audit logs:

  • Key pattern: auditlog:{keyId} or auditlog:owner:{ownerId}
  • Use timestamps as scores for sorting
  • Implement filtering and pagination using Redis commands

DrizzleStore

Add an audit log table to the schema:

export const auditLog = pgTable('audit_log', {
  id: text().primaryKey(),
  action: text().notNull(),
  keyId: text().notNull(),
  ownerId: text().notNull(),
  timestamp: timestamp().notNull(),
  data: jsonb('data'),
  // Indexes for efficient queries
})

PrismaStore

Similar to Drizzle, add a Prisma model:

model AuditLog {
  id        String   @id
  action    String
  keyId     String
  ownerId   String
  timestamp DateTime
  data      Json?
  
  @@index([keyOf])
  @@index([ownerId])
  @@index([timestamp])
}

Related Files

  • src/storage/memory.ts - Reference implementation (lines 76-209)
  • src/storage/redis.ts - Needs audit logging methods
  • src/storage/drizzle.ts - Needs audit logging methods
  • src/storage/prisma.ts - Needs audit logging methods
  • src/types/audit-log-types.ts - Type definitions
  • src/types/storage-types.ts - Storage interface

Benefits

  • Users can track API key operations in production databases
  • Consistent audit logging across all storage adapters
  • Better compliance and security monitoring capabilities
  • No need to switch to memory storage for audit logging

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions