Skip to content

Commit

Permalink
[Fleet] Return empty agents list when submitting a kuery with no keys (
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacqary authored Mar 9, 2021
1 parent 3992ed1 commit 5d119cf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
57 changes: 35 additions & 22 deletions x-pack/plugins/fleet/server/services/agents/crud_so.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,42 @@ export async function listAgents(
if (showInactive === false) {
filters.push(ACTIVE_AGENT_CONDITION);
}
let { saved_objects: agentSOs, total } = await soClient.find<AgentSOAttributes>({
type: AGENT_SAVED_OBJECT_TYPE,
filter: _joinFilters(filters) || '',
sortField,
sortOrder,
page,
perPage,
});
// filtering for a range on the version string will not work,
// nor does filtering on a flattened field (local_metadata), so filter here
if (showUpgradeable) {
agentSOs = agentSOs.filter((agent) =>
isAgentUpgradeable(savedObjectToAgent(agent), appContextService.getKibanaVersion())
);
total = agentSOs.length;
try {
let { saved_objects: agentSOs, total } = await soClient.find<AgentSOAttributes>({
type: AGENT_SAVED_OBJECT_TYPE,
filter: _joinFilters(filters) || '',
sortField,
sortOrder,
page,
perPage,
});
// filtering for a range on the version string will not work,
// nor does filtering on a flattened field (local_metadata), so filter here
if (showUpgradeable) {
agentSOs = agentSOs.filter((agent) =>
isAgentUpgradeable(savedObjectToAgent(agent), appContextService.getKibanaVersion())
);
total = agentSOs.length;
}

return {
agents: agentSOs.map(savedObjectToAgent),
total,
page,
perPage,
};
} catch (e) {
if (e.output?.payload?.message?.startsWith('The key is empty')) {
return {
agents: [],
total: 0,
page: 0,
perPage: 0,
};
} else {
throw e;
}
}

return {
agents: agentSOs.map(savedObjectToAgent),
total,
page,
perPage,
};
}

export async function listAllAgents(
Expand Down
8 changes: 8 additions & 0 deletions x-pack/test/fleet_api_integration/apis/agents/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ export default function ({ getService }: FtrProviderContext) {
it('should return a 400 when given an invalid "kuery" value', async () => {
await supertest.get(`/api/fleet/agents?kuery=.test%3A`).expect(400);
});

it('should return a 200 and an empty list when given a "kuery" value with a missing saved object type', async () => {
const { body: apiResponse } = await supertest
.get(`/api/fleet/agents?kuery=m`) // missing saved object type
.expect(200);
expect(apiResponse.total).to.eql(0);
});

it('should accept a valid "kuery" value', async () => {
const filter = encodeURIComponent('fleet-agents.access_api_key_id : "api-key-2"');
const { body: apiResponse } = await supertest
Expand Down

0 comments on commit 5d119cf

Please sign in to comment.