Skip to content

Commit 4eb490d

Browse files
committed
refactored the implemented classes for OpenSSFMetric providers; reviewed the OpenSSF thresholds
1 parent ec8024c commit 4eb490d

File tree

8 files changed

+153
-54
lines changed

8 files changed

+153
-54
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
# Component with OpenSSF Scorecard
3+
apiVersion: backstage.io/v1alpha1
4+
kind: Component
5+
metadata:
6+
name: rhdh-plugins
7+
annotations:
8+
github.com/project-slug: redhat-developer/rhdh-plugins
9+
openssf/project: redhat-developer/rhdh-plugins
10+
backstage.io/source-location: url:https://github.com/redhat-developer/rhdh-plugins
11+
spec:
12+
type: service
13+
owner: guests
14+
lifecycle: experimental

workspaces/scorecard/packages/backend/src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ const backend = createBackend();
2020

2121
backend.add(import('@backstage/plugin-app-backend'));
2222
backend.add(import('@backstage/plugin-proxy-backend'));
23-
backend.add(import('@backstage/plugin-scaffolder-backend'));
24-
backend.add(import('@backstage/plugin-scaffolder-backend-module-github'));
23+
// TODO: Temporarily disabled - isolated-vm native module build issue
24+
// backend.add(import('@backstage/plugin-scaffolder-backend'));
25+
// backend.add(import('@backstage/plugin-scaffolder-backend-module-github'));
2526
backend.add(import('@backstage/plugin-techdocs-backend'));
2627

2728
// auth plugin
@@ -33,9 +34,10 @@ backend.add(import('@backstage/plugin-auth-backend-module-github-provider'));
3334

3435
// catalog plugin
3536
backend.add(import('@backstage/plugin-catalog-backend'));
36-
backend.add(
37-
import('@backstage/plugin-catalog-backend-module-scaffolder-entity-model'),
38-
);
37+
// TODO: Temporarily disabled - depends on scaffolder
38+
// backend.add(
39+
// import('@backstage/plugin-catalog-backend-module-scaffolder-entity-model'),
40+
// );
3941

4042
// See https://backstage.io/docs/features/software-catalog/configuration#subscribing-to-catalog-errors
4143
backend.add(import('@backstage/plugin-catalog-backend-module-logs'));

workspaces/scorecard/plugins/scorecard-backend-module-openssf/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
* @packageDocumentation
2121
*/
2222

23-
export { scorecardModuleOpenSSF as default } from './module';
23+
export { scorecardOpenSFFModule as default } from './module';

workspaces/scorecard/plugins/scorecard-backend-module-openssf/src/metricProviders/AbstractMetricProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ export abstract class AbstractMetricProvider
4747
this.thresholds = thresholds ?? DEFAULT_NUMBER_THRESHOLDS;
4848
}
4949

50-
protected abstract getMetricName(): string;
50+
abstract getMetricName(): string;
5151

52-
protected abstract getMetricDisplayTitle(): string;
52+
abstract getMetricDisplayTitle(): string;
5353

54-
protected abstract getMetricDescription(): string;
54+
abstract getMetricDescription(): string;
5555

