Skip to content

Commit

Permalink
Add validation for cert paths when pushing to cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
emilioalvap committed Sep 13, 2024
1 parent 6145794 commit c64818e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 17 deletions.
27 changes: 27 additions & 0 deletions __tests__/push/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Push abort on push with clientCertificate.certPath used in cloud 1`] = `
"Aborted. Invalid synthetics project settings.
Certificate path options (certPath, keyPath, pfxPath) are not supported on cloud locations, use in-memory alternatives (cert, key, pfx) when running on cloud.
Run 'npx @elastic/synthetics init' to create project with default settings.
"
`;

exports[`Push abort on push with clientCertificate.keyPath used in cloud 1`] = `
"Aborted. Invalid synthetics project settings.
Certificate path options (certPath, keyPath, pfxPath) are not supported on cloud locations, use in-memory alternatives (cert, key, pfx) when running on cloud.
Run 'npx @elastic/synthetics init' to create project with default settings.
"
`;

exports[`Push abort on push with clientCertificate.pfxPath used in cloud 1`] = `
"Aborted. Invalid synthetics project settings.
Certificate path options (certPath, keyPath, pfxPath) are not supported on cloud locations, use in-memory alternatives (cert, key, pfx) when running on cloud.
Run 'npx @elastic/synthetics init' to create project with default settings.
"
`;

exports[`Push error on empty project id 1`] = `
"Aborted. Invalid synthetics project settings.
Expand Down
53 changes: 38 additions & 15 deletions __tests__/push/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ describe('Push', () => {
) {
await writeFile(
join(PROJECT_DIR, filename),
`export default { monitor: ${JSON.stringify(
monitor
)}, project: ${JSON.stringify(settings)} }`
`export default ${JSON.stringify({ ...settings, monitor })}`
);
}

Expand Down Expand Up @@ -89,20 +87,23 @@ describe('Push', () => {
});

it('error on invalid location', async () => {
await fakeProjectSetup({ id: 'test-project' }, {});
await fakeProjectSetup({ project: { id: 'test-project' } }, {});
const output = await runPush();
expect(output).toMatchSnapshot();
});

it('error when schedule is not present', async () => {
await fakeProjectSetup({ id: 'test-project' }, { locations: ['test-loc'] });
await fakeProjectSetup(
{ project: { id: 'test-project' } },
{ locations: ['test-loc'] }
);
const output = await runPush();
expect(output).toMatchSnapshot();
});

it('error on invalid schedule', async () => {
await fakeProjectSetup(
{ id: 'test-project' },
{ project: { id: 'test-project' } },
{ locations: ['test-loc'], schedule: 12 }
);
const output = await runPush();
Expand All @@ -111,7 +112,7 @@ describe('Push', () => {

it('abort on push with different project id', async () => {
await fakeProjectSetup(
{ id: 'test-project' },
{ project: { id: 'test-project' } },
{ locations: ['test-loc'], schedule: 3 }
);
const output = await runPush(
Expand All @@ -125,7 +126,13 @@ describe('Push', () => {

it('error on invalid schedule in monitor DSL', async () => {
await fakeProjectSetup(
{ id: 'test-project', space: 'dummy', url: 'http://localhost:8080' },
{
project: {
id: 'test-project',
space: 'dummy',
url: 'http://localhost:8080',
},
},
{ locations: ['test-loc'], schedule: 3 }
);
const testJourney = join(PROJECT_DIR, 'test.journey.ts');
Expand All @@ -141,7 +148,7 @@ journey('journey 1', () => monitor.use({ id: 'j1', schedule: 8 }));`

it('errors on duplicate browser monitors', async () => {
await fakeProjectSetup(
{ id: 'test-project', space: 'dummy', url: server.PREFIX },
{ project: { id: 'test-project', space: 'dummy', url: server.PREFIX } },
{ locations: ['test-loc'], schedule: 3 }
);

Expand All @@ -164,7 +171,7 @@ journey('duplicate name', () => monitor.use({ schedule: 15 }));`

it('warn if throttling config is set', async () => {
await fakeProjectSetup(
{ id: 'test-project' },
{ project: { id: 'test-project' } },
{ locations: ['test-loc'], schedule: 3 }
);
const testJourney = join(PROJECT_DIR, 'test.journey.ts');
Expand All @@ -180,7 +187,7 @@ journey('duplicate name', () => monitor.use({ schedule: 15 }));`

it('errors on duplicate lightweight monitors', async () => {
await fakeProjectSetup(
{ id: 'test-project', space: 'dummy', url: server.PREFIX },
{ project: { id: 'test-project', space: 'dummy', url: server.PREFIX } },
{ locations: ['test-loc'], schedule: 3 }
);

Expand Down Expand Up @@ -220,7 +227,7 @@ heartbeat.monitors:

it('error on invalid CHUNK SIZE', async () => {
await fakeProjectSetup(
{ id: 'test-project', space: 'dummy', url: server.PREFIX },
{ project: { id: 'test-project', space: 'dummy', url: server.PREFIX } },
{ locations: ['test-loc'], schedule: 3 }
);
const output = await runPush(undefined, { CHUNK_SIZE: '251' });
Expand All @@ -231,7 +238,7 @@ heartbeat.monitors:

it('respects valid CHUNK SIZE', async () => {
await fakeProjectSetup(
{ id: 'test-project', space: 'dummy', url: server.PREFIX },
{ project: { id: 'test-project', space: 'dummy', url: server.PREFIX } },
{ locations: ['test-loc'], schedule: 3 }
);
const testJourney = join(PROJECT_DIR, 'chunk.journey.ts');
Expand Down Expand Up @@ -260,7 +267,9 @@ heartbeat.monitors:
beforeAll(async () => {
server = await createKibanaTestServer(version);
await fakeProjectSetup(
{ id: 'test-project', space: 'dummy', url: server.PREFIX },
{
project: { id: 'test-project', space: 'dummy', url: server.PREFIX },
},
{ locations: ['test-loc'], schedule: 3 }
);
});
Expand Down Expand Up @@ -314,7 +323,7 @@ heartbeat.monitors:
journey('journey 1', () => monitor.use({ id: 'j1' }));`
);
await fakeProjectSetup(
{ id: 'bar', space: 'dummy', url: server.PREFIX },
{ project: { id: 'bar', space: 'dummy', url: server.PREFIX } },
{ locations: ['test-loc'], schedule: 3 },
'synthetics.config.test.ts'
);
Expand All @@ -330,4 +339,18 @@ heartbeat.monitors:
});
});
});

['certPath', 'keyPath', 'pfxPath'].forEach(key => {
it(`abort on push with clientCertificate.${key} used in cloud`, async () => {
await fakeProjectSetup(
{
project: { id: 'test-project', space: 'dummy', url: server.PREFIX },
playwrightOptions: { clientCertificates: [{ [key]: 'test.file' }] },
},
{ locations: ['test-loc'], schedule: 3 }
);
const output = await runPush();
expect(output).toMatchSnapshot();
});
});
});
12 changes: 10 additions & 2 deletions src/push/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,16 @@ export function validateSettings(opts: PushOptions) {
- CLI '--schedule <mins>'
- Config file 'monitors.schedule' field`;
} else if (opts.schedule && !ALLOWED_SCHEDULES.includes(opts.schedule)) {
reason = `Set default schedule(${opts.schedule
}) to one of the allowed values - ${ALLOWED_SCHEDULES.join(',')}`;
reason = `Set default schedule(${
opts.schedule
}) to one of the allowed values - ${ALLOWED_SCHEDULES.join(',')}`;
} else if (
opts.locations &&
(opts?.playwrightOptions?.clientCertificates ?? []).filter(cert => {
return cert.certPath || cert.keyPath || cert.pfxPath;
}).length > 0
) {
reason = `Certificate path options (certPath, keyPath, pfxPath) are not supported on cloud locations, use in-memory alternatives (cert, key, pfx) when running on cloud.`;
}

if (!reason) return;
Expand Down

0 comments on commit c64818e

Please sign in to comment.