Skip to content

Commit

Permalink
Allow multiple aliases to same field when creating/editing mappings f…
Browse files Browse the repository at this point in the history
…or detector (#973)

* allow same log field to be mapped for multiple aliases

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

* updated snapshots

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

---------

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>
(cherry picked from commit 890c486)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Mar 27, 2024
1 parent 3cdad4c commit a5319ad
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export interface MappingProps {
[MappingViewType.Edit]: {
type: MappingViewType.Edit;
existingMappings: ruleFieldToIndexFieldMap;
invalidMappingFieldNames: string[];
onMappingCreation: (fieldName: string, aliasName: string) => void;
};
}
Expand Down Expand Up @@ -98,7 +97,7 @@ export default class FieldMappingsTable<T extends MappingViewType> extends Compo
width: '45%',
render: (logFieldName: string, entry: FieldMappingsTableItem) => {
if (this.props.mappingProps.type === MappingViewType.Edit) {
const { onMappingCreation, invalidMappingFieldNames, existingMappings } = this.props
const { onMappingCreation, existingMappings } = this.props
.mappingProps as MappingProps[MappingViewType.Edit];
const onMappingSelected = (selectedField: string) => {
onMappingCreation(entry.ruleFieldName, selectedField);
Expand All @@ -107,7 +106,6 @@ export default class FieldMappingsTable<T extends MappingViewType> extends Compo
<FieldNameSelector
fieldNameOptions={indexFields}
selectedField={existingMappings[entry.ruleFieldName]}
isInvalid={invalidMappingFieldNames.includes(entry.ruleFieldName)}
onChange={onMappingSelected}
/>
);
Expand All @@ -131,14 +129,11 @@ export default class FieldMappingsTable<T extends MappingViewType> extends Compo
align: 'center',
width: '15%',
render: (_status: 'mapped' | 'unmapped', entry: FieldMappingsTableItem) => {
const { existingMappings: createdMappings, invalidMappingFieldNames } = this.props
const { existingMappings: createdMappings } = this.props
.mappingProps as MappingProps[MappingViewType.Edit];
let iconProps = STATUS_ICON_PROPS['unmapped'];
let iconTooltip = 'This field needs to be mapped with a field from your log source.';
if (
createdMappings[entry.ruleFieldName] &&
!invalidMappingFieldNames.includes(entry.ruleFieldName)
) {
if (createdMappings[entry.ruleFieldName]) {
iconProps = STATUS_ICON_PROPS['mapped'];
iconTooltip = 'This field has been mapped.';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { EuiComboBox, EuiComboBoxOptionOption, EuiFormRow } from '@elastic/eui';

interface SIEMFieldNameProps {
fieldNameOptions: string[];
isInvalid: boolean;
selectedField: string;
onChange: (option: string) => void;
}
Expand Down Expand Up @@ -51,18 +50,14 @@ export default class FieldNameSelector extends Component<SIEMFieldNameProps, SIE

render() {
const { selectedOptions } = this.state;
const { isInvalid, fieldNameOptions } = this.props;
const { fieldNameOptions } = this.props;

const comboOptions = fieldNameOptions.map((option) => ({
label: option,
}));

return (
<EuiFormRow
style={{ width: '100%' }}
isInvalid={isInvalid}
error={isInvalid ? 'Field already used.' : undefined}
>
<EuiFormRow style={{ width: '100%' }}>
<EuiComboBox
data-test-subj={'detector-field-mappings-select'}
placeholder="Select a mapping field"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ interface ConfigureFieldMappingState {
detector: Detector;
mappingsData: GetFieldMappingViewResponse;
createdMappings: ruleFieldToIndexFieldMap;
invalidMappingFieldNames: string[];
fieldMappingIsOpen: boolean;
showMappingEmptyPrompt: boolean;
selectedTabId: FieldMappingTabId;
Expand All @@ -69,7 +68,6 @@ export default class ConfigureFieldMapping extends Component<
loading: props.loading || false,
mappingsData: EMPTY_FIELD_MAPPINGS_VIEW,
createdMappings,
invalidMappingFieldNames: [],
detector: props.detector,
fieldMappingIsOpen: false,
tabs: [],
Expand Down Expand Up @@ -109,7 +107,6 @@ export default class ConfigureFieldMapping extends Component<
loading,
mappingsData,
createdMappings,
invalidMappingFieldNames,
selectedTabId,
detector: { detector_type, inputs },
} = this.state;
Expand Down Expand Up @@ -160,7 +157,6 @@ export default class ConfigureFieldMapping extends Component<
mappingProps={{
type: MappingViewType.Edit,
existingMappings,
invalidMappingFieldNames,
onMappingCreation: this.onMappingCreation,
}}
/>
Expand Down Expand Up @@ -193,7 +189,6 @@ export default class ConfigureFieldMapping extends Component<
mappingProps={{
type: MappingViewType.Edit,
existingMappings,
invalidMappingFieldNames,
onMappingCreation: this.onMappingCreation,
}}
/>
Expand Down Expand Up @@ -268,24 +263,6 @@ export default class ConfigureFieldMapping extends Component<
}
};

/**
* Returns the fieldName(s) that have duplicate alias assigned to them
*/
getInvalidMappingFieldNames(mappings: ruleFieldToIndexFieldMap): string[] {
const seenAliases = new Set();
const invalidFields: string[] = [];

Object.entries(mappings).forEach((entry) => {
if (seenAliases.has(entry[1])) {
invalidFields.push(entry[0]);
}

seenAliases.add(entry[1]);
});

return invalidFields;
}

onMappingCreation = (ruleFieldName: string, indexFieldName: string): void => {
const newMappings: ruleFieldToIndexFieldMap = {
...this.state.createdMappings,
Expand All @@ -296,10 +273,8 @@ export default class ConfigureFieldMapping extends Component<
delete newMappings[ruleFieldName];
}

const invalidMappingFieldNames = this.getInvalidMappingFieldNames(newMappings);
this.setState({
createdMappings: newMappings,
invalidMappingFieldNames: invalidMappingFieldNames,
});
this.updateMappingSharedState(newMappings);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ interface EditFieldMappingsProps extends RouteComponentProps {
interface EditFieldMappingsState {
loading: boolean;
createdMappings: ruleFieldToIndexFieldMap;
invalidMappingFieldNames: string[];
mappedRuleFields: string[];
unmappedRuleFields: string[];
logFieldOptions: string[];
Expand All @@ -61,7 +60,6 @@ export default class EditFieldMappings extends Component<
ruleQueryFields: props.ruleQueryFields ? props.ruleQueryFields : new Set<string>(),
loading: props.loading || false,
createdMappings,
invalidMappingFieldNames: [],
mappedRuleFields: [],
unmappedRuleFields: [],
logFieldOptions: [],
Expand Down Expand Up @@ -126,7 +124,7 @@ export default class EditFieldMappings extends Component<
});

items.forEach((ruleField) => {
existingMappings[ruleField.ruleFieldName] = ruleField.logFieldName;
existingMappings[ruleField.ruleFieldName] = ruleField.logFieldName || '';
});

for (let key in existingMappings) {
Expand Down Expand Up @@ -167,24 +165,6 @@ export default class EditFieldMappings extends Component<
this.setState({ loading: false });
};

/**
* Returns the fieldName(s) that have duplicate alias assigned to them
*/
getInvalidMappingFieldNames(mappings: ruleFieldToIndexFieldMap): string[] {
const seenAliases = new Set();
const invalidFields: string[] = [];

Object.entries(mappings).forEach((entry) => {
if (seenAliases.has(entry[1])) {
invalidFields.push(entry[0]);
}

seenAliases.add(entry[1]);
});

return invalidFields;
}

onMappingCreation = (ruleFieldName: string, indexFieldName: string): void => {
const newMappings: ruleFieldToIndexFieldMap = {
...this.state.createdMappings,
Expand All @@ -193,10 +173,8 @@ export default class EditFieldMappings extends Component<
if (!indexFieldName) {
delete newMappings[ruleFieldName];
}
const invalidMappingFieldNames = this.getInvalidMappingFieldNames(newMappings);
this.setState({
createdMappings: newMappings,
invalidMappingFieldNames: invalidMappingFieldNames,
});
this.updateMappingSharedState(newMappings);
};
Expand All @@ -216,7 +194,6 @@ export default class EditFieldMappings extends Component<
const {
loading,
createdMappings,
invalidMappingFieldNames,
mappedRuleFields,
unmappedRuleFields,
logFieldOptions,
Expand Down Expand Up @@ -253,7 +230,6 @@ export default class EditFieldMappings extends Component<
mappingProps={{
type: MappingViewType.Edit,
existingMappings,
invalidMappingFieldNames,
onMappingCreation: this.onMappingCreation,
}}
/>
Expand Down Expand Up @@ -286,7 +262,6 @@ export default class EditFieldMappings extends Component<
mappingProps={{
type: MappingViewType.Edit,
existingMappings,
invalidMappingFieldNames,
onMappingCreation: this.onMappingCreation,
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ exports[`<EditFieldMappings /> spec renders the component 1`] = `
mappingProps={
Object {
"existingMappings": Object {},
"invalidMappingFieldNames": Array [],
"onMappingCreation": [Function],
"type": 1,
}
Expand Down

0 comments on commit a5319ad

Please sign in to comment.