-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fleet] rollback input package install on failure (#182665)
## Summary Closes #181032 2 improvements on input package policy creation failure handling: - if the package was not installed initially, rolling back on failure - only saving es references with the input package installation if the templates are added successfully, to prevent issues with upgrade later if the references would contain invalid template names - this is needed if the input package was installed before attempting to add a package policy, in this case we don't want to completely uninstall the package on failure To verify: Custom Logs package uninstalled: - add Custom Logs integration with dataset name with a * in it e.g. `generic*` - the package policy creation is expected to fail - verify that the Custom Logs package is not installed Custom Logs package installed: - Install Custom Logs package without package policy or add integration with the default dataset name to succeed - try adding another policy with dataset `generic*` - the package policy creation is expected to fail - verify that the Custom Logs package doesn't have any `installed_es` references with the invalid `generic*` prefix `GET .kibana_ingest/_search?q=epm-packages.name:log` ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --------- Co-authored-by: Jen Huang <its.jenetic@gmail.com>
- Loading branch information
1 parent
718e6f0
commit 0833045
Showing
5 changed files
with
186 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
x-pack/test/fleet_api_integration/apis/package_policy/input_package_rollback.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import expect from '@kbn/expect'; | ||
import { sortBy } from 'lodash'; | ||
import { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; | ||
import { skipIfNoDockerRegistry } from '../../helpers'; | ||
import { setupFleetAndAgents } from '../agents/services'; | ||
const PACKAGE_NAME = 'input_package_upgrade'; | ||
const START_VERSION = '1.0.0'; | ||
|
||
const expectIdArraysEqual = (arr1: any[], arr2: any[]) => { | ||
expect(sortBy(arr1, 'id')).to.eql(sortBy(arr2, 'id')); | ||
}; | ||
export default function (providerContext: FtrProviderContext) { | ||
const { getService } = providerContext; | ||
const supertest = getService('supertest'); | ||
const uninstallPackage = async (name: string, version: string) => { | ||
await supertest.delete(`/api/fleet/epm/packages/${name}/${version}`).set('kbn-xsrf', 'xxxx'); | ||
}; | ||
|
||
const installPackage = async (name: string, version: string) => { | ||
return await supertest | ||
.post(`/api/fleet/epm/packages/${name}/${version}`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ force: true }) | ||
.expect(200); | ||
}; | ||
|
||
const getInstallationSavedObject = async (name: string, version: string) => { | ||
const res = await supertest.get(`/api/fleet/epm/packages/${name}/${version}`).expect(200); | ||
return res.body.item.savedObject.attributes; | ||
}; | ||
|
||
const getPackage = async (name: string, version: string) => { | ||
const res = await supertest.get(`/api/fleet/epm/packages/${name}/${version}`).expect(200); | ||
return res.body.item; | ||
}; | ||
|
||
const createPackagePolicyWithDataset = async ( | ||
agentPolicyId: string, | ||
dataset: string, | ||
expectStatusCode = 200, | ||
force = false | ||
) => { | ||
const policy = { | ||
force, | ||
policy_id: agentPolicyId, | ||
package: { | ||
name: PACKAGE_NAME, | ||
version: START_VERSION, | ||
}, | ||
name: 'test-policy-' + dataset, | ||
description: '', | ||
namespace: 'default', | ||
inputs: { | ||
'logs-logfile': { | ||
enabled: true, | ||
streams: { | ||
'input_package_upgrade.logs': { | ||
enabled: true, | ||
vars: { | ||
paths: ['/tmp/test/log'], | ||
tags: ['tag1'], | ||
ignore_older: '72h', | ||
'data_stream.dataset': dataset, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
const res = await supertest | ||
.post(`/api/fleet/package_policies`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send(policy) | ||
.expect(expectStatusCode); | ||
|
||
return res.body.item; | ||
}; | ||
|
||
const createAgentPolicy = async (name = 'Input Package Test 3') => { | ||
const res = await supertest | ||
.post(`/api/fleet/agent_policies`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ | ||
name, | ||
namespace: 'default', | ||
}) | ||
.expect(200); | ||
return res.body.item; | ||
}; | ||
|
||
const deleteAgentPolicy = async (agentPolicyId: string) => { | ||
if (!agentPolicyId) return; | ||
return supertest | ||
.post(`/api/fleet/agent_policies/delete`) | ||
.set('kbn-xsrf', 'xxxx') | ||
.send({ agentPolicyId }); | ||
}; | ||
|
||
describe('input package policy rollback', async function () { | ||
skipIfNoDockerRegistry(providerContext); | ||
|
||
let agentPolicyId: string; | ||
before(async () => { | ||
const agentPolicy = await createAgentPolicy(); | ||
agentPolicyId = agentPolicy.id; | ||
}); | ||
|
||
after(async () => { | ||
await deleteAgentPolicy(agentPolicyId); | ||
}); | ||
setupFleetAndAgents(providerContext); | ||
|
||
it('should rollback package install on package policy create failure', async () => { | ||
await createPackagePolicyWithDataset(agentPolicyId, 'test*', 400); | ||
|
||
const pkg = await getPackage(PACKAGE_NAME, START_VERSION); | ||
expect(pkg?.status).to.eql('not_installed'); | ||
}); | ||
|
||
it('should not add es references on package policy create failure when package is already installed', async () => { | ||
await installPackage(PACKAGE_NAME, START_VERSION); | ||
await createPackagePolicyWithDataset(agentPolicyId, 'test*', 400); | ||
|
||
const installation = await getInstallationSavedObject(PACKAGE_NAME, START_VERSION); | ||
expectIdArraysEqual(installation.installed_es, []); | ||
|
||
await uninstallPackage(PACKAGE_NAME, START_VERSION); | ||
}); | ||
}); | ||
} |