diff --git a/src/commands/audit-log.ts b/src/commands/audit-log.ts index 4a57cfb0..f3ebebf5 100644 --- a/src/commands/audit-log.ts +++ b/src/commands/audit-log.ts @@ -9,10 +9,11 @@ import { handleUnsuccessfulApiResponse } from '../utils/api-helpers' import { printFlagList } from '../utils/formatting' -import { FREE_API_KEY, getDefaultKey, setupSdk } from '../utils/sdk' +import { getDefaultKey, setupSdk } from '../utils/sdk' import type { CliSubcommand } from '../utils/meow-with-subcommands' import type { Ora } from 'ora' +import { AuthError } from '../utils/errors' export const auditLog: CliSubcommand = { description: 'Look up the audit log for an organization', @@ -21,8 +22,12 @@ export const auditLog: CliSubcommand = { const input = setupCommand(name, auditLog.description, argv, importMeta) if (input) { + const apiKey = getDefaultKey() + if(!apiKey){ + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") + } const spinner = ora(`Looking up audit log for ${input.orgSlug}\n`).start() - await fetchOrgAuditLog(input.orgSlug, input, spinner) + await fetchOrgAuditLog(input.orgSlug, input, spinner, apiKey) } } } @@ -132,9 +137,10 @@ type AuditChoices = (Separator | AuditChoice)[] async function fetchOrgAuditLog( orgSlug: string, input: CommandContext, - spinner: Ora + spinner: Ora, + apiKey: string ): Promise { - const socketSdk = await setupSdk(getDefaultKey() || FREE_API_KEY) + const socketSdk = await setupSdk(apiKey) const result = await handleApiCall( socketSdk.getAuditLogEvents(orgSlug, input), `Looking up audit log for ${orgSlug}\n` diff --git a/src/commands/dependencies.ts b/src/commands/dependencies.ts index 01cd4b4f..2eabb692 100644 --- a/src/commands/dependencies.ts +++ b/src/commands/dependencies.ts @@ -13,7 +13,7 @@ import { printFlagList } from '../utils/formatting' import { getDefaultKey, setupSdk } from '../utils/sdk' import type { CliSubcommand } from '../utils/meow-with-subcommands' -import type { Ora } from 'ora' +import { AuthError } from '../utils/errors' export const dependencies: CliSubcommand = { description: @@ -23,9 +23,7 @@ export const dependencies: CliSubcommand = { const input = setupCommand(name, dependencies.description, argv, importMeta) if (input) { - const spinnerText = 'Searching dependencies...' - const spinner = ora(spinnerText).start() - await searchDeps(input, spinner) + await searchDeps(input) } } } @@ -100,10 +98,17 @@ function setupCommand( } async function searchDeps( - { limit, offset, outputJson }: CommandContext, - spinner: Ora + { limit, offset, outputJson }: CommandContext ): Promise { - const socketSdk = await setupSdk(getDefaultKey()) + const apiKey = getDefaultKey() + if(!apiKey){ + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") + } + const spinnerText = 'Searching dependencies...' + const spinner = ora(spinnerText).start() + + const socketSdk = await setupSdk(apiKey) + const result = await handleApiCall( socketSdk.searchDependencies({ limit, offset }), 'Searching dependencies' diff --git a/src/commands/organization.ts b/src/commands/organization.ts index 41479d41..1a970c5a 100644 --- a/src/commands/organization.ts +++ b/src/commands/organization.ts @@ -9,6 +9,7 @@ import { import { getDefaultKey, setupSdk } from '../utils/sdk' import type { CliSubcommand } from '../utils/meow-with-subcommands' +import { AuthError } from '../utils/errors' export const organizations: CliSubcommand = { description: 'List organizations associated with the API key used', @@ -42,6 +43,9 @@ function setupCommand( async function fetchOrganizations(): Promise { const apiKey = getDefaultKey() + if(!apiKey){ + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") + } const socketSdk = await setupSdk(apiKey) const spinner = ora('Fetching organizations...').start() @@ -57,13 +61,10 @@ async function fetchOrganizations(): Promise { spinner.stop() const organizations = Object.values(result.data.organizations) - if (apiKey) { - console.log( - `List of organizations associated with your API key: ${chalk.italic(apiKey)}` - ) - } else { - console.log('List of organizations associated with your API key.') - } + + console.log( + `List of organizations associated with your API key: ${chalk.italic(apiKey)}` + ) for (const o of organizations) { console.log(` diff --git a/src/commands/report/index.ts b/src/commands/report/index.ts index c25c7b9a..d4fbe6b4 100644 --- a/src/commands/report/index.ts +++ b/src/commands/report/index.ts @@ -4,7 +4,7 @@ import { meowWithSubcommands } from '../../utils/meow-with-subcommands' import type { CliSubcommand } from '../../utils/meow-with-subcommands' -const description = 'Project report related commands' +const description = '[Deprecated] Project report related commands' export const report: CliSubcommand = { description, diff --git a/src/commands/repos/create.ts b/src/commands/repos/create.ts index 5f4350a7..ee4d05f2 100644 --- a/src/commands/repos/create.ts +++ b/src/commands/repos/create.ts @@ -12,6 +12,7 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk' import type { CliSubcommand } from '../../utils/meow-with-subcommands' import type { Ora } from 'ora' +import { AuthError } from '../../utils/errors' export const create: CliSubcommand = { description: 'Create a repository in an organization', @@ -19,9 +20,13 @@ export const create: CliSubcommand = { const name = `${parentName} create` const input = setupCommand(name, create.description, argv, importMeta) if (input) { + const apiKey = getDefaultKey() + if(!apiKey){ + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") + } const spinnerText = 'Creating repository... \n' const spinner = ora(spinnerText).start() - await createRepo(input.orgSlug, input, spinner) + await createRepo(input.orgSlug, input, spinner, apiKey) } } } @@ -145,9 +150,10 @@ function setupCommand( async function createRepo( orgSlug: string, input: CommandContext, - spinner: Ora + spinner: Ora, + apiKey: string ): Promise { - const socketSdk = await setupSdk(getDefaultKey()) + const socketSdk = await setupSdk(apiKey) const result = await handleApiCall( socketSdk.createOrgRepo(orgSlug, input), 'creating repository' diff --git a/src/commands/repos/delete.ts b/src/commands/repos/delete.ts index f30746ee..76286fda 100644 --- a/src/commands/repos/delete.ts +++ b/src/commands/repos/delete.ts @@ -10,6 +10,7 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk' import type { CliSubcommand } from '../../utils/meow-with-subcommands' import type { Ora } from 'ora' +import { AuthError } from '../../utils/errors' export const del: CliSubcommand = { description: 'Delete a repository in an organization', @@ -17,9 +18,13 @@ export const del: CliSubcommand = { const name = `${parentName} del` const input = setupCommand(name, del.description, argv, importMeta) if (input) { + const apiKey = getDefaultKey() + if(!apiKey){ + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") + } const spinnerText = 'Deleting repository... \n' const spinner = ora(spinnerText).start() - await deleteRepository(input.orgSlug, input.repoName, spinner) + await deleteRepository(input.orgSlug, input.repoName, spinner, apiKey) } } } @@ -71,9 +76,10 @@ function setupCommand( async function deleteRepository( orgSlug: string, repoName: string, - spinner: Ora + spinner: Ora, + apiKey: string ): Promise { - const socketSdk = await setupSdk(getDefaultKey()) + const socketSdk = await setupSdk(apiKey) const result = await handleApiCall( socketSdk.deleteOrgRepo(orgSlug, repoName), 'deleting repository' diff --git a/src/commands/repos/list.ts b/src/commands/repos/list.ts index a7b4af16..4a32a2a8 100644 --- a/src/commands/repos/list.ts +++ b/src/commands/repos/list.ts @@ -14,6 +14,7 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk' import type { CliSubcommand } from '../../utils/meow-with-subcommands' import type { Ora } from 'ora' +import { AuthError } from '../../utils/errors' export const list: CliSubcommand = { description: 'List repositories in an organization', @@ -21,9 +22,13 @@ export const list: CliSubcommand = { const name = `${parentName} list` const input = setupCommand(name, list.description, argv, importMeta) if (input) { + const apiKey = getDefaultKey() + if(!apiKey){ + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") + } const spinnerText = 'Listing repositories... \n' const spinner = ora(spinnerText).start() - await listOrgRepos(input.orgSlug, input, spinner) + await listOrgRepos(input.orgSlug, input, spinner, apiKey) } } } @@ -129,9 +134,10 @@ function setupCommand( async function listOrgRepos( orgSlug: string, input: CommandContext, - spinner: Ora + spinner: Ora, + apiKey: string ): Promise { - const socketSdk = await setupSdk(getDefaultKey()) + const socketSdk = await setupSdk(apiKey) const result = await handleApiCall( socketSdk.getOrgRepoList(orgSlug, input), 'listing repositories' diff --git a/src/commands/repos/update.ts b/src/commands/repos/update.ts index 62fa7013..80d224f1 100644 --- a/src/commands/repos/update.ts +++ b/src/commands/repos/update.ts @@ -12,6 +12,7 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk' import type { CliSubcommand } from '../../utils/meow-with-subcommands' import type { Ora } from 'ora' +import { AuthError } from '../../utils/errors' export const update: CliSubcommand = { description: 'Update a repository in an organization', @@ -19,9 +20,13 @@ export const update: CliSubcommand = { const name = `${parentName} update` const input = setupCommand(name, update.description, argv, importMeta) if (input) { + const apiKey = getDefaultKey() + if(!apiKey){ + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") + } const spinnerText = 'Updating repository... \n' const spinner = ora(spinnerText).start() - await updateRepository(input.orgSlug, input, spinner) + await updateRepository(input.orgSlug, input, spinner, apiKey) } } } @@ -145,9 +150,10 @@ function setupCommand( async function updateRepository( orgSlug: string, input: CommandContext, - spinner: Ora + spinner: Ora, + apiKey: string ): Promise { - const socketSdk = await setupSdk(getDefaultKey()) + const socketSdk = await setupSdk(apiKey) const result = await handleApiCall( socketSdk.updateOrgRepo(orgSlug, input.name, input), 'updating repository' diff --git a/src/commands/repos/view.ts b/src/commands/repos/view.ts index 6541f258..bad9fdef 100644 --- a/src/commands/repos/view.ts +++ b/src/commands/repos/view.ts @@ -14,6 +14,7 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk' import type { CliSubcommand } from '../../utils/meow-with-subcommands' import type { Ora } from 'ora' +import { AuthError } from '../../utils/errors' export const view: CliSubcommand = { description: 'View repositories in an organization', @@ -21,9 +22,13 @@ export const view: CliSubcommand = { const name = `${parentName} view` const input = setupCommand(name, view.description, argv, importMeta) if (input) { + const apiKey = getDefaultKey() + if(!apiKey){ + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") + } const spinnerText = 'Fetching repository... \n' const spinner = ora(spinnerText).start() - await viewRepository(input.orgSlug, input.repositoryName, spinner) + await viewRepository(input.orgSlug, input.repositoryName, spinner, apiKey) } } } @@ -89,9 +94,10 @@ function setupCommand( async function viewRepository( orgSlug: string, repoName: string, - spinner: Ora + spinner: Ora, + apiKey: string ): Promise { - const socketSdk = await setupSdk(getDefaultKey()) + const socketSdk = await setupSdk(apiKey) const result = await handleApiCall( socketSdk.getOrgRepo(orgSlug, repoName), 'fetching repository' diff --git a/src/commands/scan/create.ts b/src/commands/scan/create.ts index 59fe98f2..f2296b9e 100644 --- a/src/commands/scan/create.ts +++ b/src/commands/scan/create.ts @@ -18,6 +18,7 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk' import type { CliSubcommand } from '../../utils/meow-with-subcommands' import type { Ora } from 'ora' +import { AuthError } from '../../utils/errors' export const create: CliSubcommand = { description: 'Create a scan', @@ -25,9 +26,13 @@ export const create: CliSubcommand = { const name = `${parentName} create` const input = await setupCommand(name, create.description, argv, importMeta) if (input) { + const apiKey = getDefaultKey() + if(!apiKey){ + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") + } const spinnerText = 'Creating a scan... \n' const spinner = ora(spinnerText).start() - await createFullScan(input, spinner) + await createFullScan(input, spinner, apiKey) } } } @@ -203,9 +208,10 @@ async function setupCommand( async function createFullScan( input: CommandContext, - spinner: Ora + spinner: Ora, + apiKey: string ): Promise { - const socketSdk = await setupSdk(getDefaultKey()) + const socketSdk = await setupSdk(apiKey) const { orgSlug, repoName, diff --git a/src/commands/scan/delete.ts b/src/commands/scan/delete.ts index deb5e50a..f1f401a3 100644 --- a/src/commands/scan/delete.ts +++ b/src/commands/scan/delete.ts @@ -12,6 +12,7 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk' import type { CliSubcommand } from '../../utils/meow-with-subcommands' import type { Ora } from 'ora' +import { AuthError } from '../../utils/errors' export const del: CliSubcommand = { description: 'Delete a scan', @@ -19,9 +20,13 @@ export const del: CliSubcommand = { const name = `${parentName} del` const input = setupCommand(name, del.description, argv, importMeta) if (input) { + const apiKey = getDefaultKey() + if(!apiKey){ + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") + } const spinnerText = 'Deleting scan...' const spinner = ora(spinnerText).start() - await deleteOrgFullScan(input.orgSlug, input.fullScanId, spinner) + await deleteOrgFullScan(input.orgSlug, input.fullScanId, spinner, apiKey) } } } @@ -87,9 +92,10 @@ function setupCommand( async function deleteOrgFullScan( orgSlug: string, fullScanId: string, - spinner: Ora + spinner: Ora, + apiKey: string ): Promise { - const socketSdk = await setupSdk(getDefaultKey()) + const socketSdk = await setupSdk(apiKey) const result = await handleApiCall( socketSdk.deleteOrgFullScan(orgSlug, fullScanId), 'Deleting scan' diff --git a/src/commands/scan/list.ts b/src/commands/scan/list.ts index 995a8a6b..55648fef 100644 --- a/src/commands/scan/list.ts +++ b/src/commands/scan/list.ts @@ -14,6 +14,7 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk' import type { CliSubcommand } from '../../utils/meow-with-subcommands' import type { Ora } from 'ora' +import { AuthError } from '../../utils/errors' export const list: CliSubcommand = { description: 'List scans for an organization', @@ -21,9 +22,13 @@ export const list: CliSubcommand = { const name = `${parentName} list` const input = setupCommand(name, list.description, argv, importMeta) if (input) { + const apiKey = getDefaultKey() + if(!apiKey){ + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") + } const spinnerText = 'Listing scans... \n' const spinner = ora(spinnerText).start() - await listOrgFullScan(input.orgSlug, input, spinner) + await listOrgFullScan(input.orgSlug, input, spinner, apiKey) } } } @@ -149,9 +154,10 @@ function setupCommand( async function listOrgFullScan( orgSlug: string, input: CommandContext, - spinner: Ora + spinner: Ora, + apiKey: string ): Promise { - const socketSdk = await setupSdk(getDefaultKey()) + const socketSdk = await setupSdk(apiKey) const result = await handleApiCall( socketSdk.getOrgFullScanList(orgSlug, input), 'Listing scans' diff --git a/src/commands/scan/metadata.ts b/src/commands/scan/metadata.ts index dddec4d6..dbddd8fb 100644 --- a/src/commands/scan/metadata.ts +++ b/src/commands/scan/metadata.ts @@ -12,6 +12,7 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk' import type { CliSubcommand } from '../../utils/meow-with-subcommands' import type { Ora } from 'ora' +import { AuthError } from '../../utils/errors' export const metadata: CliSubcommand = { description: "Get a scan's metadata", @@ -19,9 +20,13 @@ export const metadata: CliSubcommand = { const name = `${parentName} metadata` const input = setupCommand(name, metadata.description, argv, importMeta) if (input) { + const apiKey = getDefaultKey() + if(!apiKey){ + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") + } const spinnerText = "Getting scan's metadata... \n" const spinner = ora(spinnerText).start() - await getOrgScanMetadata(input.orgSlug, input.scanID, spinner) + await getOrgScanMetadata(input.orgSlug, input.scanID, spinner, apiKey) } } } @@ -87,9 +92,10 @@ function setupCommand( async function getOrgScanMetadata( orgSlug: string, scanId: string, - spinner: Ora + spinner: Ora, + apiKey: string ): Promise { - const socketSdk = await setupSdk(getDefaultKey()) + const socketSdk = await setupSdk(apiKey) const result = await handleApiCall( socketSdk.getOrgFullScanMetadata(orgSlug, scanId), 'Listing scans' diff --git a/src/commands/scan/stream.ts b/src/commands/scan/stream.ts index be1cccf9..be7d4fe4 100644 --- a/src/commands/scan/stream.ts +++ b/src/commands/scan/stream.ts @@ -12,6 +12,7 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk' import type { CliSubcommand } from '../../utils/meow-with-subcommands' import type { Ora } from 'ora' +import { AuthError } from '../../utils/errors' export const stream: CliSubcommand = { description: 'Stream the output of a scan', @@ -19,9 +20,13 @@ export const stream: CliSubcommand = { const name = `${parentName} stream` const input = setupCommand(name, stream.description, argv, importMeta) if (input) { + const apiKey = getDefaultKey() + if(!apiKey){ + throw new AuthError("User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.") + } const spinnerText = 'Streaming scan...\n' const spinner = ora(spinnerText).start() - await getOrgFullScan(input.orgSlug, input.fullScanId, input.file, spinner) + await getOrgFullScan(input.orgSlug, input.fullScanId, input.file, spinner, apiKey) } } } @@ -90,9 +95,10 @@ async function getOrgFullScan( orgSlug: string, fullScanId: string, file: string | undefined, - spinner: Ora + spinner: Ora, + apiKey: string ): Promise { - const socketSdk = await setupSdk(getDefaultKey()) + const socketSdk = await setupSdk(apiKey) const result = await handleApiCall( socketSdk.getOrgFullScan(orgSlug, fullScanId, file), 'Streaming a scan'