d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
79 lines
2.8 KiB
TypeScript
79 lines
2.8 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*
|
|
* Tests for the pure helpers used by `backfill-api-key-hash.ts`. The script
|
|
* itself does I/O against Postgres and is not tested here; idempotency is a
|
|
* property of the helpers — running them twice on the same input produces the
|
|
* same output, so re-running the script after an interruption recomputes the
|
|
* same hash for every row.
|
|
*/
|
|
import { createCipheriv } from 'crypto'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { deriveKeyHashForStoredKey, hashApiKey, isEncryptedKey } from './backfill-api-key-hash'
|
|
|
|
const FIXED_ENCRYPTION_KEY = '0'.repeat(64)
|
|
|
|
function encryptForTest(plainKey: string, keyHex: string): string {
|
|
const key = Buffer.from(keyHex, 'hex')
|
|
const iv = Buffer.from('11'.repeat(16), 'hex')
|
|
const cipher = createCipheriv('aes-256-gcm', key, iv, { authTagLength: 16 })
|
|
let encrypted = cipher.update(plainKey, 'utf8', 'hex')
|
|
encrypted += cipher.final('hex')
|
|
const authTag = cipher.getAuthTag()
|
|
return `${iv.toString('hex')}:${encrypted}:${authTag.toString('hex')}`
|
|
}
|
|
|
|
describe('hashApiKey', () => {
|
|
it('is deterministic', () => {
|
|
expect(hashApiKey('sim_abc')).toBe(hashApiKey('sim_abc'))
|
|
})
|
|
|
|
it('returns a 64-char hex digest', () => {
|
|
expect(hashApiKey('sim_abc')).toMatch(/^[0-9a-f]{64}$/)
|
|
})
|
|
})
|
|
|
|
describe('isEncryptedKey', () => {
|
|
it('detects "iv:encrypted:authTag" format', () => {
|
|
expect(isEncryptedKey('aa:bb:cc')).toBe(true)
|
|
})
|
|
|
|
it('rejects plain text', () => {
|
|
expect(isEncryptedKey('sim_plain')).toBe(false)
|
|
})
|
|
|
|
it('rejects strings with the wrong number of colons', () => {
|
|
expect(isEncryptedKey('a:b')).toBe(false)
|
|
expect(isEncryptedKey('a:b:c:d')).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('deriveKeyHashForStoredKey — backfill idempotency', () => {
|
|
it('hashes a legacy plain-text row', () => {
|
|
const hash = deriveKeyHashForStoredKey('sim_legacy-plain', null)
|
|
expect(hash).toBe(hashApiKey('sim_legacy-plain'))
|
|
})
|
|
|
|
it('decrypts + hashes an encrypted row, matching the plaintext hash', () => {
|
|
const plainKey = 'sk-sim-some-example-key'
|
|
const encrypted = encryptForTest(plainKey, FIXED_ENCRYPTION_KEY)
|
|
|
|
const hash = deriveKeyHashForStoredKey(encrypted, FIXED_ENCRYPTION_KEY)
|
|
expect(hash).toBe(hashApiKey(plainKey))
|
|
})
|
|
|
|
it('produces the same hash when re-run on the same row', () => {
|
|
const plainKey = 'sk-sim-idempotent'
|
|
const encrypted = encryptForTest(plainKey, FIXED_ENCRYPTION_KEY)
|
|
|
|
const first = deriveKeyHashForStoredKey(encrypted, FIXED_ENCRYPTION_KEY)
|
|
const second = deriveKeyHashForStoredKey(encrypted, FIXED_ENCRYPTION_KEY)
|
|
expect(first).toBe(second)
|
|
})
|
|
|
|
it('throws when the row looks encrypted but no encryption key is supplied', () => {
|
|
const encrypted = encryptForTest('sk-sim-x', FIXED_ENCRYPTION_KEY)
|
|
expect(() => deriveKeyHashForStoredKey(encrypted, null)).toThrow(/API_ENCRYPTION_KEY/)
|
|
})
|
|
})
|