From fdafbb9aaac5ab85cfc1084c2e94bcc542617340 Mon Sep 17 00:00:00 2001 From: Danyal Date: Tue, 11 Jul 2023 23:28:56 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20=20fix=20pi-hole=20int?= =?UTF-8?q?egration=20field=20from=20password=20to=20apiKey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/api/modules/dns-hole/control.ts | 2 +- src/pages/api/modules/dns-hole/summary.spec.ts | 6 +++--- src/pages/api/modules/dns-hole/summary.ts | 2 +- src/server/api/routers/dns-hole.ts | 4 ++-- src/types/app.ts | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/pages/api/modules/dns-hole/control.ts b/src/pages/api/modules/dns-hole/control.ts index c3a3424dc..4dfe4b522 100644 --- a/src/pages/api/modules/dns-hole/control.ts +++ b/src/pages/api/modules/dns-hole/control.ts @@ -57,7 +57,7 @@ const processAdGuard = async (app: ConfigAppType, enable: boolean) => { }; const processPiHole = async (app: ConfigAppType, enable: boolean) => { - const pihole = new PiHoleClient(app.url, findAppProperty(app, 'password')); + const pihole = new PiHoleClient(app.url, findAppProperty(app, 'apiKey')); if (enable) { await pihole.enable(); diff --git a/src/pages/api/modules/dns-hole/summary.spec.ts b/src/pages/api/modules/dns-hole/summary.spec.ts index e80999a3e..05470be69 100644 --- a/src/pages/api/modules/dns-hole/summary.spec.ts +++ b/src/pages/api/modules/dns-hole/summary.spec.ts @@ -28,7 +28,7 @@ describe('DNS hole', () => { type: 'pihole', properties: [ { - field: 'password', + field: 'apiKey', type: 'private', value: 'hf3829fj238g8', }, @@ -130,7 +130,7 @@ describe('DNS hole', () => { type: 'pihole', properties: [ { - field: 'password', + field: 'apiKey', type: 'private', value: 'hf3829fj238g8', }, @@ -144,7 +144,7 @@ describe('DNS hole', () => { type: 'pihole', properties: [ { - field: 'password', + field: 'apiKey', type: 'private', value: 'ayaka', }, diff --git a/src/pages/api/modules/dns-hole/summary.ts b/src/pages/api/modules/dns-hole/summary.ts index 8970f0851..2807b711b 100644 --- a/src/pages/api/modules/dns-hole/summary.ts +++ b/src/pages/api/modules/dns-hole/summary.ts @@ -32,7 +32,7 @@ export const Get = async (request: NextApiRequest, response: NextApiResponse) => try { switch (app.integration?.type) { case 'pihole': { - const piHole = new PiHoleClient(app.url, findAppProperty(app, 'password')); + const piHole = new PiHoleClient(app.url, findAppProperty(app, 'apiKey')); const summary = await piHole.getSummary(); data.domainsBeingBlocked += summary.domains_being_blocked; diff --git a/src/server/api/routers/dns-hole.ts b/src/server/api/routers/dns-hole.ts index 13d98e962..19d903c84 100644 --- a/src/server/api/routers/dns-hole.ts +++ b/src/server/api/routers/dns-hole.ts @@ -97,7 +97,7 @@ const processAdGuard = async (app: ConfigAppType, enable: boolean) => { }; const processPiHole = async (app: ConfigAppType, enable: boolean) => { - const pihole = new PiHoleClient(app.url, findAppProperty(app, 'password')); + const pihole = new PiHoleClient(app.url, findAppProperty(app, 'apiKey')); if (enable) { await pihole.enable(); @@ -108,7 +108,7 @@ const processPiHole = async (app: ConfigAppType, enable: boolean) => { }; const collectPiHoleSummary = async (app: ConfigAppType) => { - const piHole = new PiHoleClient(app.url, findAppProperty(app, 'password')); + const piHole = new PiHoleClient(app.url, findAppProperty(app, 'apiKey')); const summary = await piHole.getSummary(); return { diff --git a/src/types/app.ts b/src/types/app.ts index 847ef8353..7d4f481f9 100644 --- a/src/types/app.ts +++ b/src/types/app.ts @@ -86,7 +86,7 @@ export const integrationFieldProperties: { transmission: ['username', 'password'], jellyfin: ['username', 'password'], plex: ['apiKey'], - pihole: ['password'], + pihole: ['apiKey'], adGuardHome: ['username', 'password'], }; From c092abf5f0323112566373d53b455abd1d3d19f3 Mon Sep 17 00:00:00 2001 From: Danyal Date: Wed, 12 Jul 2023 20:43:32 +0500 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=97=83=EF=B8=8F=20=20Migrate=20old=20?= =?UTF-8?q?pihole=20intergrations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tools/config/getFrontendConfig.ts | 48 +++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/src/tools/config/getFrontendConfig.ts b/src/tools/config/getFrontendConfig.ts index 0a4003697..73b559a9f 100644 --- a/src/tools/config/getFrontendConfig.ts +++ b/src/tools/config/getFrontendConfig.ts @@ -1,11 +1,13 @@ import Consola from 'consola'; import fs from 'fs'; +import { IntegrationField } from '~/types/app'; import { BackendConfigType, ConfigType } from '../../types/config'; import { getConfig } from './getConfig'; import { fetchCity } from '~/server/api/routers/weather'; export const getFrontendConfig = async (name: string): Promise => { let config = getConfig(name); + let shouldMigrateConfig = false; const anyWeatherWidgetWithStringLocation = config.widgets.some( (widget) => widget.type === 'weather' && typeof widget.properties.location === 'string' @@ -13,6 +15,27 @@ export const getFrontendConfig = async (name: string): Promise => { if (anyWeatherWidgetWithStringLocation) { config = await migrateLocation(config); + shouldMigrateConfig = true; + } + + const anyPiholeIntegrationWithPassword = config.apps.some( + (app) => + app?.integration?.type === 'pihole' && + app?.integration?.properties.length && + app.integration.properties.some((property) => property.field === 'password') + ); + + if (anyPiholeIntegrationWithPassword) { + config = migratePiholeIntegrationField(config); + shouldMigrateConfig = true; + } + + if (shouldMigrateConfig) { + Consola.info(`Migrating config ${config.configProperties.name}`); + fs.writeFileSync( + `./data/configs/${config.configProperties.name}.json`, + JSON.stringify(config, null, 2) + ); } Consola.info(`Requested frontend content of configuration '${name}'`); @@ -54,7 +77,6 @@ export const getFrontendConfig = async (name: string): Promise => { const migrateLocation = async (config: BackendConfigType) => { Consola.log('Migrating config file to new location schema...', config.configProperties.name); - const configName = config.configProperties.name; const migratedConfig = { ...config, widgets: await Promise.all( @@ -82,7 +104,27 @@ const migrateLocation = async (config: BackendConfigType) => { ), }; - fs.writeFileSync(`./data/configs/${configName}.json`, JSON.stringify(migratedConfig, null, 2)); - return migratedConfig; }; + +const migratePiholeIntegrationField = (config: BackendConfigType) => { + Consola.log('Migrating pihole integration field to apiKey...', config.configProperties.name); + return { + ...config, + apps: config.apps.map((app) => { + if (app?.integration?.type === 'pihole' && Array.isArray(app?.integration?.properties)) { + const migratedProperties = app.integration.properties.map((property) => { + if (property.field === 'password') { + return { + ...property, + field: 'apiKey' as IntegrationField, + }; + } + return property; + }); + return { ...app, integration: { ...app.integration, properties: migratedProperties } }; + } + return app; + }), + }; +};