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 cyclic relation bug in tasks and notes #4920

Merged
merged 8 commits into from
Nov 28, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import Alert from '@mui/material/Alert';
import FormGroup from '@mui/material/FormGroup';
import FormControlLabel from '@mui/material/FormControlLabel';
import Checkbox from '@mui/material/Checkbox';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface OrganizationForm {

// endregion

const useStyles = makeStyles<Theme>((theme) => ({
const useStyles = makeStyles<Theme>(() => ({
organization: {
margin: '0 7px 0 0',
float: 'left',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import ListItem from '@mui/material/ListItem';
import ListItemIcon from '@mui/material/ListItemIcon';
import { VpnKeyOutlined } from '@mui/icons-material';
import ListItemText from '@mui/material/ListItemText';
import Chip from '@mui/material/Chip';
import EEChip from '@components/common/entreprise_edition/EEChip';
import EETooltip from '@components/common/entreprise_edition/EETooltip';
import AccessesMenu from './AccessesMenu';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ListItem from '@mui/material/ListItem';
import ListItemText from '@mui/material/ListItemText';
import { makeStyles } from '@mui/styles';
import Switch from '@mui/material/Switch';
import EEChip from '@components/common/entreprise_edition/EEChip';
import EEChip from '../common/entreprise_edition/EEChip';
import EnterpriseEditionButton from '../common/entreprise_edition/EnterpriseEditionButton';
import { SubscriptionFocus } from '../../../components/Subscription';
import { commitMutation, QueryRenderer } from '../../../relay/environment';
Expand Down
23 changes: 20 additions & 3 deletions opencti-platform/opencti-graphql/src/domain/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import * as R from 'ramda';
import { v4 as uuidv4 } from 'uuid';
import { RELATION_OBJECT } from '../schema/stixRefRelationship';
import { listAllThings, listThings } from '../database/middleware';
import { internalFindByIds, listAllRelations, listEntities, storeLoadById } from '../database/middleware-loader';
import {
internalFindByIds,
listAllRelations,
listEntities,
storeLoadById
} from '../database/middleware-loader';
import {
ABSTRACT_BASIC_RELATIONSHIP,
ABSTRACT_STIX_REF_RELATIONSHIP,
Expand All @@ -13,7 +18,7 @@ import {
import { isStixDomainObjectContainer } from '../schema/stixDomainObject';
import { buildPagination, isInferredIndex, READ_INDEX_STIX_DOMAIN_OBJECTS } from '../database/utils';
import { now } from '../utils/format';
import { elCount } from '../database/engine';
import { elBatchIds, elCount } from '../database/engine';
import { findById as findInvestigationById } from '../modules/workspace/workspace-domain';
import { stixCoreObjectAddRelations } from './stixCoreObject';
import { addFilter } from '../utils/filtering/filtering-utils';
Expand Down Expand Up @@ -148,9 +153,21 @@ export const containersObjectsOfObject = async (context, user, { id, types, filt
return buildPagination(0, null, resolvedObjects.map((r) => ({ node: r })), resolvedObjects.length);
};

export const filterUnwantedEntitiesOut = async ({ context, user, ids }) => {
const filteredOutInvestigatedIds = [];
const entities = await elBatchIds(context, user, ids);
entities?.forEach((entity) => {
if (!['Task', 'Note'].includes(entity.entity_type)) {
Goumies marked this conversation as resolved.
Show resolved Hide resolved
filteredOutInvestigatedIds.push(entity.id);
}
});
return filteredOutInvestigatedIds;
};

export const knowledgeAddFromInvestigation = async (context, user, { containerId, workspaceId }) => {
const investigation = await findInvestigationById(context, user, workspaceId);
const toIds = investigation.investigated_entities_ids.filter((id) => id !== containerId);
const ids = investigation.investigated_entities_ids?.filter((id) => id !== containerId);
const toIds = await filterUnwantedEntitiesOut({ context, user, ids });
const containerInput = { toIds, relationship_type: 'object' };
return await stixCoreObjectAddRelations(context, user, containerId, containerInput);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { BasicStoreEntity, StoreEntity, StoreEntityReport } from '../../typ
import { nowTime } from '../../utils/format';
import { READ_STIX_INDICES } from '../../database/utils';
import { getParentTypes } from '../../schema/schemaUtils';
import { filterUnwantedEntitiesOut } from '../../domain/container';

const buildStixReportForExport = (workspace: BasicStoreEntityWorkspace, investigatedEntities: StoreEntity[]): StixObject => {
const id = generateStandardId(ENTITY_TYPE_CONTAINER_REPORT, { name: workspace.name, published: workspace.created_at }) as StixId;
Expand Down Expand Up @@ -44,6 +45,8 @@ export const toStixReportBundle = async (context: AuthContext, user: AuthUser, w
export const investigationAddFromContainer = async (context: AuthContext, user: AuthUser, containerId: string) => {
const container = await internalLoadById<BasicStoreEntity>(context, user, containerId);
const investigationToStartCanonicalName = `[${container.entity_type}] "${container.name}" (${nowTime()})`;
const investigationInput = { type: 'investigation', name: investigationToStartCanonicalName, investigated_entities_ids: container.object };
const filteredOutInvestigatedIds = await filterUnwantedEntitiesOut({ context, user, ids: container.object });

const investigationInput = { type: 'investigation', name: investigationToStartCanonicalName, investigated_entities_ids: filteredOutInvestigatedIds };
return addWorkspace(context, user, investigationInput);
};