-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Fleet] Test creating|copying a policy create a correct POLICY_CHANGE action #81661
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
Merged
nchaulet
merged 4 commits into
elastic:master
from
nchaulet:feature-test-agent-policy-policy-change-action
Oct 28, 2020
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9367cbc
[Fleet] Test creating|copying a policy create a correct POLICY_CHANGE…
nchaulet ad0bbfa
Fix after review
nchaulet 1924f3d
Fix after review
nchaulet eff7769
Merge branch 'master' into feature-test-agent-policy-policy-change-ac…
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
170 changes: 170 additions & 0 deletions
170
...k/test/ingest_manager_api_integration/apis/agent_policy/agent_policy_with_agents_setup.ts
This file contains hidden or 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,170 @@ | ||
| /* | ||
| * 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 { skipIfNoDockerRegistry } from '../../helpers'; | ||
| import { FtrProviderContext } from '../../../api_integration/ftr_provider_context'; | ||
| import { setupIngest, getSupertestWithoutAuth } from '../fleet/agents/services'; | ||
|
|
||
| export default function (providerContext: FtrProviderContext) { | ||
| const { getService } = providerContext; | ||
| const supertest = getService('supertest'); | ||
| const esArchiver = getService('esArchiver'); | ||
| const supertestWithoutAuth = getSupertestWithoutAuth(providerContext); | ||
| const kibanaServer = getService('kibanaServer'); | ||
|
|
||
| async function getEnrollmentKeyForPolicyId(policyId: string) { | ||
| const listRes = await supertest.get(`/api/fleet/enrollment-api-keys`).expect(200); | ||
|
|
||
| const key = listRes.body.list.find( | ||
| (item: { policy_id: string; id: string }) => item.policy_id === policyId | ||
| ); | ||
|
|
||
| expect(key).not.empty(); | ||
|
|
||
| const res = await supertest.get(`/api/fleet/enrollment-api-keys/${key.id}`).expect(200); | ||
|
|
||
| return res.body.item; | ||
| } | ||
|
|
||
| // Enroll an agent to get the actions for an agent as encrypted saved object are not expose otherwise | ||
| async function getAgentActionsForEnrollmentKey(enrollmentAPIToken: string) { | ||
| const kibanaVersionAccessor = kibanaServer.version; | ||
| const kibanaVersion = await kibanaVersionAccessor.get(); | ||
|
|
||
| const { body: enrollmentResponse } = await supertestWithoutAuth | ||
| .post(`/api/ingest_manager/fleet/agents/enroll`) | ||
| .set('kbn-xsrf', 'xxx') | ||
| .set('Authorization', `ApiKey ${enrollmentAPIToken}`) | ||
| .send({ | ||
| type: 'PERMANENT', | ||
| metadata: { | ||
| local: { | ||
| elastic: { agent: { version: kibanaVersion } }, | ||
| }, | ||
| user_provided: {}, | ||
| }, | ||
| }) | ||
| .expect(200); | ||
|
|
||
| const agentAccessAPIKey = enrollmentResponse.item.access_api_key; | ||
|
|
||
| // Agent checkin | ||
| const { body: checkinApiResponse } = await supertestWithoutAuth | ||
| .post(`/api/ingest_manager/fleet/agents/${enrollmentResponse.item.id}/checkin`) | ||
| .set('kbn-xsrf', 'xx') | ||
| .set('Authorization', `ApiKey ${agentAccessAPIKey}`) | ||
| .send({ | ||
| events: [], | ||
| }) | ||
| .expect(200); | ||
|
|
||
| expect(checkinApiResponse.actions).length(1); | ||
|
|
||
| return checkinApiResponse.actions[0]; | ||
| } | ||
|
|
||
| // Test all the side effect that should occurs when we create|update an agent policy | ||
| describe('ingest_manager_agent_policies_with_agents_setup', () => { | ||
| skipIfNoDockerRegistry(providerContext); | ||
|
|
||
| before(async () => { | ||
| await esArchiver.loadIfNeeded('fleet/agents'); | ||
| }); | ||
| after(async () => { | ||
| await esArchiver.unload('fleet/agents'); | ||
| }); | ||
|
|
||
| setupIngest(providerContext); | ||
|
|
||
| describe('POST /api/fleet/agent_policies', () => { | ||
| it('should work and create an enrollment key for the policy', async () => { | ||
| const name = `test-${Date.now()}`; | ||
|
|
||
| const res = await supertest | ||
| .post(`/api/fleet/agent_policies`) | ||
| .set('kbn-xsrf', 'xxxx') | ||
| .send({ | ||
| name, | ||
| namespace: 'default', | ||
| }) | ||
| .expect(200); | ||
|
|
||
| const policyId = res.body.item.id; | ||
| const enrollmentKey = await getEnrollmentKeyForPolicyId(policyId); | ||
| expect(enrollmentKey).not.empty(); | ||
| }); | ||
|
|
||
| it('should create an agent action `POLICY_CHANGE` for the policy', async () => { | ||
| const name = `test-${Date.now()}`; | ||
|
|
||
| const res = await supertest | ||
| .post(`/api/fleet/agent_policies?sys_monitoring=true`) | ||
| .set('kbn-xsrf', 'xxxx') | ||
| .send({ | ||
| name, | ||
| namespace: 'default', | ||
| }) | ||
| .expect(200); | ||
|
|
||
| const policyId = res.body.item.id; | ||
| const enrollmentKey = await getEnrollmentKeyForPolicyId(policyId); | ||
| expect(enrollmentKey).not.empty(); | ||
|
|
||
| const action = await getAgentActionsForEnrollmentKey(enrollmentKey.api_key); | ||
|
|
||
| expect(action.type).to.be('POLICY_CHANGE'); | ||
| const agentPolicy = action.data.policy; | ||
| expect(agentPolicy.id).to.be(policyId); | ||
| // should have system inputes | ||
nchaulet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| expect(agentPolicy.inputs).length(2); | ||
| // should have default output | ||
| expect(agentPolicy.outputs.default).not.empty(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('POST /api/fleet/agent_policies/copy', () => { | ||
| const TEST_POLICY_ID = `policy1`; | ||
| it('should work and create an enrollment key for the new policy', async () => { | ||
| const name = `test-${Date.now()}`; | ||
|
|
||
| const res = await supertest | ||
| .post(`/api/fleet/agent_policies/${TEST_POLICY_ID}/copy`) | ||
| .set('kbn-xsrf', 'xxxx') | ||
| .send({ | ||
| name, | ||
| description: 'Test', | ||
| }) | ||
| .expect(200); | ||
|
|
||
| const policyId = res.body.item.id; | ||
| const enrollmentKey = await getEnrollmentKeyForPolicyId(policyId); | ||
| expect(enrollmentKey).not.empty(); | ||
| }); | ||
nchaulet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| it('should create an agent action `POLICY_CHANGE` for the policy', async () => { | ||
| const name = `test-${Date.now()}`; | ||
|
|
||
| const res = await supertest | ||
| .post(`/api/fleet/agent_policies/${TEST_POLICY_ID}/copy`) | ||
| .set('kbn-xsrf', 'xxxx') | ||
| .send({ | ||
| name, | ||
| description: 'Test', | ||
| }) | ||
| .expect(200); | ||
|
|
||
| const policyId = res.body.item.id; | ||
| const enrollmentKey = await getEnrollmentKeyForPolicyId(policyId); | ||
| expect(enrollmentKey).not.empty(); | ||
|
|
||
| const action = await getAgentActionsForEnrollmentKey(enrollmentKey.api_key); | ||
| expect(action.type).to.be('POLICY_CHANGE'); | ||
| expect(action.data.policy.id).to.be(policyId); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.