Skip to content

Commit aafe525

Browse files
committed
apply review remarks
1 parent f5cbfac commit aafe525

1 file changed

Lines changed: 16 additions & 26 deletions

File tree

  • x-pack/solutions/security/plugins/security_solution/server/lib/entity_analytics/entity_store/routes/entities

x-pack/solutions/security/plugins/security_solution/server/lib/entity_analytics/entity_store/routes/entities/list.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* 2.0; you may not use this file except in compliance with the Elastic License
55
* 2.0.
66
*/
7-
/* eslint-disable no-continue */
87
import type { IKibanaResponse, Logger } from '@kbn/core/server';
98
import { buildSiemResponse } from '@kbn/lists-plugin/server/routes/utils';
109
import { transformError } from '@kbn/securitysolution-es-utils';
@@ -70,12 +69,14 @@ export const listEntitiesRoute = (
7069
sortOrder,
7170
});
7271

72+
// just override the entity field with the normalized fields
7373
records.forEach((record) => {
74-
return lowercaseEntityKeysForProperties(record, [
74+
const result = buildNormalizedFields(record.entity, [
7575
'behaviors',
7676
'lifecycle',
7777
'attributes',
7878
]);
79+
record.entity = { ...record.entity, ...result };
7980
});
8081

8182
telemetry.reportEBT(ENTITY_STORE_API_CALL_EVENT, {
@@ -106,29 +107,18 @@ export const listEntitiesRoute = (
106107
);
107108
};
108109

109-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
110-
export function lowercaseEntityKeysForProperties<T extends Record<string, any>>(
111-
obj: T,
112-
properties: string[]
113-
): void {
114-
for (const prop of properties) {
115-
if (!(prop in obj.entity)) {
116-
continue;
117-
}
118-
const sub = obj.entity[prop];
119-
120-
if (!sub || typeof sub !== 'object' || Array.isArray(sub)) {
121-
continue;
122-
}
110+
function buildNormalizedFields(obj: Record<string, unknown>, properties: string[]) {
111+
// only use properties whose val is an object, skip them if undefined or some other type
112+
const hasObjVal = (p: string) =>
113+
obj[p] !== null && typeof obj[p] === 'object' && !Array.isArray(obj[p]);
114+
const entries = properties
115+
.filter(hasObjVal)
116+
.map((p) => [p, toLowercaseKeys(obj[p] as Record<string, unknown>)]);
117+
return Object.fromEntries(entries);
118+
}
123119

124-
const keys = Object.keys(sub);
125-
for (const k of keys) {
126-
const newKey = k.toLowerCase();
127-
if (newKey in sub) {
128-
continue;
129-
}
130-
sub[newKey] = sub[k];
131-
delete sub[k];
132-
}
133-
}
120+
function toLowercaseKeys(obj: Record<string, unknown>): Record<string, unknown> {
121+
// iterate to rebuild the sub object with the lowercase keys.
122+
// No need for checking if it appears in the old sub or to `delete`
123+
return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k.toLowerCase(), v]));
134124
}

0 commit comments

Comments
 (0)