-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tls.js
214 lines (177 loc) · 7.98 KB
/
tls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright (C) 2017-2021 BinaryMist Limited. All rights reserved.
// This file is part of PurpleTeam.
// PurpleTeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation version 3.
// PurpleTeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this PurpleTeam project. If not, see <https://www.gnu.org/licenses/>.
const { spawn } = require('child_process');
const { readFile } = require('fs').promises;
const cucumber = require('@cucumber/cucumber');
const { GherkinStreams } = require('@cucumber/gherkin-streams');
const statusMap = {
'Awaiting Job.': true,
'Initialising Tester.': false,
'Tester initialised.': false,
'Tls tests are running.': false
};
let testingProps = null;
class Tls {
constructor({ log, strings, emissary, cucumber: cucumberConfig, results, publisher, debug }) {
// Todo: make class fields private.
this.log = log;
this.strings = strings;
this.emissary = emissary;
this.cucumber = cucumberConfig;
this.results = results;
this.publisher = publisher; // Todo: Remove publisher?
this.debug = debug;
this.status = (state) => {
if (state) {
Object.keys(statusMap).forEach((k) => { statusMap[k] = false; });
statusMap[state] = true;
this.log.info(`Setting status to: "${state}"`, { tags: ['tls'] });
return state;
}
return Object.entries(statusMap).find((e) => e[1] === true)[0];
};
}
#startCuc(testingProperties) {
const { debug: { execArgvDebugString, firstChildProcessInspectPort } } = this;
const cucumberArgs = this.createCucumberArgs(testingProperties.runableSessionProps);
// runableSessionProps: {
// sessionProps: {
// protocol: "http",
// ip: "pt-sut-cont",
// port: 4000,
// testSession: {
// type: "tlsScanner",
// id: "NA",
// attributes: {
// alertThreshold: 12,
// },
// }
// }
// }
if (this.status() === 'Tls tests are running.') return;
const cucCli = spawn('node', [...(execArgvDebugString ? [`${execArgvDebugString}:${firstChildProcessInspectPort}`] : []), ...cucumberArgs], { cwd: process.cwd(), env: process.env, argv0: process.argv[0] });
this.log.notice(`cucCli process with PID "${cucCli.pid}" has been spawned for Test Session with Id "${testingProperties.runableSessionProps.sessionProps.testSession.id}"`, { tags: ['tls'] });
this.status('Tls tests are running.');
cucCli.stdout.on('data', (data) => {
process.stdout.write(data); // Todo: Check these, can they use log?
});
cucCli.stderr.on('data', (data) => {
process.stdout.write(data); // Todo: Check these, can they use log?
});
cucCli.on('exit', async (code, signal) => { // Do we need this async?
const message = `Child process "cucumber Cli" running session with id: "${testingProperties.runableSessionProps.sessionProps.testSession.id}" exited with code: "${code}", and signal: "${signal}".`;
this.log[`${code === 0 ? 'info' : 'error'}`](message, { tags: ['tls'] });
this.status('Awaiting Job.');
});
cucCli.on('close', (code) => {
const message = `"close" event was emitted with code: "${code}" for "cucumber Cli" running session with id "${testingProperties.runableSessionProps.sessionProps.testSession.id}".`;
this.log[`${code === 0 ? 'info' : 'error'}`](message, { tags: ['tls'] });
});
cucCli.on('error', (err) => {
process.stdout.write(`Failed to start cucCli sub-process. The error was: ${err}.`, { tags: ['tls'] });
this.status('Awaiting Job.');
});
}
async initTester(testJob) {
this.log.info(`Status currently set to: "${this.status()}"`, { tags: ['tls'] });
if (this.status() !== 'Awaiting Job.') return this.status();
this.status('Initialising Tester.');
const testSession = testJob.included.find((resourceObject) => resourceObject.type === 'tlsScanner');
testingProps = {
runableSessionProps: {
sessionProps: {
protocol: testJob.data.attributes.sutProtocol,
ip: testJob.data.attributes.sutIp,
port: testJob.data.attributes.sutPort,
testSession
}
}
};
return this.status('Tester initialised.');
}
startCucs() {
this.#startCuc(testingProps);
}
async testPlan(testJob) { // eslint-disable-line no-unused-vars
const cucumberArgs = this.createCucumberArgs({});
const cucumberCliInstance = new cucumber.Cli({
argv: ['node', ...cucumberArgs],
cwd: process.cwd(),
stdout: process.stdout
});
const activeFeatureFileUris = await this.#getActiveFeatureFileUris(cucumberCliInstance);
const testPlanText = await this.#getTestPlanText(activeFeatureFileUris);
return testPlanText;
}
createCucumberArgs({ sessionProps = {} }) {
const emissaryProperties = { reportDir: this.emissary.report.dir };
const cucumberParameters = {
emissaryProperties,
sutProperties: sessionProps,
cucumber: { timeout: this.cucumber.timeout }
};
const parameters = JSON.stringify(cucumberParameters);
this.log.debug(`The cucumberParameters are: ${parameters}`, { tags: ['tls'] });
const cucumberArgs = [
this.cucumber.binary,
this.cucumber.features,
'--require',
this.cucumber.steps,
/* '--exit', */
`--format=message:${this.results.dir}result_tlsScannerId-${sessionProps.testSession ? sessionProps.testSession.id : 'noSessionPropsAvailable'}_${this.strings.NowAsFileName('-')}.NDJSON`,
/* Todo: Provide ability for Build User to pass flag to disable colours */
'--format-options',
'{"colorsEnabled": true}',
'--tags',
this.cucumber.tagExpression,
'--world-parameters',
parameters
];
// Todo: KC: Validation, Filtering and Sanitisation required, as these are being executed, although they should all be under our control.
return cucumberArgs;
}
// eslint-disable-next-line class-methods-use-this
async #getActiveFeatureFileUris(cucumberCli) {
const configuration = await cucumberCli.getConfiguration();
const pickleFilter = (() => new (require('@cucumber/cucumber/lib/pickle_filter')).default(configuration.pickleFilterOptions))(); // eslint-disable-line global-require, new-cap
const streamToArray = async (readableStream) => new Promise((resolve, reject) => {
const items = [];
readableStream.on('data', (item) => items.push(item));
readableStream.on('error', (err) => reject(err));
readableStream.on('end', () => resolve(items));
});
const activeFeatureFileUris = async () => {
const envelopes = await streamToArray(GherkinStreams.fromPaths(configuration.featurePaths, { includeSource: false, includeGherkinDocument: true, includePickles: true }));
let gherkinDocument = null;
const pickles = [];
envelopes.forEach((e) => {
if (e.gherkinDocument) {
gherkinDocument = e.gherkinDocument;
} else if (e.pickle && gherkinDocument) {
const { pickle } = e;
if (pickleFilter.matches({ gherkinDocument, pickle })) pickles.push({ pickle });
}
});
return pickles
.map((p) => p.pickle.uri)
.reduce((accum, cV) => [...accum, ...(accum.includes(cV) ? [] : [cV])], []);
};
return activeFeatureFileUris();
}
// eslint-disable-next-line class-methods-use-this
async #getTestPlanText(activeFeatureFileUris) {
return (await Promise.all(activeFeatureFileUris
.map((aFFU) => readFile(aFFU, { encoding: 'utf8' }))))
.reduce((accumulatedFeatures, feature) => `${accumulatedFeatures}${!accumulatedFeatures.length > 0 ? feature : `\n\n${feature}`}`, '');
}
}
module.exports = Tls;