|
3 | 3 | const path = require('path')
|
4 | 4 | const debug = require('debug')('cwe-sdk:build')
|
5 | 5 | const { convertXmlArchiveToJson, writeJsonToFile, createCweDictionary } = require('./xmlParser')
|
6 |
| - |
7 |
| -debug('begin building CWE assets') |
| 6 | +const request = require('./httpClient') |
| 7 | +const AdmZip = require('adm-zip') |
| 8 | +const { rename } = require('fs').promises |
8 | 9 |
|
9 | 10 | // @TODO add a script that creates a copy of cwec_v4.1.xml to
|
10 | 11 | // its alias cwe-archive.xml
|
11 | 12 | const RAW_INPUT_XML_FILENAME = 'cwe-archive.xml'
|
12 | 13 | const RAW_OUTPUT_JSON_FILENAME = 'cwe-archive.json'
|
13 | 14 | const OUTPUT_JSON_DICT_FILENAME = 'cwe-dictionary.json'
|
14 | 15 | const OUTPUT_JSON_HIERARCHY_FILENAME = 'cwe-hierarchy.json'
|
| 16 | +const ARCHIVE_DOWNLOAD_OPTIONS = { |
| 17 | + hostname: 'cwe.mitre.org', |
| 18 | + port: 443, |
| 19 | + path: '/data/xml/cwec_latest.xml.zip', |
| 20 | + method: 'GET' |
| 21 | +} |
| 22 | +let cweArchiveVersion |
| 23 | + |
| 24 | +debug('begin downloading latest CWE archive') |
| 25 | + |
| 26 | +updateArchive() |
| 27 | + .then(() => { |
| 28 | + debug(`archive updated to version ${cweArchiveVersion}`) |
| 29 | + debug('begin building CWE assets') |
| 30 | + |
| 31 | + const rawJsonCweArchive = convertXmlArchiveToJson({ |
| 32 | + cweArchiveFilepath: path.join(__dirname, '..', 'raw', RAW_INPUT_XML_FILENAME) |
| 33 | + }) |
| 34 | + writeJsonToFile({ |
| 35 | + jsonFilepath: path.join(__dirname, '..', 'raw', RAW_OUTPUT_JSON_FILENAME), |
| 36 | + jsonData: rawJsonCweArchive |
| 37 | + }) |
| 38 | + |
| 39 | + const { cweDictionary, cweHierarchy } = createCweDictionary({ cweArchive: rawJsonCweArchive }) |
15 | 40 |
|
16 |
| -const rawJsonCweArchive = convertXmlArchiveToJson({ |
17 |
| - cweArchiveFilepath: path.join(__dirname, '..', 'raw', RAW_INPUT_XML_FILENAME) |
18 |
| -}) |
19 |
| -writeJsonToFile({ |
20 |
| - jsonFilepath: path.join(__dirname, '..', 'raw', RAW_OUTPUT_JSON_FILENAME), |
21 |
| - jsonData: rawJsonCweArchive |
22 |
| -}) |
| 41 | + writeJsonToFile({ |
| 42 | + jsonFilepath: path.join(__dirname, '..', 'raw', OUTPUT_JSON_DICT_FILENAME), |
| 43 | + jsonData: cweDictionary |
| 44 | + }) |
23 | 45 |
|
24 |
| -const { cweDictionary, cweHierarchy } = createCweDictionary({ cweArchive: rawJsonCweArchive }) |
| 46 | + writeJsonToFile({ |
| 47 | + jsonFilepath: path.join(__dirname, '..', 'raw', OUTPUT_JSON_HIERARCHY_FILENAME), |
| 48 | + jsonData: cweHierarchy |
| 49 | + }) |
25 | 50 |
|
26 |
| -writeJsonToFile({ |
27 |
| - jsonFilepath: path.join(__dirname, '..', 'raw', OUTPUT_JSON_DICT_FILENAME), |
28 |
| - jsonData: cweDictionary |
29 |
| -}) |
| 51 | + debug('finished') |
| 52 | + }) |
| 53 | + .catch(console.error) |
30 | 54 |
|
31 |
| -writeJsonToFile({ |
32 |
| - jsonFilepath: path.join(__dirname, '..', 'raw', OUTPUT_JSON_HIERARCHY_FILENAME), |
33 |
| - jsonData: cweHierarchy |
34 |
| -}) |
| 55 | +async function updateArchive() { |
| 56 | + const { data } = await request(ARCHIVE_DOWNLOAD_OPTIONS) |
35 | 57 |
|
36 |
| -debug('finished') |
| 58 | + const zip = new AdmZip(data) |
| 59 | + const zippedFile = zip.getEntries()[0].entryName |
| 60 | + cweArchiveVersion = zippedFile.substring(zippedFile.search(/v/) + 1, zippedFile.search(/\.xml/)) |
| 61 | + zip.extractEntryTo(zippedFile, 'raw', false) |
| 62 | + return rename( |
| 63 | + path.join(__dirname, '..', 'raw', zippedFile), |
| 64 | + path.join(__dirname, '..', 'raw', RAW_INPUT_XML_FILENAME) |
| 65 | + ) |
| 66 | +} |
0 commit comments