|
| 1 | +import * as models from "./models"; |
| 2 | +import * as platform from "./api"; |
| 3 | +import { wait } from "./misc"; |
| 4 | +import { TwoColumnLogger } from "./logger"; |
| 5 | +import fs = require("fs"); |
| 6 | + |
| 7 | +if(process.argv.length < 4 || process.argv.length > 5) { |
| 8 | + console.error("Usage: analyse-project.ts https://somehub.com/user/repo.git branchname [sast-report.json]"); |
| 9 | + process.exit(1); |
| 10 | +} |
| 11 | + |
| 12 | +async function securityAnalysis() { |
| 13 | + |
| 14 | + const projectName = `gitlab-${Date.now()}`; |
| 15 | + let localMachine: models.Instance = { |
| 16 | + name: "local-executor", |
| 17 | + ip: "localhost", |
| 18 | + log: new TwoColumnLogger("local-executor"), |
| 19 | + project: { |
| 20 | + name: projectName, |
| 21 | + fullName: "not-needed", |
| 22 | + uri: process.argv[2], |
| 23 | + branch: process.argv[3], |
| 24 | + manualVerify: { verifyBranch: "x", innerDir: "y", jacocoFile: "z" } |
| 25 | + }, |
| 26 | + status: "USER_CREATED", |
| 27 | + type: "notype", |
| 28 | + zones: [], |
| 29 | + zone: "nozone", |
| 30 | + zoneIndex: 0, |
| 31 | + diskImg: { name: "nodiskimgname", link: "nodiskimglink" }, |
| 32 | + username: "diffblue", |
| 33 | + duration: { start: "nodurationstart" }, |
| 34 | + }; |
| 35 | + |
| 36 | + let startTime = Date.now(); |
| 37 | + |
| 38 | + localMachine = await platform.createProject(localMachine); |
| 39 | + localMachine = await platform.initialiseProject(localMachine); |
| 40 | + localMachine = await platform.startAnalysis(localMachine); |
| 41 | + |
| 42 | + do { |
| 43 | + localMachine = await platform.getAnalysisProgress(localMachine); |
| 44 | + if (localMachine.analysisStatus === "RUNNING" || localMachine.analysisStatus === "WAITING") { |
| 45 | + let elapsed = Date.now() - startTime; |
| 46 | + await wait(elapsed / 10000); // Wait for 10% of time elapsed so far |
| 47 | + } |
| 48 | + } while (localMachine.analysisStatus === "RUNNING" || localMachine.analysisStatus === "WAITING"); |
| 49 | + |
| 50 | + let issues = await platform.getIssues(localMachine); |
| 51 | + |
| 52 | + localMachine.log.debug(`Identified ${issues.length} issues`); |
| 53 | + localMachine.log.debug(JSON.stringify(issues)); |
| 54 | + |
| 55 | + if(process.argv.length == 5) { |
| 56 | + |
| 57 | + let report = issues.map( |
| 58 | + function(issue : any) { |
| 59 | + |
| 60 | + // TODO: query the platform's public-facing URL |
| 61 | + let testUrl = `http://localhost/${projectName}/${localMachine.analysisNum}/tests/${issue.id}/trace`; |
| 62 | + |
| 63 | + return { |
| 64 | + file: `${issue.testClassDir}/${issue.testClass}`, |
| 65 | + message: issue.testName, |
| 66 | + priority: "High", |
| 67 | + tool: "Diffblue-security", |
| 68 | + url: testUrl, |
| 69 | + cve: `Click here ${testUrl}` |
| 70 | + //cve: testUrl // Mandatory key |
| 71 | + }; |
| 72 | + } |
| 73 | + ); |
| 74 | + |
| 75 | + localMachine.log.debug(`Writing issue report to ${process.argv[4]}`); |
| 76 | + fs.writeFileSync(process.argv[4], JSON.stringify(report)); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + process.exit(issues.length == 0 ? 0 : 2); |
| 81 | +} |
| 82 | + |
| 83 | +securityAnalysis(); |
0 commit comments