Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 2 additions & 41 deletions src/commands/audit-log/get-audit-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SocketSdkReturnType } from '@socketsecurity/sdk'
import constants from '../../constants'
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { AuthError } from '../../utils/errors'
import { mdTable } from '../../utils/markdown'
import { getDefaultToken, setupSdk } from '../../utils/sdk'

import type { Choice } from '@socketsecurity/registry/lib/prompts'
Expand Down Expand Up @@ -111,7 +112,7 @@ async function outputAsMarkdown(
perPage: number
): Promise<void> {
try {
const table = mdTable(auditLogs, [
const table = mdTable<any>(auditLogs, [
'event_id',
'created_at',
'type',
Expand Down Expand Up @@ -144,46 +145,6 @@ ${table}
}
}

function mdTable<
T extends SocketSdkReturnType<'getAuditLogEvents'>['data']['results']
>(
logs: T,
// This is saying "an array of strings and the strings are a valid key of elements of T"
// In turn, T is defined above as the audit log event type from our OpenAPI docs.
cols: Array<string & keyof T[number]>
): string {
// Max col width required to fit all data in that column
const cws = cols.map(col => col.length)

for (const log of logs) {
for (let i = 0; i < cols.length; ++i) {
// @ts-ignore
const val: unknown = log[cols[i] ?? ''] ?? ''
cws[i] = Math.max(cws[i] ?? 0, String(val).length)
}
}

let div = '|'
for (const cw of cws) div += ' ' + '-'.repeat(cw) + ' |'

let header = '|'
for (let i = 0; i < cols.length; ++i)
header += ' ' + String(cols[i]).padEnd(cws[i] ?? 0, ' ') + ' |'

let body = ''
for (const log of logs) {
body += '|'
for (let i = 0; i < cols.length; ++i) {
// @ts-ignore
const val: unknown = log[cols[i] ?? ''] ?? ''
body += ' ' + String(val).padEnd(cws[i] ?? 0, ' ') + ' |'
}
body += '\n'
}

return [div, header, div, body.trim(), div].filter(s => !!s.trim()).join('\n')
}

async function outputAsPrint(
auditLogs: SocketSdkReturnType<'getAuditLogEvents'>['data']['results'],
orgSlug: string,
Expand Down
6 changes: 1 addition & 5 deletions src/commands/raw-npm/cmd-raw-npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { logger } from '@socketsecurity/registry/lib/logger'
import { runRawNpm } from './run-raw-npm'
import constants from '../../constants'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'

import type { CliCommandConfig } from '../../utils/meow-with-subcommands'

Expand All @@ -14,13 +13,10 @@ const config: CliCommandConfig = {
description: `Temporarily disable the Socket ${NPM} wrapper`,
hidden: false,
flags: {},
help: (command, config) => `
help: command => `
Usage
$ ${command} <command>

Options
${getFlagListOutput(config.flags, 6)}

Examples
$ ${command} install
`
Expand Down
6 changes: 1 addition & 5 deletions src/commands/raw-npx/cmd-raw-npx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { logger } from '@socketsecurity/registry/lib/logger'
import { runRawNpx } from './run-raw-npx'
import constants from '../../constants'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'

import type { CliCommandConfig } from '../../utils/meow-with-subcommands'

Expand All @@ -14,13 +13,10 @@ const config: CliCommandConfig = {
description: `Temporarily disable the Socket ${NPX} wrapper`,
hidden: false,
flags: {},
help: (command, config) => `
help: command => `
Usage
$ ${command} <command>

Options
${getFlagListOutput(config.flags, 6)}

Examples
$ ${command} install
`
Expand Down
27 changes: 4 additions & 23 deletions src/commands/report/cmd-report-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import constants from '../../constants'
import { commonFlags, outputFlags, validationFlags } from '../../flags'
import { ColorOrMarkdown } from '../../utils/color-or-markdown'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'

import type { CliCommandConfig } from '../../utils/meow-with-subcommands'

const { DRY_RUN_BAIL_TEXT } = constants

const config: CliCommandConfig = {
commandName: 'create',
description: 'Create a project report',
description: '[Deprecated] Create a project report',
hidden: false,
flags: {
...commonFlags,
Expand All @@ -36,27 +35,9 @@ const config: CliCommandConfig = {
description: 'Will wait for and return the created report'
}
},
help: (command, config) => `
Usage
$ ${command} <paths-to-package-folders-and-files>

Uploads the specified "package.json" and lock files for JavaScript, Python, and Go dependency manifests.
If any folder is specified, the ones found in there recursively are uploaded.

Supports globbing such as "**/package.json", "**/requirements.txt", "**/pyproject.toml", and "**/go.mod".

Ignores any file specified in your project's ".gitignore", your project's
"socket.yml" file's "projectIgnorePaths" and also has a sensible set of
default ignores from the "ignore-by-default" module.

Options
${getFlagListOutput(config.flags, 6)}

Examples
$ ${command} .
$ ${command} '**/package.json'
$ ${command} /path/to/a/package.json /path/to/another/package.json
$ ${command} . --view --json
help: () => `
This command is deprecated in favor of \`socket scan create\`.
It will be removed in the next major release of the CLI.
`
}

Expand Down
15 changes: 4 additions & 11 deletions src/commands/report/cmd-report-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,23 @@ import { viewReport } from './view-report'
import constants from '../../constants'
import { commonFlags, outputFlags, validationFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'

import type { CliCommandConfig } from '../../utils/meow-with-subcommands'

const { DRY_RUN_BAIL_TEXT } = constants

const config: CliCommandConfig = {
commandName: 'view',
description: 'View a project report',
description: '[Deprecated] View a project report',
hidden: false,
flags: {
...commonFlags,
...outputFlags,
...validationFlags
},
help: (command, config) => `
Usage
$ ${command} <report-identifier>

Options
${getFlagListOutput(config.flags, 6)}

Examples
$ ${command} QXU8PmK7LfH608RAwfIKdbcHgwEd_ZeWJ9QEGv05FJUQ
help: () => `
This command is deprecated in favor of \`socket scan view\`.
It will be removed in the next major release of the CLI.
`
}

Expand Down
1 change: 1 addition & 0 deletions src/commands/report/cmd-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const description = '[Deprecated] Project report related commands'

export const cmdReport: CliSubcommand = {
description,
hidden: true, // Deprecated in favor of `scan`
async run(argv, importMeta, { parentName }) {
await meowWithSubcommands(
{
Expand Down
10 changes: 5 additions & 5 deletions src/commands/report/create-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { pluralize } from '@socketsecurity/registry/lib/words'

import constants from '../../constants'
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { getPackageFiles } from '../../utils/path-resolve'
import { getPackageFilesFullScans } from '../../utils/path-resolve'
import { setupSdk } from '../../utils/sdk'

import type { SocketYml } from '@socketsecurity/config'
Expand Down Expand Up @@ -40,13 +40,13 @@ export async function createReport(
cause
})
})
const packagePaths = await getPackageFiles(
const packagePaths = await getPackageFilesFullScans(
cwd,
inputPaths,
socketConfig,
supportedFiles
supportedFiles,
socketConfig
)
const { length: packagePathsCount } = packagePaths
const packagePathsCount = packagePaths.length
if (packagePathsCount && isDebug()) {
for (const pkgPath of packagePaths) {
debugLog(`Uploading: ${pkgPath}`)
Expand Down
21 changes: 19 additions & 2 deletions src/commands/scan/cmd-scan-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,30 @@ const config: CliCommandConfig = {
default: false,
description:
'Set the visibility (true/false) of the scan in your dashboard'
},
view: {
type: 'boolean',
shortFlag: 'v',
default: true,
description:
'Will wait for and return the created report. Use --no-view to disable.'
}
},
// TODO: your project's "socket.yml" file's "projectIgnorePaths"
help: (command, config) => `
Usage
$ ${command} [...options] <org> <TARGET> [TARGET...]

Where TARGET is a FILE or DIR that _must_ be inside the CWD.
Uploads the specified "package.json" and lock files for JavaScript, Python,
Go, Scala, Gradle, and Kotlin dependency manifests.
If any folder is specified, the ones found in there recursively are uploaded.

Supports globbing such as "**/package.json", "**/requirements.txt", etc.

Ignores any file specified in your project's ".gitignore" and also has a
sensible set of default ignores from the "ignore-by-default" module.

TARGET should be a FILE or DIR that _must_ be inside the CWD.

When a FILE is given only that FILE is targeted. Otherwise any eligible
files in the given DIR will be considered.
Expand Down Expand Up @@ -134,7 +151,7 @@ async function run(

let { branch: branchName, repo: repoName } = cli.flags

const apiToken = getDefaultToken()
const apiToken = getDefaultToken() // This checks if we _can_ suggest anything

if (!apiToken && (!orgSlug || !repoName || !branchName || !targets.length)) {
// Without api token we cannot recover because we can't request more info
Expand Down
11 changes: 1 addition & 10 deletions src/commands/scan/cmd-scan-del.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
import { deleteOrgFullScan } from './delete-full-scan'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { AuthError } from '../../utils/errors'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'
import { getDefaultToken } from '../../utils/sdk'

import type { CliCommandConfig } from '../../utils/meow-with-subcommands'

Expand Down Expand Up @@ -75,12 +73,5 @@ async function run(
return
}

const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}

await deleteOrgFullScan(orgSlug, fullScanId, apiToken)
await deleteOrgFullScan(orgSlug, fullScanId)
}
46 changes: 12 additions & 34 deletions src/commands/scan/cmd-scan-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
import { listFullScans } from './list-full-scans'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { AuthError } from '../../utils/errors'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'
import { getDefaultToken } from '../../utils/sdk'

import type {
CliCommandConfig,
Expand Down Expand Up @@ -113,37 +111,17 @@ async function run(
return
}

const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}

await listFullScans(
await listFullScans({
direction: String(cli.flags['direction'] || ''),
from_time: String(cli.flags['fromTime'] || ''),
orgSlug,
// TODO: refine this object to what we need
{
outputJson: cli.flags['json'],
outputMarkdown: cli.flags['markdown'],
orgSlug,
sort: cli.flags['sort'],
direction: cli.flags['direction'],
per_page: cli.flags['perPage'],
page: cli.flags['page'],
from_time: cli.flags['fromTime'],
until_time: cli.flags['untilTime']
} as {
outputJson: boolean
outputMarkdown: boolean
orgSlug: string
sort: string
direction: string
per_page: number
page: number
from_time: string
until_time: string
},
apiToken
)
outputKind: cli.flags['json']
? 'json'
: cli.flags['markdown']
? 'markdown'
: 'print',
page: Number(cli.flags['page'] || 1),
per_page: Number(cli.flags['perPage'] || 30),
sort: String(cli.flags['sort'] || '')
})
}
15 changes: 5 additions & 10 deletions src/commands/scan/cmd-scan-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
import { getOrgScanMetadata } from './get-full-scan-metadata'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { AuthError } from '../../utils/errors'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'
import { getDefaultToken } from '../../utils/sdk'

import type {
CliCommandConfig,
Expand Down Expand Up @@ -78,12 +76,9 @@ async function run(
return
}

const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}

await getOrgScanMetadata(orgSlug, fullScanId, apiToken)
await getOrgScanMetadata(
orgSlug,
fullScanId,
cli.flags['json'] ? 'json' : cli.flags['markdown'] ? 'markdown' : 'print'
)
}
Loading
Loading