Skip to content

Commit 8cd00dd

Browse files
John Schulzelasticmachine
andauthored
[Ingest Manager] API sends 404 when package config id is missing (#73212) (#73659)
* Add test to confirm missing config responds w/ 404 Currently failing with a 500 as in #66388 * Use after() to remove items added by test. The test initally failed with a 500 when the `after` was added. Debugging narrowed it down to a missing default config. getDefaultAgentConfigId errors if there isn't a default config. The config is added by `setupIngestManager` which _was_ always called during plugin#start but is no longer. We could add the setup call to the test/suite, but instead I changed AgentConfigService.delete to use ensureDefaultAgentConfig instead of getDefaultAgentConfigId. ensureDefaultAgentConfig adds one if it's missing. The check in delete is to make sure we don't delete the default config. We can still do that and now we add a config if it wasn't already there (which seems like A Good Thing) * Fix package config path in OpenApi spec * Return 404 if package config id is invalid/missing * Change test for error displayed text Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent d9dfd96 commit 8cd00dd

File tree

6 files changed

+108
-13
lines changed

6 files changed

+108
-13
lines changed

x-pack/plugins/ingest_manager/common/openapi/spec_oas3.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@
922922
},
923923
"parameters": []
924924
},
925-
"/packageConfigs": {
925+
"/package_configs": {
926926
"get": {
927927
"summary": "PackageConfigs - List",
928928
"tags": [],
@@ -1237,7 +1237,7 @@
12371237
]
12381238
}
12391239
},
1240-
"/packageConfigs/{packageConfigId}": {
1240+
"/package_configs/{packageConfigId}": {
12411241
"get": {
12421242
"summary": "PackageConfigs - Info",
12431243
"tags": [],

x-pack/plugins/ingest_manager/server/routes/package_config/handlers.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
import { TypeOf } from '@kbn/config-schema';
77
import Boom from 'boom';
8-
import { RequestHandler } from 'src/core/server';
8+
import { RequestHandler, SavedObjectsErrorHelpers } from '../../../../../../src/core/server';
99
import { appContextService, packageConfigService } from '../../services';
1010
import { getPackageInfo } from '../../services/epm/packages';
1111
import {
@@ -49,26 +49,31 @@ export const getOnePackageConfigHandler: RequestHandler<TypeOf<
4949
typeof GetOnePackageConfigRequestSchema.params
5050
>> = async (context, request, response) => {
5151
const soClient = context.core.savedObjects.client;
52+
const { packageConfigId } = request.params;
53+
const notFoundResponse = () =>
54+
response.notFound({ body: { message: `Package config ${packageConfigId} not found` } });
55+
5256
try {
53-
const packageConfig = await packageConfigService.get(soClient, request.params.packageConfigId);
57+
const packageConfig = await packageConfigService.get(soClient, packageConfigId);
5458
if (packageConfig) {
5559
return response.ok({
5660
body: {
5761
item: packageConfig,
5862
success: true,
5963
},
6064
});
65+
} else {
66+
return notFoundResponse();
67+
}
68+
} catch (e) {
69+
if (SavedObjectsErrorHelpers.isNotFoundError(e)) {
70+
return notFoundResponse();
6171
} else {
6272
return response.customError({
63-
statusCode: 404,
64-
body: { message: 'Package config not found' },
73+
statusCode: 500,
74+
body: { message: e.message },
6575
});
6676
}
67-
} catch (e) {
68-
return response.customError({
69-
statusCode: 500,
70-
body: { message: e.message },
71-
});
7277
}
7378
};
7479

x-pack/plugins/ingest_manager/server/services/agent_config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ class AgentConfigService {
334334
throw new Error('Agent configuration not found');
335335
}
336336

337-
const defaultConfigId = await this.getDefaultAgentConfigId(soClient);
337+
const { id: defaultConfigId } = await this.ensureDefaultAgentConfig(soClient);
338338
if (id === defaultConfigId) {
339339
throw new Error('The default agent configuration cannot be deleted');
340340
}

x-pack/test/ingest_manager_api_integration/apis/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default function ({ loadTestFile }) {
2222
// Package configs
2323
loadTestFile(require.resolve('./package_config/create'));
2424
loadTestFile(require.resolve('./package_config/update'));
25+
loadTestFile(require.resolve('./package_config/get'));
2526
// Agent config
2627
loadTestFile(require.resolve('./agent_config/index'));
2728
});
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import expect from '@kbn/expect';
8+
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
9+
import { skipIfNoDockerRegistry } from '../../helpers';
10+
11+
export default function (providerContext: FtrProviderContext) {
12+
const { getService } = providerContext;
13+
const supertest = getService('supertest');
14+
const dockerServers = getService('dockerServers');
15+
16+
const server = dockerServers.get('registry');
17+
// use function () {} and not () => {} here
18+
// because `this` has to point to the Mocha context
19+
// see https://mochajs.org/#arrow-functions
20+
21+
describe('Package Config - get by id', async function () {
22+
skipIfNoDockerRegistry(providerContext);
23+
let agentConfigId: string;
24+
let packageConfigId: string;
25+
26+
before(async function () {
27+
if (!server.enabled) {
28+
return;
29+
}
30+
const { body: agentConfigResponse } = await supertest
31+
.post(`/api/ingest_manager/agent_configs`)
32+
.set('kbn-xsrf', 'xxxx')
33+
.send({
34+
name: 'Test config',
35+
namespace: 'default',
36+
});
37+
agentConfigId = agentConfigResponse.item.id;
38+
39+
const { body: packageConfigResponse } = await supertest
40+
.post(`/api/ingest_manager/package_configs`)
41+
.set('kbn-xsrf', 'xxxx')
42+
.send({
43+
name: 'filetest-1',
44+
description: '',
45+
namespace: 'default',
46+
config_id: agentConfigId,
47+
enabled: true,
48+
output_id: '',
49+
inputs: [],
50+
package: {
51+
name: 'filetest',
52+
title: 'For File Tests',
53+
version: '0.1.0',
54+
},
55+
});
56+
packageConfigId = packageConfigResponse.item.id;
57+
});
58+
59+
after(async function () {
60+
if (!server.enabled) {
61+
return;
62+
}
63+
64+
await supertest
65+
.post(`/api/ingest_manager/agent_configs/delete`)
66+
.set('kbn-xsrf', 'xxxx')
67+
.send({ agentConfigId })
68+
.expect(200);
69+
70+
await supertest
71+
.post(`/api/ingest_manager/package_configs/delete`)
72+
.set('kbn-xsrf', 'xxxx')
73+
.send({ packageConfigIds: [packageConfigId] })
74+
.expect(200);
75+
});
76+
77+
it('should succeed with a valid id', async function () {
78+
const { body: apiResponse } = await supertest
79+
.get(`/api/ingest_manager/package_configs/${packageConfigId}`)
80+
.expect(200);
81+
82+
expect(apiResponse.success).to.be(true);
83+
});
84+
85+
it('should return a 404 with an invalid id', async function () {
86+
await supertest.get(`/api/ingest_manager/package_configs/IS_NOT_PRESENT`).expect(404);
87+
});
88+
});
89+
}

x-pack/test/security_solution_endpoint/apps/endpoint/policy_details.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
2727
await pageObjects.policy.navigateToPolicyDetails('invalid-id');
2828
await testSubjects.existOrFail('policyDetailsIdNotFoundMessage');
2929
expect(await testSubjects.getVisibleText('policyDetailsIdNotFoundMessage')).to.equal(
30-
'Saved object [ingest-package-configs/invalid-id] not found'
30+
'Package config invalid-id not found'
3131
);
3232
});
3333
});

0 commit comments

Comments
 (0)