Skip to content

Commit

Permalink
[Inventory][ECO] Fix asKqlFilter (elastic#200984)
Browse files Browse the repository at this point in the history
fixes [elastic#200981](elastic#200981)

## Summary

This PR fixes a problem with the asKqlFilter throwing an error when
building filters with string containing special characters.
  • Loading branch information
crespocarlos authored Nov 21, 2024
1 parent f026208 commit 8f8a671
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions x-pack/plugins/entity_manager/public/lib/entity_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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"');
});
});

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/entity_manager/public/lib/entity_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand Down

0 comments on commit 8f8a671

Please sign in to comment.