Skip to content

Commit 60db615

Browse files
agustingrohfrancostramana
authored andcommitted
DT-1077 Keeps cryptography data updated over the identification process
1 parent 9b65e27 commit 60db615

File tree

8 files changed

+642
-80
lines changed

8 files changed

+642
-80
lines changed

package-lock.json

Lines changed: 560 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@
141141
"react-syntax-highlighter": "^15.4.3",
142142
"react-virtualized": "^9.22.3",
143143
"react-virtualized-tree": "^3.4.1",
144+
"reflect-metadata": "^0.1.13",
144145
"regenerator-runtime": "^0.13.5",
146+
"scanoss": "^0.9.3",
145147
"sort-paths": "^1.1.1",
146148
"source-map-support": "^0.5.19",
147149
"translation-check": "^1.0.2",

src/main/services/ComponentService.ts

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ import { QueryBuilderCreator } from '../model/queryBuilder/QueryBuilderCreator';
1111
import { workspace } from '../workspace/Workspace';
1212
import { modelProvider } from './ModelProvider';
1313
import { ComponentAdapter } from '../adapters/ComponentAdapter';
14-
import { AddVulnerabilityTask } from '../task/vulnerability/AddVulnerabilityTask';
1514
import {
1615
ComponentSource,
1716
ComponentVersion,
1817
} from '../model/entity/ComponentVersion';
18+
import { AddCrypto } from './utils/cryptography';
19+
import { AddVulnerability } from './utils/vulnerability';
20+
import { After } from './utils/hookAfter';
1921

