-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
feat: add reverse proxy checker #21030
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9abfda6
Add reverse proper checker
zlwaterfield 49d0deb
Create reverseProxyCheckerLogic.test.ts
zlwaterfield e93f32d
Update reverseProxyCheckerLogic.test.ts
zlwaterfield 3da3456
Update UI snapshots for `chromium` (1)
github-actions[bot] f39f52a
Update UI snapshots for `chromium` (1)
github-actions[bot] 192af2e
Merge branch 'master' into reverse-proxy-check
zlwaterfield d2aaecf
Update UI snapshots for `chromium` (2)
github-actions[bot] fc027e0
Update UI snapshots for `chromium` (2)
github-actions[bot] 3d2c3fe
Merge remote-tracking branch 'origin/master' into reverse-proxy-check
zlwaterfield dbcd436
Hook up reverseProxyCheckerLogic to the quick start menu task
zlwaterfield a8444aa
Merge branch 'reverse-proxy-check' of github.com:PostHog/posthog into…
zlwaterfield 247afe6
Add a timestamp so it doesn't request too often
zlwaterfield 7f154ee
Merge branch 'master' into reverse-proxy-check
zlwaterfield 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 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
63 changes: 63 additions & 0 deletions
63
frontend/src/lib/components/ReverseProxyChecker/reverseProxyCheckerLogic.test.ts
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,63 @@ | ||
import { expectLogic } from 'kea-test-utils' | ||
|
||
import { useMocks } from '~/mocks/jest' | ||
import { initKeaTests } from '~/test/init' | ||
|
||
import { reverseProxyCheckerLogic } from './reverseProxyCheckerLogic' | ||
|
||
const hasReverseProxyValues = [['https://proxy.example.com'], [null]] | ||
const doesNotHaveReverseProxyValues = [[null], [null]] | ||
|
||
const useMockedValues = (results: (string | null)[][]): void => { | ||
useMocks({ | ||
post: { | ||
'/api/projects/:team/query': () => [ | ||
200, | ||
{ | ||
results, | ||
}, | ||
], | ||
}, | ||
}) | ||
} | ||
|
||
describe('reverseProxyCheckerLogic', () => { | ||
let logic: ReturnType<typeof reverseProxyCheckerLogic.build> | ||
|
||
beforeEach(() => { | ||
initKeaTests() | ||
localStorage.clear() | ||
logic = reverseProxyCheckerLogic() | ||
}) | ||
|
||
afterEach(() => { | ||
logic.unmount() | ||
}) | ||
|
||
it('should not have a reverse proxy set - when no data', async () => { | ||
useMockedValues([]) | ||
|
||
logic.mount() | ||
await expectLogic(logic).toFinishAllListeners().toMatchValues({ | ||
hasReverseProxy: false, | ||
}) | ||
}) | ||
|
||
it('should not have a reverse proxy set - when data with no lib_custom_api_host values', async () => { | ||
useMockedValues(doesNotHaveReverseProxyValues) | ||
|
||
logic.mount() | ||
await expectLogic(logic).toFinishAllListeners().toMatchValues({ | ||
hasReverseProxy: false, | ||
}) | ||
}) | ||
|
||
it('should have a reverse proxy set', async () => { | ||
useMockedValues(hasReverseProxyValues) | ||
|
||
logic.mount() | ||
await expectLogic(logic).toFinishAllListeners().toMatchValues({ | ||
hasReverseProxy: true, | ||
}) | ||
}) | ||
}) |
49 changes: 49 additions & 0 deletions
49
frontend/src/lib/components/ReverseProxyChecker/reverseProxyCheckerLogic.ts
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,49 @@ | ||
import { afterMount, kea, path, reducers } from 'kea' | ||
import { loaders } from 'kea-loaders' | ||
import api from 'lib/api' | ||
|
||
import { HogQLQuery, NodeKind } from '~/queries/schema' | ||
import { hogql } from '~/queries/utils' | ||
|
||
import type { reverseProxyCheckerLogicType } from './reverseProxyCheckerLogicType' | ||
|
||
const CHECK_INTERVAL_MS = 1000 * 60 * 60 // 1 hour | ||
|
||
export const reverseProxyCheckerLogic = kea<reverseProxyCheckerLogicType>([ | ||
path(['components', 'ReverseProxyChecker', 'reverseProxyCheckerLogic']), | ||
loaders({ | ||
hasReverseProxy: [ | ||
false as boolean | null, | ||
{ | ||
loadHasReverseProxy: async () => { | ||
const query: HogQLQuery = { | ||
kind: NodeKind.HogQLQuery, | ||
query: hogql`SELECT properties.$lib_custom_api_host AS lib_custom_api_host | ||
FROM events | ||
WHERE timestamp >= now() - INTERVAL 1 DAY | ||
AND timestamp <= now() | ||
ORDER BY timestamp DESC | ||
limit 10`, | ||
} | ||
|
||
const res = await api.query(query) | ||
return !!res.results?.find((x) => !!x[0]) | ||
}, | ||
}, | ||
], | ||
}), | ||
reducers({ | ||
lastCheckedTimestamp: [ | ||
0, | ||
{ persist: true }, | ||
{ | ||
loadHasReverseProxySuccess: () => Date.now(), | ||
}, | ||
], | ||
}), | ||
afterMount(({ actions, values }) => { | ||
if (values.lastCheckedTimestamp < Date.now() - CHECK_INTERVAL_MS) { | ||
actions.loadHasReverseProxy() | ||
} | ||
}), | ||
]) |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was incorrectly set.