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.

3.0 KB
Raw Blame
import {
    ActivityLogItem,
    ActivityLogUserName,
    Describer,
    HumanizedChange,
    activityLogSummary,
    defaultDescriber,
} from 'lib/components/ActivityLog/humanizeActivity'

import { ActivityScope } from '~/types'

// Kept in sync with the sentinels the backend records in posthog/api/instance_settings.py.
const REDACTED = '<redacted>'
const UNSET = '<unset>'

// Secret settings are never logged in cleartext — the backend records these
// sentinels instead of the value. Translate the before/after sentinel pair into
// the operation that happened so the audit line is legible without exposing the
// raw markers. Returns null for any non-secret transition.
const describeSecretTransition = (before: unknown, after: unknown): string | null => {
    if (before === UNSET && after === REDACTED) {
        return 'set'
    }
    if (before === REDACTED && after === REDACTED) {
        return 'rotated'
    }
    if (before === REDACTED && after === UNSET) {
        return 'cleared'
    }
    return null
}

const isSentinel = (value: unknown): boolean => value === REDACTED || value === UNSET

export const instanceSettingActivityDescriber: Describer = (
    logItem: ActivityLogItem,
    asNotification?: boolean
): HumanizedChange => {
    if (logItem.scope !== ActivityScope.INSTANCE_SETTING || logItem.activity !== 'updated') {
        return defaultDescriber(logItem, asNotification)
    }

    const change = logItem.detail.changes?.[0]
    if (!change) {
        return defaultDescriber(logItem, asNotification)
    }

    const key = change.field || logItem.detail.name || 'unknown setting'
    const transition = describeSecretTransition(change.before, change.after)
    const actor = <ActivityLogUserName logItem={logItem} />

    if (transition) {
        return {
            summary: activityLogSummary(logItem, `${transition} the instance setting`, <code>{key}</code>),
            description: (
                <>
                    {actor} {transition} instance setting <code>{key}</code>
                </>
            ),
        }
    }

    // A secret transition we don't have a verb for: render a generic line rather than
    // echo the raw sentinel into the audit log.
    if (isSentinel(change.before) || isSentinel(change.after)) {
        return {
            summary: activityLogSummary(logItem, 'Updated the instance setting', <code>{key}</code>),
            description: (
                <>
                    {actor} updated instance setting <code>{key}</code>
                </>
            ),
        }
    }

    return {
        summary: activityLogSummary(
            logItem,
            'Changed the instance setting',
            <code>{key}</code>,
            JSON.stringify(change.after)
        ),
        description: (
            <>
                {actor} changed instance setting <code>{key}</code> from <code>{JSON.stringify(change.before)}</code> to{' '}
                <code>{JSON.stringify(change.after)}</code>
            </>
        ),
    }
}