Skip to content

Commit e28a554

Browse files
committed
more mapping guards
Signed-off-by: Aleksandar Djindjic <[email protected]>
1 parent 491e7a2 commit e28a554

File tree

5 files changed

+65
-34
lines changed

5 files changed

+65
-34
lines changed

models/interfaces.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ export interface Rule {
99
log_source: string;
1010
title: string;
1111
description: string;
12-
tags: { value: string }[];
13-
false_positives: { value: string }[];
12+
tags: Array<{ value: string }>;
13+
false_positives: Array<{ value: string }>;
1414
level: string;
1515
status: string;
16-
references: { value: string }[];
16+
references: Array<{ value: string }>;
1717
author: string;
1818
detection: string;
1919
}

public/pages/Rules/components/RuleEditor/RuleEditor.tsx

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import React, { useState } from 'react';
77
import { ContentPanel } from '../../../../components/ContentPanel';
88
import { EuiSpacer, EuiButtonGroup } from '@elastic/eui';
99
import { Rule } from '../../../../../models/interfaces';
10-
import { RuleEditorFormState } from './RuleEditorFormState.model';
10+
import { RuleEditorFormState, ruleEditorStateDefaultValue } from './RuleEditorFormState.model';
1111
import { mapFormToRule, mapRuleToForm } from './mappers';
1212
import { VisualRuleEditor } from './VisualRuleEditor';
1313
import { YamlRuleEditor } from './YamlRuleEditor';
@@ -24,21 +24,6 @@ export interface VisualEditorFormErrorsState {
2424
authorError: string | null;
2525
}
2626

