[Inventory][ECO] Fix asKqlFilter (#200984)

fixes [#200981](https://github.com/elastic/kibana/issues/200981)

## Summary

This PR fixes a problem with the asKqlFilter throwing an error when
building filters with string containing special characters.
This commit is contained in:
Carlos Crespo 2024-11-21 13:04:53 +01:00 committed by GitHub
parent f0262080c8
commit 8f8a671567
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 4 deletions

View file

@ -39,7 +39,22 @@ describe('EntityClient', () => {
};
const result = entityClient.asKqlFilter(entityLatest);
expect(result).toEqual('service.name: my-service');
expect(result).toEqual('service.name: "my-service"');
});
it('should return the kql filter when an indentity field value contain special characters', () => {
const entityLatest: EntityInstance = {
entity: {
...commonEntityFields.entity,
identity_fields: ['host.name', 'foo.bar'],
},
host: {
name: 'my-host:some-value:some-other-value',
},
};
const result = entityClient.asKqlFilter(entityLatest);
expect(result).toEqual('host.name: "my-host:some-value:some-other-value"');
});
it('should return the kql filter when indentity_fields is composed by multiple fields', () => {
@ -56,7 +71,7 @@ describe('EntityClient', () => {
};
const result = entityClient.asKqlFilter(entityLatest);
expect(result).toEqual('(service.name: my-service AND service.environment: staging)');
expect(result).toEqual('(service.name: "my-service" AND service.environment: "staging")');
});
it('should ignore fields that are not present in the entity', () => {
@ -71,7 +86,7 @@ describe('EntityClient', () => {
};
const result = entityClient.asKqlFilter(entityLatest);
expect(result).toEqual('host.name: my-host');
expect(result).toEqual('host.name: "my-host"');
});
});

View file

@ -95,7 +95,7 @@ export class EntityClient {
const identityFieldsValue = this.getIdentityFieldsValue(entityInstance);
const nodes: KueryNode[] = Object.entries(identityFieldsValue).map(([identityField, value]) => {
return nodeTypes.function.buildNode('is', identityField, value);
return nodeTypes.function.buildNode('is', identityField, `"${value}"`);
});
if (nodes.length === 0) return '';