From 079e6dd56e27ccfd95f511ac741bee08cd7dcffc Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Thu, 20 Mar 2025 15:26:40 +0100 Subject: [PATCH 1/2] Apply handle pattern to audit-logs --- src/commands/audit-log/cmd-audit-log.ts | 4 +- src/commands/audit-log/fetch-audit-log.ts | 81 ++++++++++++++++ src/commands/audit-log/handle-audit-log.ts | 33 +++++++ .../{get-audit-log.ts => output-audit-log.ts} | 97 ++++--------------- 4 files changed, 134 insertions(+), 81 deletions(-) create mode 100644 src/commands/audit-log/fetch-audit-log.ts create mode 100644 src/commands/audit-log/handle-audit-log.ts rename src/commands/audit-log/{get-audit-log.ts => output-audit-log.ts} (63%) diff --git a/src/commands/audit-log/cmd-audit-log.ts b/src/commands/audit-log/cmd-audit-log.ts index d65dfc9d..356568b1 100644 --- a/src/commands/audit-log/cmd-audit-log.ts +++ b/src/commands/audit-log/cmd-audit-log.ts @@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs' import { logger } from '@socketsecurity/registry/lib/logger' -import { getAuditLog } from './get-audit-log' +import { handleAuditLog } from './handle-audit-log' import constants from '../../constants' import { commonFlags, outputFlags } from '../../flags' import { meowOrExit } from '../../utils/meow-with-subcommands' @@ -96,7 +96,7 @@ async function run( return } - await getAuditLog({ + await handleAuditLog({ orgSlug, outputKind: json ? 'json' : markdown ? 'markdown' : 'print', page: Number(page || 0), diff --git a/src/commands/audit-log/fetch-audit-log.ts b/src/commands/audit-log/fetch-audit-log.ts new file mode 100644 index 00000000..17ae7255 --- /dev/null +++ b/src/commands/audit-log/fetch-audit-log.ts @@ -0,0 +1,81 @@ +import constants from '../../constants' +import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api' +import { AuthError } from '../../utils/errors' +import { getDefaultToken, setupSdk } from '../../utils/sdk' + +import type { SocketSdkReturnType } from '@socketsecurity/sdk' + +export async function fetchAuditLog({ + logType, + orgSlug, + outputKind, + page, + perPage +}: { + outputKind: 'json' | 'markdown' | 'print' + orgSlug: string + page: number + perPage: number + logType: string +}): Promise['data'] | void> { + 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.' + ) + } + + return await fetchAuditLogWithToken({ + apiToken, + logType, + orgSlug, + outputKind, + page, + perPage + }) +} + +export async function fetchAuditLogWithToken({ + apiToken, + logType, + orgSlug, + outputKind, + page, + perPage +}: { + apiToken: string + outputKind: 'json' | 'markdown' | 'print' + orgSlug: string + page: number + perPage: number + logType: string +}): Promise['data'] | void> { + // Lazily access constants.spinner. + const { spinner } = constants + + spinner.start(`Looking up audit log for ${orgSlug}`) + + const socketSdk = await setupSdk(apiToken) + const result = await handleApiCall( + socketSdk.getAuditLogEvents(orgSlug, { + // I'm not sure this is used at all. + outputJson: String(outputKind === 'json'), + // I'm not sure this is used at all. + outputMarkdown: String(outputKind === 'markdown'), + orgSlug, + type: logType, + page: String(page), + per_page: String(perPage) + }), + `Looking up audit log for ${orgSlug}\n` + ) + + if (!result.success) { + handleUnsuccessfulApiResponse('getAuditLogEvents', result) + return + } + + spinner.stop() + + return result.data +} diff --git a/src/commands/audit-log/handle-audit-log.ts b/src/commands/audit-log/handle-audit-log.ts new file mode 100644 index 00000000..dfa18855 --- /dev/null +++ b/src/commands/audit-log/handle-audit-log.ts @@ -0,0 +1,33 @@ +import { fetchAuditLog } from './fetch-audit-log' +import { outputAuditLog } from './output-audit-log' + +export async function handleAuditLog({ + logType, + orgSlug, + outputKind, + page, + perPage +}: { + outputKind: 'json' | 'markdown' | 'print' + orgSlug: string + page: number + perPage: number + logType: string +}): Promise { + const auditLogs = await fetchAuditLog({ + orgSlug, + outputKind, + page, + perPage, + logType + }) + if (!auditLogs) return + + await outputAuditLog(auditLogs, { + logType, + orgSlug, + outputKind, + page, + perPage + }) +} diff --git a/src/commands/audit-log/get-audit-log.ts b/src/commands/audit-log/output-audit-log.ts similarity index 63% rename from src/commands/audit-log/get-audit-log.ts rename to src/commands/audit-log/output-audit-log.ts index ed12ee7d..56f31bee 100644 --- a/src/commands/audit-log/get-audit-log.ts +++ b/src/commands/audit-log/output-audit-log.ts @@ -3,11 +3,7 @@ import { stripIndents } from 'common-tags' import { logger } from '@socketsecurity/registry/lib/logger' import { Separator, select } from '@socketsecurity/registry/lib/prompts' -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' import type { SocketSdkReturnType } from '@socketsecurity/sdk' @@ -16,41 +12,29 @@ type AuditChoice = Choice type AuditChoices = Array -export async function getAuditLog({ - logType, - orgSlug, - outputKind, - page, - perPage -}: { - outputKind: 'json' | 'markdown' | 'print' - orgSlug: string - page: number - perPage: number - logType: string -}): Promise { - 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.' - ) - } - - const auditLogs = await getAuditLogWithToken({ - apiToken, +export async function outputAuditLog( + auditLogs: SocketSdkReturnType<'getAuditLogEvents'>['data'], + { + logType, orgSlug, outputKind, page, - perPage, - logType - }) - if (!auditLogs) return - - if (outputKind === 'json') + perPage + }: { + outputKind: 'json' | 'markdown' | 'print' + orgSlug: string + page: number + perPage: number + logType: string + } +): Promise { + if (outputKind === 'json') { await outputAsJson(auditLogs.results, orgSlug, logType, page, perPage) - else if (outputKind === 'markdown') + } else if (outputKind === 'markdown') { await outputAsMarkdown(auditLogs.results, orgSlug, logType, page, perPage) - else await outputAsPrint(auditLogs.results, orgSlug, logType) + } else { + await outputAsPrint(auditLogs.results, orgSlug, logType) + } } async function outputAsJson( @@ -174,48 +158,3 @@ async function outputAsPrint( ] ) } - -async function getAuditLogWithToken({ - apiToken, - logType, - orgSlug, - outputKind, - page, - perPage -}: { - apiToken: string - outputKind: 'json' | 'markdown' | 'print' - orgSlug: string - page: number - perPage: number - logType: string -}): Promise['data'] | void> { - // Lazily access constants.spinner. - const { spinner } = constants - - spinner.start(`Looking up audit log for ${orgSlug}`) - - const socketSdk = await setupSdk(apiToken) - const result = await handleApiCall( - socketSdk.getAuditLogEvents(orgSlug, { - // I'm not sure this is used at all. - outputJson: String(outputKind === 'json'), - // I'm not sure this is used at all. - outputMarkdown: String(outputKind === 'markdown'), - orgSlug, - type: logType, - page: String(page), - per_page: String(perPage) - }), - `Looking up audit log for ${orgSlug}\n` - ) - - if (!result.success) { - handleUnsuccessfulApiResponse('getAuditLogEvents', result) - return - } - - spinner.stop() - - return result.data -} From 8e736f730c1160a4ae1e1115253a02243ae517d0 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Thu, 20 Mar 2025 15:55:42 +0100 Subject: [PATCH 2/2] Maybe it makes more sense this way --- src/commands/audit-log/fetch-audit-log.ts | 34 +++++++++++------------ 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/commands/audit-log/fetch-audit-log.ts b/src/commands/audit-log/fetch-audit-log.ts index 17ae7255..1aaa5c93 100644 --- a/src/commands/audit-log/fetch-audit-log.ts +++ b/src/commands/audit-log/fetch-audit-log.ts @@ -25,8 +25,7 @@ export async function fetchAuditLog({ ) } - return await fetchAuditLogWithToken({ - apiToken, + return await fetchAuditLogWithToken(apiToken, { logType, orgSlug, outputKind, @@ -35,21 +34,22 @@ export async function fetchAuditLog({ }) } -export async function fetchAuditLogWithToken({ - apiToken, - logType, - orgSlug, - outputKind, - page, - perPage -}: { - apiToken: string - outputKind: 'json' | 'markdown' | 'print' - orgSlug: string - page: number - perPage: number - logType: string -}): Promise['data'] | void> { +export async function fetchAuditLogWithToken( + apiToken: string, + { + logType, + orgSlug, + outputKind, + page, + perPage + }: { + outputKind: 'json' | 'markdown' | 'print' + orgSlug: string + page: number + perPage: number + logType: string + } +): Promise['data'] | void> { // Lazily access constants.spinner. const { spinner } = constants