-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from snyk/feat/use-best-practice-packages-route
feat: use best practice /packages route for sha1 hash search
- Loading branch information
Showing
6 changed files
with
159 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { PackageURL } from 'packageurl-js'; | ||
|
||
import { debugLog } from '.'; | ||
import type { GetPackageData, PomCoords, SnykHttpClient } from './types'; | ||
|
||
const PACKAGE_SEARCH_TYPE = 'maven'; | ||
const PACKAGE_SEARCH_VERSION = '2022-09-21~beta'; | ||
|
||
export async function getMavenPackageInfo( | ||
sha1: string, | ||
depCoords: Partial<PomCoords>, | ||
snykHttpClient: SnykHttpClient, | ||
): Promise<string> { | ||
const { res, body } = await snykHttpClient({ | ||
method: 'get', | ||
path: `/rest/packages`, | ||
qs: { | ||
version: PACKAGE_SEARCH_VERSION, | ||
/* eslint-disable @typescript-eslint/camelcase */ | ||
package_type: PACKAGE_SEARCH_TYPE, | ||
package_sha1: sha1, | ||
package_namespace: depCoords.groupId, | ||
package_name: depCoords.artifactId, | ||
package_version: depCoords.version, | ||
/* eslint-enable @typescript-eslint/camelcase */ | ||
}, | ||
}); | ||
|
||
if (res?.statusCode >= 400 || !body) { | ||
debugLog( | ||
`Failed to resolve ${JSON.stringify(depCoords)} using sha1 '${sha1}.`, | ||
); | ||
} | ||
|
||
let groupId: string; | ||
let artifactId: string; | ||
let version: string; | ||
try { | ||
const purlString = (body as GetPackageData)?.data[0]?.id; | ||
const pkg = PackageURL.fromString(purlString); | ||
const { namespace, name, version: pkgVersion } = pkg; | ||
groupId = namespace; | ||
artifactId = name; | ||
version = pkgVersion; | ||
} catch (_error) { | ||
debugLog( | ||
`Failed to parse purl components for ${JSON.stringify( | ||
depCoords, | ||
)} using sha1 '${sha1}.`, | ||
); | ||
} | ||
|
||
return `${groupId || 'unknown'}:${artifactId || 'unknown'}@${ | ||
version || 'unknown' | ||
}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type { NeedleResponse } from 'needle'; | ||
|
||
import { getMavenPackageInfo } from '../../lib/search'; | ||
|
||
describe('should get Maven package info', () => { | ||
it('returns maven coordinate string when purl string returned', async () => { | ||
const received = await getMavenPackageInfo( | ||
'ba55c13d7ac2fd44df9cc8074455719a33f375b9', // log4j-core | ||
{}, | ||
async () => ({ | ||
res: {} as NeedleResponse, | ||
body: { | ||
data: [ | ||
{ | ||
id: 'pkg:maven/org.apache.logging.log4j/log4j-core@2.15.0', | ||
type: 'package', | ||
}, | ||
], | ||
}, | ||
}), | ||
); | ||
expect(received).toBe('org.apache.logging.log4j:log4j-core@2.15.0'); | ||
}); | ||
|
||
it("should return 'unknown' values when partial purl string returned", async () => { | ||
const received = await getMavenPackageInfo( | ||
'ba55c13d7ac2fd44df9cc8074455719a33f375b9', // log4j-core | ||
{}, | ||
async () => ({ | ||
res: {} as NeedleResponse, | ||
body: { | ||
data: [ | ||
{ | ||
id: 'pkg:maven/log4j-core@2.15.0', | ||
type: 'package', | ||
}, | ||
], | ||
}, | ||
}), | ||
); | ||
expect(received).toBe('unknown:log4j-core@2.15.0'); | ||
}); | ||
|
||
it("should return all 'unknown' values when no/non purl string returned", async () => { | ||
const noPurlStringReceived = await getMavenPackageInfo( | ||
'ba55c13d7ac2fd44df9cc8074455719a33f375b9', // log4j-core | ||
{}, | ||
async () => ({ | ||
res: {} as NeedleResponse, | ||
body: { | ||
data: [], | ||
}, | ||
}), | ||
); | ||
expect(noPurlStringReceived).toBe('unknown:unknown@unknown'); | ||
|
||
const nonPurlStringReceived = await getMavenPackageInfo( | ||
'ba55c13d7ac2fd44df9cc8074455719a33f375b9', // log4j-core | ||
{}, | ||
async () => ({ | ||
res: {} as NeedleResponse, | ||
body: { | ||
data: [ | ||
{ | ||
id: 'corruptedId', | ||
type: 'package', | ||
}, | ||
], | ||
}, | ||
}), | ||
); | ||
expect(nonPurlStringReceived).toBe('unknown:unknown@unknown'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters