-
Notifications
You must be signed in to change notification settings - Fork 20
fix: enhance oidc logs #3787
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
fix: enhance oidc logs #3787
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| jest.mock('../../../store', () => ({ | ||
| backend: undefined, | ||
| clusterName: undefined, | ||
| })); | ||
|
|
||
| import {isRedirectToAuth} from '../../../utils/response'; | ||
| import {handleBaseApiResponseError, recoverXhrResponseFromNetworkError} from '../base'; | ||
| import * as needResetModule from '../utils/needReset'; | ||
|
|
||
| interface XhrRequestMockParams { | ||
| readyState?: number; | ||
| status?: number; | ||
| statusText?: string; | ||
| responseText?: string; | ||
| responseURL?: string; | ||
| rawHeaders?: string; | ||
| } | ||
|
|
||
| interface NetworkErrorMock { | ||
| name: string; | ||
| message: string; | ||
| code: string; | ||
| config: { | ||
| url: string; | ||
| method: string; | ||
| }; | ||
| request?: unknown; | ||
| response?: unknown; | ||
| status?: number; | ||
| } | ||
|
|
||
| function createXhrRequestMock({ | ||
| readyState = 4, | ||
| status = 504, | ||
| statusText = 'Deadline Exceeded', | ||
| responseText = '', | ||
| responseURL = 'https://oidc-proxy-preprod.example.net/viewer/json/whoami?database=%2Fdb', | ||
| rawHeaders = '', | ||
| }: XhrRequestMockParams = {}) { | ||
| return { | ||
| readyState, | ||
| status, | ||
| statusText, | ||
| responseText, | ||
| responseURL, | ||
| getAllResponseHeaders: () => rawHeaders, | ||
| }; | ||
| } | ||
|
|
||
| function createNetworkError(request?: unknown): NetworkErrorMock { | ||
| return { | ||
| name: 'AxiosError', | ||
| message: 'Network Error', | ||
| code: 'ERR_NETWORK', | ||
| config: { | ||
| url: '/viewer/json/whoami?database=%2Fdb', | ||
| method: 'get', | ||
| }, | ||
| request, | ||
| }; | ||
| } | ||
|
|
||
| describe('recoverXhrResponseFromNetworkError', () => { | ||
| test('restores HTTP response details from recoverable XHR network error', () => { | ||
| const error = createNetworkError( | ||
| createXhrRequestMock({ | ||
| rawHeaders: [ | ||
| 'Content-Type: text/plain; charset=utf-8', | ||
| 'X-Worker-Name: oidc-proxy-1-vm-preprod.example.net', | ||
| 'X-Trace-Id: trace-id-504', | ||
| ].join('\r\n'), | ||
| }), | ||
| ); | ||
|
|
||
| const response = recoverXhrResponseFromNetworkError(error); | ||
|
|
||
| expect(response).toEqual( | ||
| expect.objectContaining({ | ||
| status: 504, | ||
| statusText: 'Deadline Exceeded', | ||
| data: '', | ||
| headers: { | ||
| 'content-type': 'text/plain; charset=utf-8', | ||
| 'x-worker-name': 'oidc-proxy-1-vm-preprod.example.net', | ||
| 'x-trace-id': 'trace-id-504', | ||
| }, | ||
| config: expect.objectContaining({ | ||
| url: 'https://oidc-proxy-preprod.example.net/viewer/json/whoami?database=%2Fdb', | ||
| method: 'get', | ||
| }), | ||
| code: 'ERR_NETWORK', | ||
| message: 'Network Error', | ||
| }), | ||
| ); | ||
| expect(error.response).toBe(response); | ||
| expect(error.status).toBe(504); | ||
| }); | ||
|
|
||
| test('does not treat array-like errors as recoverable records', () => { | ||
| const error = Object.assign([], createNetworkError(createXhrRequestMock())); | ||
|
|
||
| const response = recoverXhrResponseFromNetworkError(error); | ||
|
|
||
| expect(response).toBeUndefined(); | ||
| expect(error.response).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('restores recovered auth response in shape compatible with redirect-to-auth checks', () => { | ||
| const error = createNetworkError( | ||
| createXhrRequestMock({ | ||
| status: 401, | ||
| statusText: 'Unauthorized', | ||
| responseText: JSON.stringify({authUrl: 'https://auth.example.com/login'}), | ||
| rawHeaders: 'Content-Type: application/json', | ||
| }), | ||
| ); | ||
|
|
||
| const response = recoverXhrResponseFromNetworkError(error); | ||
|
|
||
| expect(response).toEqual( | ||
| expect.objectContaining({ | ||
| status: 401, | ||
| data: {authUrl: 'https://auth.example.com/login'}, | ||
| }), | ||
| ); | ||
| expect(isRedirectToAuth(response)).toBe(true); | ||
| }); | ||
|
|
||
| test.each([ | ||
| { | ||
| title: 'status is zero', | ||
| request: createXhrRequestMock({status: 0}), | ||
| }, | ||
| { | ||
| title: 'readyState is not done', | ||
| request: createXhrRequestMock({readyState: 3}), | ||
| }, | ||
| { | ||
| title: 'request does not expose headers reader', | ||
| request: { | ||
| readyState: 4, | ||
| status: 504, | ||
| statusText: 'Deadline Exceeded', | ||
| }, | ||
| }, | ||
| { | ||
| title: 'request is missing entirely', | ||
| request: undefined, | ||
| }, | ||
| ])('does not restore response when $title', ({request}) => { | ||
| const error = createNetworkError(request); | ||
|
|
||
| const response = recoverXhrResponseFromNetworkError(error); | ||
|
|
||
| expect(response).toBeUndefined(); | ||
| expect(error.response).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('handleBaseApiResponseError', () => { | ||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| test('preserves NEED_RESET behavior after recovered response JSON parsing', async () => { | ||
| const visibilityStateDescriptor = Object.getOwnPropertyDescriptor( | ||
| document, | ||
| 'visibilityState', | ||
| ); | ||
| Object.defineProperty(document, 'visibilityState', { | ||
| configurable: true, | ||
| value: 'visible', | ||
| }); | ||
|
|
||
| const processNeedResetSpy = jest | ||
| .spyOn(needResetModule, 'processNeedReset') | ||
| .mockImplementation(jest.fn()); | ||
| const error = createNetworkError( | ||
| createXhrRequestMock({ | ||
| status: 401, | ||
| statusText: 'Unauthorized', | ||
| responseText: JSON.stringify({code: 'NEED_RESET'}), | ||
| rawHeaders: 'Content-Type: application/json', | ||
| }), | ||
| ); | ||
|
|
||
| await expect(handleBaseApiResponseError(error)).rejects.toBe(error); | ||
|
|
||
| expect(processNeedResetSpy).toHaveBeenCalledTimes(1); | ||
|
|
||
| if (visibilityStateDescriptor) { | ||
| Object.defineProperty(document, 'visibilityState', visibilityStateDescriptor); | ||
| } else { | ||
| Reflect.deleteProperty(document, 'visibilityState'); | ||
| } | ||
| }); | ||
| }); |
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.
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.