From 782f99453924439eccf043587be03d674fc495aa Mon Sep 17 00:00:00 2001 From: marie flores Date: Thu, 11 Jan 2024 19:23:07 +0100 Subject: [PATCH] [backend] improve schema and domain methods (#4903) --- .../src/schema/relay.schema.graphql | 15 +++++- .../opencti-graphql/src/config/conf.js | 6 +++ .../opencti-graphql/src/generated/graphql.ts | 42 +++++++++++++-- .../publicDashboard-converter.ts | 3 +- .../publicDashboard/publicDashboard-domain.ts | 54 +++++++++++++++++-- .../publicDashboard-resolvers.ts | 13 +++-- .../publicDashboard/publicDashboard-types.ts | 9 ++-- .../publicDashboard/publicDashboard.graphql | 33 ++++++++---- .../publicDashboard/publicDashboard.ts | 3 +- 9 files changed, 148 insertions(+), 30 deletions(-) diff --git a/opencti-platform/opencti-front/src/schema/relay.schema.graphql b/opencti-platform/opencti-front/src/schema/relay.schema.graphql index d82f3a0c6c67..7c3b13540ae7 100644 --- a/opencti-platform/opencti-front/src/schema/relay.schema.graphql +++ b/opencti-platform/opencti-front/src/schema/relay.schema.graphql @@ -7279,6 +7279,7 @@ type Query { csvMappers(first: Int, after: ID, orderBy: CsvMapperOrdering, orderMode: OrderingMode, filters: FilterGroup, search: String): CsvMapperConnection csvMapperTest(configuration: String!, content: String!): CsvMapperTestResult publicDashboard(id: String!): PublicDashboard + publicDashboardPublic(uri_key: String!): PublicDashboard } type Subscription { @@ -8022,7 +8023,9 @@ type Mutation { csvMapperAdd(input: CsvMapperAddInput!): CsvMapper csvMapperDelete(id: ID!): ID csvMapperFieldPatch(id: ID!, input: [EditInput!]!): CsvMapper - publicDashboardAdd(dashboard_id: String!): PublicDashboard + publicDashboardAdd(input: PublicDashboardAddInput!): PublicDashboard + publicDashboardDelete(id: ID!): ID + publicDashboardFieldPatch(id: ID!, input: [EditInput!]!): PublicDashboard } type Channel implements BasicObject & StixObject & StixCoreObject & StixDomainObject { @@ -10884,9 +10887,17 @@ type PublicDashboard implements InternalObject & BasicObject { dashboard_id: String user_id: String! public_manifest: String - uri_keys: [String] + private_manifest: String + published_until: DateTime + uri_key: String max_marking: [MarkingDefinition!] created_at: DateTime updated_at: DateTime editContext: [EditUserContext!] } + +input PublicDashboardAddInput { + name: String! + description: String + dashboard_id: String! +} diff --git a/opencti-platform/opencti-graphql/src/config/conf.js b/opencti-platform/opencti-graphql/src/config/conf.js index 871ac9676cb7..323104a885f8 100644 --- a/opencti-platform/opencti-graphql/src/config/conf.js +++ b/opencti-platform/opencti-graphql/src/config/conf.js @@ -29,6 +29,7 @@ import { ENTITY_TYPE_MANAGER_CONFIGURATION } from '../modules/managerConfigurati import { ENTITY_TYPE_WORKSPACE } from '../modules/workspace/workspace-types'; import { ENTITY_TYPE_NOTIFIER } from '../modules/notifier/notifier-types'; import { UnknownError, UnsupportedError } from './errors'; +import { ENTITY_TYPE_PUBLIC_DASHBOARD } from '../modules/publicDashboard/publicDashboard-types'; // https://golang.org/src/crypto/x509/root_linux.go const LINUX_CERTFILES = [ @@ -72,6 +73,11 @@ export const BUS_TOPICS = { EDIT_TOPIC: `${TOPIC_PREFIX}WORKSPACE_EDIT_TOPIC`, ADDED_TOPIC: `${TOPIC_PREFIX}WORKSPACE_ADDED_TOPIC`, }, + [ENTITY_TYPE_PUBLIC_DASHBOARD]: { + EDIT_TOPIC: `${TOPIC_PREFIX}PUBLIC_DASHBOARD_EDIT_TOPIC`, + ADDED_TOPIC: `${TOPIC_PREFIX}PUBLIC_DASHBOARD_ADDED_TOPIC`, + DELETE_TOPIC: `${TOPIC_PREFIX}PUBLIC_DASHBOARD_DELETE_TOPIC`, + }, [M.ENTITY_TYPE_LABEL]: { EDIT_TOPIC: `${TOPIC_PREFIX}LABEL_EDIT_TOPIC`, ADDED_TOPIC: `${TOPIC_PREFIX}LABEL_ADDED_TOPIC`, diff --git a/opencti-platform/opencti-graphql/src/generated/graphql.ts b/opencti-platform/opencti-graphql/src/generated/graphql.ts index 3ca5a736dc57..2d61ddcc92dc 100644 --- a/opencti-platform/opencti-graphql/src/generated/graphql.ts +++ b/opencti-platform/opencti-graphql/src/generated/graphql.ts @@ -12049,6 +12049,8 @@ export type Mutation = { positionAdd?: Maybe; positionEdit?: Maybe; publicDashboardAdd?: Maybe; + publicDashboardDelete?: Maybe; + publicDashboardFieldPatch?: Maybe; queryTaskAdd: BackgroundTask; regionAdd?: Maybe; regionEdit?: Maybe; @@ -13224,7 +13226,18 @@ export type MutationPositionEditArgs = { export type MutationPublicDashboardAddArgs = { - dashboard_id: Scalars['String']['input']; + input: PublicDashboardAddInput; +}; + + +export type MutationPublicDashboardDeleteArgs = { + id: Scalars['ID']['input']; +}; + + +export type MutationPublicDashboardFieldPatchArgs = { + id: Scalars['ID']['input']; + input: Array; }; @@ -16885,13 +16898,21 @@ export type PublicDashboard = BasicObject & InternalObject & { max_marking?: Maybe>; name: Scalars['String']['output']; parent_types: Array; + private_manifest?: Maybe; public_manifest?: Maybe; + published_until?: Maybe; standard_id: Scalars['String']['output']; updated_at?: Maybe; - uri_keys?: Maybe>>; + uri_key?: Maybe; user_id: Scalars['String']['output']; }; +export type PublicDashboardAddInput = { + dashboard_id: Scalars['String']['input']; + description?: InputMaybe; + name: Scalars['String']['input']; +}; + export type Query = { __typename?: 'Query'; about?: Maybe; @@ -17061,6 +17082,7 @@ export type Query = { position?: Maybe; positions?: Maybe; publicDashboard?: Maybe; + publicDashboardPublic?: Maybe; publicStixCoreObjectsMultiTimeSeries?: Maybe>>; rabbitMQMetrics?: Maybe; region?: Maybe; @@ -18362,6 +18384,11 @@ export type QueryPublicDashboardArgs = { }; +export type QueryPublicDashboardPublicArgs = { + uri_key: Scalars['String']['input']; +}; + + export type QueryPublicStixCoreObjectsMultiTimeSeriesArgs = { dashboardId: Scalars['String']['input']; endDate?: InputMaybe; @@ -27640,6 +27667,7 @@ export type ResolversTypes = ResolversObject<{ ProcessAddInput: ProcessAddInput; Provider: ResolverTypeWrapper; PublicDashboard: ResolverTypeWrapper; + PublicDashboardAddInput: PublicDashboardAddInput; Query: ResolverTypeWrapper<{}>; QueryTask: ResolverTypeWrapper; QueryTaskAddInput: QueryTaskAddInput; @@ -28316,6 +28344,7 @@ export type ResolversParentTypes = ResolversObject<{ ProcessAddInput: ProcessAddInput; Provider: Provider; PublicDashboard: BasicStoreEntityPublicDashboard; + PublicDashboardAddInput: PublicDashboardAddInput; Query: {}; QueryTask: QueryTask; QueryTaskAddInput: QueryTaskAddInput; @@ -32690,7 +32719,9 @@ export type MutationResolvers, ParentType, ContextType, RequireFields>; positionAdd?: Resolver, ParentType, ContextType, RequireFields>; positionEdit?: Resolver, ParentType, ContextType, RequireFields>; - publicDashboardAdd?: Resolver, ParentType, ContextType, RequireFields>; + publicDashboardAdd?: Resolver, ParentType, ContextType, RequireFields>; + publicDashboardDelete?: Resolver, ParentType, ContextType, RequireFields>; + publicDashboardFieldPatch?: Resolver, ParentType, ContextType, RequireFields>; queryTaskAdd?: Resolver>; regionAdd?: Resolver, ParentType, ContextType, RequireFields>; regionEdit?: Resolver, ParentType, ContextType, RequireFields>; @@ -33773,10 +33804,12 @@ export type PublicDashboardResolvers>, ParentType, ContextType>; name?: Resolver; parent_types?: Resolver, ParentType, ContextType>; + private_manifest?: Resolver, ParentType, ContextType>; public_manifest?: Resolver, ParentType, ContextType>; + published_until?: Resolver, ParentType, ContextType>; standard_id?: Resolver; updated_at?: Resolver, ParentType, ContextType>; - uri_keys?: Resolver>>, ParentType, ContextType>; + uri_key?: Resolver, ParentType, ContextType>; user_id?: Resolver; __isTypeOf?: IsTypeOfResolverFn; }>; @@ -33949,6 +33982,7 @@ export type QueryResolvers, ParentType, ContextType, RequireFields>; positions?: Resolver, ParentType, ContextType, Partial>; publicDashboard?: Resolver, ParentType, ContextType, RequireFields>; + publicDashboardPublic?: Resolver, ParentType, ContextType, RequireFields>; publicStixCoreObjectsMultiTimeSeries?: Resolver>>, ParentType, ContextType, RequireFields>; rabbitMQMetrics?: Resolver, ParentType, ContextType, Partial>; region?: Resolver, ParentType, ContextType, RequireFields>; diff --git a/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-converter.ts b/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-converter.ts index bba0ab36e08d..a8faa3d43030 100644 --- a/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-converter.ts +++ b/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-converter.ts @@ -11,7 +11,8 @@ const convertPublicDashboardToStix = (instance: StoreEntityPublicDashboard): Sti dashboard_id: instance.dashboard_id, user_id: instance.user_id, public_manifest: instance.public_manifest, - uri_keys: instance.uri_keys, + private_manifest: instance.private_manifest, + uri_key: instance.uri_key, aliases: instance.x_opencti_aliases ?? [], extensions: { [STIX_EXT_OCTI]: cleanObject({ diff --git a/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-domain.ts b/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-domain.ts index 51431a508420..adf3978937b9 100644 --- a/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-domain.ts +++ b/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-domain.ts @@ -1,11 +1,13 @@ +import { v4 as uuidv4 } from 'uuid'; import type { AuthContext, AuthUser } from '../../types/user'; import { storeLoadById } from '../../database/middleware-loader'; import { ENTITY_TYPE_PUBLIC_DASHBOARD, type BasicStoreEntityPublicDashboard } from './publicDashboard-types'; -import { createEntity } from '../../database/middleware'; +import { createEntity, deleteElementById, updateAttribute } from '../../database/middleware'; import { type BasicStoreEntityWorkspace, ENTITY_TYPE_WORKSPACE } from '../workspace/workspace-types'; import { fromBase64, toBase64 } from '../../database/utils'; import { notify } from '../../database/redis'; import { BUS_TOPICS } from '../../config/conf'; +import type { EditInput, PublicDashboardAddInput } from '../../generated/graphql'; export const findById = ( context: AuthContext, @@ -20,16 +22,24 @@ export const findById = ( ); }; +export const publicDashboardPublic = async ( + context: AuthContext, + user: AuthUser, + uri_key: string, +) => { + return await storeLoadById(context, user, uri_key, ENTITY_TYPE_PUBLIC_DASHBOARD) as unknown as BasicStoreEntityPublicDashboard; +}; + export const addPublicDashboard = async ( context: AuthContext, user: AuthUser, - dashboard_id: string, + input: PublicDashboardAddInput, ) => { // Get private dashboard manifest const dashboard: BasicStoreEntityWorkspace = await storeLoadById( context, user, - dashboard_id, + input.dashboard_id, ENTITY_TYPE_WORKSPACE, ); const parsedManifest = JSON.parse(fromBase64(dashboard.manifest) ?? '{}'); @@ -41,14 +51,48 @@ export const addPublicDashboard = async ( // Create public manifest const publicManifest = toBase64(JSON.stringify(parsedManifest)); - const publicDashboardToCreate = { public_manifest: publicManifest }; // Create publicDashboard + const publicDashboardToCreate = { // TODO add marking max + name: input.dashboard_id, + description: input.description, + public_manifest: publicManifest, + private_manifest: dashboard.manifest, + user_id: user.id, + uri_key: uuidv4(), + }; + const created = await createEntity( context, user, publicDashboardToCreate, ENTITY_TYPE_PUBLIC_DASHBOARD, ); - return notify(BUS_TOPICS[ENTITY_TYPE_WORKSPACE].ADDED_TOPIC, created, user); + return notify(BUS_TOPICS[ENTITY_TYPE_PUBLIC_DASHBOARD].ADDED_TOPIC, created, user); +}; + +export const publicDashboardEditField = async ( + context: AuthContext, + user: AuthUser, + id: string, + input: EditInput[], +) => { + const { element } = await updateAttribute( + context, + user, + id, + ENTITY_TYPE_PUBLIC_DASHBOARD, + input, + ); + return notify(BUS_TOPICS[ENTITY_TYPE_PUBLIC_DASHBOARD].EDIT_TOPIC, element, user); +}; + +export const publicDashboardDelete = async (context: AuthContext, user: AuthUser, id: string) => { + const element = await deleteElementById( + context, + user, + id, + ENTITY_TYPE_PUBLIC_DASHBOARD + ); + return notify(BUS_TOPICS[ENTITY_TYPE_PUBLIC_DASHBOARD].DELETE_TOPIC, element, user).then(() => id); }; diff --git a/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-resolvers.ts b/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-resolvers.ts index 24e9ab4e3612..fdf5776a8ddd 100644 --- a/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-resolvers.ts +++ b/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-resolvers.ts @@ -1,13 +1,20 @@ import type { Resolvers } from '../../generated/graphql'; -import { addPublicDashboard, findById } from './publicDashboard-domain'; +import { addPublicDashboard, findById, publicDashboardDelete, publicDashboardEditField, publicDashboardPublic } from './publicDashboard-domain'; const publicDashboardResolvers: Resolvers = { Query: { publicDashboard: (_, { id }, context) => findById(context, context.user, id), + publicDashboardPublic: (_, { uri_key }, context) => publicDashboardPublic(context, context.user, uri_key), }, Mutation: { - publicDashboardAdd: (_, { dashboard_id }, context) => { - return addPublicDashboard(context, context.user, dashboard_id); + publicDashboardAdd: (_, { input }, context) => { + return addPublicDashboard(context, context.user, input); + }, + publicDashboardDelete: (_, { id }, context) => { + return publicDashboardDelete(context, context.user, id); + }, + publicDashboardFieldPatch: (_, { id, input }, context) => { + return publicDashboardEditField(context, context.user, id, input); }, }, }; diff --git a/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-types.ts b/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-types.ts index 5c0e92c33005..1206ec7b6523 100644 --- a/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-types.ts +++ b/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard-types.ts @@ -11,7 +11,8 @@ export interface BasicStoreEntityPublicDashboard extends BasicStoreEntity { dashboard_id: string; user_id: string; public_manifest: string; - uri_keys: Array; + private_manifest: string; + uri_key: string; } export interface StoreEntityPublicDashboard extends StoreEntity { @@ -20,7 +21,8 @@ export interface StoreEntityPublicDashboard extends StoreEntity { dashboard_id: string; user_id: string; public_manifest: string; - uri_keys: Array; + private_manifest: string; + uri_key: string; } // endregion @@ -31,7 +33,8 @@ export interface StixPublicDashboard extends StixDomainObject { dashboard_id: string; user_id: string; public_manifest: string; - uri_keys: Array; + private_manifest: string; + uri_key: string; aliases: Array; extensions: { [STIX_EXT_OCTI] : StixOpenctiExtensionSDO diff --git a/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard.graphql b/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard.graphql index b54fc4e8b29c..e580af0c2a7f 100644 --- a/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard.graphql +++ b/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard.graphql @@ -1,28 +1,39 @@ type PublicDashboard implements InternalObject & BasicObject { - id: ID! - entity_type: String! - standard_id: String! - parent_types: [String!]! + id: ID! @auth(for: [KNOWLEDGE, EXPLORE]) + entity_type: String! @auth(for: [KNOWLEDGE, EXPLORE]) + standard_id: String! @auth(for: [KNOWLEDGE, EXPLORE]) + parent_types: [String!]! @auth(for: [KNOWLEDGE, EXPLORE]) # PublicDashboard name: String! description: String - dashboard_id: String - user_id: String! + dashboard_id: String @auth(for: [KNOWLEDGE, EXPLORE]) + user_id: String! @auth(for: [KNOWLEDGE, EXPLORE]) public_manifest: String - uri_keys: [String] - max_marking: [MarkingDefinition!] + private_manifest: String @auth(for: [KNOWLEDGE, EXPLORE]) + published_until: DateTime + uri_key: String @auth(for: [KNOWLEDGE, EXPLORE]) + max_marking: [MarkingDefinition!] @auth(for: [KNOWLEDGE, EXPLORE]) created_at: DateTime updated_at: DateTime - editContext: [EditUserContext!] + editContext: [EditUserContext!] @auth(for: [KNOWLEDGE, EXPLORE]) } # Queries type Query { - publicDashboard(id: String!): PublicDashboard @auth + publicDashboard(id: String!): PublicDashboard @auth(for: [KNOWLEDGE, EXPLORE]) + publicDashboardPublic(uri_key: String!): PublicDashboard } # Mutation +input PublicDashboardAddInput { + name: String! + description: String + dashboard_id: String! +} + type Mutation { - publicDashboardAdd(dashboard_id: String!): PublicDashboard @auth(for: [KNOWLEDGE, EXPLORE]) + publicDashboardAdd(input: PublicDashboardAddInput!): PublicDashboard @auth(for: [EXPLORE_EXUPDATE_PUBLISH]) + publicDashboardDelete(id: ID!): ID @auth(for: [EXPLORE_EXUPDATE_PUBLISH]) + publicDashboardFieldPatch(id: ID!, input: [EditInput!]!): PublicDashboard @auth(for: [EXPLORE_EXUPDATE_PUBLISH]) } diff --git a/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard.ts b/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard.ts index f2ff9767aff4..94bbef2afd45 100644 --- a/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard.ts +++ b/opencti-platform/opencti-graphql/src/modules/publicDashboard/publicDashboard.ts @@ -22,7 +22,8 @@ export const PUBLIC_DASHBOARD_DEFINITION: ModuleDefinition {