Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/commands/fix/coana-fix-dynamic-sbom-inference.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('socket fix --dynamic-sbom-inference', () => {
}
const uploadManifestFiles = vi.fn()
const generated = {
artifactsFlags: ['--maven-use-only-socket-facts'],
factsOnlyTypes: ['maven'],
paths: [FACTS],
sidecarFile: '/tmp/socket-fix-facts/sidecar.json',
remove: vi.fn(),
Expand Down Expand Up @@ -155,7 +155,7 @@ describe('socket fix --dynamic-sbom-inference', () => {
{ pathsRelativeTo: '/test/cwd' },
)
const args = coanaCalls('compute-fixes-and-upgrade-purls')[0]!
expect(args).toContain('--maven-use-only-socket-facts')
expect(args).toContain('--use-only-socket-facts-artifacts')
expect(args).toContain('--socket-facts-sidecar')
expect(args[args.indexOf('--socket-facts-sidecar') + 1]).toBe(
generated.sidecarFile,
Expand All @@ -177,7 +177,7 @@ describe('socket fix --dynamic-sbom-inference', () => {
await coanaFix({ ...baseConfig, all: true, ghsas: [] })

const args = coanaCalls('find-vulnerabilities')[0]
expect(args).toContain('--maven-use-only-socket-facts')
expect(args).toContain('--use-only-socket-facts-artifacts')
expect(args).not.toContain('--socket-facts-sidecar')
})

Expand Down Expand Up @@ -225,7 +225,7 @@ describe('socket fix --dynamic-sbom-inference', () => {

expect(mockGenerateSocketFactsForFix).not.toHaveBeenCalled()
expect(coanaCalls('compute-fixes-and-upgrade-purls')[0]).not.toContain(
'--maven-use-only-socket-facts',
'--use-only-socket-facts-artifacts',
)
})

Expand Down
5 changes: 4 additions & 1 deletion src/commands/fix/coana-fix.mts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { handleApiCall } from '../../utils/api.mts'
import { joinAnd } from '../../utils/arrays.mts'
import { spawnCoana } from '../../utils/coana/release.mts'
import { useOnlySocketFactsArtifactsFlags } from '../../utils/coana.mts'
import {
isReachabilityReportPath,
isSocketFactsFile,
Expand Down Expand Up @@ -417,7 +418,9 @@ async function coanaFixWithFacts(
const sidecarFile = factsSlot?.generated?.sidecarFile
// Discovery reads the artifacts resolved by the facts files.
// Applying a fix also reads the saved state for each build root.
const discoveryFlags = factsSlot?.generated?.artifactsFlags ?? []
const discoveryFlags = factsSlot?.generated
? useOnlySocketFactsArtifactsFlags(factsSlot.generated.factsOnlyTypes)
: []
const factsFlags = [
...discoveryFlags,
...(sidecarFile ? ['--socket-facts-sidecar', sidecarFile] : []),
Expand Down
31 changes: 15 additions & 16 deletions src/commands/fix/generated-socket-facts.mts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { copyFile, mkdir, rm, writeFile } from 'node:fs/promises'
import path from 'node:path'

import { useOnlySocketFactsArtifactsFlags } from '../../utils/coana.mts'
import {
EXCLUDE_PATHS_HINTS,
createSocketFactsWorkDir,
socketFactsSidecar,
} from '../manifest/run-manifest-facts.mts'
import { runDynamicSbomInference } from '../scan/run-dynamic-sbom-inference.mts'

import type { FactsOnlyTypes } from '../../utils/coana.mts'
import type { PURL_Type } from '../../utils/ecosystem.mts'
import type { SocketFactsContext } from '../manifest/run-manifest-facts.mts'

export type GeneratedSocketFacts = {
// Coana's flags selecting what the facts files resolve.
artifactsFlags: string[]
// Facts files of dropped runs, which Coana must not read.
discardedPaths: string[]
// The purl types whose artifacts Coana takes from the facts files alone.
factsOnlyTypes: FactsOnlyTypes
paths: string[]
// Records each build root's saved state for `--socket-facts-sidecar`.
// The file lives outside the repository.
Expand Down Expand Up @@ -70,26 +70,25 @@ export async function generateSocketFactsForFix({
)
await restoreAll(originals)
}
let artifactsFlags: string[]
let discardedPaths: string[]
let factsOnlyTypes: FactsOnlyTypes
let paths: string[]
const sidecarFile = path.join(tmpDir, 'sidecar.json')
const workDir = await createSocketFactsWorkDir(
path.join(tmpDir, 'work'),
'fix',
)
try {
const { discardedFactsPaths, factsOnlyTypes, factsPaths } =
await runDynamicSbomInference({
context: { ...context, workDir },
cwd,
excludePaths,
hints: EXCLUDE_PATHS_HINTS,
purlTypes,
})
artifactsFlags = useOnlySocketFactsArtifactsFlags(factsOnlyTypes)
discardedPaths = discardedFactsPaths
paths = factsPaths.map(p => path.resolve(cwd, p))
const inferred = await runDynamicSbomInference({
context: { ...context, workDir },
cwd,
excludePaths,
hints: EXCLUDE_PATHS_HINTS,
purlTypes,
})
discardedPaths = inferred.discardedFactsPaths
factsOnlyTypes = inferred.factsOnlyTypes
paths = inferred.factsPaths.map(p => path.resolve(cwd, p))
// Written even when empty, as for reach; applying fixes looks each facts
// file up in it, so an empty one is the same as none.
await writeFile(sidecarFile, JSON.stringify(socketFactsSidecar(workDir)))
Expand All @@ -104,8 +103,8 @@ export async function generateSocketFactsForFix({
...(await backUp(paths, path.join(tmpDir, 'generated'))),
]
return {
artifactsFlags,
discardedPaths,
factsOnlyTypes,
paths,
sidecarFile,
remove: () => reset(paths),
Expand Down
11 changes: 4 additions & 7 deletions src/commands/fix/generated-socket-facts.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('generateSocketFactsForFix', () => {
}
})

const { artifactsFlags, sidecarFile } = await generate()
const { factsOnlyTypes, sidecarFile } = await generate()

expect(workDirExisted).toBe(true)
expect(mockRunDynamicSbomInference).toHaveBeenCalledWith(
Expand All @@ -99,10 +99,7 @@ describe('generateSocketFactsForFix', () => {
purlTypes: ['maven'],
}),
)
expect(artifactsFlags).toEqual([
'--use-only-socket-facts-artifacts',
'maven,nuget',
])
expect(factsOnlyTypes).toEqual(['maven', 'nuget'])
expect(path.dirname(sidecarFile)).toBe(tmpDir)
expect(JSON.parse(await fs.readFile(sidecarFile, 'utf8'))).toEqual([entry])
})
Expand All @@ -114,9 +111,9 @@ describe('generateSocketFactsForFix', () => {
factsPaths: [],
})

const { artifactsFlags, sidecarFile } = await generate()
const { factsOnlyTypes, sidecarFile } = await generate()

expect(artifactsFlags).toEqual(['--maven-use-only-socket-facts'])
expect(factsOnlyTypes).toEqual(['maven'])
expect(JSON.parse(await fs.readFile(sidecarFile, 'utf8'))).toEqual([])
})

Expand Down
10 changes: 3 additions & 7 deletions src/commands/scan/perform-reachability-analysis.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,7 @@ async function coanaArgsFor(

function factsOnlyArgs(args: string[]): string[] {
return args.flatMap((arg, i) =>
arg === '--maven-use-only-socket-facts'
? [arg]
: arg === '--use-only-socket-facts-artifacts'
? [arg, args[i + 1]!]
: [],
arg === '--use-only-socket-facts-artifacts' ? [arg, args[i + 1]!] : [],
)
}

Expand Down Expand Up @@ -370,10 +366,10 @@ describe('performReachabilityAnalysis --use-only-socket-facts-artifacts gating',
expectedSidecar: sidecar,
},
{
name: 'keeps the Maven-only flag older Coana releases know, with an empty sidecar',
name: 'writes an empty sidecar when facts-only types come without entries',
factsOnlyTypes: ['maven'],
socketFactsSidecar: undefined,
expected: ['--maven-use-only-socket-facts'],
expected: ['--use-only-socket-facts-artifacts', 'maven'],
expectedSidecar: [],
},
{
Expand Down
6 changes: 1 addition & 5 deletions src/utils/coana.mts
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,8 @@ export function isFactsOnlyTypes(
return !!types.length
}

// Maven alone keeps the older flag, which Coana releases before 15.14.1 also
// know.
export function useOnlySocketFactsArtifactsFlags(
types: FactsOnlyTypes,
): string[] {
return types.length === 1 && types[0] === 'maven'
? ['--maven-use-only-socket-facts']
: ['--use-only-socket-facts-artifacts', [...types].sort().join(',')]
return ['--use-only-socket-facts-artifacts', [...types].sort().join(',')]
Comment thread
jfblaa marked this conversation as resolved.
Resolved
}
2 changes: 0 additions & 2 deletions src/utils/coana.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,6 @@ describe('coana facts-file utils', () => {

describe('useOnlySocketFactsArtifactsFlags', () => {
it.for([
// Maven alone keeps the flag Coana releases before 15.14.1 also know.
{ types: ['maven'], expected: ['--maven-use-only-socket-facts'] },
{
types: ['nuget', 'maven'],
expected: ['--use-only-socket-facts-artifacts', 'maven,nuget'],
Expand Down
Loading