Repository navigation
refactor(service): optimize serverless app initialization and add cache #268
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 all commits
Commits
Show all changes
3 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
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
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,82 @@ | ||
| import { checkAuth } from '../auth'; | ||
| import { getProject } from '../../../framework/mongo/projects'; | ||
| import { verifyHash } from '../../../utils/hashUtils'; | ||
| import { CreateCommitRecordAuthType } from '../../../consts/commitRecords'; | ||
|
|
||
| import type { FastifyBaseLogger } from 'fastify'; | ||
|
|
||
| jest.mock('../../../framework/mongo/projects'); | ||
| jest.mock('../../../framework/github'); | ||
| jest.mock('../../../utils/hashUtils'); | ||
|
|
||
| const log = { warn: jest.fn(), info: jest.fn() } as unknown as FastifyBaseLogger; | ||
|
|
||
| // the cache is module level, each test uses its own project id | ||
| let testCounter = 0; | ||
|
|
||
| const mockProject = (hash = 'salt:hash') => { | ||
| const id = `project-${++testCounter}`; | ||
|
|
||
| jest.mocked(getProject).mockResolvedValue({ id, apiKey: { hash } } as any); | ||
|
|
||
| return id; | ||
| }; | ||
|
|
||
| const auth = (projectId: string, token: string) => | ||
| checkAuth(projectId, { authType: CreateCommitRecordAuthType.ProjectApiKey, token }, undefined, log); | ||
|
|
||
| describe('checkAuth - API key', () => { | ||
| beforeEach(() => { | ||
| jest.resetAllMocks(); | ||
| }); | ||
|
|
||
| test('same key twice runs verifyHash once', async () => { | ||
| const projectId = mockProject(); | ||
| jest.mocked(verifyHash).mockResolvedValue(true); | ||
|
|
||
| expect(await auth(projectId, 'key')).toEqual({ authenticated: true }); | ||
| expect(await auth(projectId, 'key')).toEqual({ authenticated: true }); | ||
|
|
||
| expect(verifyHash).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('wrong key is never cached', async () => { | ||
| const projectId = mockProject(); | ||
| jest.mocked(verifyHash).mockResolvedValue(false); | ||
|
|
||
| expect(await auth(projectId, 'wrong-key')).toEqual({ authenticated: false, error: 'forbidden' }); | ||
| expect(await auth(projectId, 'wrong-key')).toEqual({ authenticated: false, error: 'forbidden' }); | ||
|
|
||
| expect(verifyHash).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| test('a cached key does not authenticate a different key', async () => { | ||
| const projectId = mockProject(); | ||
| jest.mocked(verifyHash).mockResolvedValueOnce(true).mockResolvedValueOnce(false); | ||
|
|
||
| expect(await auth(projectId, 'key')).toEqual({ authenticated: true }); | ||
| expect(await auth(projectId, 'other-key')).toEqual({ authenticated: false, error: 'forbidden' }); | ||
| }); | ||
|
|
||
| test('a cached key does not authenticate another project', async () => { | ||
| const projectId1 = mockProject(); | ||
| jest.mocked(verifyHash).mockResolvedValueOnce(true).mockResolvedValueOnce(false); | ||
|
|
||
| expect(await auth(projectId1, 'key')).toEqual({ authenticated: true }); | ||
|
|
||
| const projectId2 = mockProject(); | ||
| expect(await auth(projectId2, 'key')).toEqual({ authenticated: false, error: 'forbidden' }); | ||
| }); | ||
|
|
||
| test('rotated API key (new stored hash) invalidates the cached key', async () => { | ||
| const projectId = mockProject('salt:old-hash'); | ||
| jest.mocked(verifyHash).mockResolvedValueOnce(true).mockResolvedValueOnce(false); | ||
|
|
||
| expect(await auth(projectId, 'old-key')).toEqual({ authenticated: true }); | ||
|
|
||
| jest.mocked(getProject).mockResolvedValue({ id: projectId, apiKey: { hash: 'salt:new-hash' } } as any); | ||
|
|
||
| expect(await auth(projectId, 'old-key')).toEqual({ authenticated: false, error: 'forbidden' }); | ||
| expect(verifyHash).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| import { createOctokitClientByAction, createOctokitClientByRepo, getInstallationId } from '../github'; | ||
|
|
||
| import type { FastifyBaseLogger } from 'fastify'; | ||
|
|
||
| const mockGetRepoInstallation = jest.fn(); | ||
| const mockGetWorkflowRun = jest.fn(); | ||
| let mockOctokitCreatedCount = 0; | ||
|
|
||
| jest.mock('../env', () => ({ | ||
| githubAppId: '1', | ||
| githubAppPrivateKey: 'private-key', | ||
| githubAppClientId: 'client-id', | ||
| githubAppClientSecret: 'client-secret', | ||
| })); | ||
| jest.mock('../mongo/commitRecords', () => ({})); | ||
| jest.mock('@octokit/auth-app', () => ({ createAppAuth: jest.fn(), createOAuthUserAuth: jest.fn() })); | ||
| jest.mock('@octokit/rest', () => ({ | ||
| Octokit: class { | ||
| id = ++mockOctokitCreatedCount; | ||
| apps = { getRepoInstallation: mockGetRepoInstallation }; | ||
| actions = { getWorkflowRun: mockGetWorkflowRun }; | ||
| }, | ||
| })); | ||
|
|
||
| const log = { info: jest.fn(), warn: jest.fn() } as unknown as FastifyBaseLogger; | ||
|
|
||
| // the caches are module level, each test uses its own repo / run id | ||
| let testCounter = 0; | ||
| const uniqueRepo = () => ({ owner: 'owner', repo: `repo-${++testCounter}` }); | ||
|
|
||
| const flushPromises = () => new Promise((resolve) => setImmediate(resolve)); | ||
|
|
||
| describe('github caches', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('getInstallationId', () => { | ||
| test('parallel calls trigger one fetch', async () => { | ||
| const { owner, repo } = uniqueRepo(); | ||
| mockGetRepoInstallation.mockResolvedValue({ data: { id: 123 } }); | ||
|
|
||
| const results = await Promise.all([ | ||
| getInstallationId(owner, repo), | ||
| getInstallationId(owner, repo), | ||
| getInstallationId(owner, repo), | ||
| ]); | ||
|
|
||
| expect(results).toEqual([123, 123, 123]); | ||
| expect(mockGetRepoInstallation).toHaveBeenCalledTimes(1); | ||
|
|
||
| // cached for later calls | ||
| expect(await getInstallationId(owner, repo)).toBe(123); | ||
| expect(mockGetRepoInstallation).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('cache key is case insensitive', async () => { | ||
| const { owner, repo } = uniqueRepo(); | ||
| mockGetRepoInstallation.mockResolvedValue({ data: { id: 123 } }); | ||
|
|
||
| await getInstallationId(owner, repo); | ||
| await getInstallationId(owner.toUpperCase(), repo.toUpperCase()); | ||
|
|
||
| expect(mockGetRepoInstallation).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('undefined (app not installed) is not cached', async () => { | ||
| const { owner, repo } = uniqueRepo(); | ||
| mockGetRepoInstallation.mockRejectedValueOnce({ status: 404 }); | ||
|
|
||
| expect(await getInstallationId(owner, repo)).toBeUndefined(); | ||
|
|
||
| mockGetRepoInstallation.mockResolvedValueOnce({ data: { id: 456 } }); | ||
|
|
||
| expect(await getInstallationId(owner, repo)).toBe(456); | ||
| expect(mockGetRepoInstallation).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| test('errors are not cached', async () => { | ||
| const { owner, repo } = uniqueRepo(); | ||
| mockGetRepoInstallation.mockRejectedValueOnce({ status: 500 }); | ||
|
|
||
| await expect(getInstallationId(owner, repo)).rejects.toEqual({ status: 500 }); | ||
|
|
||
| mockGetRepoInstallation.mockResolvedValueOnce({ data: { id: 789 } }); | ||
|
|
||
| expect(await getInstallationId(owner, repo)).toBe(789); | ||
| expect(mockGetRepoInstallation).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| test('cleanup of an expired entry does not delete a newer entry', async () => { | ||
| jest.useFakeTimers({ doNotFake: ['setImmediate', 'nextTick'] }); | ||
|
|
||
| try { | ||
| const { owner, repo } = uniqueRepo(); | ||
|
|
||
| let rejectFirst: (err: unknown) => void = () => {}; | ||
| mockGetRepoInstallation.mockReturnValueOnce(new Promise((_, reject) => (rejectFirst = reject))); | ||
|
|
||
| const first = getInstallationId(owner, repo).catch(() => undefined); | ||
|
|
||
| // the first entry expires while still in flight, a newer one replaces it | ||
| jest.advanceTimersByTime(60 * 60 * 1000 + 1); | ||
| mockGetRepoInstallation.mockResolvedValueOnce({ data: { id: 1000 } }); | ||
| expect(await getInstallationId(owner, repo)).toBe(1000); | ||
|
|
||
| rejectFirst({ status: 500 }); | ||
| await first; | ||
| await flushPromises(); | ||
|
|
||
| // the newer entry is still cached | ||
| expect(await getInstallationId(owner, repo)).toBe(1000); | ||
| expect(mockGetRepoInstallation).toHaveBeenCalledTimes(2); | ||
| } finally { | ||
| jest.useRealTimers(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe('createOctokitClientByRepo', () => { | ||
| test('reuses the client of the same installation', async () => { | ||
| const first = uniqueRepo(); | ||
| const second = uniqueRepo(); | ||
| mockGetRepoInstallation.mockResolvedValue({ data: { id: 2000 + testCounter } }); | ||
|
|
||
| const client1 = await createOctokitClientByRepo(first.owner, first.repo); | ||
| const client2 = await createOctokitClientByRepo(first.owner, first.repo); | ||
|
|
||
| expect(client1).toBe(client2); | ||
|
|
||
| // another repo with a different installation gets its own client | ||
| mockGetRepoInstallation.mockResolvedValue({ data: { id: 3000 + testCounter } }); | ||
| expect(await createOctokitClientByRepo(second.owner, second.repo)).not.toBe(client1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('createOctokitClientByAction', () => { | ||
| test('successful authentication is cached, parallel calls trigger one request', async () => { | ||
| const { owner, repo } = uniqueRepo(); | ||
| mockGetRepoInstallation.mockResolvedValue({ data: { id: 4000 + testCounter } }); | ||
| mockGetWorkflowRun.mockResolvedValue({ data: { status: 'in_progress' } }); | ||
|
|
||
| const params = { owner, repo, runId: '1' }; | ||
| const results = await Promise.all([ | ||
| createOctokitClientByAction(params, log), | ||
| createOctokitClientByAction(params, log), | ||
| ]); | ||
| const another = await createOctokitClientByAction(params, log); | ||
|
|
||
| expect(results[0].authenticated).toBe(true); | ||
| expect(results[1]).toBe(results[0]); | ||
| expect(another).toBe(results[0]); | ||
| expect(mockGetWorkflowRun).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('failed authentication is not cached', async () => { | ||
| const { owner, repo } = uniqueRepo(); | ||
| mockGetRepoInstallation.mockResolvedValue({ data: { id: 5000 + testCounter } }); | ||
| mockGetWorkflowRun.mockResolvedValueOnce({ data: { status: 'completed' } }); | ||
|
|
||
| const params = { owner, repo, runId: '2' }; | ||
|
|
||
| expect((await createOctokitClientByAction(params, log)).authenticated).toBe(false); | ||
|
|
||
| mockGetWorkflowRun.mockResolvedValueOnce({ data: { status: 'in_progress' } }); | ||
|
|
||
| expect((await createOctokitClientByAction(params, log)).authenticated).toBe(true); | ||
| expect(mockGetWorkflowRun).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| test('stale installation id is evicted on error', async () => { | ||
| const { owner, repo } = uniqueRepo(); | ||
| const staleInstallationId = 6000 + testCounter; | ||
|
|
||
| mockGetRepoInstallation.mockResolvedValueOnce({ data: { id: staleInstallationId } }); | ||
| mockGetWorkflowRun.mockRejectedValueOnce({ status: 404 }); | ||
|
|
||
| const result = await createOctokitClientByAction({ owner, repo, runId: '3' }, log); | ||
|
|
||
| expect(result).toMatchObject({ authenticated: false }); | ||
|
|
||
| // the app was reinstalled, the next request looks up the new installation id | ||
| mockGetRepoInstallation.mockResolvedValueOnce({ data: { id: staleInstallationId + 1 } }); | ||
| mockGetWorkflowRun.mockResolvedValueOnce({ data: { status: 'queued' } }); | ||
|
|
||
| const retry = await createOctokitClientByAction({ owner, repo, runId: '4' }, log); | ||
|
|
||
| expect(retry.authenticated).toBe(true); | ||
| expect(mockGetRepoInstallation).toHaveBeenCalledTimes(2); | ||
| expect(await getInstallationId(owner, repo)).toBe(staleInstallationId + 1); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.