Skip to content

Commit b858a21

Browse files
committed
wip
1 parent 80ca958 commit b858a21

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ try {
2020
entry[0] = 'raw-npm'
2121
} else if (entry[0] === 'rawNpx') {
2222
entry[0] = 'raw-npx'
23+
} else if (entry[0] === 'auditlog') {
24+
entry[0] = 'audit-log'
2325
}
2426
return entry
2527
}))

lib/commands/audit-log/index.js

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// @ts-nocheck
2+
/* eslint-disable no-console */
3+
4+
import chalk from 'chalk'
5+
//@ts-ignore
6+
import chalkTable from 'chalk-table'
7+
import meow from 'meow'
8+
import ora from 'ora'
9+
10+
import { outputFlags } from '../../flags/index.js'
11+
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
12+
import { InputError } from '../../utils/errors.js'
13+
import { prepareFlags } from '../../utils/flags.js'
14+
import { printFlagList } from '../../utils/formatting.js'
15+
import { FREE_API_KEY, getDefaultKey, setupSdk } from '../../utils/sdk.js'
16+
17+
/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */
18+
export const auditlog = {
19+
description: 'Look up the audit log for an organization',
20+
async run (argv, importMeta, { parentName }) {
21+
const name = parentName + ' audit-log'
22+
23+
const input = setupCommand(name, auditlog.description, argv, importMeta)
24+
if (input) {
25+
const spinner = ora(`Looking up audit log for ${input.orgSlug}\n`).start()
26+
await fetchOrgAuditLog(input.orgSlug, input, spinner)
27+
}
28+
}
29+
}
30+
31+
const auditLogFlags = prepareFlags({
32+
type: {
33+
type: 'string',
34+
shortFlag: 't',
35+
default: '',
36+
description: 'Type of audit log',
37+
},
38+
perPage: {
39+
type: 'number',
40+
shortFlag: 'pp',
41+
default: 30,
42+
description: 'Results per page - default is 30',
43+
},
44+
page: {
45+
type: 'number',
46+
shortFlag: 'p',
47+
default: 1,
48+
description: 'Page number - default is 1',
49+
}
50+
})
51+
52+
// Internal functions
53+
54+
/**
55+
* @typedef CommandContext
56+
* @property {boolean} outputJson
57+
* @property {boolean} outputMarkdown
58+
* @property {string} orgSlug
59+
*/
60+
61+
/**
62+
* @param {string} name
63+
* @param {string} description
64+
* @param {readonly string[]} argv
65+
* @param {ImportMeta} importMeta
66+
* @returns {void|CommandContext}
67+
*/
68+
function setupCommand (name, description, argv, importMeta) {
69+
const flags = {
70+
...auditLogFlags,
71+
...outputFlags
72+
}
73+
74+
const cli = meow(`
75+
Usage
76+
$ ${name} <org slug>
77+
78+
Options
79+
${printFlagList(flags, 6)}
80+
81+
Examples
82+
$ ${name} FakeOrg
83+
`, {
84+
argv,
85+
description,
86+
importMeta,
87+
flags
88+
})
89+
90+
const {
91+
json: outputJson,
92+
markdown: outputMarkdown
93+
} = cli.flags
94+
95+
if (cli.input.length > 1) {
96+
throw new InputError('Only one package lookup supported at once')
97+
}
98+
99+
const [orgSlug = ''] = cli.input
100+
101+
if (!orgSlug) {
102+
cli.showHelp()
103+
return
104+
}
105+
106+
return {
107+
outputJson,
108+
outputMarkdown,
109+
orgSlug
110+
}
111+
}
112+
113+
/**
114+
* @typedef AuditLogData
115+
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getAuditLogEvents'>["data"]} data
116+
*/
117+
118+
/**
119+
* @param {string} orgSlug
120+
* @param {CommandContext} input
121+
* @param {import('ora').Ora} spinner
122+
* @returns {Promise<void|AuditLogData>}
123+
*/
124+
async function fetchOrgAuditLog (orgSlug, input, spinner) {
125+
const socketSdk = await setupSdk(getDefaultKey() || FREE_API_KEY)
126+
const result = await handleApiCall(socketSdk.getAuditLogEvents(orgSlug, input), 'looking up package')
127+
128+
if (!result.success) {
129+
return handleUnsuccessfulApiResponse('getAuditLogEvents', result, spinner)
130+
}
131+
spinner.stop()
132+
133+
console.log(`\n Audit log for: ${orgSlug} \n`)
134+
135+
const options = {
136+
columns: [
137+
{ field: 'event_id', name: chalk.magenta('Event id') },
138+
{ field: 'country_code', name: chalk.magenta('Country code') },
139+
{ field: 'created_at', name: chalk.magenta('Created at') },
140+
{ field: 'ip_address', name: chalk.magenta('IP address') },
141+
{ field: 'payload', name: chalk.magenta('Payload') },
142+
{ field: 'type', name: chalk.magenta('Type') },
143+
{ field: 'user_agent', name: chalk.magenta('User agent') },
144+
{ field: 'user_id', name: chalk.magenta('User Id') },
145+
{ field: 'user_email', name: chalk.magenta('User email') }
146+
]
147+
}
148+
149+
const formattedResults = result.data.results.map((/** @type {{ event_id: any; country_code: any; created_at: string | number | Date; }} */ d) => {
150+
return {
151+
...d,
152+
event_id: d.event_id,
153+
country_code: chalk.underline(`${d.country_code}`),
154+
created_at: d.created_at ? new Date(d.created_at).toLocaleDateString('en-us', { year: 'numeric', month: 'short', day: 'numeric' }) : '',
155+
}
156+
})
157+
158+
const table = chalkTable(options, formattedResults)
159+
console.log(table, '\n')
160+
161+
return {
162+
data: result.data
163+
}
164+
}

0 commit comments

Comments
 (0)