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

Wrong field mappings for the cloud trail logs #574

5 changes: 4 additions & 1 deletion cypress/integration/1_detectors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dns_name_rule_data from '../fixtures/integration_tests/rule/create_dns_ru
import dns_type_rule_data from '../fixtures/integration_tests/rule/create_dns_rule_with_type_selection.json';
import dns_mapping_fields from '../fixtures/integration_tests/rule/sample_dns_field_mappings.json';
import _ from 'lodash';
import { getMappingFields } from '../../public/pages/Detectors/utils/helpers';

const cypressIndexDns = 'cypress-index-dns';
const cypressIndexWindows = 'cypress-index-windows';
Expand Down Expand Up @@ -56,7 +57,9 @@ const validateFieldMappingsTable = () => {
if (_.isEmpty(properties)) {
validatePendingFieldMappingsPanel(Object.entries(mappingFields));
} else {
validateAutomaticFieldMappingsPanel(Object.entries(properties));
const items = getMappingFields(properties, [], '');
items.map((item) => [item.ruleFieldName, item.logFieldName]);
validateAutomaticFieldMappingsPanel(items);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { FieldMapping } from '../../../../../models/interfaces';
import { errorNotificationToast } from '../../../../utils/helpers';
import { NotificationsStart } from 'opensearch-dashboards/public';
import { Detector } from '../../../../../types';
import { getMappingFields } from '../../utils/helpers';

export interface FieldMappingsViewProps {
detector: Detector;
Expand Down Expand Up @@ -60,13 +61,11 @@ export const FieldMappingsView: React.FC<FieldMappingsViewProps> = ({
if (getMappingRes?.ok) {
const mappingsData = getMappingRes.response[indexName];
if (mappingsData) {
let items: FieldMappingsTableItem[] = [];
Object.entries(mappingsData.mappings.properties).forEach((entry) => {
items.push({
ruleFieldName: entry[0],
logFieldName: entry[1].path,
});
});
const items: FieldMappingsTableItem[] = getMappingFields(
mappingsData.mappings.properties,
[],
''
);

setFieldMappingItems(items);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { FieldMapping } from '../../../../../models/interfaces';
import FieldMappingService from '../../../../services/FieldMappingService';
import { MappingViewType } from '../../../CreateDetector/components/ConfigureFieldMapping/components/RequiredFieldMapping/FieldMappingsTable';
import { Detector } from '../../../../../types';
import { FieldMappingsTableItem } from '../../../CreateDetector/models/interfaces';
import { getMappingFields } from '../../utils/helpers';
import _ from 'lodash';

export interface ruleFieldToIndexFieldMap {
Expand Down Expand Up @@ -117,13 +119,14 @@ export default class EditFieldMappings extends Component<
const mappingsRes = await this.props.fieldMappingService?.getMappings(indexName);
if (mappingsRes?.ok) {
const mappedFieldsInfo = mappingsRes.response[indexName].mappings.properties;
let mappedRuleFields = Object.keys(mappedFieldsInfo);
const items: FieldMappingsTableItem[] = getMappingFields(mappedFieldsInfo, [], '');
let mappedRuleFields = _.map(items, 'ruleFieldName');
unmappedRuleFields = unmappedRuleFields.filter((ruleField) => {
return !mappedRuleFields.includes(ruleField);
});

mappedRuleFields.forEach((ruleField) => {
existingMappings[ruleField] = mappedFieldsInfo[ruleField].path;
items.forEach((ruleField) => {
existingMappings[ruleField.ruleFieldName] = ruleField.logFieldName;
});

for (let key in existingMappings) {
Expand Down
21 changes: 21 additions & 0 deletions public/pages/Detectors/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { DetectorHit } from '../../../../server/models/interfaces';
import { FieldMappingsTableItem } from '../../../../types';

export function getDetectorIds(detectors: DetectorHit[]) {
return detectors.map((detector) => detector._id).join(', ');
Expand All @@ -12,3 +13,23 @@ export function getDetectorIds(detectors: DetectorHit[]) {
export function getDetectorNames(detectors: DetectorHit[]) {
return detectors.map((detector) => detector._source.name).join(', ');
}

export const getMappingFields = (
properties: any,
items: FieldMappingsTableItem[] = [],
prefix: string = ''
): FieldMappingsTableItem[] => {
for (let field in properties) {
const fullFieldName = prefix ? `${prefix}.${field}` : field;
const nextProperties = properties[field].properties;
if (!nextProperties) {
items.push({
ruleFieldName: fullFieldName,
logFieldName: properties[field].path,
});
} else {
getMappingFields(nextProperties, items, fullFieldName);
}
}
return items;
};