[Fleet] Agent policy search, support simple text filter. (#107306)

* feat: fall back to simple search on parse error

* fix: simplify query

* lint: fix docs

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Mark Hopkin 2021-08-03 16:16:17 +01:00 committed by GitHub
parent 5cd7358834
commit 48a97f6d18
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 6 deletions

View file

@ -333,14 +333,30 @@ class AgentPolicyService {
withPackagePolicies = false,
} = options;
const agentPoliciesSO = await soClient.find<AgentPolicySOAttributes>({
const baseFindParams = {
type: SAVED_OBJECT_TYPE,
sortField,
sortOrder,
page,
perPage,
filter: kuery ? normalizeKuery(SAVED_OBJECT_TYPE, kuery) : undefined,
});
};
const filter = kuery ? normalizeKuery(SAVED_OBJECT_TYPE, kuery) : undefined;
let agentPoliciesSO;
try {
agentPoliciesSO = await soClient.find<AgentPolicySOAttributes>({ ...baseFindParams, filter });
} catch (e) {
const isBadRequest = e.output?.statusCode === 400;
const isKQLSyntaxError = e.message?.startsWith('KQLSyntaxError');
if (isBadRequest && !isKQLSyntaxError) {
// fall back to simple search if the kuery is just a search term i.e not KQL
agentPoliciesSO = await soClient.find<AgentPolicySOAttributes>({
...baseFindParams,
search: kuery,
});
} else {
throw e;
}
}
const agentPolicies = await Promise.all(
agentPoliciesSO.saved_objects.map(async (agentPolicySO) => {

View file

@ -19,9 +19,9 @@ export function escapeSearchQueryPhrase(val: string): string {
return `"${val.replace(/["]/g, '"')}"`;
}
// Adds `.attribute` to any kuery strings that are missing it, this comes from
// internal SO structure. Kuery strings that come from UI will typicall have
// `.attribute` hidden to simplify UX, so this normalizes any kuery string for
// Adds `.attributes` to any kuery strings that are missing it, this comes from
// internal SO structure. Kuery strings that come from UI will typically have
// `.attributes` hidden to simplify UX, so this normalizes any kuery string for
// filtering SOs
export const normalizeKuery = (savedObjectType: string, kuery: string): string => {
return kuery.replace(