diff --git a/src/plugins.ts b/src/plugins.ts index 78a5819e6..f6f39d595 100644 --- a/src/plugins.ts +++ b/src/plugins.ts @@ -6,7 +6,7 @@ import { clearError, processError } from './error' import { getPluginAttachmentRows, getPluginConfigRows, getPluginRows } from './sql' import { status } from './status' import { PluginConfig, PluginJsonConfig, PluginsServer, PluginTask, TeamId } from './types' -import { getFileFromArchive } from './utils' +import { getFileFromArchive, pluginDigest } from './utils' import { createPluginConfigVM } from './vm/vm' export async function setupPlugins(server: PluginsServer): Promise { @@ -119,7 +119,7 @@ async function loadPlugin(server: PluginsServer, pluginConfig: PluginConfig): Pr await processError( server, pluginConfig, - `Could not load posthog config at "${configPath}" for plugin "${plugin.name}"` + `Could not load posthog config at "${configPath}" for ${pluginDigest(plugin)}` ) return false } @@ -129,7 +129,7 @@ async function loadPlugin(server: PluginsServer, pluginConfig: PluginConfig): Pr await processError( server, pluginConfig, - `No "main" config key or "index.js" file found for plugin "${plugin.name}"` + `No "main" config key or "index.js" file found for ${pluginDigest(plugin)}` ) return false } @@ -145,7 +145,7 @@ async function loadPlugin(server: PluginsServer, pluginConfig: PluginConfig): Pr try { pluginConfig.vm = await createPluginConfigVM(server, pluginConfig, indexJs, libJs) - status.info('🔌', `Loaded local plugin "${plugin.name}" from "${pluginPath}"!`) + status.info('🔌', `Loaded local ${pluginDigest(plugin)} from "${pluginPath}"!`) await clearError(server, pluginConfig) return true } catch (error) { @@ -159,7 +159,7 @@ async function loadPlugin(server: PluginsServer, pluginConfig: PluginConfig): Pr try { config = JSON.parse(json) } catch (error) { - await processError(server, pluginConfig, `Can not load plugin.json for plugin "${plugin.name}"`) + await processError(server, pluginConfig, `Can not load plugin.json for ${pluginDigest(plugin)}`) return false } } @@ -173,19 +173,19 @@ async function loadPlugin(server: PluginsServer, pluginConfig: PluginConfig): Pr if (indexJs) { try { pluginConfig.vm = await createPluginConfigVM(server, pluginConfig, indexJs, libJs || '') - status.info('🔌', `Loaded plugin "${plugin.name}"!`) + status.info('🔌', `Loaded ${pluginDigest(plugin)}!`) await clearError(server, pluginConfig) return true } catch (error) { await processError(server, pluginConfig, error) } } else { - await processError(server, pluginConfig, `Could not load index.js for plugin "${plugin.name}"!`) + await processError(server, pluginConfig, `Could not load index.js for ${pluginDigest(plugin)}!`) } } else if (plugin.plugin_type === 'source' && plugin.source) { try { pluginConfig.vm = await createPluginConfigVM(server, pluginConfig, plugin.source) - status.info('🔌', `Loaded plugin "${plugin.name}"!`) + status.info('🔌', `Loaded ${pluginDigest(plugin)}!`) await clearError(server, pluginConfig) return true } catch (error) { @@ -195,7 +195,7 @@ async function loadPlugin(server: PluginsServer, pluginConfig: PluginConfig): Pr await processError( server, pluginConfig, - `Un-downloaded remote plugins not supported! Plugin: "${plugin.name}"` + `Un-downloaded remote plugins not supported! Tried with ${pluginDigest(plugin)}` ) } } catch (error) { diff --git a/src/sql.ts b/src/sql.ts index 1e7199d22..c997e6b6d 100644 --- a/src/sql.ts +++ b/src/sql.ts @@ -1,35 +1,35 @@ import { Plugin, PluginAttachmentDB, PluginConfig, PluginConfigId, PluginError, PluginsServer } from './types' +function pluginConfigsInForceQuery(specificField?: string): string { + return `SELECT posthog_pluginconfig.${specificField || '*'} + FROM posthog_pluginconfig + LEFT JOIN posthog_team ON posthog_team.id = posthog_pluginconfig.team_id + LEFT JOIN posthog_organization ON posthog_organization.id = posthog_team.organization_id + LEFT JOIN posthog_plugin ON posthog_plugin.id = posthog_pluginconfig.plugin_id + WHERE ( + posthog_pluginconfig.enabled='t' AND posthog_organization.plugins_access_level > 0 + AND (posthog_plugin.organization_id = posthog_organization.id OR posthog_plugin.is_global) + )` +} + export async function getPluginRows(server: PluginsServer): Promise { const { rows: pluginRows }: { rows: Plugin[] } = await server.db.postgresQuery( - `SELECT posthog_plugin.* FROM posthog_plugin WHERE id in - (SELECT posthog_pluginconfig.plugin_id - FROM posthog_pluginconfig - LEFT JOIN posthog_team ON posthog_team.id = posthog_pluginconfig.team_id - WHERE posthog_pluginconfig.enabled='t' - GROUP BY posthog_pluginconfig.plugin_id)` + `SELECT posthog_plugin.* FROM posthog_plugin + WHERE id IN (${pluginConfigsInForceQuery('plugin_id')} GROUP BY posthog_pluginconfig.plugin_id)` ) return pluginRows } export async function getPluginAttachmentRows(server: PluginsServer): Promise { const { rows }: { rows: PluginAttachmentDB[] } = await server.db.postgresQuery( - `SELECT posthog_pluginattachment.* FROM posthog_pluginattachment WHERE plugin_config_id in - (SELECT posthog_pluginconfig.id - FROM posthog_pluginconfig - LEFT JOIN posthog_team ON posthog_team.id = posthog_pluginconfig.team_id - WHERE posthog_pluginconfig.enabled='t')` + `SELECT posthog_pluginattachment.* FROM posthog_pluginattachment + WHERE plugin_config_id IN (${pluginConfigsInForceQuery('id')})` ) return rows } export async function getPluginConfigRows(server: PluginsServer): Promise { - const { rows }: { rows: PluginConfig[] } = await server.db.postgresQuery( - `SELECT posthog_pluginconfig.* - FROM posthog_pluginconfig - LEFT JOIN posthog_team ON posthog_team.id = posthog_pluginconfig.team_id - WHERE posthog_pluginconfig.enabled='t'` - ) + const { rows }: { rows: PluginConfig[] } = await server.db.postgresQuery(pluginConfigsInForceQuery()) return rows } diff --git a/src/types.ts b/src/types.ts index 4e66541c6..aa5a0573e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -99,6 +99,7 @@ export interface Plugin { name: string plugin_type: 'local' | 'respository' | 'custom' | 'source' description?: string + is_global: boolean url?: string config_schema: Record | PluginConfigSchema[] tag?: string diff --git a/src/utils.ts b/src/utils.ts index 0f3a2dd69..0f86fde0c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -8,7 +8,7 @@ import * as tar from 'tar-stream' import * as zlib from 'zlib' import { status } from './status' -import { LogLevel, PluginsServerConfig, TimestampFormat } from './types' +import { LogLevel, Plugin, PluginsServerConfig, TimestampFormat } from './types' /** Time until autoexit (due to error) gives up on graceful exit and kills the process right away. */ const GRACEFUL_EXIT_PERIOD_SECONDS = 5 @@ -386,3 +386,11 @@ export async function createRedis(serverConfig: PluginsServerConfig): Promise