Skip to content

Commit

Permalink
elastic#68019 Enforce unique package config names, per agent config
Browse files Browse the repository at this point in the history
  • Loading branch information
jen-huang committed Jul 10, 2020
1 parent d3301ac commit cf8fae3
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 0 deletions.
29 changes: 29 additions & 0 deletions x-pack/plugins/ingest_manager/server/services/package_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ class PackageConfigService {
packageConfig: NewPackageConfig,
options?: { id?: string; user?: AuthenticatedUser }
): Promise<PackageConfig> {
// Check that its agent config does not have a package config with the same name
const parentAgentConfig = await agentConfigService.get(soClient, packageConfig.config_id);
if (!parentAgentConfig) {
throw new Error('Agent config not found');
} else {
if (
(parentAgentConfig.package_configs as PackageConfig[]).find(
(siblingPackageConfig) => siblingPackageConfig.name === packageConfig.name
)
) {
throw new Error('There is already a package with the same name on this agent config');
}
}

// Make sure the associated package is installed
if (packageConfig.package?.name) {
const [, pkgInfo] = await Promise.all([
Expand Down Expand Up @@ -225,6 +239,21 @@ class PackageConfigService {
throw new Error('Package config not found');
}

// Check that its agent config does not have a package config with the same name
const parentAgentConfig = await agentConfigService.get(soClient, packageConfig.config_id);
if (!parentAgentConfig) {
throw new Error('Agent config not found');
} else {
if (
(parentAgentConfig.package_configs as PackageConfig[]).find(
(siblingPackageConfig) =>
siblingPackageConfig.id !== id && siblingPackageConfig.name === packageConfig.name
)
) {
throw new Error('There is already a package with the same name on this agent config');
}
}

await soClient.update<PackageConfigSOAttributes>(
SAVED_OBJECT_TYPE,
id,
Expand Down
1 change: 1 addition & 0 deletions x-pack/test/ingest_manager_api_integration/apis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export default function ({ loadTestFile }) {

// Package configs
loadTestFile(require.resolve('./package_config/create'));
loadTestFile(require.resolve('./package_config/update'));
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,48 @@ export default function ({ getService }: FtrProviderContext) {
warnAndSkipTest(this, log);
}
});

it('should return a 500 if there is another package config with the same name', async function () {
if (server.enabled) {
await supertest
.post(`/api/ingest_manager/package_configs`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'same-name-test-1',
description: '',
namespace: 'default',
config_id: agentConfigId,
enabled: true,
output_id: '',
inputs: [],
package: {
name: 'filetest',
title: 'For File Tests',
version: '0.1.0',
},
})
.expect(200);
await supertest
.post(`/api/ingest_manager/package_configs`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'same-name-test-1',
description: '',
namespace: 'default',
config_id: agentConfigId,
enabled: true,
output_id: '',
inputs: [],
package: {
name: 'filetest',
title: 'For File Tests',
version: '0.1.0',
},
})
.expect(500);
} else {
warnAndSkipTest(this, log);
}
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
import { warnAndSkipTest } from '../../helpers';

export default function ({ getService }: FtrProviderContext) {
const log = getService('log');
const supertest = getService('supertest');
const dockerServers = getService('dockerServers');

const server = dockerServers.get('registry');
// use function () {} and not () => {} here
// because `this` has to point to the Mocha context
// see https://mochajs.org/#arrow-functions

describe('Package Config - update', async function () {
let agentConfigId: string;
let packageConfigId: string;
let packageConfigId2: string;

before(async function () {
const { body: agentConfigResponse } = await supertest
.post(`/api/ingest_manager/agent_configs`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'Test config',
namespace: 'default',
});
agentConfigId = agentConfigResponse.item.id;

const { body: packageConfigResponse } = await supertest
.post(`/api/ingest_manager/package_configs`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'filetest-1',
description: '',
namespace: 'default',
config_id: agentConfigId,
enabled: true,
output_id: '',
inputs: [],
package: {
name: 'filetest',
title: 'For File Tests',
version: '0.1.0',
},
});
packageConfigId = packageConfigResponse.item.id;

const { body: packageConfigResponse2 } = await supertest
.post(`/api/ingest_manager/package_configs`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'filetest-2',
description: '',
namespace: 'default',
config_id: agentConfigId,
enabled: true,
output_id: '',
inputs: [],
package: {
name: 'filetest',
title: 'For File Tests',
version: '0.1.0',
},
});
packageConfigId2 = packageConfigResponse2.item.id;
});

it('should work with valid values', async function () {
if (server.enabled) {
const { body: apiResponse } = await supertest
.put(`/api/ingest_manager/package_configs/${packageConfigId}`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'filetest-1',
description: '',
namespace: 'updated_namespace',
config_id: agentConfigId,
enabled: true,
output_id: '',
inputs: [],
package: {
name: 'filetest',
title: 'For File Tests',
version: '0.1.0',
},
})
.expect(200);

expect(apiResponse.success).to.be(true);
} else {
warnAndSkipTest(this, log);
}
});

it('should return a 500 if there is another package config with the same name', async function () {
if (server.enabled) {
await supertest
.put(`/api/ingest_manager/package_configs/${packageConfigId2}`)
.set('kbn-xsrf', 'xxxx')
.send({
name: 'filetest-1',
description: '',
namespace: 'updated_namespace',
config_id: agentConfigId,
enabled: true,
output_id: '',
inputs: [],
package: {
name: 'filetest',
title: 'For File Tests',
version: '0.1.0',
},
})
.expect(500);
} else {
warnAndSkipTest(this, log);
}
});
});
}

0 comments on commit cf8fae3

Please sign in to comment.