Skip to content

Commit

Permalink
[backend] finish creating decayRule module
Browse files Browse the repository at this point in the history
  • Loading branch information
SouadHadjiat committed Jan 23, 2024
1 parent 85f3454 commit 573f727
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ const IndicatorDetails = createFragmentContainer(IndicatorDetailsComponent, {
decay_pound
decay_points
decay_revoke_score
indicator_types
}
decayLiveDetails {
live_score
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10509,15 +10509,12 @@ type DecayHistory {
score: Int!
}

type DecayRule {
id: String!
type IndicatorDecayRule {
decay_rule_id: String
decay_lifetime: Int!
decay_pound: Float!
decay_points: [Int!]
decay_revoke_score: Int!
indicator_types: [String!]
order: Int!
enabled: Boolean!
}

type DecayLiveDetails {
Expand Down Expand Up @@ -10578,7 +10575,7 @@ type Indicator implements BasicObject & StixObject & StixCoreObject & StixDomain
observables(first: Int): StixCyberObservableConnection
decay_base_score: Int
decay_base_score_date: DateTime
decay_applied_rule: DecayRule
decay_applied_rule: IndicatorDecayRule
decay_history: [DecayHistory!]
decayLiveDetails: DecayLiveDetails
creators: [Creator!]
Expand Down
1 change: 1 addition & 0 deletions opencti-platform/opencti-graphql/graphql-codegen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ generates:
IngestionRss: ../modules/ingestion/ingestion-types#BasicStoreEntityIngestionRss
IngestionTaxii: ../modules/ingestion/ingestion-types#BasicStoreEntityIngestionTaxii
Indicator: ../modules/indicator/indicator-types#BasicStoreEntityIndicator
DecayRule: ../modules/decayRule/decayRule-types#BasicStoreEntityDecayRule
Organization: ../modules/organization/organization-types#BasicStoreEntityOrganization
CsvMapper: ../modules/internal/csvMapper/csvMapper-types#BasicStoreEntityCsvMapper
Playbook: ../modules/playbook/playbook-types#BasicStoreEntityPlaybook
Expand Down
161 changes: 146 additions & 15 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
Expand Up @@ -8,11 +8,11 @@ const convertDecayRuleToStix = (instance: StoreEntityDecayRule): StixDecayRule =
...stixObject,
name: instance.name,
description: instance.description,
decay_rule_lifetime: instance.decay_rule_lifetime,
decay_rule_points: instance.decay_rule_points,
decay_rule_pound: instance.decay_rule_pound,
decay_rule_revoke_score: instance.decay_rule_revoke_score,
decay_rule_observable_types: instance.decay_rule_observable_types,
decay_lifetime: instance.decay_lifetime,
decay_points: instance.decay_points,
decay_pound: instance.decay_pound,
decay_revoke_score: instance.decay_revoke_score,
decay_observable_types: instance.decay_observable_types,
extensions: {
[STIX_EXT_OCTI]: cleanObject({
...stixObject.extensions[STIX_EXT_OCTI],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { AuthContext, AuthUser } from '../../types/user';
import { listEntitiesPaginated, storeLoadById } from '../../database/middleware-loader';
import type { EditInput, QueryDecayRulesArgs, DecayRuleAddInput } from '../../generated/graphql';
import { type BasicStoreEntityDecayRule, ENTITY_TYPE_DECAY_RULE, type StoreEntityDecayRule } from './decayRule-types';
import { createInternalObject, deleteInternalObject, editInternalObject } from '../../domain/internalObject';

export const findById = (context: AuthContext, user: AuthUser, id: string) => {
return storeLoadById<BasicStoreEntityDecayRule>(context, user, id, ENTITY_TYPE_DECAY_RULE);
};

export const findAll = (context: AuthContext, user: AuthUser, args: QueryDecayRulesArgs) => {
return listEntitiesPaginated<BasicStoreEntityDecayRule>(context, user, [ENTITY_TYPE_DECAY_RULE], args);
};

export const addDecayRule = async (context: AuthContext, user: AuthUser, input: DecayRuleAddInput) => {
return createInternalObject<StoreEntityDecayRule>(context, user, input, ENTITY_TYPE_DECAY_RULE);
};

export const fieldPatchDecayRule = async (context: AuthContext, user: AuthUser, id: string, input: EditInput[]) => {
return editInternalObject<StoreEntityDecayRule>(context, user, id, ENTITY_TYPE_DECAY_RULE, input);
};

export const deleteDecayRule = async (context: AuthContext, user: AuthUser, id: string) => {
return deleteInternalObject(context, user, id, ENTITY_TYPE_DECAY_RULE);
};

Check warning on line 25 in opencti-platform/opencti-graphql/src/modules/decayRule/decayRule-domain.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/modules/decayRule/decayRule-domain.ts#L1-L25

Added lines #L1 - L25 were not covered by tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { registerGraphqlSchema } from '../../graphql/schema';
import decayRuleTypeDefs from './decayRule.graphql';
import decayRuleResolvers from './decayRule-resolver';

registerGraphqlSchema({
schema: decayRuleTypeDefs,
resolver: decayRuleResolvers,
});

Check warning on line 8 in opencti-platform/opencti-graphql/src/modules/decayRule/decayRule-graphql.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/modules/decayRule/decayRule-graphql.ts#L1-L8

Added lines #L1 - L8 were not covered by tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Resolvers } from '../../generated/graphql';
import { addDecayRule, deleteDecayRule, fieldPatchDecayRule, findAll, findById } from './decayRule-domain';

const decayRuleResolvers: Resolvers = {
Query: {
decayRule: (_, { id }, context) => findById(context, context.user, id),
decayRules: (_, args, context) => findAll(context, context.user, args),
},
Mutation: {
decayRuleAdd: (_, { input }, context) => {
return addDecayRule(context, context.user, input);
},
decayRuleDelete: (_, { id }, context) => {
return deleteDecayRule(context, context.user, id);
},
decayRuleFieldPatch: (_, { id, input }, context) => {
return fieldPatchDecayRule(context, context.user, id, input);
},
}
};

export default decayRuleResolvers;

Check warning on line 22 in opencti-platform/opencti-graphql/src/modules/decayRule/decayRule-resolver.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/modules/decayRule/decayRule-resolver.ts#L1-L22

Added lines #L1 - L22 were not covered by tests
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@ export const ENTITY_TYPE_DECAY_RULE = 'DecayRule';

export interface BasicStoreEntityDecayRule extends BasicStoreEntity {
name: string
decay_rule_lifetime: number // in days
decay_rule_pound: number // can be changed in other model when feature is ready.
decay_rule_points: number[] // reactions points
decay_rule_revoke_score: number // revoked when score is <= 20
decay_rule_observable_types: string[] // indicator x_opencti_main_observable_type
decay_lifetime: number // in days
decay_pound: number // can be changed in other model when feature is ready.
decay_points: number[] // reactions points
decay_revoke_score: number // revoked when score is <= 20
decay_observable_types: string[] // indicator x_opencti_main_observable_type
order: number // low priority = 0
enabled: boolean
}

export interface StoreEntityDecayRule extends StoreEntity {
name: string
decay_rule_lifetime: number
decay_rule_pound: number
decay_rule_points: number[]
decay_rule_revoke_score: number
decay_rule_observable_types: string[]
decay_lifetime: number
decay_pound: number
decay_points: number[]
decay_revoke_score: number
decay_observable_types: string[]
order: number
enabled: boolean
}

export interface StixDecayRule extends StixObject {
name: string
description: string
decay_rule_lifetime: number
decay_rule_pound: number
decay_rule_points: number[]
decay_rule_revoke_score: number
decay_rule_observable_types: string[]
decay_lifetime: number
decay_pound: number
decay_points: number[]
decay_revoke_score: number
decay_observable_types: string[]
extensions: {
[STIX_EXT_OCTI]: StixOpenctiExtensionSDO
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
type DecayRule implements InternalObject & BasicObject {
id: ID!
standard_id: String!
entity_type: String!
parent_types: [String]!
created_at: DateTime!
updated_at: DateTime!
# DecayRule
name: String!
description: String
order: Int!
active: Boolean!
activated_at: DateTime
built_in: Boolean
# Decay rule settings
decay_lifetime: Int!
decay_pound: Float!
decay_points: [Int!]
decay_revoke_score: Int!
decay_observable_types: [String!]
}

# Ordering
enum DecayRuleOrdering {
name
order
}

# Relay connections
type DecayRuleConnection {
pageInfo: PageInfo!
edges: [DecayRuleEdge!]!
}
type DecayRuleEdge {
cursor: String!
node: DecayRule!
}

# Queries
type Query {
decayRule(id: String!): DecayRule @auth(for: [SETTINGS])
decayRules(
first: Int
after: ID
orderBy: DecayRuleOrdering
orderMode: OrderingMode
filters: FilterGroup
search: String
): DecayRuleConnection @auth(for: [SETTINGS])
}

input DecayRuleAddInput {
name: String! @constraint(minLength: 2)
description: String
order: Int!
active: Boolean!
# Decay rule settings
decay_lifetime: Int!
decay_pound: Float!
decay_points: [Int!]
decay_revoke_score: Int!
decay_observable_types: [String!]
}

type Mutation {
decayRuleAdd(input: DecayRuleAddInput!): DecayRule @auth(for: [SETTINGS])
decayRuleDelete(id: ID!): ID @auth(for: [SETTINGS])
decayRuleFieldPatch(id: ID!, input: [EditInput!]!): DecayRule @auth(for: [SETTINGS])
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ const DECAY_RULE_DEFINITION: ModuleDefinition<StoreEntityDecayRule, StixDecayRul
{ name: 'name', label: 'Name', type: 'string', format: 'short', mandatoryType: 'internal', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'description', label: 'Description', type: 'string', format: 'short', mandatoryType: 'customizable', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'order', label: 'Order', type: 'numeric', precision: 'integer', mandatoryType: 'no', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'builtIn', label: 'Built-in', type: 'boolean', mandatoryType: 'no', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'enabled', label: 'Enabled', type: 'boolean', mandatoryType: 'no', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'enabled_at', label: 'Enabled at', type: 'date', mandatoryType: 'no', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'decay_rule_lifetime', label: 'Decay lifetime', type: 'numeric', precision: 'integer', mandatoryType: 'no', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'decay_rule_pound', label: 'Decay pound', type: 'numeric', precision: 'float', mandatoryType: 'no', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'decay_rule_points', label: 'Decay points', type: 'numeric', precision: 'integer', mandatoryType: 'no', editDefault: false, multiple: true, upsert: false, isFilterable: true },
{ name: 'decay_rule_revoke_score', label: 'Decay revoke score', type: 'numeric', precision: 'integer', mandatoryType: 'no', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'decay_rule_observable_types', label: 'Indicator observable types', type: 'string', format: 'short', mandatoryType: 'no', editDefault: false, multiple: true, upsert: false, isFilterable: true },
{ name: 'built_in', label: 'Built-in', type: 'boolean', mandatoryType: 'no', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'active', label: 'Status', type: 'boolean', mandatoryType: 'no', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'activated_at', label: 'Last activation', type: 'date', mandatoryType: 'no', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'decay_lifetime', label: 'Lifetime', type: 'numeric', precision: 'integer', mandatoryType: 'no', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'decay_pound', label: 'Decay factor', type: 'numeric', precision: 'float', mandatoryType: 'no', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'decay_points', label: 'Reaction points', type: 'numeric', precision: 'integer', mandatoryType: 'no', editDefault: false, multiple: true, upsert: false, isFilterable: true },
{ name: 'decay_revoke_score', label: 'Revoke score', type: 'numeric', precision: 'integer', mandatoryType: 'no', editDefault: false, multiple: false, upsert: false, isFilterable: true },
{ name: 'decay_observable_types', label: 'Indicator observable types', type: 'string', format: 'short', mandatoryType: 'no', editDefault: false, multiple: true, upsert: false, isFilterable: true },
],
relations: [],
representative: (stix: StixDecayRule) => {

Check warning on line 33 in opencti-platform/opencti-graphql/src/modules/decayRule/decayRule.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/modules/decayRule/decayRule.ts#L33

Added line #L33 was not covered by tests
Expand Down
2 changes: 2 additions & 0 deletions opencti-platform/opencti-graphql/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import './playbook/playbook';
import './ingestion/ingestion-rss';
import './ingestion/ingestion-taxii';
import './indicator/indicator';
import './decayRule/decayRule';
import './organization/organization';
import './internal/csvMapper/csvMapper';
import './internal/document/document';
Expand Down Expand Up @@ -87,6 +88,7 @@ import './playbook/playbook-graphql';
import './ingestion/ingestion-rss-graphql';
import './ingestion/ingestion-taxii-graphql';
import './indicator/indicator-graphql';
import './decayRule/decayRule.graphql';
import './organization/organization-graphql';
import './internal/csvMapper/csvMapper-graphql';
// import './internal/document/document-graphql'; # Not needed as document is not fully registered
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export const addIndicator = async (context: AuthContext, user: AuthUser, indicat
let finalIndicatorToCreate;
if (isDecayActivated) {
const indicatorDecayRule = {
// TODO decay_rule_id

Check warning on line 169 in opencti-platform/opencti-graphql/src/modules/indicator/indicator-domain.ts

View check run for this annotation

Codecov / codecov/patch

opencti-platform/opencti-graphql/src/modules/indicator/indicator-domain.ts#L169

Added line #L169 was not covered by tests
decay_lifetime: decayRule.decay_lifetime,
decay_pound: decayRule.decay_pound,
decay_points: [...decayRule.decay_points],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type { StixDate, StixDomainObject, StixMitreExtension, StixOpenctiExtensi
import type { StixKillChainPhase } from '../../types/stix-smo';
import { STIX_EXT_MITRE, STIX_EXT_OCTI } from '../../types/stix-extensions';
import type { BasicStoreEntity, StoreEntity } from '../../types/store';
import type { DecayHistory } from './decay-domain';
import type { DecayRule } from '../../generated/graphql';
import type { DecayHistory, DecayRule } from './decay-domain';

export const ENTITY_TYPE_INDICATOR = 'Indicator';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { MARKING_TLP_AMBER, MARKING_TLP_AMBER_STRICT, MARKING_TLP_CLEAR, MARKING
import { getEntitiesMapFromCache } from '../../database/cache';
import { ENTITY_TYPE_MARKING_DEFINITION } from '../../schema/stixMetaObject';
import type { AuthContext, AuthUser } from '../../types/user';
import type { DecayRule, IndicatorAddInput } from '../../generated/graphql';
import type { IndicatorAddInput } from '../../generated/graphql';
import type { BasicStoreEntity } from '../../types/store';
import { isNotEmptyField } from '../../database/utils';
import { utcDate } from '../../utils/format';
import type { DecayRule } from './decay-domain';

interface TTL_DEFINITION {
target: Array<string>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@ type DecayHistory {
score: Int!
}

type DecayRule {
id: String!
type IndicatorDecayRule {
decay_rule_id: String
decay_lifetime: Int!
decay_pound: Float!
decay_points: [Int!]
decay_revoke_score: Int!
indicator_types: [String!]
order: Int!
enabled: Boolean!
}

type DecayLiveDetails {
Expand Down Expand Up @@ -162,7 +159,7 @@ type Indicator implements BasicObject & StixObject & StixCoreObject & StixDomain
#Decay
decay_base_score : Int
decay_base_score_date : DateTime
decay_applied_rule: DecayRule
decay_applied_rule: IndicatorDecayRule
decay_history: [DecayHistory!]
decayLiveDetails: DecayLiveDetails
# Technical
Expand Down

0 comments on commit 573f727

Please sign in to comment.