Skip to content

Commit 29573c9

Browse files
Merge pull request diffblue#573 from diffblue/cleanup/gitlab-wrapper
Cleanup the gitlab wrapper [SEC-626]
2 parents 3b239bb + b0f85ca commit 29573c9

File tree

12 files changed

+1115
-122
lines changed

12 files changed

+1115
-122
lines changed

gitlab-integration/.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
*.js
2-
*.js.map
1+
built
2+
dist
33
node_modules
4-

gitlab-integration/analyse-project.ts

Lines changed: 0 additions & 83 deletions
This file was deleted.
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
#!/bin/bash
22

3-
WORKDIR=$(readlink -f $(pwd))
4-
5-
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
6-
cd $DIR
7-
8-
node analyse-project.js $CI_REPOSITORY_URL $CI_COMMIT_REF_NAME $WORKDIR/gl-sast-report.json
9-
exit 0
3+
npm start --prefix "$( dirname "${BASH_SOURCE[0]}" )"

gitlab-integration/package.json

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@
1212
"John Bergqvist <[email protected]>",
1313
"Nathan Phillips <[email protected]>"
1414
],
15-
"main": "analyse-project.js",
15+
"main": "./built/analyse-project.js",
16+
"bin": {
17+
"gitlab-integration": "./built/analyse-project.js"
18+
},
1619
"scripts": {
17-
"build": "tsc"
20+
"build": "tsc",
21+
"lint": "tslint src/**/*.ts",
22+
"fix-lint": "tslint src/**/*.ts --fix",
23+
"gen-docs": "typedoc --out doc",
24+
"start": "node built/analyse-project.js"
1825
},
1926
"dependencies": {
20-
"@types/moment-duration-format": "^2.2.1",
2127
"blow-http-statuses": "0.2.4",
22-
"express": "4.16.3",
2328
"form-data": "2.3.2",
2429
"fs-extra": "6.0.1",
2530
"js-yaml": "3.12.0",
@@ -28,19 +33,20 @@
2833
"moment-duration-format": "2.2.2",
2934
"node-fetch": "2.1.2",
3035
"node-ssh": "5.1.2",
31-
"util.promisify": "1.0.0",
3236
"winston": "2.4.3",
3337
"winston-papertrail": "1.0.5"
3438
},
3539
"devDependencies": {
36-
"@types/es6-shim": "0.31.37",
37-
"@types/express": "4.16.0",
3840
"@types/form-data": "2.2.1",
3941
"@types/fs-extra": "5.0.2",
4042
"@types/lodash": "4.14.110",
43+
"@types/moment-duration-format": "2.2.2",
4144
"@types/node": "8.0.47",
42-
"@types/node-fetch": "2.1.1",
45+
"@types/node-fetch": "2.1.2",
4346
"@types/winston": "2.3.9",
47+
"tslint": "^5.11.0",
48+
"tslint-language-service": "^0.9.9",
49+
"typedoc": "^0.12.0",
4450
"typescript": "2.9.2"
4551
}
4652
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env node
2+
3+
import * as fs from "fs-extra";
4+
import * as path from "path";
5+
import * as platform from "./api";
6+
import { TwoColumnLogger } from "./logger";
7+
import { wait } from "./misc";
8+
import * as models from "./models";
9+
10+
async function securityAnalysis() {
11+
if (process.env.CI_REPOSITORY_URL === undefined || process.env.CI_COMMIT_REF_NAME === undefined) {
12+
console.error("This utility must be run inside a Gitlab runner");
13+
process.exit(1);
14+
return;
15+
}
16+
if (process.env.INIT_CWD === undefined) {
17+
console.error("This utility must be run inside npm from the folder where the output file should be written");
18+
process.exit(1);
19+
return;
20+
}
21+
22+
const log = new TwoColumnLogger("Diffblue Gitlab Integration");
23+
try {
24+
const projectName = `gitlab-${Date.now()}`;
25+
let localMachine: models.Instance = {
26+
name: "local-executor",
27+
ip: "localhost",
28+
log: log,
29+
project: {
30+
name: projectName,
31+
fullName: "not-needed",
32+
uri: process.env.CI_REPOSITORY_URL,
33+
branch: process.env.CI_COMMIT_REF_NAME,
34+
manualVerify: { verifyBranch: "x", innerDir: "y", jacocoFile: "z" },
35+
},
36+
status: "USER_CREATED",
37+
type: "notype",
38+
zones: [],
39+
zone: "nozone",
40+
zoneIndex: 0,
41+
diskImg: { name: "nodiskimgname", link: "nodiskimglink" },
42+
username: "diffblue",
43+
duration: { start: "nodurationstart" },
44+
};
45+
46+
const startTime = Date.now();
47+
48+
localMachine = await platform.createProject(localMachine);
49+
localMachine = await platform.initialiseProject(localMachine);
50+
localMachine = await platform.startAnalysis(localMachine);
51+
52+
do {
53+
localMachine = await platform.getAnalysisProgress(localMachine);
54+
if (localMachine.analysisStatus === "RUNNING" || localMachine.analysisStatus === "WAITING") {
55+
const elapsed = (Date.now() - startTime) / 1000;
56+
const nextWait = Math.ceil(elapsed / 10); // Wait for 10% of time elapsed so far
57+
log.debug(`Waiting ${nextWait} seconds before checking again whether Diffblue platform is ready`);
58+
await wait(nextWait);
59+
}
60+
} while (localMachine.analysisStatus === "RUNNING" || localMachine.analysisStatus === "WAITING");
61+
62+
const issues = await platform.getIssues(localMachine);
63+
64+
log.debug(`Identified ${issues.length} issues`);
65+
log.debug(JSON.stringify(issues));
66+
67+
const report = issues.map(
68+
function (issue: any) {
69+
70+
// TODO: query the platform's public-facing URL
71+
const testUrl = `http://localhost/${projectName}/${localMachine.analysisNum}/tests/${issue.id}/trace`;
72+
73+
return {
74+
file: `${issue.testClassDir}/${issue.testClass}`,
75+
message: issue.testName,
76+
priority: "High",
77+
tool: "Diffblue-security",
78+
url: testUrl,
79+
cve: `Click here ${testUrl}`,
80+
// cve: testUrl // Mandatory key
81+
};
82+
}
83+
);
84+
85+
const reportFileName = path.join(process.env.INIT_CWD, "gl-sast-report.json");
86+
log.debug(`Writing issue report to ${reportFileName}`);
87+
await fs.writeFile(reportFileName, JSON.stringify(report));
88+
89+
process.exit(issues.length === 0 ? 0 : 2);
90+
} catch (err) {
91+
if (err.message) {
92+
log.debug(err.message);
93+
console.log(err.message);
94+
} else {
95+
log.debug(err);
96+
console.dir(err);
97+
}
98+
process.exit(3);
99+
}
100+
}
101+
102+
securityAnalysis();
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import * as FormData from "form-data";
33
import * as fs from "fs-extra";
44
import * as moment from "moment";
55
import fetch, { Response } from "node-fetch";
6-
import * as models from "./models";
76
import { hasExceeded, wait } from "./misc";
7+
import * as models from "./models";
88

