-
Notifications
You must be signed in to change notification settings - Fork 731
feat: add refresh security contats api (CM-1348) #4393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
119 changes: 119 additions & 0 deletions
119
backend/src/api/public/v1/packages/ingestAkritesExternalContactDetail.test.ts
This file contains hidden or 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,119 @@ | ||
| import type { Request, Response } from 'express' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import type { AkritesExternalContactDetailRow } from '@crowd/data-access-layer' | ||
| import { WorkflowIdConflictPolicy, WorkflowIdReusePolicy } from '@crowd/temporal' | ||
|
|
||
| import { ingestAkritesExternalContactDetail } from './ingestAkritesExternalContactDetail' | ||
|
|
||
| const { execute, getContactDetailsByPurls } = vi.hoisted(() => ({ | ||
| execute: vi.fn(), | ||
| getContactDetailsByPurls: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/db/packagesTemporal', () => ({ | ||
| getPackagesTemporalClient: vi.fn().mockResolvedValue({ workflow: { execute } }), | ||
| })) | ||
|
|
||
| vi.mock('@/db/packagesDb', () => ({ | ||
| getPackagesQx: vi.fn().mockResolvedValue({}), | ||
| })) | ||
|
|
||
| vi.mock('@crowd/data-access-layer', () => ({ | ||
| getContactDetailsByPurls, | ||
| })) | ||
|
|
||
| function baseRow( | ||
| overrides: Partial<AkritesExternalContactDetailRow> = {}, | ||
| ): AkritesExternalContactDetailRow { | ||
| return { | ||
| purl: 'pkg:npm/lodash', | ||
| name: 'lodash', | ||
| ecosystem: 'npm', | ||
| securityPolicyUrl: null, | ||
| vulnerabilityReportingUrl: null, | ||
| bugBountyUrl: null, | ||
| pvrEnabled: null, | ||
| declaredRepositoryUrl: null, | ||
| resolvedRepositoryUrl: null, | ||
| repoMappingConfidence: null, | ||
| securityContacts: [], | ||
| ...overrides, | ||
| } | ||
| } | ||
|
|
||
| function mockReqRes(body: unknown) { | ||
| execute.mockClear() | ||
| getContactDetailsByPurls.mockClear() | ||
|
|
||
| const req = { body } as unknown as Request | ||
|
|
||
| const json = vi.fn() | ||
| const status = vi.fn().mockReturnValue({ json }) | ||
| const res = { status, json } as unknown as Response | ||
|
|
||
| return { req, res, status, json } | ||
| } | ||
|
|
||
| describe('ingestAkritesExternalContactDetail', () => { | ||
| it('executes ingestSecurityContactsForPurlWorkflow and returns the re-read contact detail', async () => { | ||
| execute.mockResolvedValue({ found: true, repoId: 'repo-1' }) | ||
| getContactDetailsByPurls.mockResolvedValue([baseRow()]) | ||
|
|
||
| const { req, res, json } = mockReqRes({ purl: 'pkg:npm/lodash' }) | ||
|
|
||
| await ingestAkritesExternalContactDetail(req, res) | ||
|
|
||
| expect(execute).toHaveBeenCalledTimes(1) | ||
| const [workflowType, options] = execute.mock.calls[0] | ||
| expect(workflowType).toBe('ingestSecurityContactsForPurlWorkflow') | ||
| expect(options.taskQueue).toBe('security-contacts-worker') | ||
| expect(options.workflowId).toMatch(/^security-contacts-ondemand:[0-9a-f]{64}$/) | ||
| expect(options.workflowIdConflictPolicy).toBe(WorkflowIdConflictPolicy.USE_EXISTING) | ||
| expect(options.workflowIdReusePolicy).toBe(WorkflowIdReusePolicy.ALLOW_DUPLICATE) | ||
| expect(options.args).toEqual(['pkg:npm/lodash']) | ||
|
|
||
| expect(getContactDetailsByPurls).toHaveBeenCalledWith(expect.anything(), ['pkg:npm/lodash']) | ||
| expect(json).toHaveBeenCalledWith(expect.objectContaining({ purl: 'pkg:npm/lodash' })) | ||
| }) | ||
|
|
||
| it('derives the same deterministic workflowId for the same purl', async () => { | ||
| execute.mockResolvedValue({ found: true }) | ||
| getContactDetailsByPurls.mockResolvedValue([baseRow()]) | ||
|
|
||
| const { req: req1, res: res1 } = mockReqRes({ purl: 'pkg:npm/lodash' }) | ||
| await ingestAkritesExternalContactDetail(req1, res1) | ||
| const id1 = execute.mock.calls[0][1].workflowId | ||
|
|
||
| const { req: req2, res: res2 } = mockReqRes({ purl: 'pkg:npm/lodash' }) | ||
| await ingestAkritesExternalContactDetail(req2, res2) | ||
| const id2 = execute.mock.calls[0][1].workflowId | ||
|
|
||
| expect(id1).toBe(id2) | ||
| }) | ||
|
|
||
| it('throws NotFoundError when the workflow reports no linked repo', async () => { | ||
| execute.mockResolvedValue({ found: false }) | ||
|
|
||
| const { req, res } = mockReqRes({ purl: 'pkg:npm/left-pad' }) | ||
|
|
||
| await expect(ingestAkritesExternalContactDetail(req, res)).rejects.toThrow() | ||
| expect(getContactDetailsByPurls).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('throws NotFoundError when the re-read finds no row', async () => { | ||
| execute.mockResolvedValue({ found: true }) | ||
| getContactDetailsByPurls.mockResolvedValue([]) | ||
|
|
||
| const { req, res } = mockReqRes({ purl: 'pkg:npm/lodash' }) | ||
|
|
||
| await expect(ingestAkritesExternalContactDetail(req, res)).rejects.toThrow() | ||
| }) | ||
|
|
||
| it('rejects a request missing purl without executing a workflow', async () => { | ||
| const { req, res } = mockReqRes({}) | ||
|
|
||
| await expect(ingestAkritesExternalContactDetail(req, res)).rejects.toThrow() | ||
| expect(execute).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
65 changes: 65 additions & 0 deletions
65
backend/src/api/public/v1/packages/ingestAkritesExternalContactDetail.ts
This file contains hidden or 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,65 @@ | ||
| import { createHash } from 'crypto' | ||
| import type { Request, Response } from 'express' | ||
|
|
||
| import { NotFoundError } from '@crowd/common' | ||
| import { getContactDetailsByPurls } from '@crowd/data-access-layer' | ||
| import { WorkflowIdConflictPolicy, WorkflowIdReusePolicy } from '@crowd/temporal' | ||
| import { TemporalWorkflowId } from '@crowd/types' | ||
|
|
||
| import { getPackagesQx } from '@/db/packagesDb' | ||
| import { getPackagesTemporalClient } from '@/db/packagesTemporal' | ||
| import { ok } from '@/utils/api' | ||
| import { validateOrThrow } from '@/utils/validation' | ||
|
|
||
| import { toAkritesExternalContactDetail } from './akritesExternalContactDetail' | ||
| import { purlBodySchema } from './purl' | ||
|
|
||
| interface IngestSecurityContactsForPurlResult { | ||
| found: boolean | ||
| repoId?: string | ||
| } | ||
|
|
||
| // Deterministic, purl-derived workflowId: concurrent callers hitting the same purl attach | ||
| // to the same running workflow (USE_EXISTING) instead of each starting their own ingest — | ||
| // same pattern as integrationService.ts's github-nango-sync workflow start. | ||
| function ingestWorkflowId(purl: string): string { | ||
| return `${TemporalWorkflowId.SECURITY_CONTACTS_ONDEMAND}:${createHash('sha256').update(purl).digest('hex')}` | ||
| } | ||
|
|
||
| // Sync, single-purl on-demand ingest. Blocks ~10-20s (the worker's single-repo bound — | ||
| // see security-contacts/workflows.ts's singleActs timeout) — no batch variant, since | ||
| // fanning this out over many purls would multiply concurrent Temporal workflow starts. | ||
|
ulemons marked this conversation as resolved.
Outdated
|
||
| // | ||
| // Note: the workflow only ingests contacts it has never seen before — it does not | ||
| // refresh/update contacts already ingested for a purl. That's why this endpoint is | ||
| // named "ingest", not "refresh". | ||
|
ulemons marked this conversation as resolved.
Outdated
|
||
| export async function ingestAkritesExternalContactDetail( | ||
| req: Request, | ||
| res: Response, | ||
| ): Promise<void> { | ||
| const { purl } = validateOrThrow(purlBodySchema, req.body) | ||
|
|
||
| const packagesTemporal = await getPackagesTemporalClient() | ||
| const result = await packagesTemporal.workflow.execute< | ||
| (purl: string) => Promise<IngestSecurityContactsForPurlResult> | ||
| >('ingestSecurityContactsForPurlWorkflow', { | ||
| taskQueue: 'security-contacts-worker', | ||
| workflowId: ingestWorkflowId(purl), | ||
| workflowIdConflictPolicy: WorkflowIdConflictPolicy.USE_EXISTING, | ||
| workflowIdReusePolicy: WorkflowIdReusePolicy.ALLOW_DUPLICATE, | ||
|
ulemons marked this conversation as resolved.
|
||
| args: [purl], | ||
|
ulemons marked this conversation as resolved.
|
||
| }) | ||
|
ulemons marked this conversation as resolved.
|
||
|
|
||
| if (!result.found) { | ||
| throw new NotFoundError('Purl has no linked repository to ingest security contacts from') | ||
| } | ||
|
|
||
| const qx = await getPackagesQx() | ||
| const [row] = await getContactDetailsByPurls(qx, [purl]) | ||
|
|
||
| if (!row) { | ||
| throw new NotFoundError('Contact detail not found after ingest') | ||
| } | ||
|
|
||
| ok(res, toAkritesExternalContactDetail(row)) | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.