|
| 1 | +import chalk from 'chalk' |
| 2 | +import fs from 'fs' |
| 3 | +import meow from 'meow' |
| 4 | +import ora from 'ora' |
| 5 | +import util from 'util' |
| 6 | + |
| 7 | +import { outputFlags } from '../../flags' |
| 8 | +import { printFlagList } from '../../utils/formatting' |
| 9 | +import { getDefaultKey } from '../../utils/sdk' |
| 10 | + |
| 11 | +import type { CliSubcommand } from '../../utils/meow-with-subcommands' |
| 12 | +import type { Ora } from 'ora' |
| 13 | +import { AuthError } from '../../utils/errors' |
| 14 | +import { handleAPIError, queryAPI } from '../../utils/api-helpers' |
| 15 | + |
| 16 | +export const get: CliSubcommand = { |
| 17 | + description: 'Get a diff scan for an organization', |
| 18 | + async run(argv, importMeta, { parentName }) { |
| 19 | + const name = `${parentName} get` |
| 20 | + const input = setupCommand(name, get.description, argv, importMeta) |
| 21 | + if (input) { |
| 22 | + const apiKey = getDefaultKey() |
| 23 | + if(!apiKey){ |
| 24 | + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") |
| 25 | + } |
| 26 | + const spinnerText = 'Getting diff scan... \n' |
| 27 | + const spinner = ora(spinnerText).start() |
| 28 | + await getDiffScan(input, spinner, apiKey) |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +const getDiffScanFlags: { [key: string]: any } = { |
| 34 | + before: { |
| 35 | + type: 'string', |
| 36 | + shortFlag: 'b', |
| 37 | + default: '', |
| 38 | + description: 'The full scan ID of the base scan' |
| 39 | + }, |
| 40 | + after: { |
| 41 | + type: 'string', |
| 42 | + shortFlag: 'a', |
| 43 | + default: '', |
| 44 | + description: 'The full scan ID of the head scan' |
| 45 | + }, |
| 46 | + preview: { |
| 47 | + type: 'boolean', |
| 48 | + shortFlag: 'p', |
| 49 | + default: true, |
| 50 | + description: 'A boolean flag to persist or not the diff scan result' |
| 51 | + }, |
| 52 | + file: { |
| 53 | + type: 'string', |
| 54 | + shortFlag: 'f', |
| 55 | + default: '', |
| 56 | + description: 'Path to a local file where the output should be saved' |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Internal functions |
| 61 | + |
| 62 | +type CommandContext = { |
| 63 | + outputJson: boolean |
| 64 | + outputMarkdown: boolean |
| 65 | + before: string |
| 66 | + after: string |
| 67 | + preview: boolean |
| 68 | + orgSlug: string |
| 69 | + file: string |
| 70 | +} |
| 71 | + |
| 72 | +function setupCommand( |
| 73 | + name: string, |
| 74 | + description: string, |
| 75 | + argv: readonly string[], |
| 76 | + importMeta: ImportMeta |
| 77 | +): CommandContext | undefined { |
| 78 | + const flags: { [key: string]: any } = { |
| 79 | + ...outputFlags, |
| 80 | + ...getDiffScanFlags |
| 81 | + } |
| 82 | + |
| 83 | + const cli = meow( |
| 84 | + ` |
| 85 | + Usage |
| 86 | + $ ${name} <org slug> --before=<before> --after=<after> |
| 87 | +
|
| 88 | + Options |
| 89 | + ${printFlagList(flags, 6)} |
| 90 | +
|
| 91 | + Examples |
| 92 | + $ ${name} FakeCorp --before=aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 --after=aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 |
| 93 | + `, |
| 94 | + { |
| 95 | + argv, |
| 96 | + description, |
| 97 | + importMeta, |
| 98 | + flags |
| 99 | + } |
| 100 | + ) |
| 101 | + |
| 102 | + const { |
| 103 | + json: outputJson, |
| 104 | + markdown: outputMarkdown, |
| 105 | + before, |
| 106 | + after, |
| 107 | + preview, |
| 108 | + file |
| 109 | + } = cli.flags |
| 110 | + |
| 111 | + if (!before || !after) { |
| 112 | + console.error( |
| 113 | + `${chalk.bgRed.white('Input error')}: Please specify a before and after full scan ID. To get full scans IDs, you can run the command "socket scan list <your org slug>". \n` |
| 114 | + ) |
| 115 | + cli.showHelp() |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + if(cli.input.length < 1){ |
| 120 | + console.error( |
| 121 | + `${chalk.bgRed.white('Input error')}: Please provide an organization slug \n` |
| 122 | + ) |
| 123 | + cli.showHelp() |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + const [orgSlug = ''] = cli.input |
| 128 | + |
| 129 | + return <CommandContext>{ |
| 130 | + outputJson, |
| 131 | + outputMarkdown, |
| 132 | + before, |
| 133 | + after, |
| 134 | + preview, |
| 135 | + orgSlug, |
| 136 | + file |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +async function getDiffScan( |
| 141 | + { before, after, orgSlug, file }: CommandContext, |
| 142 | + spinner: Ora, |
| 143 | + apiKey: string, |
| 144 | +): Promise<void> { |
| 145 | + const response = await queryAPI(`${orgSlug}/full-scans/diff?before=${before}&after=${after}&preview`, apiKey) |
| 146 | + const data = await response.json(); |
| 147 | + |
| 148 | + if(!response.ok){ |
| 149 | + spinner.stop() |
| 150 | + const err = await handleAPIError(response.status) |
| 151 | + console.error( |
| 152 | + `${chalk.bgRed.white(response.statusText)}: ${err} \n` |
| 153 | + ) |
| 154 | + return |
| 155 | + } |
| 156 | + |
| 157 | + spinner.stop() |
| 158 | + |
| 159 | + if(file){ |
| 160 | + fs.writeFile(file, JSON.stringify(data), err => { |
| 161 | + err ? console.error(err) : console.log(`Data successfully written to ${file}`) |
| 162 | + }) |
| 163 | + return |
| 164 | + } |
| 165 | + |
| 166 | + console.log(`\n Diff scan result: \n`) |
| 167 | + console.log(util.inspect(data, {showHidden: false, depth: null, colors: true})) |
| 168 | +} |
0 commit comments