-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathcmd-scan-reach.mts
More file actions
245 lines (215 loc) · 6.79 KB
/
cmd-scan-reach.mts
File metadata and controls
245 lines (215 loc) · 6.79 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import path from 'node:path'
import { joinAnd } from '@socketsecurity/registry/lib/arrays'
import { logger } from '@socketsecurity/registry/lib/logger'
import { handleScanReach } from './handle-scan-reach.mts'
import { reachabilityFlags } from './reachability-flags.mts'
import { suggestTarget } from './suggest_target.mts'
import { validateReachabilityTarget } from './validate-reachability-target.mts'
import constants from '../../constants.mts'
import { commonFlags, outputFlags } from '../../flags.mts'
import { checkCommandInput } from '../../utils/check-input.mts'
import { cmdFlagValueToArray } from '../../utils/cmd.mts'
import { determineOrgSlug } from '../../utils/determine-org-slug.mts'
import { getEcosystemChoicesForMeow } from '../../utils/ecosystem.mts'
import { getOutputKind } from '../../utils/get-output-kind.mts'
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
import {
getFlagApiRequirementsOutput,
getFlagListOutput,
} from '../../utils/output-formatting.mts'
import { hasDefaultApiToken } from '../../utils/sdk.mts'
import type { MeowFlags } from '../../flags.mts'
import type { PURL_Type } from '../../utils/ecosystem.mts'
import type {
CliCommandConfig,
CliCommandContext,
} from '../../utils/meow-with-subcommands.mts'
export const CMD_NAME = 'reach'
const description = 'Compute tier 1 reachability'
const hidden = true
const generalFlags: MeowFlags = {
...commonFlags,
...outputFlags,
cwd: {
type: 'string',
default: '',
description: 'working directory, defaults to process.cwd()',
},
org: {
type: 'string',
default: '',
description:
'Force override the organization slug, overrides the default org from config',
},
}
export const cmdScanReach = {
description,
hidden,
run,
}
async function run(
argv: string[] | readonly string[],
importMeta: ImportMeta,
{ parentName }: CliCommandContext,
): Promise<void> {
const config: CliCommandConfig = {
commandName: CMD_NAME,
description,
hidden,
flags: {
...generalFlags,
...reachabilityFlags,
},
help: command =>
`
Usage
$ ${command} [options] [CWD=.]
API Token Requirements
${getFlagApiRequirementsOutput(`${parentName}:${CMD_NAME}`)}
Options
${getFlagListOutput(generalFlags)}
Reachability Options
${getFlagListOutput(reachabilityFlags)}
Runs the Socket reachability analysis without creating a scan in Socket.
The output is written to .socket.facts.json in the current working directory.
Note: Manifest files are uploaded to Socket's backend services because the
reachability analysis requires creating a Software Bill of Materials (SBOM)
from these files before the analysis can run.
Examples
$ ${command}
$ ${command} ./proj
$ ${command} ./proj --reach-ecosystems npm,pypi
`,
}
const cli = meowOrExit({
argv,
config,
importMeta,
parentName,
})
const {
cwd: cwdOverride,
interactive = true,
json,
markdown,
org: orgFlag,
reachAnalysisMemoryLimit,
reachAnalysisTimeout,
reachConcurrency,
reachDisableAnalysisSplitting,
reachDisableAnalytics,
reachSkipCache,
} = cli.flags as {
cwd: string
interactive: boolean
json: boolean
markdown: boolean
org: string
reachAnalysisTimeout: number
reachAnalysisMemoryLimit: number
reachConcurrency: number
reachDisableAnalytics: boolean
reachDisableAnalysisSplitting: boolean
reachSkipCache: boolean
}
const dryRun = !!cli.flags['dryRun']
// Process comma-separated values for isMultiple flags.
const reachEcosystemsRaw = cmdFlagValueToArray(cli.flags['reachEcosystems'])
const reachExcludePaths = cmdFlagValueToArray(cli.flags['reachExcludePaths'])
// Validate ecosystem values.
const reachEcosystems: PURL_Type[] = []
const validEcosystems = getEcosystemChoicesForMeow()
for (const ecosystem of reachEcosystemsRaw) {
if (!validEcosystems.includes(ecosystem)) {
throw new Error(
`Invalid ecosystem: "${ecosystem}". Valid values are: ${joinAnd(validEcosystems)}`,
)
}
reachEcosystems.push(ecosystem as PURL_Type)
}
const processCwd = process.cwd()
const cwd =
cwdOverride && cwdOverride !== '.' && cwdOverride !== processCwd
? path.resolve(processCwd, cwdOverride)
: processCwd
// Accept zero or more paths. Default to cwd() if none given.
let targets = cli.input.length ? cli.input : [cwd]
// Use suggestTarget if no targets specified and in interactive mode
if (!targets.length && !dryRun && interactive) {
targets = await suggestTarget()
}
const { 0: orgSlug } = await determineOrgSlug(orgFlag, interactive, dryRun)
const hasApiToken = hasDefaultApiToken()
const outputKind = getOutputKind(json, markdown)
// Validate target constraints for reachability analysis.
const targetValidation = await validateReachabilityTarget(targets, cwd)
const wasValidInput = checkCommandInput(
outputKind,
{
nook: true,
test: !!orgSlug,
message: 'Org name by default setting, --org, or auto-discovered',
fail: 'missing',
},
{
nook: true,
test: hasApiToken,
message: 'This command requires an API token for access',
fail: 'try `socket login`',
},
{
nook: true,
test: !json || !markdown,
message: 'The json and markdown flags cannot be both set, pick one',
fail: 'omit one',
},
{
nook: true,
test: targetValidation.isValid,
message: 'Reachability analysis requires exactly one target directory',
fail: 'provide exactly one directory path',
},
{
nook: true,
test: targetValidation.isDirectory,
message: 'Reachability analysis target must be a directory',
fail: 'provide a directory path, not a file',
},
{
nook: true,
test: targetValidation.targetExists,
message: 'Target directory must exist',
fail: 'provide an existing directory path',
},
{
nook: true,
test: targetValidation.isInsideCwd,
message: 'Target directory must be inside the current working directory',
fail: 'provide a path inside the working directory',
},
)
if (!wasValidInput) {
return
}
if (dryRun) {
logger.log(constants.DRY_RUN_BAILING_NOW)
return
}
await handleScanReach({
cwd,
orgSlug,
outputKind,
targets,
interactive,
reachabilityOptions: {
reachAnalysisTimeout: Number(reachAnalysisTimeout),
reachAnalysisMemoryLimit: Number(reachAnalysisMemoryLimit),
reachConcurrency: Number(reachConcurrency),
reachDisableAnalytics: Boolean(reachDisableAnalytics),
reachDisableAnalysisSplitting: Boolean(reachDisableAnalysisSplitting),
reachEcosystems,
reachExcludePaths,
reachSkipCache: Boolean(reachSkipCache),
},
})
}