8000 feat: load secret from files by sweatybridge · Pull Request #337 · supabase/postgres-meta · GitHub
[go: up one dir, main page]

Skip to content

feat: load secret from files #337

8000
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/lib/secrets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Use dynamic import to support module mock
const fs = await import('fs/promises')

export const getSecret = async (key: string) => {
if (!key) {
return ''
}

const env = process.env[key]
if (env) {
return env
}

const file = process.env[key + '_FILE']
if (!file) {
return ''
}

return await fs.readFile(file, { encoding: 'utf8' }).catch((e) => {
if (e.code == 'ENOENT') {
return ''
}
throw e
})
}
6 changes: 4 additions & 2 deletions src/server/constants.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { getSecret } from '../lib/secrets.js'

export const PG_META_HOST = process.env.PG_META_HOST || '0.0.0.0'
export const PG_META_PORT = Number(process.env.PG_META_PORT || 1337)
export const CRYPTO_KEY = process.env.CRYPTO_KEY || 'SAMPLE_KEY'
export const CRYPTO_KEY = (await getSecret('CRYPTO_KEY')) || 'SAMPLE_KEY'

const PG_META_DB_HOST = process.env.PG_META_DB_HOST || 'localhost'
const PG_META_DB_NAME = process.env.PG_META_DB_NAME || 'postgres'
const PG_META_DB_USER = process.env.PG_META_DB_USER || 'postgres'
const PG_META_DB_PORT = Number(process.env.PG_META_DB_PORT) || 5432
const PG_META_DB_PASSWORD = process.env.PG_META_DB_PASSWORD || 'postgres'
const PG_META_DB_PASSWORD = (await getSecret('PG_META_DB_PASSWORD')) || 'postgres'
const PG_META_DB_SSL_MODE = process.env.PG_META_DB_SSL_MODE || 'disable'

const PG_CONN_TIMEOUT_SECS = Number(process.env.PG_CONN_TIMEOUT_SECS || 15)
Expand Down
< 10000 /div>
1 change: 1 addition & 0 deletions test/lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// TODO: Test server.
import './query'
import './config'
import './secrets'
import './version'
import './schemas'
import './types'
Expand Down
57 changes: 57 additions & 0 deletions test/lib/secrets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { jest } from '@jest/globals'

// Ref: https://jestjs.io/docs/ecmascript-modules
jest.unstable_mockModule('fs/promises', () => ({
readFile: jest.fn(),
}))
const { readFile } = await import('fs/promises')
const { getSecret } = await import('../../src/lib/secrets')

describe('getSecret', () => {
const value = 'dummy'

beforeEach(() => {
// Clears env var
jest.resetModules()
})

afterEach(() => {
delete process.env.SECRET
delete process.env.SECRET_FILE
})

it('loads from env', async () => {
process.env.SECRET = value
const res = await getSecret('SECRET')
expect(res).toBe(value)
})

it('loads from file', async () => {
process.env.SECRET_FILE = '/run/secrets/db_password'
jest.mocked(readFile).mockResolvedValueOnce(value)
const res = await getSecret('SECRET')
expect(res).toBe(value)
})

it('defaults to empty string', async () => {
expect(await getSecret('')).toBe('')
expect(await getSecret('SECRET')).toBe('')
})

it('default on file not found', async () => {
process.env.SECRET_FILE = '/run/secrets/db_password'
const e: NodeJS.ErrnoException = new Error('no such file or directory')
e.code = 'ENOENT'
jest.mocked(readFile).mockRejectedValueOnce(e)
const res = await getSecret('SECRET')
expect(res).toBe('')
})

it('throws on permission denied', async () => {
process.env.SECRET_FILE = '/run/secrets/db_password'
const e: NodeJS.ErrnoException = new Error('permission denied')
e.code = 'EACCES'
jest.mocked(readFile).mockRejectedValueOnce(e)
expect(getSecret('SECRET')).rejects.toThrow()
})
})
0