Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] [Ingest Manager] Return 400 (not 500) when given a bad kuery param (#77796) #78085

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 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 @@ -30,6 +30,7 @@ import {
import * as AgentService from '../../services/agents';
import * as APIKeyService from '../../services/api_keys';
import { appContextService } from '../../services/app_context';
import { defaultIngestErrorHandler } from '../../errors';

export const getAgentHandler: RequestHandler<TypeOf<
typeof GetOneAgentRequestSchema.params
Expand Down Expand Up @@ -83,17 +84,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 @@ -288,11 +285,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');
});
});
}