Repository navigation
fix(github): /status reports PAT liveness, not just presence #2959
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
d506173
fix(github): /status reports PAT liveness, not just presence
lilyshen0722 d1e8bb4
test(github): stub the PAT liveness probe in the browser-JWT route test
lilyshen0722 f0c24f5
test(github): pin the admin gate on /status and the probe timeout
lilyshen0722 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // GET /api/github/status answers "is the GitHub credential working?", not "is | ||
| // one set?". A dead PAT is a successful diagnosis: 200 with credentialLive | ||
| // false, never a 5xx that looks like our fault. | ||
|
|
||
| jest.mock('../../../middleware/agentRuntimeAuth', () => jest.fn((req, res, next) => next())); | ||
|
|
||
| // The role is switched per test, so the non-admin path can be exercised. | ||
| let mockRole = 'admin'; | ||
| jest.mock('../../../middleware/auth', () => jest.fn((req, res, next) => { | ||
| req.user = { _id: 'user-1', role: mockRole }; | ||
| req.userId = 'user-1'; | ||
| next(); | ||
| })); | ||
|
|
||
| jest.mock('../../../services/githubAppService', () => ({ | ||
| isConfigured: jest.fn(), | ||
| isPatConfigured: jest.fn(), | ||
| checkPatLiveness: jest.fn(), | ||
| generateAppJWT: jest.fn(), | ||
| })); | ||
|
|
||
| const express = require('express'); | ||
| const request = require('supertest'); | ||
| // eslint-disable-next-line import/no-unresolved, import/extensions | ||
| const GitHubAppService = require('../../../services/githubAppService'); | ||
| // eslint-disable-next-line import/no-unresolved, import/extensions | ||
| const router = require('../../../routes/github'); | ||
|
|
||
| const app = express(); | ||
| app.use(express.json()); | ||
| app.use('/api/github', router); | ||
|
|
||
| describe('GET /api/github/status', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mockRole = 'admin'; | ||
| GitHubAppService.isConfigured.mockReturnValue(false); | ||
| GitHubAppService.isPatConfigured.mockReturnValue(true); | ||
| }); | ||
|
|
||
| it('reports a PAT the upstream accepts as live', async () => { | ||
| GitHubAppService.checkPatLiveness.mockResolvedValue({ live: true, status: 'accepted', upstreamStatus: 200 }); | ||
| const res = await request(app).get('/api/github/status'); | ||
| expect(res.status).toBe(200); | ||
| expect(res.body).toEqual({ | ||
| mode: 'pat', configured: true, credentialLive: true, credentialStatus: 'accepted', upstreamStatus: 200, | ||
| }); | ||
| }); | ||
|
|
||
| it('reports a set but refused PAT as dead, with a 200 and no 5xx', async () => { | ||
| GitHubAppService.checkPatLiveness.mockResolvedValue({ live: false, status: 'rejected', upstreamStatus: 401 }); | ||
| const res = await request(app).get('/api/github/status'); | ||
| expect(res.status).toBe(200); | ||
| expect(res.body.configured).toBe(true); | ||
| expect(res.body.credentialLive).toBe(false); | ||
| expect(res.body.credentialStatus).toBe('rejected'); | ||
| expect(res.body.upstreamStatus).toBe(401); | ||
| }); | ||
|
|
||
| it('reports unknown, not dead, when GitHub cannot be reached', async () => { | ||
| GitHubAppService.checkPatLiveness.mockResolvedValue({ live: null, status: 'unreachable', upstreamStatus: 503 }); | ||
| const res = await request(app).get('/api/github/status'); | ||
| expect(res.status).toBe(200); | ||
| expect(res.body.credentialLive).toBeNull(); | ||
| expect(res.body.credentialStatus).toBe('unreachable'); | ||
| }); | ||
|
|
||
| it('refuses a non-admin before any upstream probe runs', async () => { | ||
| mockRole = 'user'; | ||
| GitHubAppService.checkPatLiveness.mockResolvedValue({ live: true, status: 'accepted', upstreamStatus: 200 }); | ||
| const res = await request(app).get('/api/github/status'); | ||
| expect(res.status).toBe(403); | ||
| expect(res.body).toEqual({ message: 'Admin only' }); | ||
| expect(GitHubAppService.checkPatLiveness).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('keeps the mode none answer when nothing is configured, with no probe', async () => { | ||
| GitHubAppService.isPatConfigured.mockReturnValue(false); | ||
| const res = await request(app).get('/api/github/status'); | ||
| expect(res.body).toEqual({ mode: 'none', configured: false, message: 'Set GitHub App env vars' }); | ||
| expect(GitHubAppService.checkPatLiveness).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
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
73 changes: 73 additions & 0 deletions
73
backend/__tests__/unit/services/githubAppService.patLiveness.test.js
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,73 @@ | ||
| // PAT liveness: presence (GITHUB_PAT is set) is not the same as the credential | ||
| // working. checkPatLiveness asks GitHub, and only a 401 counts as a rejection. | ||
| // Anything else is unknown (live: null), so a flaky upstream never reads as a | ||
| // dead credential. | ||
| const axios = require('axios'); | ||
| const GitHubAppService = require('../../../services/githubAppService'); | ||
|
|
||
| describe('GitHubAppService.checkPatLiveness', () => { | ||
| const savedPat = process.env.GITHUB_PAT; | ||
| let get; | ||
|
|
||
| beforeEach(() => { | ||
| process.env.GITHUB_PAT = 'ghp_test_token'; | ||
| get = jest.spyOn(axios, 'get'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (savedPat === undefined) delete process.env.GITHUB_PAT; else process.env.GITHUB_PAT = savedPat; | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('reports absent without any network call when no PAT is set', async () => { | ||
| delete process.env.GITHUB_PAT; | ||
| await expect(GitHubAppService.checkPatLiveness()).resolves.toEqual({ live: false, status: 'absent' }); | ||
| expect(get).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('reports accepted when GitHub answers 200 to the rate-limit probe', async () => { | ||
| get.mockResolvedValue({ status: 200, data: {} }); | ||
| await expect(GitHubAppService.checkPatLiveness()).resolves.toEqual({ | ||
| live: true, status: 'accepted', upstreamStatus: 200, | ||
| }); | ||
| expect(get).toHaveBeenCalledWith( | ||
| 'https://api.github.com/rate_limit', | ||
| expect.objectContaining({ headers: expect.objectContaining({ Authorization: 'Bearer ghp_test_token' }) }), | ||
| ); | ||
| }); | ||
|
|
||
| it('bounds the probe with a 5 second timeout so a hung GitHub cannot hold /status', async () => { | ||
| get.mockResolvedValue({ status: 200, data: {} }); | ||
| await GitHubAppService.checkPatLiveness(); | ||
| expect(get.mock.calls[0][1]).toEqual(expect.objectContaining({ timeout: 5000 })); | ||
| }); | ||
|
|
||
| it('reports rejected on a 401, which is the outage signature', async () => { | ||
| get.mockRejectedValue({ message: 'Request failed with status code 401', response: { status: 401 } }); | ||
| await expect(GitHubAppService.checkPatLiveness()).resolves.toEqual({ | ||
| live: false, status: 'rejected', upstreamStatus: 401, | ||
| }); | ||
| }); | ||
|
|
||
| it('reports unknown, not dead, on a 5xx', async () => { | ||
| get.mockRejectedValue({ message: 'Request failed with status code 503', response: { status: 503 } }); | ||
| const result = await GitHubAppService.checkPatLiveness(); | ||
| expect(result.live).toBeNull(); | ||
| expect(result.status).toBe('unreachable'); | ||
| expect(result.upstreamStatus).toBe(503); | ||
| }); | ||
|
|
||
| it('reports unknown, not dead, on a 403 (never guess the confident direction)', async () => { | ||
| get.mockRejectedValue({ message: 'Request failed with status code 403', response: { status: 403 } }); | ||
| const result = await GitHubAppService.checkPatLiveness(); | ||
| expect(result.live).toBeNull(); | ||
| expect(result.status).toBe('unreachable'); | ||
| }); | ||
|
|
||
| it('reports unknown on a network failure with no response at all', async () => { | ||
| get.mockRejectedValue({ message: 'socket hang up' }); | ||
| await expect(GitHubAppService.checkPatLiveness()).resolves.toEqual({ | ||
| live: null, status: 'unreachable', upstreamStatus: undefined, detail: 'socket hang up', | ||
| }); | ||
| }); | ||
| }); |
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.