Skip to content

Commit

Permalink
add auth checks to other commands
Browse files Browse the repository at this point in the history
  • Loading branch information
charliegerard committed Aug 21, 2024
1 parent 7d1f26f commit 3ff7fac
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 42 deletions.
14 changes: 10 additions & 4 deletions src/commands/audit-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Check warning on line 15 in src/commands/audit-log.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

There should be at least one empty line between import groups
import { AuthError } from '../utils/errors'

Check warning on line 16 in src/commands/audit-log.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

`../utils/errors` import should occur before import of `../utils/formatting`

export const auditLog: CliSubcommand = {
description: 'Look up the audit log for an organization',
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -132,9 +137,10 @@ type AuditChoices = (Separator | AuditChoice)[]
async function fetchOrgAuditLog(
orgSlug: string,
input: CommandContext,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
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`
Expand Down
15 changes: 8 additions & 7 deletions src/commands/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { getDefaultKey, setupSdk } from '../utils/sdk'

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

Check warning on line 11 in src/commands/organization.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

There should be at least one empty line between import groups
import { AuthError } from '../utils/errors'

Check warning on line 12 in src/commands/organization.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

`../utils/errors` import should occur before import of `../utils/sdk`

export const organizations: CliSubcommand = {
description: 'List organizations associated with the API key used',
Expand Down Expand Up @@ -42,6 +43,9 @@ function setupCommand(

async function fetchOrganizations(): Promise<void> {
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()

Expand All @@ -57,13 +61,10 @@ async function fetchOrganizations(): Promise<void> {
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(`
Expand Down
2 changes: 1 addition & 1 deletion src/commands/report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 9 additions & 3 deletions src/commands/repos/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk'

import type { CliSubcommand } from '../../utils/meow-with-subcommands'
import type { Ora } from 'ora'

Check warning on line 14 in src/commands/repos/create.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

There should be at least one empty line between import groups
import { AuthError } from '../../utils/errors'

Check warning on line 15 in src/commands/repos/create.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

`../../utils/errors` import should occur before import of `../../utils/formatting`

export const create: CliSubcommand = {
description: 'Create a repository in an organization',
async run(argv, importMeta, { parentName }) {
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)
}
}
}
Expand Down Expand Up @@ -145,9 +150,10 @@ function setupCommand(
async function createRepo(
orgSlug: string,
input: CommandContext,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const result = await handleApiCall(
socketSdk.createOrgRepo(orgSlug, input),
'creating repository'
Expand Down
12 changes: 9 additions & 3 deletions src/commands/repos/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ import { getDefaultKey, setupSdk } from '../../utils/sdk'

import type { CliSubcommand } from '../../utils/meow-with-subcommands'
import type { Ora } from 'ora'

Check warning on line 12 in src/commands/repos/delete.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

There should be at least one empty line between import groups
import { AuthError } from '../../utils/errors'

Check warning on line 13 in src/commands/repos/delete.ts

View workflow job for this annotation

GitHub Actions / Linting / Test (20, ubuntu-latest)

`../../utils/errors` import should occur before import of `../../utils/sdk`

export const del: CliSubcommand = {
description: 'Delete a repository in an organization',
async run(argv, importMeta, { parentName }) {
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)
}
}
}
Expand Down Expand Up @@ -71,9 +76,10 @@ function setupCommand(
async function deleteRepository(
orgSlug: string,
repoName: string,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const result = await handleApiCall(
socketSdk.deleteOrgRepo(orgSlug, repoName),
'deleting repository'
Expand Down
12 changes: 9 additions & 3 deletions src/commands/repos/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ 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',
async run(argv, importMeta, { parentName }) {
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)
}
}
}
Expand Down Expand Up @@ -129,9 +134,10 @@ function setupCommand(
async function listOrgRepos(
orgSlug: string,
input: CommandContext,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const result = await handleApiCall(
socketSdk.getOrgRepoList(orgSlug, input),
'listing repositories'
Expand Down
12 changes: 9 additions & 3 deletions src/commands/repos/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ 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',
async run(argv, importMeta, { parentName }) {
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)
}
}
}
Expand Down Expand Up @@ -145,9 +150,10 @@ function setupCommand(
async function updateRepository(
orgSlug: string,
input: CommandContext,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const result = await handleApiCall(
socketSdk.updateOrgRepo(orgSlug, input.name, input),
'updating repository'
Expand Down
12 changes: 9 additions & 3 deletions src/commands/repos/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ 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',
async run(argv, importMeta, { parentName }) {
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)
}
}
}
Expand Down Expand Up @@ -89,9 +94,10 @@ function setupCommand(
async function viewRepository(
orgSlug: string,
repoName: string,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const result = await handleApiCall(
socketSdk.getOrgRepo(orgSlug, repoName),
'fetching repository'
Expand Down
12 changes: 9 additions & 3 deletions src/commands/scan/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ 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',
async run(argv, importMeta, { parentName }) {
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)
}
}
}
Expand Down Expand Up @@ -203,9 +208,10 @@ async function setupCommand(

async function createFullScan(
input: CommandContext,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const {
orgSlug,
repoName,
Expand Down
12 changes: 9 additions & 3 deletions src/commands/scan/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ 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',
async run(argv, importMeta, { parentName }) {
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)
}
}
}
Expand Down Expand Up @@ -87,9 +92,10 @@ function setupCommand(
async function deleteOrgFullScan(
orgSlug: string,
fullScanId: string,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const result = await handleApiCall(
socketSdk.deleteOrgFullScan(orgSlug, fullScanId),
'Deleting scan'
Expand Down
12 changes: 9 additions & 3 deletions src/commands/scan/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ 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',
async run(argv, importMeta, { parentName }) {
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)
}
}
}
Expand Down Expand Up @@ -149,9 +154,10 @@ function setupCommand(
async function listOrgFullScan(
orgSlug: string,
input: CommandContext,
spinner: Ora
spinner: Ora,
apiKey: string
): Promise<void> {
const socketSdk = await setupSdk(getDefaultKey())
const socketSdk = await setupSdk(apiKey)
const result = await handleApiCall(
socketSdk.getOrgFullScanList(orgSlug, input),
'Listing scans'
Expand Down
Loading

0 comments on commit 3ff7fac

Please sign in to comment.