Skip to content
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 13 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { actions, connect, events, kea, listeners, path, reducers, selectors } f
import { loaders } from 'kea-loaders'
import { router } from 'kea-router'
import api from 'lib/api'
import { reverseProxyCheckerLogic } from 'lib/components/ReverseProxyChecker/reverseProxyCheckerLogic'
import { permanentlyMount } from 'lib/utils/kea-logic-builders'
import posthog from 'posthog-js'
import { membersLogic } from 'scenes/organization/membersLogic'
Expand Down Expand Up @@ -58,6 +59,8 @@ export const activationLogic = kea<activationLogicType>([
['insights'],
dashboardsModel,
['rawDashboards'],
reverseProxyCheckerLogic,
['hasReverseProxy'],
],
actions: [
inviteLogic,
Expand Down Expand Up @@ -193,6 +196,7 @@ export const activationLogic = kea<activationLogicType>([
s.customEventsCount,
s.installedPlugins,
s.currentTeamSkippedTasks,
s.hasReverseProxy,
],
(
currentTeam,
Expand All @@ -202,7 +206,8 @@ export const activationLogic = kea<activationLogicType>([
dashboards,
customEventsCount,
installedPlugins,
skippedTasks
skippedTasks,
hasReverseProxy
) => {
const tasks: ActivationTaskType[] = []
for (const task of Object.values(ActivationTasks)) {
Expand Down Expand Up @@ -286,7 +291,7 @@ export const activationLogic = kea<activationLogicType>([
id: ActivationTasks.SetUpReverseProxy,
name: 'Set up a reverse proxy',
description: 'Send your events from your own domain to avoid tracking blockers',
completed: false,
completed: hasReverseProxy || false,
canSkip: true,
skipped: skippedTasks.includes(ActivationTasks.SetUpReverseProxy),
url: 'https://posthog.com/docs/advanced/proxy',
Expand Down
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,
})
})
})
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()
}
}),
])
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { hogql } from '~/queries/utils'

import type { versionCheckerLogicType } from './versionCheckerLogicType'

const CHECK_INTERVAL_MS = 1000 * 60 * 60 // 6 hour
const CHECK_INTERVAL_MS = 1000 * 60 * 60 * 6 // 6 hour
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was incorrectly set.


export type SDKVersion = {
version: string
Expand Down
Loading