Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backend] Fix logging for tests / Enforce report creation and adapt test #8900

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion opencti-platform/opencti-graphql/src/domain/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { ENTITY_TYPE_CONTAINER_REPORT } from '../schema/stixDomainObject';
import { RELATION_CREATED_BY, RELATION_OBJECT } from '../schema/stixRefRelationship';
import { ABSTRACT_STIX_CORE_OBJECT, ABSTRACT_STIX_DOMAIN_OBJECT, ABSTRACT_STIX_RELATIONSHIP, buildRefRelationKey } from '../schema/general';
import { elCount } from '../database/engine';
import { READ_DATA_INDICES_WITHOUT_INFERRED, READ_INDEX_STIX_DOMAIN_OBJECTS } from '../database/utils';
import { isEmptyField, READ_DATA_INDICES_WITHOUT_INFERRED, READ_INDEX_STIX_DOMAIN_OBJECTS } from '../database/utils';
import { isStixId } from '../schema/schemaUtils';
import { stixDomainObjectDelete } from './stixDomainObject';
import { addFilter } from '../utils/filtering/filtering-utils';
import { UnsupportedError } from '../config/errors';

export const findById = (context, user, reportId) => {
return storeLoadById(context, user, reportId, ENTITY_TYPE_CONTAINER_REPORT);
Expand Down Expand Up @@ -117,6 +118,9 @@ export const reportsDistributionByEntity = async (context, user, args) => {

// region mutations
export const addReport = async (context, user, report) => {
if (isEmptyField(report.name) || isEmptyField(report.published)) {
throw UnsupportedError('Report creation required name and published', { name: report.name, published: report.published });
}
const finalReport = R.assoc('created', report.published, report);
const created = await createEntity(context, user, finalReport, ENTITY_TYPE_CONTAINER_REPORT);
return notify(BUS_TOPICS[ABSTRACT_STIX_DOMAIN_OBJECT].ADDED_TOPIC, created, user);
Expand Down
8 changes: 4 additions & 4 deletions opencti-platform/opencti-graphql/src/manager/taskManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ export const taskHandler = async () => {
try {
// Lock the manager
lock = await lockResource([TASK_MANAGER_KEY], { retryCount: 0 });
logApp.info('[OPENCTI-MODULE][TASK-MANAGER] Starting task handler');
logApp.debug('[OPENCTI-MODULE][TASK-MANAGER] Starting task handler');
running = true;
const startingTime = new Date().getMilliseconds();
const context = executionContext('task_manager', SYSTEM_USER);
Expand All @@ -579,7 +579,7 @@ export const taskHandler = async () => {
// Fetch the user responsible for the task
const rawUser = await resolveUserByIdFromCache(context, task.initiator_id);
const user = { ...rawUser, origin: { user_id: rawUser.id, referer: 'background_task' } };
logApp.info(`[OPENCTI-MODULE][TASK-MANAGER] Executing job using userId:${rawUser.id}, for task ${task.internal_id}`);
logApp.debug(`[OPENCTI-MODULE][TASK-MANAGER] Executing job using userId:${rawUser.id}, for task ${task.internal_id}`);
let jobToExecute;
if (isQueryTask) {
jobToExecute = await computeQueryTaskElements(context, user, task);
Expand All @@ -592,7 +592,7 @@ export const taskHandler = async () => {
}
// Process the elements (empty = end of execution)
const processingElements = jobToExecute.elements;
logApp.info(`[OPENCTI-MODULE][TASK-MANAGER] Found ${processingElements.length} element(s) to process.`);
logApp.debug(`[OPENCTI-MODULE][TASK-MANAGER] Found ${processingElements.length} element(s) to process.`);
if (processingElements.length > 0) {
lock.signal.throwIfAborted();
const errors = await executeProcessing(context, user, jobToExecute, task.scope);
Expand All @@ -606,7 +606,7 @@ export const taskHandler = async () => {
task_processed_number: processedNumber,
completed: processingElements.length < MAX_TASK_ELEMENTS,
};
logApp.info('[OPENCTI-MODULE][TASK-MANAGER] Elements processing done, store task status.', { patch });
logApp.debug('[OPENCTI-MODULE][TASK-MANAGER] Elements processing done, store task status.', { patch });
await updateTask(context, task.id, patch);
const totalTime = new Date().getMilliseconds() - startingTime;
logApp.info(`[OPENCTI-MODULE][TASK-MANAGER] Task manager done in ${totalTime} ms`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';
import { prepareTaxiiGetParam, processTaxiiResponse, type TaxiiResponseData } from '../../../src/manager/ingestionManager';
import { ADMIN_USER, testContext } from '../../utils/testQuery';
import { addIngestion as addTaxiiIngestion, findById as findTaxiiIngestionById, patchTaxiiIngestion } from '../../../src/modules/ingestion/ingestion-taxii-domain';
import { addIngestion as addTaxiiIngestion, findById as findTaxiiIngestionById, ingestionDelete, patchTaxiiIngestion } from '../../../src/modules/ingestion/ingestion-taxii-domain';
import { IngestionAuthType, type IngestionTaxiiAddInput, TaxiiVersion } from '../../../src/generated/graphql';
import type { StixReport } from '../../../src/types/stix-sdo';
import { now } from '../../../src/utils/format';
Expand All @@ -20,7 +20,6 @@ describe('Verify taxii ingestion', () => {
const ingestionNotPagination = await addTaxiiIngestion(testContext, ADMIN_USER, input);
expect(ingestionNotPagination.id).toBeDefined();
expect(ingestionNotPagination.internal_id).toBeDefined();

// 2. Check parameter send to taxii server
const expectedParams = prepareTaxiiGetParam(ingestionNotPagination);
expect(expectedParams.next).toBeUndefined();
Expand All @@ -47,6 +46,9 @@ describe('Verify taxii ingestion', () => {
const result = await findTaxiiIngestionById(testContext, ADMIN_USER, ingestionNotPagination.id);
expect(result.current_state_cursor).toBeUndefined();
expect(result.added_after_start).toBeDefined();

// Delete the ingest
await ingestionDelete(testContext, ADMIN_USER, ingestionNotPagination.internal_id);
});

it('should taxii server response with data and next page and start date', async () => {
Expand Down Expand Up @@ -117,6 +119,9 @@ describe('Verify taxii ingestion', () => {
const result = await findTaxiiIngestionById(testContext, ADMIN_USER, taxiiEntityAfterfirstRequest.id);
expect(result.current_state_cursor, 'Since more is false, next value should be reset').toBeUndefined();
expect(result.added_after_start).toBe('2024-03-01T20:35:44.000Z');

// Delete the ingest
await ingestionDelete(testContext, ADMIN_USER, ingestionPaginatedWithStartDate.internal_id);
});

it('should taxii server response with no start date, and next page', async () => {
Expand Down Expand Up @@ -186,6 +191,9 @@ describe('Verify taxii ingestion', () => {
const result = await findTaxiiIngestionById(testContext, ADMIN_USER, taxiiEntityAfterFirstCall.id);
expect(result.current_state_cursor, 'Since more is false, next value should be reset').toBeUndefined();
expect(result.added_after_start).toBe('2024-03-01T20:44:44.000Z');

// Delete the ingest
await ingestionDelete(testContext, ADMIN_USER, ingestionPaginatedWithNoStartDate.internal_id);
});

it('should store nothing when no data', async () => {
Expand Down Expand Up @@ -216,6 +224,9 @@ describe('Verify taxii ingestion', () => {
const result = await findTaxiiIngestionById(testContext, ADMIN_USER, ingestionPaginatedWithStartDate.id);
expect(result.current_state_cursor).toBeUndefined(); // previous value
expect(result.added_after_start).toBe('2023-01-01T20:35:44.000Z'); // previous value

// Delete the ingest
await ingestionDelete(testContext, ADMIN_USER, ingestionPaginatedWithStartDate.internal_id);
});
});

Expand All @@ -239,5 +250,8 @@ describe('Verify taxii ingestion - patch part', () => {
const result = await patchTaxiiIngestion(testContext, ADMIN_USER, ingestion.id, state);
expect(result.id).toBeDefined();
// should not throw exception "Unknown Error: Attribute must be a string"

// Delete the ingest
await ingestionDelete(testContext, ADMIN_USER, ingestion.internal_id);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export const promoteObservableInput = {

export const promoteReportInput = {
name: 'reportTestPromote',
published: '2022-10-06T22:00:00.000Z',
description: 'description',
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('TaskManager executeReplace tests ', () => {
it('REPLACE report marking with different marking', async () => {
const reportInput = {
name: 'test report marking with different marking',
published: '2022-10-06T22:00:00.000Z',
objectMarking: [MARKING_TLP_CLEAR],
};
const report = await addReport(adminContext, adminContext.user, reportInput);
Expand Down Expand Up @@ -57,6 +58,7 @@ describe('TaskManager executeReplace tests ', () => {
it('REPLACE report marking with same marking', async () => {
const reportInput = {
name: 'test report marking with same marking',
published: '2022-10-06T22:00:00.000Z',
objectMarking: [MARKING_TLP_CLEAR],
};
const report = await addReport(adminContext, adminContext.user, reportInput);
Expand Down Expand Up @@ -85,6 +87,7 @@ describe('TaskManager executeReplace tests ', () => {
it('REPLACE report without marking with marking', async () => {
const reportInput = {
name: 'test report no marking with marking',
published: '2022-10-06T22:00:00.000Z',
objectMarking: [],
};
const report = await addReport(adminContext, adminContext.user, reportInput);
Expand Down Expand Up @@ -124,6 +127,7 @@ describe('TaskManager executeReplace tests ', () => {
it('REPLACE report author with different author', async () => {
const reportInput = {
name: 'test replace report author with different author',
published: '2022-10-06T22:00:00.000Z',
createdBy: TEST_ORGANIZATION.id,
};
const report = await addReport(adminContext, adminContext.user, reportInput);
Expand All @@ -147,6 +151,7 @@ describe('TaskManager executeReplace tests ', () => {
it('REPLACE report author with same author', async () => {
const reportInput = {
name: 'test replace report author with same author',
published: '2022-10-06T22:00:00.000Z',
createdBy: TEST_ORGANIZATION.id,
};
const report = await addReport(adminContext, adminContext.user, reportInput);
Expand All @@ -172,6 +177,7 @@ describe('TaskManager executeReplace tests ', () => {
it('REPLACE report without author with author', async () => {
const reportInput = {
name: 'test replace report without author with author',
published: '2022-10-06T22:00:00.000Z',
createdBy: '',
};
const report = await addReport(adminContext, adminContext.user, reportInput);
Expand Down Expand Up @@ -202,6 +208,7 @@ describe('TaskManager executeReplace tests ', () => {
it('REPLACE report description with different description', async () => {
const reportInput = {
name: 'test replace report description with different description',
published: '2022-10-06T22:00:00.000Z',
description: 'description',
};
const report = await addReport(adminContext, adminContext.user, reportInput);
Expand Down Expand Up @@ -229,6 +236,7 @@ describe('TaskManager executeReplace tests ', () => {
it('REPLACE report description with same description', async () => {
const reportInput = {
name: 'test replace report description with same description',
published: '2022-10-06T22:00:00.000Z',
description: 'description',
};
const report = await addReport(adminContext, adminContext.user, reportInput);
Expand All @@ -255,6 +263,7 @@ describe('TaskManager executeReplace tests ', () => {
it('REPLACE report without description with description', async () => {
const reportInput = {
name: 'test replace report without description with description',
published: '2022-10-06T22:00:00.000Z',
description: '',
};
const report = await addReport(adminContext, adminContext.user, reportInput);
Expand Down