Skip to content

Commit ee8e44b

Browse files
committed
Update coana fix code
1 parent b8f8053 commit ee8e44b

File tree

3 files changed

+104
-56
lines changed

3 files changed

+104
-56
lines changed

src/commands/fix/handle-fix.mts

Lines changed: 96 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
1+
import { debugDir } from '@socketsecurity/registry/lib/debug'
22
import { logger } from '@socketsecurity/registry/lib/logger'
3-
import { pluralize } from '@socketsecurity/registry/lib/words'
43

54
import { npmFix } from './npm-fix.mts'
65
import { outputFixResult } from './output-fix-result.mts'
76
import { pnpmFix } from './pnpm-fix.mts'
87
import { CMD_NAME } from './shared.mts'
98
import constants from '../../constants.mts'
9+
import { handleApiCall } from '../../utils/api.mts'
1010
import { cmdFlagValueToArray } from '../../utils/cmd.mts'
1111
import { spawnCoana } from '../../utils/coana.mts'
1212
import { detectAndValidatePackageEnvironment } from '../../utils/package-environment.mts'
13+
import { getPackageFilesForScan } from '../../utils/path-resolve.mts'
14+
import { setupSdk } from '../../utils/sdk.mts'
15+
import { fetchSupportedScanFileNames } from '../scan/fetch-supported-scan-file-names.mts'
1316

1417
import type { FixConfig } from './agent-fix.mts'
15-
import type { OutputKind } from '../../types.mts'
18+
import type { CResult, OutputKind } from '../../types.mts'
1619
import type { Remap } from '@socketsecurity/registry/lib/objects'
1720

