Skip to content

Commit

Permalink
[backend] handle elIndex in draft
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyCloarec committed Sep 27, 2024
1 parent 089a7bf commit ba9aaf1
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 19 deletions.
17 changes: 13 additions & 4 deletions opencti-platform/opencti-graphql/src/database/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
INDEX_DELETED_OBJECTS,
INDEX_DRAFT,
INDEX_INTERNAL_OBJECTS,
INDEX_INTERNAL_RELATIONSHIPS,
inferIndexFromConceptType,
isEmptyField,
isInferredIndex,
Expand Down Expand Up @@ -3269,17 +3270,25 @@ export const elBulk = async (args) => {
});
};
/* v8 ignore next */
export const elIndex = async (indexName, documentBody, opts = {}) => {
export const elIndex = async (context, user, indexName, documentBody, opts = {}) => {
const { refresh = true, pipeline } = opts;
const documentId = documentBody.internal_id;
const entityType = documentBody.entity_type ? documentBody.entity_type : '';
logApp.debug(`[SEARCH] index > ${entityType} ${documentId} in ${indexName}`, { documentBody });
const draftContext = inDraftContext(context, user);
let indexNameToUse = indexName;
let documentToIndex = documentBody;
if (draftContext) {
if (indexName === INDEX_INTERNAL_OBJECTS || indexName === INDEX_INTERNAL_RELATIONSHIPS) throw new Error('Cannot index internal element in draft context');
indexNameToUse = INDEX_DRAFT;
documentToIndex = { ...documentBody, draft_ids: [draftContext] };
}
let indexParams = {
index: indexName,
id: documentBody.internal_id,
index: indexNameToUse,
id: documentToIndex.internal_id,
refresh,
timeout: '60m',
body: R.dissoc('_index', documentBody),
body: R.dissoc('_index', documentToIndex),
};
if (pipeline) {
indexParams = { ...indexParams, pipeline };
Expand Down
4 changes: 2 additions & 2 deletions opencti-platform/opencti-graphql/src/database/file-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ export const elIndexFiles = async (context, user, files) => {
};
const documentBody = buildIndexFileBody(internal_id, fileObject, entity);
try {
await elIndex(INDEX_FILES, documentBody, { pipeline: 'attachment' });
await elIndex(context, user, INDEX_FILES, documentBody, { pipeline: 'attachment' });
} catch (err) {
// catch & log error
logApp.error('Error on file indexing', { message: err.message, causeStack: err.data?.cause?.stack, stack: err.stack, file_id });
// try to index without file content
const documentWithoutFileData = R.dissoc('file_data', documentBody);
await elIndex(INDEX_FILES, documentWithoutFileData).catch((e) => {
await elIndex(context, user, INDEX_FILES, documentWithoutFileData).catch((e) => {
logApp.error('Error in fallback file indexing', { message: e.message, cause: e.cause, file_id });
});
}
Expand Down
2 changes: 1 addition & 1 deletion opencti-platform/opencti-graphql/src/database/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const INDEX_STIX_CYBER_OBSERVABLES = `${ES_INDEX_PREFIX}_stix_cyber_observables`
export const READ_INDEX_STIX_CYBER_OBSERVABLES = `${INDEX_STIX_CYBER_OBSERVABLES}*`;

// Relations
const INDEX_INTERNAL_RELATIONSHIPS = `${ES_INDEX_PREFIX}_internal_relationships`;
export const INDEX_INTERNAL_RELATIONSHIPS = `${ES_INDEX_PREFIX}_internal_relationships`;
export const READ_INDEX_INTERNAL_RELATIONSHIPS = `${INDEX_INTERNAL_RELATIONSHIPS}*`;
export const INDEX_STIX_CORE_RELATIONSHIPS = `${ES_INDEX_PREFIX}_stix_core_relationships`;
export const READ_INDEX_STIX_CORE_RELATIONSHIPS = `${INDEX_STIX_CORE_RELATIONSHIPS}*`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,6 @@ export const createListTask = async (context, user, input) => {
message: 'creates `background task`',
context_data: { entity_type: ENTITY_TYPE_BACKGROUND_TASK, input: listTask }
});
await elIndex(INDEX_INTERNAL_OBJECTS, listTask);
await elIndex(context, user, INDEX_INTERNAL_OBJECTS, listTask);
return listTask;
};
4 changes: 2 additions & 2 deletions opencti-platform/opencti-graphql/src/domain/backgroundTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const createRuleTask = async (context, user, ruleDefinition, input) => {
const countExpected = queryData.pageInfo.globalCount;
const task = createDefaultTask(user, input, TASK_TYPE_RULE, countExpected);
const ruleTask = { ...task, rule, enable };
await elIndex(INDEX_INTERNAL_OBJECTS, ruleTask);
await elIndex(context, user, INDEX_INTERNAL_OBJECTS, ruleTask);
return ruleTask;
};

Expand All @@ -146,7 +146,7 @@ export const createQueryTask = async (context, user, input) => {
message: 'creates `background task`',
context_data: { entity_type: ENTITY_TYPE_BACKGROUND_TASK, input: queryTask }
});
await elIndex(INDEX_INTERNAL_OBJECTS, queryTask);
await elIndex(context, user, INDEX_INTERNAL_OBJECTS, queryTask);
return queryTask;
};

Expand Down
4 changes: 2 additions & 2 deletions opencti-platform/opencti-graphql/src/domain/retentionRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const checkRetentionRule = async (context, input) => {
};

// input { name, filters }
export const createRetentionRule = async (_, user, input) => {
export const createRetentionRule = async (context, user, input) => {
// filters must be a valid json
let { filters } = input;
if (!filters) { // filters is undefined or an empty string
Expand All @@ -77,7 +77,7 @@ export const createRetentionRule = async (_, user, input) => {
filters,
...input,
};
await elIndex(INDEX_INTERNAL_OBJECTS, retentionRule);
await elIndex(context, user, INDEX_INTERNAL_OBJECTS, retentionRule);
await publishUserAction({
user,
event_type: 'mutation',
Expand Down
2 changes: 1 addition & 1 deletion opencti-platform/opencti-graphql/src/domain/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const createStreamCollection = async (context, user, input) => {
authorized_authorities: [TAXIIAPI_SETCOLLECTIONS],
...input
};
await elIndex(INDEX_INTERNAL_OBJECTS, data);
await elIndex(context, user, INDEX_INTERNAL_OBJECTS, data);
await publishUserAction({
user,
event_type: 'mutation',
Expand Down
2 changes: 1 addition & 1 deletion opencti-platform/opencti-graphql/src/domain/taxii.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const createTaxiiCollection = async (context, user, input) => {
authorized_authorities: [TAXIIAPI_SETCOLLECTIONS],
...input,
};
await elIndex(INDEX_INTERNAL_OBJECTS, data);
await elIndex(context, user, INDEX_INTERNAL_OBJECTS, data);
await publishUserAction({
user,
event_type: 'mutation',
Expand Down
2 changes: 1 addition & 1 deletion opencti-platform/opencti-graphql/src/domain/work.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export const createWork = async (context, user, connector, friendlyName, sourceI
errors: [],
[buildRefRelationKey(RELATION_OBJECT_MARKING)]: [...fileMarkings]
};
await elIndex(INDEX_HISTORY, work);
await elIndex(context, user, INDEX_HISTORY, work);
return loadWorkById(context, user, workId);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ export const indexFileToDocument = async (context: AuthContext, file: any) => {
const internalFile = await storeLoadById(context, SYSTEM_USER, data.internal_id, ENTITY_TYPE_INTERNAL_FILE);
if (internalFile) {
// update existing internalFile (if file has been saved in another index)
return elIndex(internalFile._index, data);
return elIndex(context, context.user, internalFile._index, data);
}
return elIndex(INDEX_INTERNAL_OBJECTS, data);
return elIndex(context, context.user, INDEX_INTERNAL_OBJECTS, data);
};

export const deleteDocumentIndex = async (context: AuthContext, user: AuthUser, id: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('Elasticsearch document loader', () => {
entity_type: 'Campaign',
parent_types: ['Campaign', 'Stix-Domain-Object', 'Stix-Core-Object', 'Stix-Object', 'Basic-Object'],
};
const indexedData = await elIndex('test_index', documentBody);
const indexedData = await elIndex({}, {}, 'test_index', documentBody);
expect(indexedData).toEqual(documentBody);
const documentWithIndex = R.assoc('_index', 'test_index-000001', documentBody);
// Load by internal Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Old work of connector cleanup test', () => {
user_id: '',
};

await elIndex(INDEX_HISTORY, eightDaysAgoWork as Work);
await elIndex({}, {}, INDEX_HISTORY, eightDaysAgoWork as Work);
const workCreated = await findWorkById(testContext, ADMIN_USER, workId) as unknown as Work;
expect(workCreated.id).toBeDefined();
expect(workCreated.status).toBe(status);
Expand Down

0 comments on commit ba9aaf1

Please sign in to comment.