Skip to content
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

feat(core): support base64 format OIDC_PRIVATE_KEYS config in .env file #1903

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 14 additions & 6 deletions packages/core/src/env-set/oidc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,28 @@ describe('oidc env-set', () => {
jest.resetModules();
});

it('should read OIDC private keys if `OIDC_PRIVATE_KEYS` is provided', async () => {
process.env.OIDC_PRIVATE_KEYS = 'foo, bar';
it('should read OIDC private keys if raw `OIDC_PRIVATE_KEYS` is provided', async () => {
const rawKeys = [
'-----BEGIN PRIVATE KEY-----\nFOO\n-----END PRIVATE KEY-----',
'-----BEGIN PRIVATE KEY-----\nBAR\n-----END PRIVATE KEY-----',
];
process.env.OIDC_PRIVATE_KEYS = rawKeys.join(',');

const privateKeys = await readPrivateKeys();

expect(privateKeys).toEqual(['foo', 'bar']);
expect(privateKeys).toEqual([
'-----BEGIN PRIVATE KEY-----\nFOO\n-----END PRIVATE KEY-----',
'-----BEGIN PRIVATE KEY-----\nBAR\n-----END PRIVATE KEY-----',
]);
});

it('should read OIDC private keys if provided `OIDC_PRIVATE_KEYS` contain newline characters', async () => {
process.env.OIDC_PRIVATE_KEYS = 'foo\nbar, bob\noop';
it('should read OIDC private keys if base64-formatted `OIDC_PRIVATE_KEYS` is provided', async () => {
xiaoyijun marked this conversation as resolved.
Show resolved Hide resolved
const base64Keys = ['foo', 'bar'].map((key) => Buffer.from(key, 'utf8').toString('base64'));
process.env.OIDC_PRIVATE_KEYS = base64Keys.join(',');

const privateKeys = await readPrivateKeys();

expect(privateKeys).toEqual(['foo\nbar', 'bob\noop']);
expect(privateKeys).toEqual(['foo', 'bar']);
});

it('should read OIDC private keys if `OIDC_PRIVATE_KEY_PATHS` is provided', async () => {
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/env-set/oidc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const defaultLogtoOidcPrivateKeyPath = './oidc-private-key.pem';

const listFormatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });

const isBase64FormatPrivateKey = (key: string) => !key.includes('-');

/**
* Try to read private keys with the following order:
*
Expand All @@ -30,7 +32,13 @@ export const readPrivateKeys = async (): Promise<string[]> => {
const privateKeys = getEnvAsStringArray('OIDC_PRIVATE_KEYS');

if (privateKeys.length > 0) {
return privateKeys;
return privateKeys.map((key) => {
if (isBase64FormatPrivateKey(key)) {
return Buffer.from(key, 'base64').toString('utf8');
}

return key;
});
}

const privateKeyPaths = getEnvAsStringArray('OIDC_PRIVATE_KEY_PATHS');
Expand Down