5656
getProviderDatasourceId(): string {
5757
return 'openssf';
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {
18+
createDefaultOpenSSFMetricProviders,
19+
DefaultOpenSSFMetricProvider,
20+
} from './DefaultOpenSSFMetricProvider';
21+
import { OPENSSF_METRICS, OPENSSF_THRESHOLDS } from './OpenSSFConfig';
22+
23+
describe('DefaultOpenSSFMetricProviderTests', () => {
24+
it('should create a default OpenSSF metric provider', () => {
25+
const provider = new DefaultOpenSSFMetricProvider(
26+
OPENSSF_METRICS[0],
27+
OPENSSF_THRESHOLDS,
28+
);
29+
expect(provider.getMetricDisplayTitle()).toBe(
30+
OPENSSF_METRICS[0].displayTitle,
31+
);
32+
expect(provider.getMetricDescription()).toBe(
33+
OPENSSF_METRICS[0].description,
34+
);
35+
expect(provider.getMetricThresholds()).toBe(OPENSSF_THRESHOLDS);
36+
});
37+
38+
it('should create a default OpenSSF metric provider with custom thresholds', () => {
39+
const provider = new DefaultOpenSSFMetricProvider(
40+
OPENSSF_METRICS[0],
41+
OPENSSF_THRESHOLDS,
42+
);
43+
expect(provider).toBeDefined();
44+
});
45+
46+
it('should create all default OpenSSF metric providers', () => {
47+
const providers = createDefaultOpenSSFMetricProviders(OPENSSF_THRESHOLDS);
48+
expect(providers.length).toBe(OPENSSF_METRICS.length);
49+
for (const provider of providers) {
50+
expect(provider).toBeInstanceOf(DefaultOpenSSFMetricProvider);
51+
expect(provider.getMetricThresholds()).toBe(OPENSSF_THRESHOLDS);
52+
}
53+
});
54+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { ThresholdConfig } from '@red-hat-developer-hub/backstage-plugin-scorecard-common';
18+
import { MetricProvider } from '@red-hat-developer-hub/backstage-plugin-scorecard-node';
19+
import { AbstractMetricProvider } from './AbstractMetricProvider';
20+
import { OPENSSF_METRICS } from './OpenSSFConfig';
21+
22+
/**
23+
* Default metric provider for OpenSSF Security Scorecards.
24+
* Extracts a specific check from the OpenSSF scorecard response based on the provided configuration.
25+
*/
26+
export class DefaultOpenSSFMetricProvider extends AbstractMetricProvider {
27+
constructor(
28+
private readonly config: OpenSSFMetricConfig,
29+
thresholds?: ThresholdConfig,
30+
) {
31+
super(thresholds);
32+
}
33+
34+
getMetricName(): string {
35+
return this.config.name;
36+
}
37+
38+
getMetricDisplayTitle(): string {
39+
return this.config.displayTitle;
40+
}
41+
42+
getMetricDescription(): string {
43+
return this.config.description;
44+
}
45+
}
46+
47+
/**
48+
* Creates all default OpenSSF metric providers.
49+
* @param thresholds Optional threshold configuration to apply to all providers
50+
* @returns Array of OpenSSF metric providers
51+
*/
52+
export function createDefaultOpenSSFMetricProviders(
53+
thresholds?: ThresholdConfig,
54+
): MetricProvider<'number'>[] {
55+
return OPENSSF_METRICS.map(
56+
config => new DefaultOpenSSFMetricProvider(config, thresholds),
57+
);
58+
}

workspaces/scorecard/plugins/scorecard-backend-module-openssf/src/metricProviders/openSSFMetricProviders.ts renamed to workspaces/scorecard/plugins/scorecard-backend-module-openssf/src/metricProviders/OpenSSFConfig.ts

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
*/
1616

1717
import { ThresholdConfig } from '@red-hat-developer-hub/backstage-plugin-scorecard-common';
18-
import { MetricProvider } from '@red-hat-developer-hub/backstage-plugin-scorecard-node';
19-
import { AbstractMetricProvider } from './AbstractMetricProvider';
2018

2119
/**
2220
* Configuration for an OpenSSF metric provider.
2321
*/
24-
interface OpenSSFMetricConfig {
22+
export interface OpenSSFMetricConfig {
2523
/** The name of the OpenSSF check (e.g., "Maintained", "Code-Review") */
2624
name: string;
2725
/** Display title for the metric (e.g., "OpenSSF Maintained") */
@@ -33,7 +31,7 @@ interface OpenSSFMetricConfig {
3331
/**
3432
* All available OpenSSF Security Scorecard metrics.
3533
*/
36-
const OPENSSF_METRICS: OpenSSFMetricConfig[] = [
34+
export const OPENSSF_METRICS: OpenSSFMetricConfig[] = [
3735
{
3836
name: 'Binary-Artifacts',
3937
displayTitle: 'OpenSSF Binary Artifacts',
@@ -144,40 +142,10 @@ const OPENSSF_METRICS: OpenSSFMetricConfig[] = [
144142
},
145143
];
146144

147-
/**
148-
* Configurable metric provider for OpenSSF Security Scorecards.
149-
* Extracts a specific check from the OpenSSF scorecard response based on the provided configuration.
150-
*/
151-
class ConfigurableMetricProvider extends AbstractMetricProvider {
152-
constructor(
153-
private readonly config: OpenSSFMetricConfig,
154-
thresholds?: ThresholdConfig,
155-
) {
156-
super(thresholds);
157-
}
158-
159-
protected getMetricName(): string {
160-
return this.config.name;
161-
}
162-
163-
protected getMetricDisplayTitle(): string {
164-
return this.config.displayTitle;
165-
}
166-
167-
protected getMetricDescription(): string {
168-
return this.config.description;
169-
}
170-
}
171-
172-
/**
173-
* Creates all OpenSSF metric providers.
174-
* @param thresholds Optional threshold configuration to apply to all providers
175-
* @returns Array of OpenSSF metric providers
176-
*/
177-
export function createOpenSSFMetricProviders(
178-
thresholds?: ThresholdConfig,
179-
): MetricProvider<'number'>[] {
180-
return OPENSSF_METRICS.map(
181-
config => new ConfigurableMetricProvider(config, thresholds),
182-
);
183-
}
145+
export const OPENSSF_THRESHOLDS: ThresholdConfig = {
146+
rules: [
147+
{ key: 'error', expression: '<2' },
148+
{ key: 'warning', expression: '2-7' },
149+
{ key: 'success', expression: '>7' },
150+
],
151+
};

workspaces/scorecard/plugins/scorecard-backend-module-openssf/src/module.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
*/
1616
import { createBackendModule } from '@backstage/backend-plugin-api';
1717
import { scorecardMetricsExtensionPoint } from '@red-hat-developer-hub/backstage-plugin-scorecard-node';
18-
import { createOpenSSFMetricProviders } from './metricProviders/openSSFMetricProviders';
18+
import { createDefaultOpenSSFMetricProviders } from './metricProviders/DefaultOpenSSFMetricProvider';
19+
import { OPENSSF_THRESHOLDS } from './metricProviders/OpenSSFConfig';
1920

20-
export const scorecardModuleOpenSSF = createBackendModule({
21+
export const scorecardOpenSFFModule = createBackendModule({
2122
pluginId: 'scorecard',
2223
moduleId: 'openssf',
2324
register(reg) {
@@ -26,8 +27,10 @@ export const scorecardModuleOpenSSF = createBackendModule({
2627
metrics: scorecardMetricsExtensionPoint,
2728
},
2829
async init({ metrics }) {
29-
// Register all OpenSSF metric providers
30-
metrics.addMetricProvider(...createOpenSSFMetricProviders());
30+
// Register all default OpenSSF metric providers
31+
metrics.addMetricProvider(
32+
...createDefaultOpenSSFMetricProviders(OPENSSF_THRESHOLDS),
33+
);
3134
},
3235
});
3336
},

0 commit comments

Comments
 (0)