Skip to content

Commit

Permalink
feat: add support for in-memory client certificates
Browse files Browse the repository at this point in the history
  • Loading branch information
emilioalvap committed Sep 9, 2024
1 parent 2b28c62 commit e9ac3cb
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 22 deletions.
19 changes: 18 additions & 1 deletion __tests__/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

import { CliArgs } from '../src/common_types';
import { normalizeOptions } from '../src/options';
import { normalizeOptions, parsePlaywrightOptions } from '../src/options';
import { join } from 'path';

describe('options', () => {
Expand Down Expand Up @@ -148,4 +148,21 @@ describe('options', () => {
},
});
});

it('cli parses playwrightOptions.clientCertificates correctly', async () => {
const test = {
clientCertificates: [
{
key: Buffer.from('This should be revived'),
cert: Buffer.from('This should be revived'),
pass: Buffer.from('This should not be revived'),
},
],
};
const result = parsePlaywrightOptions(JSON.stringify(test));

expect(Buffer.isBuffer(result?.clientCertificates[0].cert)).toBeTruthy();
expect(Buffer.isBuffer(result?.clientCertificates[0].key)).toBeTruthy();
expect(Buffer.isBuffer(result?.clientCertificates[0].pass)).toBeFalsy();
});
});
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
"kleur": "^4.1.5",
"micromatch": "^4.0.7",
"pirates": "^4.0.5",
"playwright": "=1.45.1",
"playwright-chromium": "=1.45.1",
"playwright-core": "=1.45.1",
"playwright": "=1.47.0",
"playwright-chromium": "=1.47.0",
"playwright-core": "=1.47.0",
"semver": "^7.5.4",
"sharp": "^0.33.5",
"snakecase-keys": "^4.0.1",
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export async function readConfig(
options = await optionsPromise;
}
}

return options;
}

Expand Down
4 changes: 2 additions & 2 deletions src/formatter/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import {
JavaScriptLanguageGenerator,
JavaScriptFormatter,
} from 'playwright-core/lib/server/recorder/javascript';
} from 'playwright-core/lib/server/codegen/javascript';

export type Step = {
actions: ActionInContext[];
Expand Down Expand Up @@ -272,7 +272,7 @@ export class SyntheticsGenerator extends JavaScriptLanguageGenerator {
if (isAssert && action.command) {
formatter.add(toAssertCall(pageAlias, action));
} else {
formatter.add(super._generateActionCall(subject, action));
formatter.add(super._generateActionCall(subject, actionInContext));
}

if (signals.popup)
Expand Down
41 changes: 39 additions & 2 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ export async function normalizeOptions(
*/
const playwrightOpts = merge(
config.playwrightOptions,
cliArgs.playwrightOptions || {}
cliArgs.playwrightOptions || {},
{
arrayMerge(target, source) {
if (source && source.length > 0) {
return [...new Set(source)];
}
return target;
},
}
);
options.playwrightOptions = {
...playwrightOpts,
Expand Down Expand Up @@ -195,7 +203,7 @@ export function getCommonCommandOpts() {
const playwrightOpts = createOption(
'--playwright-options <jsonstring>',
'JSON object to pass in custom Playwright options for the agent. Options passed will be merged with Playwright options defined in your synthetics.config.js file.'
).argParser(JSON.parse);
).argParser(parsePlaywrightOptions);

const pattern = createOption(
'--pattern <pattern>',
Expand Down Expand Up @@ -237,3 +245,32 @@ export function getCommonCommandOpts() {
match,
};
}

export function parsePlaywrightOptions(playwrightOpts: string) {
return JSON.parse(playwrightOpts, (key, value) => {
if (key !== 'clientCertificates') {
return value;
}

// Revive serialized clientCertificates buffer objects
return (value ?? []).map(item => {
const revived = { ...item };
if (item.cert && !Buffer.isBuffer(item.cert)) {
revived.cert = parseAsBuffer(item.cert);
}
if (item.key && !Buffer.isBuffer(item.key)) {
revived.key = parseAsBuffer(item.key);
}

return revived;
});
});
}

function parseAsBuffer(value: any): Buffer {
try {
return Buffer.from(value);
} catch (e) {
return value;
}
}

0 comments on commit e9ac3cb

Please sign in to comment.