Skip to content

Commit

Permalink
[backend/frontend] API types ref mapping completed (OpenCTI-Platform#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lndrtrbn authored and daimyo007 committed Jun 4, 2024
1 parent f886946 commit 4d29ffe
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 63 deletions.
5 changes: 4 additions & 1 deletion opencti-platform/opencti-front/src/private/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ const rootPrivateQuery = graphql`
}
schemaRelationsRefTypesMapping {
key
values
values {
name
toTypes
}
}
filterKeysSchema {
entity_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,15 @@ const InvestigationExpandFormContent = ({
});
}
});

// relations refs involving the user are not expandable
const relationRefsWithUser = (schema.schemaRelationsRefTypesMapping.get('*_User') ?? []).map((ref) => ref.toLowerCase());
const relationRefsWithUser = new Set(
Array.from(schema.schemaRelationsRefTypesMapping.values())
.flat()
.filter((ref) => ref.toTypes.includes('User'))
.map((ref) => ref.name.toLowerCase()),
);

const nonNullDistribution = (
distributionRel.stixRelationshipsDistribution ?? []
)
Expand All @@ -279,7 +286,7 @@ const InvestigationExpandFormContent = ({
]
: []))
// Remove from the list relations with nothing to add and relations ref involving the user
.filter(({ label, value }) => value > 0 && !relationRefsWithUser?.includes(label.replace('-', '')))
.filter(({ label, value }) => value > 0 && !relationRefsWithUser?.has(label.replace('-', '')))
.sort((a, b) => (b.value ?? 0) - (a.value ?? 0));
setRelationships(
nonNullDistribution.map(({ label, value }) => ({
Expand Down
12 changes: 11 additions & 1 deletion opencti-platform/opencti-front/src/schema/relay.schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7012,6 +7012,16 @@ type StixRelationshipSchema {
values: [String!]!
}

type StixRelationshipRefSchemaValue {
name: String!
toTypes: [String!]!
}

type StixRelationshipRefSchema {
key: String!
values: [StixRelationshipRefSchemaValue!]!
}

enum StixCoreRelationshipsOrdering {
entity_type
relationship_type
Expand Down Expand Up @@ -7497,7 +7507,7 @@ type Query {
stixRelationshipsDistribution(field: String!, operation: StatsOperation!, startDate: DateTime, endDate: DateTime, dateAttribute: String, isTo: Boolean, limit: Int, order: String, fromOrToId: [String], elementWithTargetTypes: [String], fromId: [String], fromRole: String, fromTypes: [String], toId: [String], toRole: String, toTypes: [String], relationship_type: [String], confidences: [Int], search: String, filters: FilterGroup, dynamicFrom: FilterGroup, dynamicTo: FilterGroup, aggregateOnConnections: Boolean): [Distribution]
stixRelationshipsNumber(dateAttribute: String, authorId: String, noDirection: Boolean, endDate: DateTime, onlyInferred: Boolean, fromOrToId: [String], elementWithTargetTypes: [String], fromId: [String], fromRole: String, fromTypes: [String], toId: [String], toRole: String, toTypes: [String], relationship_type: [String], confidences: [Int], search: String, filters: FilterGroup, dynamicFrom: FilterGroup, dynamicTo: FilterGroup): Number
schemaRelationsTypesMapping: [StixRelationshipSchema!]!
schemaRelationsRefTypesMapping: [StixRelationshipSchema!]!
schemaRelationsRefTypesMapping: [StixRelationshipRefSchema!]!
filterKeysSchema: [FilterKeysSchema!]!
stixCoreRelationship(id: String): StixCoreRelationship
stixCoreRelationships(first: Int, after: ID, orderBy: StixCoreRelationshipsOrdering, orderMode: OrderingMode, fromOrToId: [String], elementWithTargetTypes: [String], fromId: [String], fromRole: String, fromTypes: [String], toId: [String], toRole: String, toTypes: [String], relationship_type: [String], startTimeStart: DateTime, startTimeStop: DateTime, stopTimeStart: DateTime, stopTimeStop: DateTime, firstSeenStart: DateTime, firstSeenStop: DateTime, lastSeenStart: DateTime, lastSeenStop: DateTime, startDate: DateTime, endDate: DateTime, confidences: [Int], search: String, filters: FilterGroup, stix: Boolean): StixCoreRelationshipConnection
Expand Down
16 changes: 5 additions & 11 deletions opencti-platform/opencti-front/src/utils/Relation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,12 @@ export const resolveTypesForRelationship = (
};

export const resolveTypesForRelationshipRef = (
schemaRelationsTypesMapping: Map<string, readonly string[]>,
schemaRelationsTypesMapping: Map<string, readonly { readonly name: string, readonly toTypes: readonly string[] }[]>,
entityType: string,
relationshipRefKey: string,
) => {
const types: string[] = [];
schemaRelationsTypesMapping.forEach((values, key) => {
if (values.includes(relationshipRefKey)) {
const [from, to] = key.split('_');
if (from.includes(entityType) || from === '*') {
types.push(to);
}
}
});
return uniq(types);
return schemaRelationsTypesMapping
.get(entityType)
?.find((ref) => ref.name === relationshipRefKey)
?.toTypes ?? [];
};
2 changes: 1 addition & 1 deletion opencti-platform/opencti-front/src/utils/hooks/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface UserContextType {
smos: { id: string, label: string }[]
scrs: { id: string, label: string }[]
schemaRelationsTypesMapping: Map<string, readonly string[]>
schemaRelationsRefTypesMapping: Map<string, readonly string[]>
schemaRelationsRefTypesMapping: Map<string, readonly { readonly name: string, readonly toTypes: readonly string[] }[]>
filterKeysSchema: Map<string, Map<string, FilterDefinition>>
} | undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Filters utils', () => {
smos: [{ id: '', label: '' }],
scrs: [{ id: '', label: '' }],
schemaRelationsTypesMapping: new Map<string, readonly string[]>(),
schemaRelationsRefTypesMapping: new Map<string, readonly string[]>(),
schemaRelationsRefTypesMapping: new Map<string, readonly { name: string, toTypes: string[] }[]>(),
filterKeysSchema,
},
})
Expand Down
11 changes: 10 additions & 1 deletion opencti-platform/opencti-graphql/config/schema/opencti.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10762,6 +10762,15 @@ type StixRelationshipSchema {
values: [String!]!
}

type StixRelationshipRefSchemaValue {
name: String!
toTypes: [String!]!
}
type StixRelationshipRefSchema {
key: String!
values: [StixRelationshipRefSchemaValue!]!
}

############## StixCoreRelationships
enum StixCoreRelationshipsOrdering {
entity_type
Expand Down Expand Up @@ -12016,7 +12025,7 @@ type Query {
dynamicTo: FilterGroup
): Number @auth(for: [KNOWLEDGE, EXPLORE])
schemaRelationsTypesMapping: [StixRelationshipSchema!]! @auth
schemaRelationsRefTypesMapping: [StixRelationshipSchema!]! @auth
schemaRelationsRefTypesMapping: [StixRelationshipRefSchema!]! @auth
filterKeysSchema: [FilterKeysSchema!]! @auth

######## STIX CORE RELATIONSHIPS
Expand Down
53 changes: 10 additions & 43 deletions opencti-platform/opencti-graphql/src/database/stix-ref.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@
import type { RelationshipMappings } from './stix';
import { REL_NEW } from './stix';
import {
ENTITY_TYPE_CONTAINER,
ENTITY_TYPE_IDENTITY,
INPUT_ASSIGNEE,
INPUT_CREATED_BY,
INPUT_EXTERNAL_REFS,
INPUT_KILLCHAIN,
INPUT_LABELS,
INPUT_MARKINGS,
INPUT_OBJECTS,
INPUT_PARTICIPANT
} from '../schema/general';
import { ENTITY_TYPE_EXTERNAL_REFERENCE, ENTITY_TYPE_KILL_CHAIN_PHASE, ENTITY_TYPE_LABEL, ENTITY_TYPE_MARKING_DEFINITION } from '../schema/stixMetaObject';
import { ENTITY_TYPE_USER } from '../schema/internalObject';
import { schemaTypesMapping } from '../domain/stixRelationship';

export const stixRefRelationshipsMapping: RelationshipMappings = {
[`*_${ENTITY_TYPE_IDENTITY}`]: [
{ name: INPUT_CREATED_BY, type: REL_NEW }
],
[`*_${ENTITY_TYPE_MARKING_DEFINITION}`]: [
{ name: INPUT_MARKINGS, type: REL_NEW }
],
[`*_${ENTITY_TYPE_CONTAINER}`]: [
{ name: INPUT_OBJECTS, type: REL_NEW }
],
[`*_${ENTITY_TYPE_USER}`]: [
{ name: INPUT_ASSIGNEE, type: REL_NEW },
{ name: INPUT_PARTICIPANT, type: REL_NEW }
],
[`*_${ENTITY_TYPE_LABEL}`]: [
{ name: INPUT_LABELS, type: REL_NEW }
],
[`*_${ENTITY_TYPE_EXTERNAL_REFERENCE}`]: [
{ name: INPUT_EXTERNAL_REFS, type: REL_NEW }
],
[`*_${ENTITY_TYPE_KILL_CHAIN_PHASE}`]: [
{ name: INPUT_KILLCHAIN, type: REL_NEW }
],
};
import { schemaRelationsRefDefinition } from '../schema/schema-relationsRef';

export const schemaRelationsRefTypesMapping = () => {
return schemaTypesMapping(stixRefRelationshipsMapping);
return Array.from(schemaRelationsRefDefinition.relationsRefCacheArray.entries()).map(([key, refs]) => {
return {
key,
values: refs.map((ref) => ({
name: ref.name,
toTypes: ref.toTypes
}))
};
});
};
34 changes: 32 additions & 2 deletions opencti-platform/opencti-graphql/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17982,7 +17982,7 @@ export type Query = {
rules?: Maybe<Array<Maybe<Rule>>>;
runtimeAttributes?: Maybe<AttributeConnection>;
schemaAttributeNames?: Maybe<AttributeConnection>;
schemaRelationsRefTypesMapping: Array<StixRelationshipSchema>;
schemaRelationsRefTypesMapping: Array<StixRelationshipRefSchema>;
schemaRelationsTypesMapping: Array<StixRelationshipSchema>;
sector?: Maybe<Sector>;
sectors?: Maybe<SectorConnection>;
Expand Down Expand Up @@ -23836,6 +23836,18 @@ export type StixRelationshipEditMutations = {
delete?: Maybe<Scalars['ID']['output']>;
};

export type StixRelationshipRefSchema = {
__typename?: 'StixRelationshipRefSchema';
key: Scalars['String']['output'];
values: Array<StixRelationshipRefSchemaValue>;
};

export type StixRelationshipRefSchemaValue = {
__typename?: 'StixRelationshipRefSchemaValue';
name: Scalars['String']['output'];
toTypes: Array<Scalars['String']['output']>;
};

export type StixRelationshipSchema = {
__typename?: 'StixRelationshipSchema';
key: Scalars['String']['output'];
Expand Down Expand Up @@ -29285,6 +29297,8 @@ export type ResolversTypes = ResolversObject<{
StixRelationshipConnection: ResolverTypeWrapper<Omit<StixRelationshipConnection, 'edges'> & { edges?: Maybe<Array<Maybe<ResolversTypes['StixRelationshipEdge']>>> }>;
StixRelationshipEdge: ResolverTypeWrapper<Omit<StixRelationshipEdge, 'node'> & { node: ResolversTypes['StixRelationship'] }>;
StixRelationshipEditMutations: ResolverTypeWrapper<StixRelationshipEditMutations>;
StixRelationshipRefSchema: ResolverTypeWrapper<StixRelationshipRefSchema>;
StixRelationshipRefSchemaValue: ResolverTypeWrapper<StixRelationshipRefSchemaValue>;
StixRelationshipSchema: ResolverTypeWrapper<StixRelationshipSchema>;
StixRelationshipsOrdering: StixRelationshipsOrdering;
StixRelationshipsTimeSeriesParameters: StixRelationshipsTimeSeriesParameters;
Expand Down Expand Up @@ -30001,6 +30015,8 @@ export type ResolversParentTypes = ResolversObject<{
StixRelationshipConnection: Omit<StixRelationshipConnection, 'edges'> & { edges?: Maybe<Array<Maybe<ResolversParentTypes['StixRelationshipEdge']>>> };
StixRelationshipEdge: Omit<StixRelationshipEdge, 'node'> & { node: ResolversParentTypes['StixRelationship'] };
StixRelationshipEditMutations: StixRelationshipEditMutations;
StixRelationshipRefSchema: StixRelationshipRefSchema;
StixRelationshipRefSchemaValue: StixRelationshipRefSchemaValue;
StixRelationshipSchema: StixRelationshipSchema;
StixRelationshipsTimeSeriesParameters: StixRelationshipsTimeSeriesParameters;
StixSightingRelationship: Omit<StixSightingRelationship, 'cases' | 'containers' | 'createdBy' | 'from' | 'groupings' | 'notes' | 'objectOrganization' | 'opinions' | 'reports' | 'to'> & { cases?: Maybe<ResolversParentTypes['CaseConnection']>, containers?: Maybe<ResolversParentTypes['ContainerConnection']>, createdBy?: Maybe<ResolversParentTypes['Identity']>, from?: Maybe<ResolversParentTypes['StixObjectOrStixRelationshipOrCreator']>, groupings?: Maybe<ResolversParentTypes['GroupingConnection']>, notes?: Maybe<ResolversParentTypes['NoteConnection']>, objectOrganization?: Maybe<Array<ResolversParentTypes['Organization']>>, opinions?: Maybe<ResolversParentTypes['OpinionConnection']>, reports?: Maybe<ResolversParentTypes['ReportConnection']>, to?: Maybe<ResolversParentTypes['StixObjectOrStixRelationshipOrCreator']> };
Expand Down Expand Up @@ -35926,7 +35942,7 @@ export type QueryResolvers<ContextType = any, ParentType extends ResolversParent
rules?: Resolver<Maybe<Array<Maybe<ResolversTypes['Rule']>>>, ParentType, ContextType>;
runtimeAttributes?: Resolver<Maybe<ResolversTypes['AttributeConnection']>, ParentType, ContextType, RequireFields<QueryRuntimeAttributesArgs, 'attributeName'>>;
schemaAttributeNames?: Resolver<Maybe<ResolversTypes['AttributeConnection']>, ParentType, ContextType, RequireFields<QuerySchemaAttributeNamesArgs, 'elementType'>>;
schemaRelationsRefTypesMapping?: Resolver<Array<ResolversTypes['StixRelationshipSchema']>, ParentType, ContextType>;
schemaRelationsRefTypesMapping?: Resolver<Array<ResolversTypes['StixRelationshipRefSchema']>, ParentType, ContextType>;
schemaRelationsTypesMapping?: Resolver<Array<ResolversTypes['StixRelationshipSchema']>, ParentType, ContextType>;
sector?: Resolver<Maybe<ResolversTypes['Sector']>, ParentType, ContextType, Partial<QuerySectorArgs>>;
sectors?: Resolver<Maybe<ResolversTypes['SectorConnection']>, ParentType, ContextType, Partial<QuerySectorsArgs>>;
Expand Down Expand Up @@ -37264,6 +37280,18 @@ export type StixRelationshipEditMutationsResolvers<ContextType = any, ParentType
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

export type StixRelationshipRefSchemaResolvers<ContextType = any, ParentType extends ResolversParentTypes['StixRelationshipRefSchema'] = ResolversParentTypes['StixRelationshipRefSchema']> = ResolversObject<{
key?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
values?: Resolver<Array<ResolversTypes['StixRelationshipRefSchemaValue']>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

export type StixRelationshipRefSchemaValueResolvers<ContextType = any, ParentType extends ResolversParentTypes['StixRelationshipRefSchemaValue'] = ResolversParentTypes['StixRelationshipRefSchemaValue']> = ResolversObject<{
name?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
toTypes?: Resolver<Array<ResolversTypes['String']>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;

export type StixRelationshipSchemaResolvers<ContextType = any, ParentType extends ResolversParentTypes['StixRelationshipSchema'] = ResolversParentTypes['StixRelationshipSchema']> = ResolversObject<{
key?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
values?: Resolver<Array<ResolversTypes['String']>, ParentType, ContextType>;
Expand Down Expand Up @@ -39221,6 +39249,8 @@ export type Resolvers<ContextType = any> = ResolversObject<{
StixRelationshipConnection?: StixRelationshipConnectionResolvers<ContextType>;
StixRelationshipEdge?: StixRelationshipEdgeResolvers<ContextType>;
StixRelationshipEditMutations?: StixRelationshipEditMutationsResolvers<ContextType>;
StixRelationshipRefSchema?: StixRelationshipRefSchemaResolvers<ContextType>;
StixRelationshipRefSchemaValue?: StixRelationshipRefSchemaValueResolvers<ContextType>;
StixRelationshipSchema?: StixRelationshipSchemaResolvers<ContextType>;
StixSightingRelationship?: StixSightingRelationshipResolvers<ContextType>;
StixSightingRelationshipConnection?: StixSightingRelationshipConnectionResolvers<ContextType>;
Expand Down

0 comments on commit 4d29ffe

Please sign in to comment.