Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.7 KB
Raw Blame
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import { KMSClient } from '@aws-sdk/client-kms'

import { MlKeyBatchController } from './batch-controller'
import { MlKeyEncryption } from './crypto'
import { MlKeyDynamoDB } from './dynamodb'
import { MlSessionKeyStore } from './key-store'
import { MlKeyReader } from './reader'
import { MlKafkaTransport } from './transport'

export interface MlKeyManagerConfig {
    AI_RESEARCH_REPLAY_KEY_TABLE: string
    AI_RESEARCH_REPLAY_KMS_KEY_ARN: string
    AI_RESEARCH_REPLAY_AWS_REGION: string
    AI_RESEARCH_REPLAY_KEY_CACHE_MAX: number
    AI_RESEARCH_REPLAY_KEY_CACHE_LIFETIME_MS: number
    AI_RESEARCH_REPLAY_ROW_CACHE_MAX: number
    AI_RESEARCH_REPLAY_ROW_CACHE_LIFETIME_MS: number
    AI_RESEARCH_REPLAY_KMS_REQUESTS_PER_SECOND: number
    SESSION_RECORDING_DYNAMODB_ENDPOINT?: string
}

export class MlKeyManager {
    public readonly encryption: MlKeyEncryption
    public readonly reader: MlKeyReader
    public readonly controller: MlKeyBatchController
    public readonly kafka: MlKafkaTransport
    private readonly db: MlKeyDynamoDB
    private readonly dynamo: DynamoDBClient
    private readonly kms: KMSClient

    constructor(config: MlKeyManagerConfig) {
        if (!config.AI_RESEARCH_REPLAY_KEY_TABLE || !config.AI_RESEARCH_REPLAY_KMS_KEY_ARN) {
            throw new Error('ML key manager requires a DynamoDB table and a KMS key ARN')
        }
        this.dynamo = new DynamoDBClient({
            region: config.AI_RESEARCH_REPLAY_AWS_REGION,
            endpoint: config.SESSION_RECORDING_DYNAMODB_ENDPOINT || undefined,
            maxAttempts: 3,
        })
        this.kms = new KMSClient({ region: config.AI_RESEARCH_REPLAY_AWS_REGION, maxAttempts: 3 })
        this.db = new MlKeyDynamoDB(
            this.dynamo,
            config.AI_RESEARCH_REPLAY_KEY_TABLE,
            undefined,
            undefined,
            config.AI_RESEARCH_REPLAY_ROW_CACHE_MAX,
            config.AI_RESEARCH_REPLAY_ROW_CACHE_LIFETIME_MS
        )
        this.encryption = new MlKeyEncryption(
            this.kms,
            config.AI_RESEARCH_REPLAY_KMS_KEY_ARN,
            config.AI_RESEARCH_REPLAY_KEY_CACHE_MAX,
            config.AI_RESEARCH_REPLAY_KEY_CACHE_LIFETIME_MS,
            8,
            config.AI_RESEARCH_REPLAY_KMS_REQUESTS_PER_SECOND
        )
        this.reader = new MlKeyReader(this.db, this.encryption)
        this.controller = new MlKeyBatchController(new MlSessionKeyStore(this.db, this.encryption), this.encryption)
        this.kafka = new MlKafkaTransport(this.reader)
    }

    public start(): Promise<void> {
        return Promise.resolve()
    }

    public stop(): void {
        this.encryption.clear()
        this.db.clear()
        this.dynamo.destroy()
        this.kms.destroy()
    }
}