From 7f0dbfd35a93cb5c586acb95e96ef0e1d89f2c11 Mon Sep 17 00:00:00 2001 From: Florian JUDITH Date: Tue, 21 Nov 2023 23:50:25 -0500 Subject: [PATCH 01/12] Fixed crash when leveraging custom Lighthouse config Signed-off-by: Florian JUDITH --- .changeset/ninety-otters-report.md | 5 +++ plugins/lighthouse-backend/README.md | 9 ++-- plugins/lighthouse-backend/src/config.ts | 42 +++++++++---------- .../src/service/createScheduler.ts | 4 +- 4 files changed, 32 insertions(+), 28 deletions(-) create mode 100644 .changeset/ninety-otters-report.md diff --git a/.changeset/ninety-otters-report.md b/.changeset/ninety-otters-report.md new file mode 100644 index 0000000000..2b70822042 --- /dev/null +++ b/.changeset/ninety-otters-report.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-lighthouse-backend': major +--- + +Updated Ligthouse schedule configuration to fix crashes when using custom confinguration diff --git a/plugins/lighthouse-backend/README.md b/plugins/lighthouse-backend/README.md index 28845d3c73..cde9135c23 100644 --- a/plugins/lighthouse-backend/README.md +++ b/plugins/lighthouse-backend/README.md @@ -19,13 +19,13 @@ import { PluginEnvironment } from '../types'; import { CatalogClient } from '@backstage/catalog-client'; export default async function createPlugin(env: PluginEnvironment) { - const { logger, scheduler, config } = env; + const { logger, scheduler, config, tokenManager } = env; const catalogClient = new CatalogClient({ discoveryApi: env.discovery, }); - await createScheduler({ logger, scheduler, config, catalogClient }); + await createScheduler({ logger, scheduler, config, catalogClient tokenManager }); } ``` @@ -80,5 +80,8 @@ You can define how often and when the scheduler should run the audits: ```yaml lighthouse: schedule: - days: 1 + frequency: + hours: 12 # Default: days 1 + timeout: + minutes: 30 # Default: null ``` diff --git a/plugins/lighthouse-backend/src/config.ts b/plugins/lighthouse-backend/src/config.ts index bea1c35661..91b4fb1a52 100644 --- a/plugins/lighthouse-backend/src/config.ts +++ b/plugins/lighthouse-backend/src/config.ts @@ -14,52 +14,48 @@ * limitations under the License. */ +import { readTaskScheduleDefinitionFromConfig } from '@backstage/backend-tasks'; import { Config } from '@backstage/config'; import { HumanDuration as HumanDuration } from '@backstage/types'; export interface LighthouseAuditScheduleConfig { - schedule: HumanDuration; + frequency: HumanDuration; timeout: HumanDuration; auditDetail: HumanDuration; } /** @public */ export type LighthouseAuditSchedule = { - getSchedule: () => HumanDuration; + getFrequency: () => HumanDuration; getTimeout: () => HumanDuration; }; /** @public */ export class LighthouseAuditScheduleImpl implements LighthouseAuditSchedule { - static fromConfig(config: Config) { - const lighthouse = config.getOptionalConfig('lighthouse'); + static fromConfig(config: Config): LighthouseAuditScheduleImpl { + // const lighthouse = config.getOptionalConfig('lighthouse'); + const lighthouse = config.has('lighthouse.schedule') + ? readTaskScheduleDefinitionFromConfig( + config.getConfig('lighthouse.schedule'), + ) + : { + frequency: { days: 1 }, + timeout: {}, + }; - let schedule: HumanDuration = { days: 1 }; - let timeout: HumanDuration = {}; + const frequency = lighthouse.frequency as HumanDuration; + const timeout = lighthouse.timeout as HumanDuration; - if (lighthouse) { - const scheduleConfig = lighthouse.getOptionalConfig('schedule'); - const timeoutConfig = lighthouse.getOptionalConfig('timeout'); - - if (scheduleConfig) { - schedule = scheduleConfig as HumanDuration; - } - - if (timeoutConfig) { - timeout = timeoutConfig as HumanDuration; - } - } - - return new LighthouseAuditScheduleImpl(schedule, timeout); + return new LighthouseAuditScheduleImpl(frequency, timeout); } constructor( - private schedule: HumanDuration, + private frequency: HumanDuration, private timeout: HumanDuration, ) {} - getSchedule(): HumanDuration { - return this.schedule; + getFrequency(): HumanDuration { + return this.frequency; } getTimeout(): HumanDuration { diff --git a/plugins/lighthouse-backend/src/service/createScheduler.ts b/plugins/lighthouse-backend/src/service/createScheduler.ts index 498e459a09..33df1226b7 100644 --- a/plugins/lighthouse-backend/src/service/createScheduler.ts +++ b/plugins/lighthouse-backend/src/service/createScheduler.ts @@ -56,14 +56,14 @@ export async function createScheduler( logger.info( `Running with Scheduler Config ${JSON.stringify( - lighthouseAuditConfig.getSchedule(), + lighthouseAuditConfig.getFrequency(), )} and timeout ${JSON.stringify(lighthouseAuditConfig.getTimeout())}`, ); if (scheduler) { await scheduler.scheduleTask({ id: 'lighthouse_audit', - frequency: lighthouseAuditConfig.getSchedule(), + frequency: lighthouseAuditConfig.getFrequency(), timeout: lighthouseAuditConfig.getTimeout(), initialDelay: { minutes: 15 }, fn: async () => { From 7515a338abf09712f0328ad8a9f93247073e5a2a Mon Sep 17 00:00:00 2001 From: Florian JUDITH Date: Tue, 21 Nov 2023 23:58:44 -0500 Subject: [PATCH 02/12] Added required frame-src to display Lighthouse dashboards Signed-off-by: Florian JUDITH --- plugins/lighthouse/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/lighthouse/README.md b/plugins/lighthouse/README.md index 1d2385e4c4..e834ad39c1 100644 --- a/plugins/lighthouse/README.md +++ b/plugins/lighthouse/README.md @@ -47,6 +47,10 @@ const routes = ( Then configure the `lighthouse-audit-service` URL in your [`app-config.yaml`](https://github.com/backstage/backstage/blob/master/app-config.yaml). ```yaml +backend: + csp: + frame-src: + - http://your-service-url lighthouse: baseUrl: http://your-service-url ``` From 623fee149ac12fb054977fbe1d806af58cd6c17a Mon Sep 17 00:00:00 2001 From: Florian JUDITH Date: Wed, 22 Nov 2023 00:52:01 -0500 Subject: [PATCH 03/12] Fixed typo Signed-off-by: Florian JUDITH --- .changeset/ninety-otters-report.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/ninety-otters-report.md b/.changeset/ninety-otters-report.md index 2b70822042..a047ac8187 100644 --- a/.changeset/ninety-otters-report.md +++ b/.changeset/ninety-otters-report.md @@ -2,4 +2,4 @@ '@backstage/plugin-lighthouse-backend': major --- -Updated Ligthouse schedule configuration to fix crashes when using custom confinguration +Updated Lighthouse schedule configuration to fix crashes when using custom confinguration From 49fccefda912736541203a4bfbbf3ae04d21a94d Mon Sep 17 00:00:00 2001 From: Florian JUDITH Date: Wed, 22 Nov 2023 08:04:28 -0500 Subject: [PATCH 04/12] Update .changeset/ninety-otters-report.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Signed-off-by: Florian JUDITH --- .changeset/ninety-otters-report.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/ninety-otters-report.md b/.changeset/ninety-otters-report.md index a047ac8187..9011e37ea8 100644 --- a/.changeset/ninety-otters-report.md +++ b/.changeset/ninety-otters-report.md @@ -2,4 +2,4 @@ '@backstage/plugin-lighthouse-backend': major --- -Updated Lighthouse schedule configuration to fix crashes when using custom confinguration +Updated Lighthouse schedule configuration to fix crashes when using custom configuration From 22cf1c55944fb8ece8bac900ea7b9f65ce75b061 Mon Sep 17 00:00:00 2001 From: Florian JUDITH Date: Wed, 22 Nov 2023 08:04:49 -0500 Subject: [PATCH 05/12] Update plugins/lighthouse-backend/README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Fredrik Adelöw Signed-off-by: Florian JUDITH --- plugins/lighthouse-backend/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/lighthouse-backend/README.md b/plugins/lighthouse-backend/README.md index cde9135c23..04be817248 100644 --- a/plugins/lighthouse-backend/README.md +++ b/plugins/lighthouse-backend/README.md @@ -25,7 +25,7 @@ export default async function createPlugin(env: PluginEnvironment) { discoveryApi: env.discovery, }); - await createScheduler({ logger, scheduler, config, catalogClient tokenManager }); + await createScheduler({ logger, scheduler, config, catalogClient, tokenManager }); } ``` From 63b050345a494abe23e249fb77c5ac203839c4fa Mon Sep 17 00:00:00 2001 From: Florian JUDITH Date: Thu, 23 Nov 2023 23:06:35 -0500 Subject: [PATCH 06/12] Enabled backward config compatibility Signed-off-by: Florian JUDITH --- .changeset/ninety-otters-report.md | 2 +- plugins/lighthouse-backend/src/config.ts | 90 +++++++++++-------- .../src/service/createScheduler.ts | 14 +-- 3 files changed, 61 insertions(+), 45 deletions(-) diff --git a/.changeset/ninety-otters-report.md b/.changeset/ninety-otters-report.md index 9011e37ea8..9b47097f73 100644 --- a/.changeset/ninety-otters-report.md +++ b/.changeset/ninety-otters-report.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-lighthouse-backend': major +'@backstage/plugin-lighthouse-backend': minor --- Updated Lighthouse schedule configuration to fix crashes when using custom configuration diff --git a/plugins/lighthouse-backend/src/config.ts b/plugins/lighthouse-backend/src/config.ts index 91b4fb1a52..cf52d86107 100644 --- a/plugins/lighthouse-backend/src/config.ts +++ b/plugins/lighthouse-backend/src/config.ts @@ -13,52 +13,66 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { LoggerService } from '@backstage/backend-plugin-api'; +import { + TaskScheduleDefinition, + readTaskScheduleDefinitionFromConfig, +} from '@backstage/backend-tasks'; +import { Config, readDurationFromConfig } from '@backstage/config'; +import { HumanDuration } from '@backstage/types'; -import { readTaskScheduleDefinitionFromConfig } from '@backstage/backend-tasks'; -import { Config } from '@backstage/config'; -import { HumanDuration as HumanDuration } from '@backstage/types'; - -export interface LighthouseAuditScheduleConfig { - frequency: HumanDuration; - timeout: HumanDuration; - auditDetail: HumanDuration; -} - -/** @public */ -export type LighthouseAuditSchedule = { - getFrequency: () => HumanDuration; - getTimeout: () => HumanDuration; +type Options = { + logger: LoggerService; }; /** @public */ -export class LighthouseAuditScheduleImpl implements LighthouseAuditSchedule { - static fromConfig(config: Config): LighthouseAuditScheduleImpl { - // const lighthouse = config.getOptionalConfig('lighthouse'); - const lighthouse = config.has('lighthouse.schedule') - ? readTaskScheduleDefinitionFromConfig( - config.getConfig('lighthouse.schedule'), - ) - : { - frequency: { days: 1 }, - timeout: {}, - }; +export class LighthouseAuditScheduleImpl implements TaskScheduleDefinition { + /** + * Looks at the `lighthouse.schedule` section in the application configuration + * and returns a TaskScheduleDefinition. + * Defaults to `{ frequency: { days: 1 }, timeout: {}, initialDelay: { minutes; 15 } }` + * + * @returns a TaskScheduleDefinition + */ + static fromConfig(config: Config, options: Options): TaskScheduleDefinition { + const { logger } = options; - const frequency = lighthouse.frequency as HumanDuration; - const timeout = lighthouse.timeout as HumanDuration; + let lighthouse: TaskScheduleDefinition = { + frequency: { days: 1 }, + timeout: {}, + initialDelay: { minutes: 15 }, + }; - return new LighthouseAuditScheduleImpl(frequency, timeout); + if (config.has('lighthouse.schedule.frequency')) { + lighthouse = readTaskScheduleDefinitionFromConfig( + config.getConfig('lighthouse.schedule'), + ); + } else if (config.has('lighthouse.schedule')) { + logger.warn( + `[Deprecation] Please migrate the schedule configuration to 'lighthouse.schedule.frequency' in ${config.config.context}`, + ); + + lighthouse.frequency = readDurationFromConfig( + config.getConfig('lighthouse.schedule'), + ); + } + + if (config.has('lighthouse.timeout')) { + logger.warn( + `[Deprecation] Please migrate the timeout configuration to 'lighthouse.schedule.timeout' in ${config.config.context}`, + ); + + lighthouse.timeout = readDurationFromConfig( + config.getConfig('lighthouse.timeout'), + ); + } + + return lighthouse; } constructor( - private frequency: HumanDuration, - private timeout: HumanDuration, + public frequency: HumanDuration, + public timeout: HumanDuration, + public initialDelay: HumanDuration, ) {} - - getFrequency(): HumanDuration { - return this.frequency; - } - - getTimeout(): HumanDuration { - return this.timeout; - } } diff --git a/plugins/lighthouse-backend/src/service/createScheduler.ts b/plugins/lighthouse-backend/src/service/createScheduler.ts index 33df1226b7..45a3e88149 100644 --- a/plugins/lighthouse-backend/src/service/createScheduler.ts +++ b/plugins/lighthouse-backend/src/service/createScheduler.ts @@ -39,7 +39,9 @@ export async function createScheduler( const { logger, scheduler, catalogClient, config, tokenManager } = options; const lighthouseApi = LighthouseRestApi.fromConfig(config); - const lighthouseAuditConfig = LighthouseAuditScheduleImpl.fromConfig(config); + const lighthouseAuditConfig = LighthouseAuditScheduleImpl.fromConfig(config, { + logger, + }); const formFactorToScreenEmulationMap = { // the default is mobile, so no need to override mobile: undefined, @@ -56,16 +58,16 @@ export async function createScheduler( logger.info( `Running with Scheduler Config ${JSON.stringify( - lighthouseAuditConfig.getFrequency(), - )} and timeout ${JSON.stringify(lighthouseAuditConfig.getTimeout())}`, + lighthouseAuditConfig.frequency, + )} and timeout ${JSON.stringify(lighthouseAuditConfig.timeout)}`, ); if (scheduler) { await scheduler.scheduleTask({ id: 'lighthouse_audit', - frequency: lighthouseAuditConfig.getFrequency(), - timeout: lighthouseAuditConfig.getTimeout(), - initialDelay: { minutes: 15 }, + frequency: lighthouseAuditConfig.frequency, + timeout: lighthouseAuditConfig.timeout, + initialDelay: lighthouseAuditConfig.initialDelay, fn: async () => { const filter: Record = { kind: 'Component', From 3c72e85d85aaa78404c2477966c14e29eaae5083 Mon Sep 17 00:00:00 2001 From: Florian JUDITH Date: Thu, 23 Nov 2023 23:34:16 -0500 Subject: [PATCH 07/12] Removed reference to app-config file in deprecation message Signed-off-by: Florian JUDITH --- plugins/lighthouse-backend/README.md | 8 +++++++- plugins/lighthouse-backend/src/config.ts | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/plugins/lighthouse-backend/README.md b/plugins/lighthouse-backend/README.md index 04be817248..e2d1917f22 100644 --- a/plugins/lighthouse-backend/README.md +++ b/plugins/lighthouse-backend/README.md @@ -25,7 +25,13 @@ export default async function createPlugin(env: PluginEnvironment) { discoveryApi: env.discovery, }); - await createScheduler({ logger, scheduler, config, catalogClient, tokenManager }); + await createScheduler({ + logger, + scheduler, + config, + catalogClient, + tokenManager, + }); } ``` diff --git a/plugins/lighthouse-backend/src/config.ts b/plugins/lighthouse-backend/src/config.ts index cf52d86107..5b17bd6ec0 100644 --- a/plugins/lighthouse-backend/src/config.ts +++ b/plugins/lighthouse-backend/src/config.ts @@ -49,7 +49,7 @@ export class LighthouseAuditScheduleImpl implements TaskScheduleDefinition { ); } else if (config.has('lighthouse.schedule')) { logger.warn( - `[Deprecation] Please migrate the schedule configuration to 'lighthouse.schedule.frequency' in ${config.config.context}`, + `[Deprecation] Please migrate the schedule configuration to 'lighthouse.schedule.frequency'`, ); lighthouse.frequency = readDurationFromConfig( @@ -59,7 +59,7 @@ export class LighthouseAuditScheduleImpl implements TaskScheduleDefinition { if (config.has('lighthouse.timeout')) { logger.warn( - `[Deprecation] Please migrate the timeout configuration to 'lighthouse.schedule.timeout' in ${config.config.context}`, + `[Deprecation] Please migrate the timeout configuration to 'lighthouse.schedule.timeout'`, ); lighthouse.timeout = readDurationFromConfig( From 19b3c34248326245dc46e1df11802df3060a68ae Mon Sep 17 00:00:00 2001 From: Florian JUDITH Date: Sat, 25 Nov 2023 10:15:02 -0500 Subject: [PATCH 08/12] Added default timeout Signed-off-by: Florian JUDITH --- plugins/lighthouse-backend/src/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/lighthouse-backend/src/config.ts b/plugins/lighthouse-backend/src/config.ts index 5b17bd6ec0..736a4e569d 100644 --- a/plugins/lighthouse-backend/src/config.ts +++ b/plugins/lighthouse-backend/src/config.ts @@ -39,7 +39,7 @@ export class LighthouseAuditScheduleImpl implements TaskScheduleDefinition { let lighthouse: TaskScheduleDefinition = { frequency: { days: 1 }, - timeout: {}, + timeout: { minutes: 10 }, initialDelay: { minutes: 15 }, }; From ffbf656299dc2a65a21926c29323a85d251d0196 Mon Sep 17 00:00:00 2001 From: Florian JUDITH Date: Sat, 25 Nov 2023 10:25:56 -0500 Subject: [PATCH 09/12] Added changeset related to Lighthouse plugin documentation Signed-off-by: Florian JUDITH --- .changeset/long-cycles-grow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/long-cycles-grow.md diff --git a/.changeset/long-cycles-grow.md b/.changeset/long-cycles-grow.md new file mode 100644 index 0000000000..4edc1905fe --- /dev/null +++ b/.changeset/long-cycles-grow.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-lighthouse': patch +--- + +Updated README From 41ea6dfcb16d2da16282174d5afddf3312442375 Mon Sep 17 00:00:00 2001 From: Florian JUDITH Date: Sat, 25 Nov 2023 11:01:48 -0500 Subject: [PATCH 10/12] Added config definition file Signed-off-by: Florian JUDITH --- plugins/lighthouse-backend/config.d.ts | 30 +++++++++++++++++++++++++ plugins/lighthouse-backend/package.json | 3 ++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 plugins/lighthouse-backend/config.d.ts diff --git a/plugins/lighthouse-backend/config.d.ts b/plugins/lighthouse-backend/config.d.ts new file mode 100644 index 0000000000..2d49f188b6 --- /dev/null +++ b/plugins/lighthouse-backend/config.d.ts @@ -0,0 +1,30 @@ +/* + * Copyright 2020 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { TaskScheduleDefinitionConfig } from '@backstage/backend-tasks'; +import { HumanDuration } from '@backstage/types'; + +export interface Config { + /** + * Configuration options for the Lighthouse backend plugin. + */ + lighthouse?: { + /** + * Schedule of the audit + */ + schedule?: HumanDuration | TaskScheduleDefinitionConfig; + }; +} diff --git a/plugins/lighthouse-backend/package.json b/plugins/lighthouse-backend/package.json index 84cff5b8eb..89d9b2f8ec 100644 --- a/plugins/lighthouse-backend/package.json +++ b/plugins/lighthouse-backend/package.json @@ -24,7 +24,8 @@ "lighthouse" ], "files": [ - "dist" + "dist", + "config.d.ts" ], "scripts": { "build": "backstage-cli package build", From bc3fba1f0bfbd343a555853a28b7468ef19da731 Mon Sep 17 00:00:00 2001 From: Florian JUDITH Date: Sat, 25 Nov 2023 20:14:46 -0500 Subject: [PATCH 11/12] Updated changeset details Signed-off-by: Florian JUDITH --- .changeset/ninety-otters-report.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/ninety-otters-report.md b/.changeset/ninety-otters-report.md index 9b47097f73..fbccc481f4 100644 --- a/.changeset/ninety-otters-report.md +++ b/.changeset/ninety-otters-report.md @@ -2,4 +2,4 @@ '@backstage/plugin-lighthouse-backend': minor --- -Updated Lighthouse schedule configuration to fix crashes when using custom configuration +Fixed crashes faced with custom schedule configuration. The configuration schema has been update to leverage the TaskScheduleDefinition interface. It is highly recommended to move the `lighthouse.shedule` and `lighthouse.timeout` respectively to `lighthouse.schedule.frequency` and `lighthouse.schedule.timeout`. From f230f8728c212eeb03cd7bef6f2118c759c4b715 Mon Sep 17 00:00:00 2001 From: Florian JUDITH Date: Wed, 29 Nov 2023 15:26:40 -0500 Subject: [PATCH 12/12] Added configSchema Signed-off-by: Florian JUDITH --- plugins/lighthouse-backend/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/lighthouse-backend/package.json b/plugins/lighthouse-backend/package.json index 89d9b2f8ec..f1a81a8342 100644 --- a/plugins/lighthouse-backend/package.json +++ b/plugins/lighthouse-backend/package.json @@ -27,6 +27,7 @@ "dist", "config.d.ts" ], + "configSchema": "config.d.ts", "scripts": { "build": "backstage-cli package build", "lint": "backstage-cli package lint",