Skip to content

Commit

Permalink
Merge pull request #3124 from snyk/test/https
Browse files Browse the repository at this point in the history
test: https connections
  • Loading branch information
Jahed Ahmed authored Apr 12, 2022
2 parents 57005e5 + 82ef3e6 commit 0935f04
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 1 deletion.
24 changes: 23 additions & 1 deletion test/acceptance/fake-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as bodyParser from 'body-parser';
import * as express from 'express';
import * as fs from 'fs';
import * as http from 'http';
import * as https from 'https';
import * as path from 'path';
import { getFixturePath } from '../jest/util/getFixturePath';

Expand All @@ -23,6 +24,10 @@ export type FakeServer = {
setFeatureFlag: (featureFlag: string, enabled: boolean) => void;
unauthorizeAction: (action: string, reason?: string) => void;
listen: (port: string | number, callback: () => void) => void;
listenWithHttps: (
port: string | number,
options: https.ServerOptions,
) => Promise<void>;
restore: () => void;
close: (callback: () => void) => void;
getPort: () => number;
Expand Down Expand Up @@ -479,7 +484,23 @@ export const fakeServer = (basePath: string, snykToken: string): FakeServer => {
});

const listen = (port: string | number, callback: () => void) => {
server = app.listen(Number(port), callback);
server = http.createServer(app).listen(Number(port), callback);
};

const listenWithHttps = (
port: string | number,
options: https.ServerOptions,
): Promise<void> => {
return new Promise((resolve, reject) => {
server = https.createServer(options, app);
server.once('listening', () => {
resolve();
});
server.once('error', (err) => {
reject(err);
});
server.listen(Number(port));
});
};