2022
class ComponentService {
2123
public async getComponentFiles(
2224
data: Partial<Component>,
23-
params: IWorkbenchFilterParams
25+
params: IWorkbenchFilterParams,
2426
): Promise<any> {
2527
try {
2628
const filter = workspace.getOpenedProjects()[0].getFilter(params);
@@ -45,7 +47,7 @@ class ComponentService {
4547
if (files[i].inventoryid) {
4648
files[i].inventory = index[files[i].inventoryid];
4749
files[i].component = components.find(
48-
(component: any) => files[i].inventory.cvid === component.compid
50+
(component: any) => files[i].inventory.cvid === component.compid,
4951
);
5052
}
5153
}
@@ -66,7 +68,7 @@ class ComponentService {
6668
}); // Keep summary independent of summary
6769
let comp = await modelProvider.model.component.getAll(queryBuilder);
6870
const summary = await modelProvider.model.component.summary(
69-
queryBuilderSummary
71+
queryBuilderSummary,
7072
);
7173
comp = componentHelper.addSummary(comp, summary);
7274
const compPurl: any = this.groupComponentsByPurl(comp);
@@ -81,7 +83,7 @@ class ComponentService {
8183

8284
public async get(
8385
component: Partial<ComponentGroup>,
84-
params: IWorkbenchFilterParams
86+
params: IWorkbenchFilterParams,
8587
) {
8688
try {
8789
const p = workspace.getOpenedProjects()[0];
@@ -130,15 +132,14 @@ class ComponentService {
130132
aux.summary.ignored += iterator.summary.ignored;
131133
aux.summary.pending += iterator.summary.pending;
132134
aux.summary.identified += iterator.summary.identified;
133-
aux.totalFiles +=
134-
iterator.summary.ignored +
135-
iterator.summary.pending +
136-
iterator.summary.identified;
135+
aux.totalFiles
136+
+= iterator.summary.ignored
137+
+ iterator.summary.pending
138+
+ iterator.summary.identified;
137139
version.summary = iterator.summary;
138-
version.files =
139-
iterator.summary.ignored +
140-
iterator.summary?.pending +
141-
iterator.summary.identified;
140+
version.files = iterator.summary.ignored
141+
+ iterator.summary?.pending
142+
+ iterator.summary.identified;
142143
}
143144
version.version = iterator.version;
144145
version.licenses = [];
@@ -150,27 +151,23 @@ class ComponentService {
150151
result.push(aux);
151152
}
152153
result.sort((a, b) => a.name.localeCompare(b.name));
153-
result.forEach((comp) =>
154-
comp.versions.sort((a, b) => b.version.localeCompare(a.version))
155-
);
154+
result.forEach((comp) => comp.versions.sort((a, b) => b.version.localeCompare(a.version)));
156155
return result;
157156
}
158157

159158
public async importComponents() {
160159
try {
161160
const components: Array<Partial<Component>> = await modelProvider.model.component.getUniqueComponentsFromResults();
162161
await modelProvider.model.component.import(components);
163-
const data =
164-
await modelProvider.model.component.getLicensesAttachedToComponentsFromResults();
162+
const data = await modelProvider.model.component.getLicensesAttachedToComponentsFromResults();
165163
const componentLicenses = new ComponentAdapter().componentLicenses(data);
166164
await modelProvider.model.license.bulkAttachComponentLicense(
167-
componentLicenses
165+
componentLicenses,
168166
);
169167
// Add most reliable license to each component
170-
const componentReliableLicense =
171-
await modelProvider.model.component.getMostReliableLicensePerComponent();
168+
const componentReliableLicense = await modelProvider.model.component.getMostReliableLicensePerComponent();
172169
await modelProvider.model.component.updateMostReliableLicense(
173-
componentReliableLicense
170+
componentReliableLicense,
174171
);
175172
return true;
176173
} catch (error: any) {
@@ -180,8 +177,7 @@ class ComponentService {
180177

181178
private async getOverrideComponents() {
182179
try {
183-
const overrideComponents =
184-
await modelProvider.model.component.getOverrideComponents();
180+
const overrideComponents = await modelProvider.model.component.getOverrideComponents();
185181
let result: any = {};
186182
if (overrideComponents.length > 0) {
187183
result = overrideComponents.reduce((acc, curr) => {
@@ -200,8 +196,10 @@ class ComponentService {
200196
}
201197
}
202198

199+
@After(AddCrypto)
200+
@After(AddVulnerability)
203201
public async create(
204-
newComp: NewComponentDTO
202+
newComp: NewComponentDTO,
205203
): Promise<Partial<ComponentGroup>> {
206204
const promises = newComp.versions.map((v) => {
207205
const component = new ComponentVersion();
@@ -213,29 +211,19 @@ class ComponentService {
213211
});
214212
const results = await Promise.all(promises.map((p) => p.catch((e) => e)));
215213
const validComponents = results.filter(
216-
(result) => !(result instanceof Error)
214+
(result) => !(result instanceof Error),
217215
);
218-
if (results.length - validComponents.length === newComp.versions.length)
216+
if (results.length - validComponents.length === newComp.versions.length) {
219217
throw new Error('Component already exists');
218+
}
220219
const component = await modelProvider.model.component.getAll(
221-
QueryBuilderCreator.create({ purl: newComp.purl })
220+
QueryBuilderCreator.create({ purl: newComp.purl }),
222221
);
223222
const compPurl: any = this.groupComponentsByPurl(component);
224223
const response = await this.mergeComponentByPurl(compPurl);
225224

226-
// TODO: Uncomment code when gRPC service is integrated
227-
// Adds component's vulnerabilities
228-
const addVulnerability = new AddVulnerabilityTask();
229-
await addVulnerability.run(this.adaptToVulnerabilityTask(newComp));
230225
return response[0];
231226
}
232-
233-
private adaptToVulnerabilityTask(component: NewComponentDTO): Array<string> {
234-
const response = component.versions.map(
235-
(v) => `${component.purl}@${v.version}`
236-
);
237-
return response;
238-
}
239227
}
240228

241229
export const componentService = new ComponentService();
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import log from 'electron-log';
2+
import { AddCryptographyTask } from '../../task/cryptography/AddCryptographyTask';
3+
import { workspace } from '../../workspace/Workspace';
4+
5+
export async function AddCrypto(data: any) {
6+
const p = workspace.getOpenProject();
7+
if (p.getGlobalApiKey()) {
8+
log.info('%c[ Crypto ]: Importing cryptography into database', 'color: green');
9+
const cryptoTask = new AddCryptographyTask();
10+
await cryptoTask.run({ components: [`${data.purl}@${data.versions[0].version}`], token: p.getGlobalApiKey() });
11+
}
12+
}

src/main/services/utils/hookAfter.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export function After(callback: (data: any)=> Promise<void>) {
2+
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
3+
const next = descriptor.value;
4+
5+
// Your logic before the function call
6+
descriptor.value = async function (...args: any[]) {
7+
try {
8+
const result = await next.apply(this, args);
9+
await callback(args[0]);
10+
return result;
11+
} catch (e: any) {
12+
throw new Error(e);
13+
}
14+
};
15+
};
16+
}

src/main/services/utils/inventoryServiceUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Inventory } from '../../../api/types';
88
export function getInventoriesGroupedByUsage(inventory: Partial<Inventory>, results: any): Array<Partial<Inventory>> {
99
const inventories = new Map<string, Partial<Inventory>>();
1010
results.forEach((r) => {
11-
if(!inventories.has(r.type)) { // create a new inventory and set usage
11+
if (!inventories.has(r.type)) { // create a new inventory and set usage
1212
const inv = {...inventory, usage:r.type , files: [r.id] };
1313
inventories.set(r.type, inv);
1414
} else inventories.get(r.type).files.push(r.id);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { NewComponentDTO } from '@api/types';
2+
import log from 'electron-log';
3+
import { AddVulnerabilityTask } from '../../task/vulnerability/AddVulnerabilityTask';
4+
5+
export async function AddVulnerability(data: NewComponentDTO) {
6+
try {
7+
// If everything goes right
8+
9+
log.info('%c[ Vulnerability ]: Importing vulnerability into database', 'color: green');
10+
// Adds component's vulnerabilities
11+
const addVulnerability = new AddVulnerabilityTask();
12+
await addVulnerability.run(adaptToVulnerabilityTask(data));
13+
} catch (e: any) {
14+
throw new Error(e);
15+
}
16+
}
17+
18+
function adaptToVulnerabilityTask(component: NewComponentDTO): Array<string> {
19+
const response = component.versions.map(
20+
(v) => `${component.purl}@${v.version}`,
21+
);
22+
return response;
23+
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"experimentalDecorators": true,
34
"incremental": true,
45
"target": "es2021",
56
"module": "commonjs",

0 commit comments

Comments
 (0)