1821
export type HandleFixConfig = Remap<
@@ -40,66 +43,105 @@ export async function handleFix({
4043
testScript,
4144
unknownFlags,
4245
}: HandleFixConfig) {
43-
let { length: ghsasCount } = ghsas
44-
if (ghsasCount) {
45-
spinner?.start('Fetching GHSA IDs...')
46-
47-
if (ghsasCount === 1 && ghsas[0] === 'auto') {
48-
const ghsasCResult = await spawnCoana(
49-
['compute-fixes-and-upgrade-purls', cwd],
50-
{ cwd, spinner },
51-
)
52-
53-
spinner?.stop()
54-
55-
if (ghsasCResult.ok) {
56-
const ghsasOutput = ghsasCResult.data as string
57-
ghsas = cmdFlagValueToArray(
58-
/(?<=Vulnerabilities found: )[^\n]+/.exec(ghsasOutput)?.[0],
59-
)
60-
ghsasCount = ghsas.length
61-
} else {
62-
debugFn('error', 'fail: Coana CLI')
63-
debugDir('inspect', {
64-
message: ghsasCResult.message,
65-
cause: ghsasCResult.cause,
66-
})
67-
ghsas = []
68-
ghsasCount = 0
69-
}
46+
if (ghsas.length === 1 && ghsas[0] === 'auto') {
47+
let lastCResult: CResult<any>
48+
const sockSdkCResult = await setupSdk()
49+
50+
lastCResult = sockSdkCResult
51+
const sockSdk = sockSdkCResult.ok ? sockSdkCResult.data : undefined
7052

71-
spinner?.start()
53+
const supportedFilesCResult = sockSdk
54+
? await fetchSupportedScanFileNames()
55+
: undefined
56+
57+
if (supportedFilesCResult) {
58+
lastCResult = supportedFilesCResult
7259
}
7360

74-
if (ghsasCount) {
75-
spinner?.info(`Found ${ghsasCount} GHSA ${pluralize('ID', ghsasCount)}.`)
61+
const supportedFiles = supportedFilesCResult?.ok
62+
? supportedFilesCResult.data
63+
: undefined
7664

77-
const ghsaFixesCResult = await spawnCoana(
78-
[
79-
'compute-fixes-and-upgrade-purls',
65+
const packagePaths = supportedFiles
66+
? await getPackageFilesForScan(['.'], supportedFiles!, {
8067
cwd,
81-
'--apply-fixes-to',
82-
...ghsas,
83-
...unknownFlags,
84-
],
85-
{ cwd, spinner },
86-
)
87-
88-
spinner?.stop()
89-
90-
if (!ghsaFixesCResult.ok) {
91-
debugFn('error', 'fail: Coana CLI')
92-
debugDir('inspect', {
93-
message: ghsaFixesCResult.message,
94-
cause: ghsaFixesCResult.cause,
9568
})
96-
}
69+
: []
70+
71+
const uploadCResult = sockSdk
72+
? await handleApiCall(
73+
sockSdk?.uploadManifestFiles(orgSlug, packagePaths),
74+
{
75+
desc: 'upload manifests',
76+
},
77+
)
78+
: undefined
9779

98-
await outputFixResult(ghsaFixesCResult, outputKind)
99-
return
80+
if (uploadCResult) {
81+
lastCResult = uploadCResult
82+
}
83+
84+
const tarHash = uploadCResult?.ok ? (uploadCResult as any).data.tarHash : ''
85+
86+
const idsOutputCResult = tarHash
87+
? await spawnCoana(
88+
[
89+
'compute-fixes-and-upgrade-purls',
90+
cwd,
91+
'--manifests-tar-hash',
92+
tarHash,
93+
],
94+
{ cwd, spinner, env: { SOCKET_ORG_SLUG: orgSlug } },
95+
)
96+
: undefined
97+
98+
if (idsOutputCResult) {
99+
lastCResult = idsOutputCResult
100100
}
101101

102-
spinner?.infoAndStop('No GHSA IDs found.')
102+
const idsOutput = idsOutputCResult?.ok
103+
? (idsOutputCResult.data as string)
104+
: ''
105+
106+
const ids = cmdFlagValueToArray(
107+
/(?<=Vulnerabilities found: )[^\n]+/.exec(idsOutput)?.[0],
108+
)
109+
110+
const fixCResult = ids.length
111+
? await spawnCoana(
112+
[
113+
'compute-fixes-and-upgrade-purls',
114+
cwd,
115+
'--manifests-tar-hash',
116+
tarHash,
117+
'--apply-fixes-to',
118+
...ids,
119+
...unknownFlags,
120+
],
121+
{ cwd, spinner, env: { SOCKET_ORG_SLUG: orgSlug } },
122+
)
123+
: undefined
124+
125+
if (fixCResult) {
126+
lastCResult = fixCResult
127+
}
128+
// const fixCResult = await spawnCoana(
129+
// [
130+
// cwd,
131+
// '--socket-mode',
132+
// DOT_SOCKET_DOT_FACTS_JSON,
133+
// '--manifests-tar-hash',
134+
// tarHash,
135+
// ...unknownFlags,
136+
// ],
137+
// { cwd, spinner, env: { SOCKET_ORG_SLUG: orgSlug } },
138+
// )
139+
debugDir('inspect', { lastCResult })
140+
141+
if (!lastCResult.ok) {
142+
await outputFixResult(lastCResult, outputKind)
143+
return
144+
}
103145

104146
await outputFixResult(
105147
{

src/commands/scan/cmd-scan-create.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ async function run(
330330
)
331331
}
332332

333-
if (updatedInput && orgSlug && targets?.length) {
333+
if (updatedInput && orgSlug && targets.length) {
334334
logger.info(
335335
'Note: You can invoke this command next time to skip the interactive questions:',
336336
)

src/utils/coana.mts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { spawn } from '@socketsecurity/registry/lib/spawn'
22

33
import constants from '../constants.mts'
44
import { getDefaultToken } from './sdk.mts'
5+
import { getDefaultOrgSlug } from '../commands/ci/fetch-default-org-slug.mts'
56

67
import type { CResult } from '../types.mts'
78
import type {
@@ -15,6 +16,9 @@ export async function spawnCoana(
1516
extra?: SpawnExtra | undefined,
1617
): Promise<CResult<unknown>> {
1718
const { env: spawnEnv } = { __proto__: null, ...options } as SpawnOptions
19+
const orgSlugCResult = await getDefaultOrgSlug()
20+
const SOCKET_CLI_API_TOKEN = getDefaultToken()
21+
const SOCKET_ORG_SLUG = orgSlugCResult.ok ? orgSlugCResult.data : undefined
1822
try {
1923
const output = await spawn(
2024
constants.execPath,
@@ -31,7 +35,9 @@ export async function spawnCoana(
3135
...process.env,
3236
// Lazily access constants.processEnv.
3337
...constants.processEnv,
34-
SOCKET_CLI_API_TOKEN: getDefaultToken(),
38+
RUN_WITHOUT_DOCKER: 'true',
39+
SOCKET_CLI_API_TOKEN,
40+
SOCKET_ORG_SLUG,
3541
...spawnEnv,
3642
},
3743
},

0 commit comments

Comments
 (0)