Skip to content

Commit

Permalink
[Ingest Manager] Return 400 (not 500) when given a bad kuery param (#…
Browse files Browse the repository at this point in the history
…77796) (#78085)

* Add tests. Add default error handler to 2 routes

* Add more API tests for /fleet/agent/events?kuery=

Co-authored-by: Elastic Machine <[email protected]>

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
John Schulz and elasticmachine authored Sep 22, 2020
1 parent e679b85 commit afc0c1e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
17 changes: 5 additions & 12 deletions x-pack/plugins/ingest_manager/server/routes/agent/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,13 @@ export const getAgentEventsHandler: RequestHandler<
return response.ok({
body,
});
} catch (e) {
if (e.isBoom && e.output.statusCode === 404) {
} catch (error) {
if (error.isBoom && error.output.statusCode === 404) {
return response.notFound({
body: { message: `Agent ${request.params.agentId} not found` },
});
}

return response.customError({
statusCode: 500,
body: { message: e.message },
});
return defaultIngestErrorHandler({ error, response });
}
};

Expand Down Expand Up @@ -253,11 +249,8 @@ export const getAgentsHandler: RequestHandler<
perPage,
};
return response.ok({ body });
} catch (e) {
return response.customError({
statusCode: 500,
body: { message: e.message },
});
} catch (error) {
return defaultIngestErrorHandler({ error, response });
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,24 @@ export default function ({ getService }: FtrProviderContext) {
expect(event).to.have.keys('type', 'subtype', 'message', 'payload');
expect(event.payload).to.have.keys('previous_state');
});

it('should return a 400 when given an invalid "kuery" value', async () => {
await supertest
.get(`/api/ingest_manager/fleet/agents/agent1/events?kuery=m`) // missing saved object type
.expect(400);
});
it('should accept a valid "kuery" value', async () => {
const filter = encodeURIComponent('fleet-agent-events.subtype : "STOPPED"');
const { body: apiResponse } = await supertest
.get(`/api/ingest_manager/fleet/agents/agent1/events?kuery=${filter}`)
.expect(200);

expect(apiResponse).to.have.keys(['list', 'total', 'page']);
expect(apiResponse.total).to.be(1);
expect(apiResponse.page).to.be(1);

const event = apiResponse.list[0];
expect(event.subtype).to.eql('STOPPED');
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,22 @@ export default function ({ getService }: FtrProviderContext) {
.auth(users.kibana_basic_user.username, users.kibana_basic_user.password)
.expect(404);
});
it('should return a 400 when given an invalid "kuery" value', async () => {
await supertest
.get(`/api/ingest_manager/fleet/agents?kuery=m`) // missing saved object type
.auth(users.fleet_user.username, users.fleet_user.password)
.expect(400);
});
it('should accept a valid "kuery" value', async () => {
const filter = encodeURIComponent('fleet-agents.shared_id : "agent2_filebeat"');
const { body: apiResponse } = await supertest
.get(`/api/ingest_manager/fleet/agents?kuery=${filter}`)
.auth(users.fleet_user.username, users.fleet_user.password)
.expect(200);

expect(apiResponse.total).to.eql(1);
const agent = apiResponse.list[0];
expect(agent.shared_id).to.eql('agent2_filebeat');
});
});
}

0 comments on commit afc0c1e

Please sign in to comment.