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

[Fleet] Simplify skipArchive behaviour for input packages #141671

Merged
merged 9 commits into from
Oct 13, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ describe('When using EPM `get` services', () => {
name: 'my-package',
version: '1.0.0',
} as RegistryPackage);
MockRegistry.fetchInfo.mockResolvedValue({
name: 'my-package',
version: '1.0.0',
} as RegistryPackage);
MockRegistry.getPackage.mockResolvedValue({
paths: [],
packageInfo: {
Expand Down
11 changes: 5 additions & 6 deletions x-pack/plugins/fleet/server/services/epm/packages/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,12 @@ export async function getPackageInfo({
// If same version is available in registry and skipArchive is true, use the info from the registry (faster),
// otherwise build it from the archive
let paths: string[];
let packageInfo: RegistryPackage | ArchivePackage | undefined = skipArchive
? await Registry.fetchInfo(pkgName, resolvedPkgVersion).catch(() => undefined)
: undefined;

const registryInfo = await Registry.fetchInfo(pkgName, resolvedPkgVersion).catch(() => undefined);
let packageInfo;
// We need to get input only packages from source to get all fields
// see https://github.com/elastic/package-registry/issues/864
if (packageInfo && packageInfo.type !== 'input') {
if (registryInfo && skipArchive && registryInfo.type !== 'input') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: isn't this the same as skipArchive && registryInfo.type !== 'input' ?

packageInfo = registryInfo;
// Fix the paths
paths =
packageInfo.assets?.map((path) =>
Expand Down Expand Up @@ -293,7 +292,7 @@ export async function getPackageFromSource(options: {
}
} else {
res = await Registry.getPackage(pkgName, pkgVersion, { ignoreUnverified });
logger.debug(`retrieved uninstalled package ${pkgName}-${pkgVersion}`);
logger.debug(`retrieved package ${pkgName}-${pkgVersion} from registry`);
}
if (!res) {
throw new FleetError(`package info for ${pkgName}-${pkgVersion} does not exist`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ async function getPackageInfoFromArchiveOrCache(
archiveBuffer,
ensureContentType(archivePath)
);
setPackageInfo({ packageInfo, name, version });
// set the download URL as it isn't contained in the manifest
// this allows us to re-download the archive during package install
setPackageInfo({ packageInfo: { ...packageInfo, download: archivePath }, name, version });
return packageInfo;
} else {
return cachedInfo;
Expand Down
12 changes: 12 additions & 0 deletions x-pack/test/fleet_api_integration/apis/epm/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ export default function (providerContext: FtrProviderContext) {
expect(packageInfo.name).to.equal('apache');
await uninstallPackage(testPkgName, testPkgVersion);
});
it('should return all fields for input only packages', async function () {
// input packages have to get their package info from the manifest directly
// not from the package registry. This is because they contain a field the registry
// does not support
const res = await supertest
.get(`/api/fleet/epm/packages/integration_to_input/0.9.1`)
.expect(200);

const packageInfo = res.body.item;
expect(packageInfo.policy_templates.length).to.equal(1);
expect(packageInfo.policy_templates[0].vars).not.to.be(undefined);
});
describe('Pkg verification', () => {
it('should return validation error for unverified input only pkg', async function () {
const res = await supertest.get(`/api/fleet/epm/packages/input_only/0.1.0`).expect(400);
Expand Down
66 changes: 66 additions & 0 deletions x-pack/test/fleet_api_integration/apis/package_policy/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,72 @@ export default function (providerContext: FtrProviderContext) {
expect(policy.name).to.equal(nameWithWhitespace.trim());
});

describe('input only packages', () => {
it('should return 400 if dataset not provided for input only pkg', async function () {
await supertest
.post(`/api/fleet/package_policies`)
.set('kbn-xsrf', 'xxxx')
.send({
policy_id: agentPolicyId,
package: {
name: 'integration_to_input',
version: '0.9.1',
},
name: 'integration_to_input-1',
description: '',
namespace: 'default',
inputs: {
'logs-logfile': {
enabled: true,
streams: {
'integration_to_input.logs': {
enabled: true,
vars: {
paths: ['/tmp/test.log'],
tags: ['tag1'],
ignore_older: '72h',
},
},
},
},
},
})
.expect(400);
});
it('should successfully create an input only package policy with all required vars', async function () {
await supertest
.post(`/api/fleet/package_policies`)
.set('kbn-xsrf', 'xxxx')
.send({
policy_id: agentPolicyId,
package: {
name: 'integration_to_input',
version: '0.9.1',
},
name: 'integration_to_input-2',
description: '',
namespace: 'default',
inputs: {
'logs-logfile': {
enabled: true,
streams: {
'integration_to_input.logs': {
enabled: true,
vars: {
paths: ['/tmp/test.log'],
tags: ['tag1'],
ignore_older: '72h',
'data_stream.dataset': 'generic',
},
},
},
},
},
})
.expect(200);
});
});

describe('Simplified package policy', () => {
it('should work with valid values', async () => {
await supertest
Expand Down