Skip to content

Commit

Permalink
[backend] Modify the migrate script to use elUpdateByQueryForMigration
Browse files Browse the repository at this point in the history
  • Loading branch information
ParamConstructor committed Feb 13, 2024
1 parent aa99909 commit bcb42b9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 73 deletions.
11 changes: 9 additions & 2 deletions opencti-platform/opencti-graphql/src/generated/graphql.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { DatabaseError } from '../config/errors';
import { elRawDeleteByQuery, elList } from '../database/engine';
import { createEntity } from '../database/middleware';
import { elUpdateByQueryForMigration } from '../database/engine';
import { READ_INDEX_STIX_CYBER_OBSERVABLES } from '../database/utils';
import { addVocabulary } from '../modules/vocabulary/vocabulary-domain';
import { SYSTEM_USER, executionContext } from '../utils/access';

const ENTITY_BANK_ACCOUNT = 'Bank-Account';
const ENTITY_CRYPTOGRAPHIC_WALLET = 'Cryptocurrency-Wallet';

const newVocabularies = {
currency_code_ov: [
{ key: '0x_(zrx)' },
Expand Down Expand Up @@ -321,71 +316,35 @@ export const up = async (next) => {
}
}

// Migrate BankAccount and CryptocurrencyWallet observables to FinancialAccounts
const bankAccounts = await elList(context, SYSTEM_USER, [READ_INDEX_STIX_CYBER_OBSERVABLES], { types: [ENTITY_BANK_ACCOUNT] });
const cryptoWallets = await elList(context, SYSTEM_USER, [READ_INDEX_STIX_CYBER_OBSERVABLES], { types: [ENTITY_CRYPTOGRAPHIC_WALLET] });

for (let i = 0; i < bankAccounts.length; i += 1) {
const bankAccount = bankAccounts[i];
const financialAccount = {
type: 'Financial-Account',
x_opencti_score: bankAccount.x_opencti_score,
x_opencti_description: bankAccount.x_opencti_description,
x_opencti_stix_ids: bankAccount.x_opencti_stix_ids,
spec_version: bankAccount.spec_version,
creator_id: bankAccount.creator_id,
created_at: bankAccount.created_at,
updated_at: bankAccount.updated_at,
base_type: bankAccount.base_type,
parent_types: bankAccount.parent_types,
account_number: bankAccount.account_number,
bic_number: bankAccount.bic,
iban_number: bankAccount.iban,
account_type: 'depository_bank_account',
};
await createEntity(context, SYSTEM_USER, financialAccount, financialAccount.type);
}
for (let i = 0; i < cryptoWallets.length; i += 1) {
const cryptoWallet = cryptoWallets[i];
const financialAccount = {
type: 'Financial-Account',
x_opencti_score: cryptoWallet.x_opencti_score,
x_opencti_description: cryptoWallet.x_opencti_description,
x_opencti_stix_ids: cryptoWallet.x_opencti_stix_ids,
spec_version: cryptoWallet.spec_version,
creator_id: cryptoWallet.creator_id,
created_at: cryptoWallet.created_at,
updated_at: cryptoWallet.updated_at,
base_type: cryptoWallet.base_type,
parent_types: cryptoWallet.parent_types,
account_number: cryptoWallet.value,
account_type: 'cryptocurrency_wallet',
};
await createEntity(context, SYSTEM_USER, financialAccount, financialAccount.type);
}
const query = {
bool: {
should: [
{
match: {
'entity_type.keyword': ENTITY_BANK_ACCOUNT,
},
},
{
match: {
'entity_type.keyword': ENTITY_CRYPTOGRAPHIC_WALLET,
}
},
],
// Query for all existing Cryptocurrency-Wallet and Bank-Account Observables and replace with a equivalent
// new Financial-Account Observable
const ENTITY_BANK_ACCOUNT = 'Bank-Account';
const ENTITY_CRYPTOGRAPHIC_WALLET = 'Cryptocurrency-Wallet';
const updateQuery = {
script: {
source:
"if (ctx._source.entity_type == 'Cryptocurrency-Wallet') { ctx._source.entity_type = 'Financial-Account'; ctx._source.account_type = 'cryptocurrency_wallet'; ctx._source.standard_id = ctx._source.standard_id.replace('cryptocurrency-wallet', 'financial-account') } "
+ "if (ctx._source.entity_type == 'Bank-Account') { ctx._source.entity_type = 'Financial-Account'; ctx._source.account_type = 'depository_bank_account'; ctx._source.standard_id = ctx._source.standard_id.replace('bank-account', 'financial-account')} "
},
query: {
bool: {
should: [
{ term: { 'entity_type.keyword': { value: ENTITY_CRYPTOGRAPHIC_WALLET } } },
{ term: { 'entity_type.keyword': { value: ENTITY_BANK_ACCOUNT } } },
]
}
}
};
await elRawDeleteByQuery({
index: READ_INDEX_STIX_CYBER_OBSERVABLES,
refresh: true,
body: { query },
}).catch((err) => {
throw DatabaseError('[MIGRATION] Error deleting files by ids', { error: err });
});
// Run _update_by_query
await elUpdateByQueryForMigration(
'[MIGRATION] Migrating Cryptocurrency-Wallet and Bank-Account to Financial-Account observables',
[READ_INDEX_STIX_CYBER_OBSERVABLES],
updateQuery
);

next();
};

export const down = async (next) => {
next();
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect } from 'vitest';
import gql from 'graphql-tag';
import { ADMIN_USER, queryAsAdmin } from '../../utils/testQuery';
import { ENTITY_BANK_ACCOUNT, ENTITY_EMAIL_ADDR, ENTITY_EMAIL_MESSAGE, ENTITY_IPV6_ADDR, ENTITY_SOFTWARE } from '../../../src/schema/stixCyberObservable';
import { ENTITY_FINANCIAL_ACCOUNT, ENTITY_EMAIL_ADDR, ENTITY_EMAIL_MESSAGE, ENTITY_IPV6_ADDR, ENTITY_SOFTWARE } from '../../../src/schema/stixCyberObservable';
import {
BUILT_IN_DECAY_RULE_IP_URL,
type DecayRuleConfiguration,
Expand Down Expand Up @@ -284,7 +284,7 @@ describe('DecayRule resolver standard behavior', () => {
description: 'Indicator that does not match any decay rule',
pattern: "[file:hashes.'SHA-256' = 'ea4c2f895f7b1c46aa8de559e7a6d8201b49437332d6d5e859052276db50c6c4']",
pattern_type: 'stix',
x_opencti_main_observable_type: ENTITY_BANK_ACCOUNT,
x_opencti_main_observable_type: ENTITY_FINANCIAL_ACCOUNT,
},
};

Expand Down

0 comments on commit bcb42b9

Please sign in to comment.