|
4 | 4 | * 2.0; you may not use this file except in compliance with the Elastic License |
5 | 5 | * 2.0. |
6 | 6 | */ |
7 | | -/* eslint-disable no-continue */ |
8 | 7 | import type { IKibanaResponse, Logger } from '@kbn/core/server'; |
9 | 8 | import { buildSiemResponse } from '@kbn/lists-plugin/server/routes/utils'; |
10 | 9 | import { transformError } from '@kbn/securitysolution-es-utils'; |
@@ -70,12 +69,14 @@ export const listEntitiesRoute = ( |
70 | 69 | sortOrder, |
71 | 70 | }); |
72 | 71 |
|
| 72 | + // just override the entity field with the normalized fields |
73 | 73 | records.forEach((record) => { |
74 | | - return lowercaseEntityKeysForProperties(record, [ |
| 74 | + const result = buildNormalizedFields(record.entity, [ |
75 | 75 | 'behaviors', |
76 | 76 | 'lifecycle', |
77 | 77 | 'attributes', |
78 | 78 | ]); |
| 79 | + record.entity = { ...record.entity, ...result }; |
79 | 80 | }); |
80 | 81 |
|
81 | 82 | telemetry.reportEBT(ENTITY_STORE_API_CALL_EVENT, { |
@@ -106,29 +107,18 @@ export const listEntitiesRoute = ( |
106 | 107 | ); |
107 | 108 | }; |
108 | 109 |
|
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 | +} |
123 | 119 |
|
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])); |
134 | 124 | } |
0 commit comments