Skip to content

Commit

Permalink
[8.x] [Fleet] Improve policy_secrets test suite performance (elastic#…
Browse files Browse the repository at this point in the history
…193359) (elastic#193476)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Fleet] Improve policy_secrets test suite performance
(elastic#193359)](elastic#193359)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Nicolas
Chaulet","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-09-19T19:39:46Z","message":"[Fleet]
Improve policy_secrets test suite performance
(elastic#193359)","sha":"8bd90857ed667cbbcbe46041a4518366baa57883","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","v9.0.0","backport:prev-minor","v8.16.0"],"title":"[Fleet]
Improve policy_secrets test suite
performance","number":193359,"url":"https://github.com/elastic/kibana/pull/193359","mergeCommit":{"message":"[Fleet]
Improve policy_secrets test suite performance
(elastic#193359)","sha":"8bd90857ed667cbbcbe46041a4518366baa57883"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/193359","number":193359,"mergeCommit":{"message":"[Fleet]
Improve policy_secrets test suite performance
(elastic#193359)","sha":"8bd90857ed667cbbcbe46041a4518366baa57883"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Nicolas Chaulet <[email protected]>
  • Loading branch information
kibanamachine and nchaulet authored Sep 19, 2024
1 parent 438284e commit 96f67b6
Showing 1 changed file with 81 additions and 50 deletions.
131 changes: 81 additions & 50 deletions x-pack/test/fleet_api_integration/apis/policy_secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,33 +108,28 @@ export default function (providerContext: FtrProviderContext) {
.expect(200);
}

try {
await es.deleteByQuery({
await Promise.all([
es.deleteByQuery({
index: ENROLLMENT_API_KEYS_INDEX,
refresh: true,
body: {
query: {
match_all: {},
},
},
});
} catch (err) {
// index doesn't exist
}

try {
await es.deleteByQuery({
}),
es.deleteByQuery({
index: AGENT_POLICY_INDEX,
refresh: true,
body: {
query: {
match_all: {},
},
},
});
} catch (err) {
}),
]).catch((err) => {
// index doesn't exist
}
});
};

const cleanupAgents = async () => {
Expand All @@ -157,6 +152,7 @@ export default function (providerContext: FtrProviderContext) {
try {
await es.deleteByQuery({
index: SECRETS_INDEX_NAME,
refresh: true,
body: {
query: {
match_all: {},
Expand Down Expand Up @@ -379,6 +375,7 @@ export default function (providerContext: FtrProviderContext) {
skipIfNoDockerRegistry(providerContext);

before(async () => {
await kibanaServer.savedObjects.cleanStandardList();
await fleetAndAgents.setup();
await getService('esArchiver').load(
'x-pack/test/functional/es_archives/fleet/empty_fleet_server'
Expand All @@ -389,20 +386,15 @@ export default function (providerContext: FtrProviderContext) {
await getService('esArchiver').unload(
'x-pack/test/functional/es_archives/fleet/empty_fleet_server'
);
});

afterEach(async () => {
await cleanupAgents();
await cleanupPolicies();
await cleanupSecrets();
await kibanaServer.savedObjects.cleanStandardList();
});

describe('create package policy with secrets', () => {
let testAgentPolicy: any;
let fleetServerAgentPolicy: any;
let packagePolicyWithSecrets: any;

beforeEach(async () => {
before(async () => {
// Policy secrets require at least one Fleet server on v8.10+
const createFleetServerAgentPolicyRes = await createFleetServerAgentPolicy();
fleetServerAgentPolicy = createFleetServerAgentPolicyRes.fleetServerAgentPolicy;
Expand All @@ -416,6 +408,11 @@ export default function (providerContext: FtrProviderContext) {
packagePolicyWithSecrets = await createPackagePolicyWithSecrets(testAgentPolicy.id);
});

after(async () => {
await Promise.all([cleanupAgents(), cleanupSecrets()]);
await cleanupPolicies();
});

it('should correctly create the policy with secrets', async () => {
const packageVarId = packagePolicyWithSecrets.vars.package_var_secret.value.id;

Expand Down Expand Up @@ -598,7 +595,7 @@ export default function (providerContext: FtrProviderContext) {
let packagePolicyWithSecrets: any;
let updatedPackagePolicy: any;

beforeEach(async () => {
before(async () => {
// Policy secrets require at least one Fleet server on v8.10+
const createFleetServerAgentPolicyRes = await createFleetServerAgentPolicy();
fleetServerAgentPolicy = createFleetServerAgentPolicyRes.fleetServerAgentPolicy;
Expand All @@ -623,6 +620,12 @@ export default function (providerContext: FtrProviderContext) {
updatedPackagePolicy = updateRes.body.item;
});

after(async () => {
await cleanupAgents();
await cleanupPolicies();
await cleanupSecrets();
});

it('should allow secret values to be updated (single policy update API)', async () => {
const updatedPackageVarId = updatedPackagePolicy.vars.package_var_secret.value.id;
expect(updatedPackageVarId).to.be.a('string');
Expand Down Expand Up @@ -698,7 +701,7 @@ export default function (providerContext: FtrProviderContext) {
let duplicatedAgentPolicy: any;
let duplicatedPackagePolicy: any;

beforeEach(async () => {
before(async () => {
// Policy secrets require at least one Fleet server on v8.10+
const createFleetServerAgentPolicyRes = await createFleetServerAgentPolicy();
fleetServerAgentPolicy = createFleetServerAgentPolicyRes.fleetServerAgentPolicy;
Expand Down Expand Up @@ -727,6 +730,12 @@ export default function (providerContext: FtrProviderContext) {
duplicatedPackagePolicy = duplicatedAgentPolicy.package_policies[0];
});

after(async () => {
await cleanupAgents();
await cleanupPolicies();
await cleanupSecrets();
});

it('should not duplicate secrets after duplicating agent policy', async () => {
const packageVarId = duplicatedPackagePolicy.vars.package_var_secret.value.id;
const inputVarId = duplicatedPackagePolicy.inputs[0].vars.input_var_secret.value.id;
Expand Down Expand Up @@ -813,7 +822,7 @@ export default function (providerContext: FtrProviderContext) {
let fleetServerAgentPolicy: any;
let packagePolicyWithSecrets: any;

beforeEach(async () => {
before(async () => {
// Policy secrets require at least one Fleet server on v8.10+
const createFleetServerAgentPolicyRes = await createFleetServerAgentPolicy();
fleetServerAgentPolicy = createFleetServerAgentPolicyRes.fleetServerAgentPolicy;
Expand All @@ -827,6 +836,12 @@ export default function (providerContext: FtrProviderContext) {
packagePolicyWithSecrets = await createPackagePolicyWithSecrets(testAgentPolicy.id);
});

after(async () => {
await cleanupAgents();
await cleanupPolicies();
await cleanupSecrets();
});

it('should delete all secrets on package policy delete', async () => {
await deletePackagePolicy(packagePolicyWithSecrets.id);

Expand All @@ -844,6 +859,11 @@ export default function (providerContext: FtrProviderContext) {
});

describe('fleet server version requirements', () => {
afterEach(async () => {
await cleanupAgents();
await cleanupPolicies();
await cleanupSecrets();
});
it('should not store secrets if fleet server does not meet minimum version', async () => {
const { fleetServerAgentPolicy } = await createFleetServerAgentPolicy();
await createFleetServerAgent(fleetServerAgentPolicy.id, 'server_1', '7.0.0');
Expand Down Expand Up @@ -1117,41 +1137,52 @@ export default function (providerContext: FtrProviderContext) {
});
});

// TODO: Output secrets should be moved to another test suite
it('should return output secrets if policy uses output with secrets', async () => {
// Output secrets require at least one Fleet server on 8.12.0 or higher (and none under 8.12.0).
const { fleetServerAgentPolicy } = await createFleetServerAgentPolicy();
await createFleetServerAgent(fleetServerAgentPolicy.id, 'server_1', '8.12.0');
await callFleetSetup();
describe('output secrets', () => {
afterEach(async () => {
await cleanupAgents();
await cleanupPolicies();
await cleanupSecrets();
});

const outputWithSecret = await createOutputWithSecret();
// TODO: Output secrets should be moved to another test suite
it('should return output secrets if policy uses output with secrets', async () => {
// ensure output is created
await callFleetSetup();

const { body: agentPolicyResponse } = await supertest
.post(`/api/fleet/agent_policies`)
.set('kbn-xsrf', 'xxxx')
.send({
name: `Test policy ${uuidv4()}`,
namespace: 'default',
data_output_id: outputWithSecret.id,
monitoring_output_id: outputWithSecret.id,
})
.expect(200);
// Output secrets require at least one Fleet server on 8.12.0 or higher (and none under 8.12.0).
const { fleetServerAgentPolicy } = await createFleetServerAgentPolicy();
await createFleetServerAgent(fleetServerAgentPolicy.id, 'server_1', '8.12.0');
await callFleetSetup();

const fullAgentPolicy = await getFullAgentPolicyById(agentPolicyResponse.item.id);
const outputWithSecret = await createOutputWithSecret();

const passwordSecretId = outputWithSecret!.secrets?.password?.id;
const { body: agentPolicyResponse } = await supertest
.post(`/api/fleet/agent_policies`)
.set('kbn-xsrf', 'xxxx')
.send({
name: `Test policy ${uuidv4()}`,
namespace: 'default',
data_output_id: outputWithSecret.id,
monitoring_output_id: outputWithSecret.id,
})
.expect(200);

expect(fullAgentPolicy.secret_references).to.eql([{ id: passwordSecretId }]);
const fullAgentPolicy = await getFullAgentPolicyById(agentPolicyResponse.item.id);

const output = Object.entries(fullAgentPolicy.outputs)[0][1];
// @ts-expect-error
expect(output.secrets.password.id).to.eql(passwordSecretId);
const passwordSecretId = outputWithSecret!.secrets?.password?.id;

// delete output with secret
await supertest
.delete(`/api/fleet/outputs/${outputWithSecret.id}`)
.set('kbn-xsrf', 'xxxx')
.expect(200);
expect(fullAgentPolicy.secret_references).to.eql([{ id: passwordSecretId }]);

const output = Object.entries(fullAgentPolicy.outputs)[0][1];
// @ts-expect-error
expect(output.secrets.password.id).to.eql(passwordSecretId);

// delete output with secret
await supertest
.delete(`/api/fleet/outputs/${outputWithSecret.id}`)
.set('kbn-xsrf', 'xxxx')
.expect(200);
});
});
});
}

0 comments on commit 96f67b6

Please sign in to comment.