Skip to content

Commit 9e7fb91

Browse files
juliaElasticnickpeihl
authored andcommitted
[Fleet] Add integration test to verify rollback integrations in multiple spaces (elastic#238025)
## Summary Closes https://github.com/elastic/ingest-dev/issues/5451 ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [ ] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [ ] [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 - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) - [ ] Review the [backport guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing) and apply applicable `backport:*` labels. ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ...
1 parent 0dcb12c commit 9e7fb91

4 files changed

Lines changed: 244 additions & 0 deletions

File tree

x-pack/platform/test/fleet_api_integration/apis/space_awareness/api_helper.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,21 @@ export class SpaceTestApiClient {
144144
expectStatusCode200(res);
145145
}
146146

147+
async upgradePackagePolicies(
148+
spaceId?: string,
149+
packagePolicyIds: string[] = []
150+
): Promise<CreatePackagePolicyResponse> {
151+
const res = await this.supertest
152+
.post(`${this.getBaseUrl(spaceId)}/api/fleet/package_policies/upgrade`)
153+
.auth(this.auth.username, this.auth.password)
154+
.set('kbn-xsrf', 'xxxx')
155+
.send({ packagePolicyIds });
156+
157+
expectStatusCode200(res);
158+
159+
return res.body;
160+
}
161+
147162
async getPackagePolicy(
148163
packagePolicyId: string,
149164
spaceId?: string
@@ -521,6 +536,18 @@ export class SpaceTestApiClient {
521536
return res.body;
522537
}
523538

539+
async rollbackPackage({ pkgName }: { pkgName: string }, spaceId?: string) {
540+
const res = await this.supertest
541+
.post(`${this.getBaseUrl(spaceId)}/api/fleet/epm/packages/${pkgName}/rollback`)
542+
.auth(this.auth.username, this.auth.password)
543+
.set('kbn-xsrf', 'xxxx')
544+
.send({});
545+
546+
expectStatusCode200(res);
547+
548+
return res.body;
549+
}
550+
524551
async uninstallPackage(
525552
{ pkgName, pkgVersion, force }: { pkgName: string; pkgVersion: string; force?: boolean },
526553
spaceId?: string

x-pack/platform/test/fleet_api_integration/apis/space_awareness/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ export default function loadTests({ loadTestFile }) {
2121
loadTestFile(require.resolve('./telemetry'));
2222
loadTestFile(require.resolve('./outputs'));
2323
loadTestFile(require.resolve('./package_policies'));
24+
loadTestFile(require.resolve('./package_rollback'));
2425
});
2526
}
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import expect from '@kbn/expect';
9+
import type { FtrProviderContext } from '../../../api_integration/ftr_provider_context';
10+
import { skipIfNoDockerRegistry } from '../../helpers';
11+
import { SpaceTestApiClient } from './api_helper';
12+
import { cleanFleetIndices, createTestSpace } from './helpers';
13+
import { setupTestUsers, testUsers } from '../test_users';
14+
15+
export default function (providerContext: FtrProviderContext) {
16+
const { getService } = providerContext;
17+
const supertest = getService('supertest');
18+
const supertestWithoutAuth = getService('supertestWithoutAuth');
19+
const esClient = getService('es');
20+
const kibanaServer = getService('kibanaServer');
21+
const spaces = getService('spaces');
22+
let TEST_SPACE_1: string;
23+
24+
describe('package rollback', function () {
25+
skipIfNoDockerRegistry(providerContext);
26+
const apiClientDefaultSpace = new SpaceTestApiClient(supertestWithoutAuth, {
27+
username: testUsers.fleet_all_int_all_default_space_only.username,
28+
password: testUsers.fleet_all_int_all_default_space_only.password,
29+
});
30+
const apiClientAllSpaces = new SpaceTestApiClient(supertestWithoutAuth, {
31+
username: testUsers.fleet_all_int_all.username,
32+
password: testUsers.fleet_all_int_all.password,
33+
});
34+
before(async () => {
35+
await setupTestUsers(getService('security'), true);
36+
TEST_SPACE_1 = spaces.getDefaultTestSpace();
37+
await kibanaServer.savedObjects.cleanStandardList();
38+
await kibanaServer.savedObjects.cleanStandardList({
39+
space: TEST_SPACE_1,
40+
});
41+
await cleanFleetIndices(esClient);
42+
43+
await apiClientDefaultSpace.postEnableSpaceAwareness();
44+
45+
await createTestSpace(providerContext, TEST_SPACE_1);
46+
});
47+
48+
after(async () => {
49+
await kibanaServer.savedObjects.cleanStandardList();
50+
await kibanaServer.savedObjects.cleanStandardList({
51+
space: TEST_SPACE_1,
52+
});
53+
await cleanFleetIndices(esClient);
54+
});
55+
56+
const packagePolicyId = 'multiple-versions-1';
57+
const policyIdInOtherSpace = 'multiple-versions-3';
58+
59+
beforeEach(async () => {
60+
await apiClientDefaultSpace.installPackage({
61+
pkgName: 'multiple_versions',
62+
force: true,
63+
pkgVersion: '0.1.0',
64+
});
65+
// Create package policies
66+
await apiClientDefaultSpace.createPackagePolicy(undefined, {
67+
id: packagePolicyId,
68+
policy_ids: [],
69+
package: {
70+
name: 'multiple_versions',
71+
version: '0.1.0',
72+
},
73+
name: packagePolicyId,
74+
description: '',
75+
namespace: '',
76+
inputs: {},
77+
});
78+
79+
await apiClientAllSpaces.createPackagePolicy(TEST_SPACE_1, {
80+
id: policyIdInOtherSpace,
81+
policy_ids: [],
82+
package: {
83+
name: 'multiple_versions',
84+
version: '0.1.0',
85+
},
86+
name: policyIdInOtherSpace,
87+
description: '',
88+
namespace: '',
89+
inputs: {},
90+
});
91+
});
92+
93+
afterEach(async () => {
94+
await supertest
95+
.delete(`/api/fleet/epm/packages/multiple_versions/0.1.0`)
96+
.set('kbn-xsrf', 'xxxx')
97+
.send({ force: true })
98+
.expect(200);
99+
100+
await apiClientDefaultSpace.deletePackagePolicy(packagePolicyId);
101+
await apiClientDefaultSpace.deletePackagePolicy(policyIdInOtherSpace);
102+
});
103+
104+
async function assertPackagePoliciesVersion(expectedVersion: string) {
105+
const res = await supertest
106+
.get(`/api/fleet/package_policies/${packagePolicyId}`)
107+
.set('kbn-xsrf', 'xxxx')
108+
.expect(200);
109+
expect(res.body.item.package.version).equal(expectedVersion);
110+
}
111+
112+
async function assertPackageInstallVersion(expectedVersion: string) {
113+
const res = await supertest
114+
.get(`/api/fleet/epm/packages/multiple_versions`)
115+
.set('kbn-xsrf', 'xxxx')
116+
.expect(200);
117+
118+
expect(res.body.item.installationInfo.version).equal(expectedVersion);
119+
}
120+
121+
async function upgradePackage(
122+
pkgName: string,
123+
oldPkgVersion: string,
124+
newPkgVersion: string,
125+
upgradePackagePolicies: boolean
126+
) {
127+
const res = await supertest
128+
.post(`/api/fleet/epm/packages/_bulk_upgrade`)
129+
.set('kbn-xsrf', 'xxxx')
130+
.send({
131+
packages: [{ name: pkgName, version: newPkgVersion }],
132+
prerelease: true,
133+
force: true,
134+
upgrade_package_policies: upgradePackagePolicies,
135+
})
136+
.expect(200);
137+
const maxTimeout = Date.now() + 60 * 1000;
138+
let lastPollResult: string = '';
139+
while (Date.now() < maxTimeout) {
140+
const pollRes = await supertest
141+
.get(`/api/fleet/epm/packages/_bulk_upgrade/${res.body.taskId}`)
142+
.set('kbn-xsrf', 'xxxx')
143+
.expect(200);
144+
145+
await new Promise((resolve) => setTimeout(resolve, 1000));
146+
147+
if (pollRes.body.status === 'success') {
148+
await assertPackageInstallVersion(newPkgVersion);
149+
await assertPackagePoliciesVersion(
150+
upgradePackagePolicies ? newPkgVersion : oldPkgVersion
151+
);
152+
return;
153+
}
154+
155+
lastPollResult = JSON.stringify(pollRes.body);
156+
}
157+
throw new Error(`bulk upgrade of ${pkgName} failed: ${lastPollResult}`);
158+
}
159+
160+
async function verifyBulkRollbackFailedResult(taskId: string) {
161+
const maxTimeout = Date.now() + 60 * 1000;
162+
let lastPollResult: string = '';
163+
while (Date.now() < maxTimeout) {
164+
const pollRes = await supertestWithoutAuth
165+
.get(`/api/fleet/epm/packages/_bulk_rollback/${taskId}`)
166+
.auth(
167+
testUsers.fleet_all_int_all_default_space_only.username,
168+
testUsers.fleet_all_int_all_default_space_only.password
169+
)
170+
.set('kbn-xsrf', 'xxxx')
171+
.expect(200);
172+
173+
await new Promise((resolve) => setTimeout(resolve, 1000));
174+
175+
if (pollRes.body.status === 'failed') {
176+
await assertPackageInstallVersion('0.2.0');
177+
await assertPackagePoliciesVersion('0.2.0');
178+
expect(pollRes.body.results[0].error.message).to.equal(
179+
'Not authorized to rollback integration policies in all spaces'
180+
);
181+
return;
182+
}
183+
184+
lastPollResult = JSON.stringify(pollRes.body);
185+
}
186+
187+
throw new Error(`bulk rollback of "multiple_versions" succeeded: ${lastPollResult}`);
188+
}
189+
190+
it('should fail _bulk_rollback if not allowed to update package policies in all spaces', async () => {
191+
await upgradePackage('multiple_versions', '0.1.0', '0.2.0', true);
192+
await apiClientAllSpaces.upgradePackagePolicies(TEST_SPACE_1, [policyIdInOtherSpace]);
193+
194+
const res = await supertestWithoutAuth
195+
.post(`/api/fleet/epm/packages/_bulk_rollback`)
196+
.auth(
197+
testUsers.fleet_all_int_all_default_space_only.username,
198+
testUsers.fleet_all_int_all_default_space_only.password
199+
)
200+
.set('kbn-xsrf', 'xxxx')
201+
.send({
202+
packages: [{ name: 'multiple_versions' }],
203+
})
204+
.expect(200);
205+
206+
await verifyBulkRollbackFailedResult(res.body.taskId);
207+
208+
// Verify rollback succeeds if user has permissions in all spaces
209+
await apiClientAllSpaces.rollbackPackage({ pkgName: 'multiple_versions' });
210+
211+
await assertPackageInstallVersion('0.1.0');
212+
await assertPackagePoliciesVersion('0.1.0');
213+
});
214+
});
215+
}

x-pack/platform/test/fleet_api_integration/config.space_awareness.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default async function ({ readConfigFile }: FtrConfigProviderContext) {
2121
'enableStrictKQLValidation',
2222
'subfeaturePrivileges',
2323
'useSpaceAwareness',
24+
'enablePackageRollback',
2425
])}`;
2526

2627
return {

0 commit comments

Comments
 (0)