-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathreport.js
More file actions
200 lines (174 loc) · 4.74 KB
/
Copy pathreport.js
File metadata and controls
200 lines (174 loc) · 4.74 KB
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
'use strict'
const path = require('path')
const analyze = require('../lib/ncm-analyze-tree')
const {
apiRequest,
formatAPIURL,
graphql
} = require('../lib/util')
const config = require('../lib/config')
const {
SEVERITY_RMAP,
moduleSort
} = require('../lib/report/util')
const longReport = require('../lib/report/long')
const shortReport = require('../lib/report/short')
const htmlReport = require('../lib/report/html')
const { helpHeader } = require('../lib/help')
const {
COLORS,
header,
failure,
formatError
} = require('../lib/ncm-style')
const chalk = require('chalk')
const L = console.log
const E = console.error
module.exports = report
module.exports.optionsList = optionsList
async function report (argv, _dir) {
const {
long,
html
} = argv
let { dir = _dir } = argv
if (!dir) dir = process.cwd()
if (argv.help) {
printHelp()
return
}
/* NCM-Cli Header */
L()
L(header(`${path.basename(dir)} Report`))
let orgId = config.getValue('orgId')
try {
const details = await apiRequest(
'GET',
formatAPIURL('/accounts/user/details')
)
if (typeof details.orgId === 'string') {
orgId = details.orgId
}
} catch (err) {
E()
E(formatError('Failed to fetch user info. Have you run `ncm signin`?', err))
E()
process.exitCode = 1
return
}
const whitelist = new Set()
try {
const data = await graphql(
formatAPIURL('/ncm2/api/v2/graphql'),
`query($organizationId: String!) {
policies(organizationId: $organizationId) {
whitelist {
name
version
}
}
}`,
{ organizationId: orgId }
)
for (const policy of data.policies) {
for (const pkg of policy.whitelist) {
whitelist.add(`${pkg.name}@${pkg.version}`)
}
}
} catch (err) {
L()
L(formatError(`Unable to fetch whitelist.`, err))
L()
}
/* verify */
let pkgScores = []
let hasFailures = false
let data
try {
data = await analyze({
dir,
url: formatAPIURL('/ncm2/api/v2/graphql')
})
} catch (err) {
if (err.code === 'ENOENT') {
E()
E(failure(err.message))
E(formatError(`Unable to read project at: ${dir}`, err))
E()
} else {
E()
E(formatError(`Unable to analyze project. ${err.message}.`, err))
E()
}
process.exitCode = 1
return
}
for (let { name, version, scores, published } of data) {
let maxSeverity = 0
let license
const failures = []
for (const score of scores) {
const severityValue = SEVERITY_RMAP.indexOf(score.severity)
if (score.group !== 'compliance' &&
score.group !== 'security' &&
score.group !== 'risk') {
continue
}
if (severityValue > maxSeverity) {
maxSeverity = severityValue
}
if (score.pass === false) {
failures.push(score)
hasFailures = true
}
if (score.name === 'license') {
license = score
}
}
if (!version) {
version = '0.0.0-UNKNOWN-VERSION'
}
pkgScores.push({
name,
version,
published,
maxSeverity,
failures,
license,
scores
})
}
pkgScores = moduleSort(pkgScores)
const whitelisted = pkgScores.filter(pkg => whitelist.has(`${pkg.name}@${pkg.version}`))
pkgScores = pkgScores.filter(pkg => !whitelist.has(`${pkg.name}@${pkg.version}`))
if (!long) shortReport(pkgScores, whitelisted, dir, argv)
if (long) longReport(pkgScores, whitelisted, dir, argv)
if (html) htmlReport(pkgScores, whitelisted, dir, html, argv)
if (hasFailures) process.exitCode = 1
}
function printHelp () {
helpHeader(
'report',
chalk`{${COLORS.light1} ncm} {${COLORS.yellow} report} {${COLORS.teal} [<directory>] [options]}`,
'ncm report [<directory>] [options]',
chalk`
Generates a project-wide report of directory risk and quality of installed or specified packages.
The top five riskiest modules detected will be displayed alongside a concise project report.
A report with a list of all modules can be generated by passing {${COLORS.teal} --long}.
Reports may be filtered based on any of the following flags:
{${COLORS.teal} --compliance}, {${COLORS.teal} --security}
`
)
L(optionsList())
L()
}
function optionsList () {
return chalk`
{${COLORS.light1} ncm} {${COLORS.yellow} report}
{${COLORS.light1} ncm} {${COLORS.yellow} report} {${COLORS.teal} <directory>}
{${COLORS.teal} -d, --dir} {white Another way to specify <directory>}
{${COLORS.teal} -l, --long} {white Full module list output}
{${COLORS.teal} -c --compliance} {white Compliance failures only output}
{${COLORS.teal} -s --security} {white Security failures only output}
`.trim()
}