const close = (callback: () => void) => {
Expand Down Expand Up @@ -509,6 +530,7 @@ export const fakeServer = (basePath: string, snykToken: string): FakeServer => {
setFeatureFlag,
unauthorizeAction,
listen,
listenWithHttps,
restore,
close,
getPort,
Expand Down
32 changes: 32 additions & 0 deletions test/fixtures/fake-server/localhost-expired.cert
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-----BEGIN CERTIFICATE-----
MIIFjTCCA3WgAwIBAgIUPnXIRAGiCjwBFH3tf7WewZ2tztwwDQYJKoZIhvcNAQEL
BQAwVjELMAkGA1UEBhMCV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6VyCw
eAt5RrKvpwFxHa/8MA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggIB
AJGo+FygzteAayjnjeX8aCIeqFyVPl6p38GHvX3paYc4nGEQ0kDcmwU7U7Eh13BF
V9gENnhdVCCBkP/WXdEJVSZ4O/f5LjmJlaopMWcgEETtQCoDUfEtP12X/9JC9MyP
1OVkdTOtu4DrmPWjF8vG1npetlx46FsOORVohg2IBtJh10zNrhs/AcrQ/H8+3Pb8
E4lFwpTRZQKyQA/SRlex0vKDTgKbVyB/Bflk9YujISbE4J7URlGf/Ah6DU83/SFQ
Un9x2eioEz2Zqqqidf3WtGb33Krp3x0I7aKKxWepQkp8KcYNY76boJTiSle8rGEi
++Mm2tEKmPhoPaSbetlOBp4M4ZF+Ncqih2fjbVI1a8g73mkcBVJOWYNc5YAVyrOP
/HxIrSJ+rOX5EO4ysxa2qwSk9pUGCzrfAfyZmTW2D3WFZl5Wz1wV+0SQYaKysWTJ
p+8v74M+u9/w3Bse7hUJtnUjEF0WQX3uA1hNOKgfAYnTOElO2PpaKgnw2MgBtlcA
pxMTHskeN5GtkPY2s2u+cGHI/6B8WpJYV39RB39692QT+X5yiPt4kWHaEYVS1B+K
NF3MleN7SlqkdhUbJ0frFXdopmQSP1xjcIsPtkwlLzQUX++Vg+BQli4IMwFwtntL
QSngAiAPDc9URvHFQ8vIeSnAqsbIyDttu/49Qvbfdc07
-----END CERTIFICATE-----
52 changes: 52 additions & 0 deletions test/fixtures/fake-server/localhost-expired.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-----BEGIN PRIVATE KEY-----
MIIJRAIBADANBgkqhkiG9w0BAQEFAASCCS4wggkqAgEAAoICAQDdUJpyW3iB47o0
kuom/lUZGEdu81HIzGCsEETPNHFGayt9ijovTyCfYvtVzy7XrD0zKXHs5EyC96QB
bo2nxclTfZHacavihfXgKGKFP8EqMXQgY8aw+2+wZ6Ej91jnYCtQ+OrnuQCQOfCv
iEeHGtpCwt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M//1Ddk1wKDvtELefQrzmSjY61EgXikxbPHNpZAQGSyIrUW7aASyu
cVWgyKhc4Wu4//id9v0Q+pqg/dJP8tqZT1Gv0ErV7XjSqMXkWBpdHrH0tLydkbJL
rGcpx7Sgxb/9jhq4ou7XW9iST0x5XN+pY5nvAqS/AoIBAQCdK00X/InPW+nYfND6
W+E4VWZ8KILL8soPdjs9kQYygCSSt/FAGeqbc7SM7jcZNpPXMDj3R3cSRNtHY+/q
oMyQAUSJishxtY/3MOUkTOt2Oq0RYoAS1JKnnnhi/+e8c5LhiW8Ny8sjdh0vcN51
FKJEDbnVnq5J/HOtWBzzrTbahhUTIzdSyGW6udS/3/yfwRRZp+YFMFMqdmIPx95B
MYp8gzXSZQHS9XX4omdszm3lYq9cKqHFmmdonTkQ+Z978sNdVSLB9Bad5ppqlq26
SG8nEHoBH7H3Lwd04HAnroXdpR9nARNe92+Okxp1F56inqXUujMfYsm8OBw/tc6b
a8ZpAoIBAQCjfc4QPsYjY6mGdA0or+FQ1HLk7St1ZV2aHrh3NFcBy8anXLXIDdi/
xas9QUJ3mgxCS93Z+5JbeKdpCSgDIg+gjRgByOtXtVOgSJiTAP6CpzSUq9VZfmd8
fySkUWScMYkiel7Bphx/yf+JxuGXUii0TvY4OvL/m0Hd596AOSYxj1SYZqgsXEX9
IjKV/9mALyClZUmTIMayuTGdMV5+j5d1IX7XqbBkRAn4ZE+QiYimgE09FGnTKESv
/AFyklbhGI1EOWRyFHigJfZGcidGG0TPOAYYWDRuEjss2bU1e/sEOy3FQ+ob6XAS
zrjGjQtLCFdeFCWcBG+WmtY0ZKRtXDBc
-----END PRIVATE KEY-----
66 changes: 66 additions & 0 deletions test/jest/acceptance/https.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import * as fs from 'fs';
import { fakeServer, FakeServer } from '../../acceptance/fake-server';
import { createProjectFromWorkspace } from '../util/createProject';
import { getFixturePath } from '../util/getFixturePath';
import { runSnykCLI } from '../util/runSnykCLI';

jest.setTimeout(1000 * 30);

describe('https', () => {
let server: FakeServer;
let env: Record<string, string>;

beforeAll(async () => {
const port = process.env.PORT || process.env.SNYK_PORT || '12345';
const baseApi = '/api/v1';
env = {
...process.env,
SNYK_API: 'https://localhost:' + port + baseApi,
SNYK_HOST: 'https://localhost:' + port,
SNYK_TOKEN: '123456789',
};
server = fakeServer(baseApi, env.SNYK_TOKEN);
await server.listenWithHttps(port, {
/**
* key and cert were generating using the command below:
* faketime 'last week' openssl req -new -newkey rsa:4096 -days 1 -nodes -x509 -subj '/C=US/ST=Denial/L=Springfield/O=Dis/CN=localhost' -keyout localhost-expired.key -out localhost-expired.cert
*/
key: fs.readFileSync(getFixturePath('fake-server/localhost-expired.key')),
cert: fs.readFileSync(
getFixturePath('fake-server/localhost-expired.cert'),
),
});
});

afterEach(() => {
server.restore();
});

afterAll((done) => {
server.close(() => {
done();
});
});

describe('expired certificate', () => {
it('rejects connections', async () => {
const project = await createProjectFromWorkspace('npm-package');
const { code, stdout } = await runSnykCLI('test', {
cwd: project.path(),
env,
});

expect(stdout).toContain('certificate has expired');
expect(code).toBe(2);
});

it('accepts connections using --insecure', async () => {
const project = await createProjectFromWorkspace('npm-package');
const { code } = await runSnykCLI('test --insecure', {
cwd: project.path(),
env,
});
expect(code).toBe(0);
});
});
});

0 comments on commit 0935f04

Please sign in to comment.