Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show required field mappings only for enabled rules #418

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"log_source": "",
"detection": "selection:\n DnsQuestionName:\n - QWE\n - ASD\n - YXC\ncondition: selection",
"detection": "selection:\n query:\n - QWE\n - ASD\n - YXC\ncondition: selection",
"level": "low",
"false_positives": [
{
Expand Down
1 change: 0 additions & 1 deletion cypress/integration/1_detectors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import { OPENSEARCH_DASHBOARDS_URL } from '../support/constants';
import sample_index_settings from '../fixtures/sample_index_settings.json';
import dns_rule_data from '../fixtures/integration_tests/rule/create_dns_rule.json';
import sample_dns_settings from '../fixtures/integration_tests/index/create_dns_settings.json';

const testMappings = {
properties: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { DetectorCreationStep } from '../../../models/types';
import { GetFieldMappingViewResponse } from '../../../../../../server/models/interfaces';
import FieldMappingService from '../../../../../services/FieldMappingService';
import { MappingViewType } from '../components/RequiredFieldMapping/FieldMappingsTable';
import { CreateDetectorRulesState } from '../../DefineDetector/components/DetectionRules/DetectionRules';

export interface ruleFieldToIndexFieldMap {
[fieldName: string]: string;
Expand All @@ -32,10 +33,11 @@ interface ConfigureFieldMappingProps extends RouteComponentProps {
isEdit: boolean;
detector: Detector;
filedMappingService: FieldMappingService;
replaceFieldMappings: (mappings: FieldMapping[]) => void;
fieldMappings: FieldMapping[];
updateDataValidState: (step: DetectorCreationStep, isValid: boolean) => void;
loading: boolean;
enabledRules: CreateDetectorRulesState['allRules'];
updateDataValidState: (step: DetectorCreationStep, isValid: boolean) => void;
replaceFieldMappings: (mappings: FieldMapping[]) => void;
}

interface ConfigureFieldMappingState {
Expand Down Expand Up @@ -67,6 +69,17 @@ export default class ConfigureFieldMapping extends Component<
this.getAllMappings();
};

private getRuleFieldsForEnabledRules(): Set<string> {
const ruleFieldsForEnabledRules = new Set<string>();
this.props.enabledRules.forEach((rule) => {
rule._source.query_field_names.forEach((fieldname) => {
ruleFieldsForEnabledRules.add(fieldname.value);
});
});

return ruleFieldsForEnabledRules;
}

getAllMappings = async () => {
this.setState({ loading: true });
const mappingsView = await this.props.filedMappingService.getMappingsView(
Expand All @@ -75,14 +88,31 @@ export default class ConfigureFieldMapping extends Component<
);
if (mappingsView.ok) {
const existingMappings = { ...this.state.createdMappings };
const ruleFieldsForEnabledRules = this.getRuleFieldsForEnabledRules();
const unmappedRuleFields = new Set(mappingsView.response.unmapped_field_aliases);

Object.keys(mappingsView.response.properties).forEach((ruleFieldName) => {
// Filter the mappings view to include only the rule fields for the enabled rules
if (!ruleFieldsForEnabledRules.has(ruleFieldName)) {
delete mappingsView.response.properties[ruleFieldName];
return;
}

existingMappings[ruleFieldName] =
this.state.createdMappings[ruleFieldName] ||
mappingsView.response.properties[ruleFieldName].path;
});
mappingsView.response.unmapped_field_aliases?.forEach((ruleFieldName) => {
if (!ruleFieldsForEnabledRules.has(ruleFieldName)) {
unmappedRuleFields.delete(ruleFieldName);
}
});
this.setState({
createdMappings: existingMappings,
mappingsData: mappingsView.response,
mappingsData: {
...mappingsView.response,
unmapped_field_aliases: Array.from(unmappedRuleFields),
},
});
this.updateMappingSharedState(existingMappings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const DetectionRules: React.FC<DetectionRulesProps> = ({
logType: rule._source.category,
name: rule._source.title,
severity: rule._source.level,
ruleInfo: rule,
})),
[rulesState.allRules]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ export default class CreateDetector extends Component<CreateDetectorProps, Creat
loading={false}
filedMappingService={services.fieldMappingService}
fieldMappings={this.state.fieldMappings}
enabledRules={this.state.rulesState.allRules.filter((rule) => rule.enabled)}
replaceFieldMappings={this.replaceFieldMappings}
updateDataValidState={this.updateDataValidState}
/>
Expand Down
1 change: 1 addition & 0 deletions server/models/interfaces/Rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ export type RuleSource = Rule & {
rule: string;
last_update_time: string;
queries: { value: string }[];
query_field_names: { value: string }[];
};