99
const nodeSSH = require("node-ssh");
1010
const yaml = require("js-yaml");
11-
const util = require("util");
12-
require("util.promisify").shim();
1311

1412
export async function tweak(vm: models.Instance): Promise<models.Instance> {
1513
vm.ssh = new nodeSSH();
@@ -68,7 +66,7 @@ async function updateYMLFile(vm: models.Instance): Promise<void> {
6866
vm.log.warn(`Unable to retrieve /opt/diffblue/docker-compose.yml: ${error}`);
6967
throw error;
7068
}
71-
const dockerCompose = yaml.safeLoad(await util.promisify(fs.readFile)(`./${vm.name}.yml`, "utf8"));
69+
const dockerCompose = yaml.safeLoad(await fs.readFile(`./${vm.name}.yml`, "utf8"));
7270
dockerCompose.services["application-server"].environment.DEBUG = "true";
7371
// dockerCompose.services["job-runner"].environment.DEBUG = "true";
7472
// dockerCompose.services["job-runner"].environment.LOGLEVEL = "debug";
@@ -163,7 +161,7 @@ async function signIn(vm: models.Instance, secsToWait = 30, secsWaiting = 0): Pr
163161
let response;
164162
try {
165163
response = await fetch(`http://${vm.ip}/api/session`, {
166-
method: "POST", body: JSON.stringify({ userName: vm.username, password: "password" }), headers: { "Content-Type": "application/json" },
164+
method: "POST", body: JSON.stringify({ userName: vm.username, password: "password123" }), headers: { "Content-Type": "application/json" },
167165
});
168166
if (response.status !== HttpStatus.OK && response.status !== HttpStatus.Unauthorized)
169167
throw new Error(`received ${response.status} (${response.statusText}) response`);
@@ -522,7 +520,7 @@ export async function getIssues(vm: models.Instance, secsToWait = 600, secsWaiti
522520
let response;
523521
try {
524522
response = await fetch(`http://${vm.ip}/api/users/${vm.username}/projects/${vm.project.name}/analyses/${vm.analysisNum}/tests`, {
525-
headers: vm.cookie ? { Cookie: vm.cookie } : {}
523+
headers: vm.cookie ? { Cookie: vm.cookie } : {},
526524
});
527525
if (response.status !== HttpStatus.OK && response.status !== HttpStatus.NotModified && response.status !== HttpStatus.Unauthorized)
528526
throw new Error(`received ${response.status} (${response.statusText}) response`);
@@ -532,7 +530,7 @@ export async function getIssues(vm: models.Instance, secsToWait = 600, secsWaiti
532530
await wait(secsToWait);
533531
return await getIssues(vm, secsToWait * 2, secsWaiting + secsToWait);
534532
}
535-
let body = await parseBody(response);
533+
const body = await parseBody(response);
536534
const fetchErrPrefix = `${errPrefix}: Received ${response.status} (${response.statusText}) response`;
537535
if (response.status === HttpStatus.Unauthorized && body.message === "Invalid or missing authorization token") {
538536
vm.log.debug(`${fetchErrPrefix}. Signing in`);
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
// Taken from diffblue/dashboard/src/api/utils/logger.ts
2-
31
// Copyright 2017-2018 Diffblue Limited. All Rights Reserved.
42

5-
import * as express from "express";
63
import * as _ from "lodash";
74
import * as winston from "winston";
85
require("winston-papertrail").Papertrail; // tslint:disable-line:no-unused-expression
96

10-
const app = express();
11-
127
const maxLevelLength = 18; // "Verbose" in colour
138

149
let transport;
15-
if (app.get("env") === "development" || !process.env.papertrailHost || !process.env.papertrailPort)
10+
if (process.env.papertrailHost === undefined || process.env.papertrailPort === undefined)
1611
transport =
1712
new winston.transports.Console({
1813
level: "debug",
@@ -39,7 +34,7 @@ else
3934
handleExceptions: true,
4035
host: process.env.papertrailHost,
4136
port: process.env.papertrailPort,
42-
hostname: app.get("env"),
37+
hostname: "gitlab-integration",
4338
program: "Dashboard",
4439
logFormat: (level: string, message: string) => { return padString(level, maxLevelLength) + message; },
4540
});

gitlab-integration/tsconfig.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
{
22
"compilerOptions": {
3-
"noImplicitAny": true,
3+
"rootDir": "src",
4+
"outDir": "built",
5+
"target": "es6",
6+
"lib": [ "es6" ],
47
"module": "commonjs",
8+
"moduleResolution": "node",
59
"noEmitOnError": true,
6-
"removeComments": false,
7-
"sourceMap": true,
8-
"target": "es5",
9-
"lib": [ "es5" ],
10+
"noImplicitAny": true,
1011
"strictNullChecks": true,
1112
"noUnusedLocals": true,
1213
"noUnusedParameters": true,
13-
"typeRoots": [ "./node_modules/@types" ]
14+
"sourceMap": true,
15+
"removeComments": true
1416
},
1517
"exclude": [
16-
"./node_modules/**/*"
18+
"node_modules",
19+
"built",
20+
"dist"
1721
],
1822
"plugins": [
1923
{ "name": "tslint-language-service" }

0 commit comments

Comments
 (0)