Skip to content

Commit

Permalink
[backend] WIP migration file
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyCloarec committed Apr 4, 2024
1 parent 1be8bf1 commit be9a56d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 76 deletions.
3 changes: 1 addition & 2 deletions opencti-platform/opencti-graphql/src/database/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
buildPagination,
cursorToOffset,
ES_INDEX_PREFIX,
INDEX_FILES,
INDEX_INTERNAL_OBJECTS,
inferIndexFromConceptType,
isEmptyField,
Expand Down Expand Up @@ -324,7 +323,7 @@ const elOperationForMigration = (operation) => {
logApp.info(`${message} > started`);
// Execute the update by query in async mode
const queryAsync = await operation({
index,
...(index ?? {}),

Check warning on line 326 in opencti-platform/opencti-graphql/src/database/engine.js

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/database/engine.js#L326

Added line #L326 was not covered by tests
refresh: true,
wait_for_completion: false,
body
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { logApp } from '../config/conf';
import { elReindexByQueryForMigration, elUpdateByQueryForMigration } from '../database/engine';
import { READ_INDEX_STIX_CORE_RELATIONSHIPS, READ_INDEX_STIX_META_RELATIONSHIPS, READ_RELATIONSHIPS_INDICES } from '../database/utils';
import { elDeleteByQueryForMigration, elReindexByQueryForMigration, elUpdateByQueryForMigration } from '../database/engine';
import { READ_DATA_INDICES, READ_INDEX_STIX_CORE_RELATIONSHIPS, READ_INDEX_STIX_META_RELATIONSHIPS, READ_RELATIONSHIPS_INDICES } from '../database/utils';
import { RELATION_RELATED_TO } from '../schema/stixCoreRelationship';

export const up = async (next) => {
logApp.info('[MIGRATION] Update all linked-to refs to related-to rels');

const linkedToType = 'x_opencti_linked-to';
// First, we reindex all linked-to meta refs to core rel index, updating all linked-to refs to related-to rel in the process
const linkedToToRelatedToSript = `
const reindexLinkedToToRelatedToSource = `
ctx._source.entity_type = params.relToType;
ctx._source.relationship_type = params.relToType;
ctx._source.parentTypes = params.relToParentTypes;
Expand All @@ -19,40 +19,38 @@ export const up = async (next) => {
const relToParentTypes = ['basic-relationship', 'stix-relationship', 'stix-core-relationship'];

const reindexLinkedToToRelatedToQuery = {
body: {
source: {
index: READ_INDEX_STIX_META_RELATIONSHIPS,
query: {
bool: {
must: [
{ term: { 'entity_type.keyword': { value: linkedToType } } }
]
}
source: {
index: READ_INDEX_STIX_META_RELATIONSHIPS,
query: {
bool: {
must: [
{ term: { 'entity_type.keyword': { value: linkedToType } } }
]
}
},
dest: {
index: READ_INDEX_STIX_CORE_RELATIONSHIPS
},
script: {
source: linkedToToRelatedToSript,
params: { linkedToType, relToType: RELATION_RELATED_TO, relToParentTypes }
}
},
dest: {
index: READ_INDEX_STIX_CORE_RELATIONSHIPS
},
script: {
source: reindexLinkedToToRelatedToSource,
params: { linkedToType, relToType: RELATION_RELATED_TO, relToParentTypes }
}
};

await elReindexByQueryForMigration('[MIGRATION] Reindexing and updating linked-to refs', null, reindexLinkedToToRelatedToQuery);

// Then, We need to update all rel that had a linked-to ref as from or to
const relToTypes = [RELATION_RELATED_TO, 'basic-relationship', 'stix-relationship', 'stix-core-relationship'];
const relWithLinkedToToRelatedToSource = `
if(ctx._source.fromType == params.linkedToType) {ctx._source.fromType=params.relToType} ;
if(ctx._source.toType == params.linkedToType) {ctx._source.toType=params.relToType} ;
const updateSCOWithLinkedToFromOrToSource = `
if(ctx._source.fromType == params.linkedToType) {ctx._source.fromType=params.relToType};
if(ctx._source.toType == params.linkedToType) {ctx._source.toType=params.relToType};
for(connection in ctx._source.connections) {
if(connection.types.contains(params.linkedToType) { connection.types = params.relToTypes) };
}`;
const updateSCOWithLinkedToFromOrToQuery = {
script: {
source: relWithLinkedToToRelatedToSource,
source: updateSCOWithLinkedToFromOrToSource,
params: { linkedToType, relToType: RELATION_RELATED_TO, relToTypes }
},
query: {
Expand All @@ -71,7 +69,43 @@ export const up = async (next) => {

await elUpdateByQueryForMigration('[MIGRATION] Updating relation with a linked-to from or to', READ_RELATIONSHIPS_INDICES, updateSCOWithLinkedToFromOrToQuery);

// Then we need to move all denormalized linked-to rel in objects to related-to
// Then we need to move all denormalized linked-to rel in objects to related-to (if id is not already in related-to)
const updateDenormalizedLinkedToSource = `
ArrayList linkedTos = ctx._source[params.relLinkedTo];
if(!ctx._source.containsKey(params.relRelatedTo)) {ctx._source[params.relRelatedTo]=[] };
for(linkedTo in linkedTos) {
if(!ctx._source[relRelatedTo].contains(linkedTo)) { ctx._source[params.relRelatedTo].add(linkedTo) }
};
ctx._source.remove(params.relLinkedTo)`;
const updateDenormalizedLinkedToQuery = {
script: {
source: updateDenormalizedLinkedToSource,
params: { relLinkedTo: `rel_${linkedToType}.internal_id`, relRelatedTo: `rel_${RELATION_RELATED_TO}.internal_id` }
},
query: {
exists: {
field: `rel_${linkedToType}`
}
},
};

await elUpdateByQueryForMigration('[MIGRATION] Updating entities with rel_linked-to to rel_related-to', READ_DATA_INDICES, updateDenormalizedLinkedToQuery);

// Finally, we delete all original linked-to refs in meta rel index

await elDeleteByQueryForMigration(
'[MIGRATION] Deleting all linked-to refs',
[READ_INDEX_STIX_META_RELATIONSHIPS],
{
query: {
bool: {
must: [
{ term: { 'entity_type.keyword': { value: linkedToType } } }
]
}
}
}
);

logApp.info('[MIGRATION] Update all linked-to refs to related-to rels finished');

Expand Down

0 comments on commit be9a56d

Please sign in to comment.