27-
const newRuyleDefaultState: RuleEditorFormState = {
28-
id: '25b9c01c-350d-4b95-bed1-836d04a4f324',
29-
log_source: '',
30-
logType: '',
31-
name: '',
32-
description: '',
33-
status: '',
34-
author: '',
35-
references: [''],
36-
tags: [],
37-
detection: '',
38-
level: '',
39-
falsePositives: [''],
40-
};
41-
4227
const editorTypes = [
4328
{
4429
id: 'visual',
@@ -52,7 +37,9 @@ const editorTypes = [
5237

5338
export const RuleEditor: React.FC<RuleEditorProps> = ({ title, rule, FooterActions }) => {
5439
const [ruleEditorFormState, setRuleEditorFormState] = useState<RuleEditorFormState>(
55-
rule ? { ...mapRuleToForm(rule), id: newRuyleDefaultState.id } : newRuyleDefaultState
40+
rule
41+
? { ...mapRuleToForm(rule), id: ruleEditorStateDefaultValue.id }
42+
: ruleEditorStateDefaultValue
5643
);
5744

5845
const [selectedEditorType, setSelectedEditorType] = useState('visual');

public/pages/Rules/components/RuleEditor/RuleEditorFormState.model.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,18 @@ export interface RuleEditorFormState {
1919
level: string;
2020
falsePositives: string[];
2121
}
22+
23+
export const ruleEditorStateDefaultValue: RuleEditorFormState = {
24+
id: '25b9c01c-350d-4b95-bed1-836d04a4f324',
25+
log_source: '',
26+
logType: '',
27+
name: '',
28+
description: '',
29+
status: '',
30+
author: '',
31+
references: [''],
32+
tags: [],
33+
detection: '',
34+
level: '',
35+
falsePositives: [''],
36+
};

public/pages/Rules/components/RuleEditor/YamlRuleEditor.tsx

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,31 @@ export interface YamlEditorState {
2020
}
2121

2222
const mapYamlObjectToYamlString = (rule: Rule): string => {
23-
try {
24-
const yamlString = dump(rule);
23+
console.log('mapYamlObjectToYamlString', rule);
2524

26-
return yamlString;
25+
try {
26+
if (!rule.detection) {
27+
const { detection, ...ruleWithoutDetection } = rule;
28+
return dump(ruleWithoutDetection);
29+
} else {
30+
return dump(rule);
31+
}
2732
} catch (error: any) {
2833
console.warn('Security Analytics - Rule Eritor - Yaml dump', error);
2934
return '';
3035
}
3136
};
3237

3338
const mapRuleToYamlObject = (rule: Rule): any => {
39+
console.log('mapRuleToYamlObject', rule);
40+
41+
let detection = undefined;
42+
if (rule.detection) {
43+
try {
44+
detection = load(rule.detection);
45+
} catch {}
46+
}
47+
3448
const yamlObject: any = {
3549
id: rule.id,
3650
logsource: { product: rule.category },
@@ -42,26 +56,36 @@ const mapRuleToYamlObject = (rule: Rule): any => {
4256
status: rule.status,
4357
references: rule.references.map((reference) => reference.value),
4458
author: rule.author,
45-
detection: load(rule.detection),
59+
detection,
4660
};
4761

4862
return yamlObject;
4963
};
5064

5165
const mapYamlObjectToRule = (obj: any): Rule => {
66+
let detection = '';
67+
if (obj.detection) {
68+
try {
69+
detection = dump(obj.detection);
70+
} catch {}
71+
}
5272
const rule: Rule = {
5373
id: obj.id,
54-
category: obj.logsource.product,
74+
category: obj.logsource ? obj.logsource.product : undefined,
5575
log_source: '',
5676
title: obj.title,
5777
description: obj.description,
58-
tags: obj.tags.map((tag: string) => ({ value: tag })),
59-
false_positives: obj.falsepositives.map((falsePositive: string) => ({ value: falsePositive })),
78+
tags: obj.tags ? obj.tags.map((tag: string) => ({ value: tag })) : undefined,
79+
false_positives: obj.falsepositives
80+
? obj.falsepositives.map((falsePositive: string) => ({ value: falsePositive }))
81+
: undefined,
6082
level: obj.level,
6183
status: obj.status,
62-
references: obj.references.map((reference: string) => ({ value: reference })),
84+
references: obj.references
85+
? obj.references.map((reference: string) => ({ value: reference }))
86+
: undefined,
6387
author: obj.author,
64-
detection: dump(obj.detection),
88+
detection,
6589
};
6690

6791
return rule;
@@ -89,7 +113,6 @@ export const YamlRuleEditor: React.FC<YamlRuleEditorProps> = ({ rule, change })
89113

90114
const rule = mapYamlObjectToRule(yamlObject);
91115

92-
console.log('onBlur rule load', yamlObject, rule);
93116
change(rule);
94117
setState((prevState) => ({ ...prevState, error: null }));
95118
} catch (error) {

public/pages/Rules/components/RuleEditor/mappers.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55
import { Rule } from '../../../../../models/interfaces';
6-
import { RuleEditorFormState } from './RuleEditorFormState.model';
6+
import { RuleEditorFormState, ruleEditorStateDefaultValue } from './RuleEditorFormState.model';
77

88
export const mapFormToRule = (formState: RuleEditorFormState): Rule => {
99
return {
@@ -33,10 +33,16 @@ export const mapRuleToForm = (rule: Rule): RuleEditorFormState => {
3333
description: rule.description,
3434
status: rule.status,
3535
author: rule.author,
36-
references: rule.references.map((ref) => ref.value),
37-
tags: rule.tags.map((tag) => ({ label: tag.value })),
36+
references: rule.references
37+
? rule.references.map((ref) => ref.value)
38+
: ruleEditorStateDefaultValue.references,
39+
tags: rule.tags
40+
? rule.tags.map((tag) => ({ label: tag.value }))
41+
: ruleEditorStateDefaultValue.tags,
3842
detection: rule.detection,
3943
level: rule.level,
40-
falsePositives: rule.false_positives.map((falsePositive) => falsePositive.value),
44+
falsePositives: rule.false_positives
45+
? rule.false_positives.map((falsePositive) => falsePositive.value)
46+
: ruleEditorStateDefaultValue.falsePositives,
4147
};
4248
};

0 commit comments

Comments
 (0)