[Fleet] Return empty agents list when submitting a kuery with no keys (#93844)

This commit is contained in:
Zacqary Adam Xeper 2021-03-09 16:10:56 -06:00 committed by GitHub
parent 3992ed13db
commit 5d119cfcbb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 22 deletions

View file

@ -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,
};
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;
}
}
}
export async function listAllAgents(

View file

@ -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