Skip to content

Commit

Permalink
[Security Solutions][Endpoint][Response Actions] File operations cypr…
Browse files Browse the repository at this point in the history
…ess tests (elastic#156604)

## Summary

Adds e2e tests for `get-file` and `execute` response actions.

~- [ ] Test response actions fail without signing~
- [x] Ensure other endpoint tests are not using the same endpoint for
tests
  
### 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
  • Loading branch information
ashokaditya authored May 22, 2023
1 parent 8a69395 commit 9b50ae9
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import type { Agent } from '@kbn/fleet-plugin/common';
import { APP_ENDPOINTS_PATH } from '../../../../../common/constants';
import { closeAllToasts } from '../../tasks/close_all_toasts';
import { createAgentPolicyTask, toggleRuleOffAndOn, visitRuleAlerts } from '../../tasks/isolate';
import { toggleRuleOffAndOn, visitRuleAlerts } from '../../tasks/isolate';
import { cleanupRule, loadRule } from '../../tasks/api_fixtures';
import { ENDPOINT_VM_NAME } from '../../tasks/common';
import { login } from '../../tasks/login';
import type { IndexedFleetEndpointPolicyResponse } from '../../../../../common/endpoint/data_loaders/index_fleet_endpoint_policy';
import {
createAgentPolicyTask,
getAgentByHostName,
getEndpointIntegrationVersion,
reassignAgentPolicy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { deleteAllLoadedEndpointData } from '../../tasks/delete_all_endpoint_dat
import { getAlertsTableRows, navigateToAlertsList } from '../../screens/alerts';
import { waitForEndpointAlerts } from '../../tasks/alerts';
import { request } from '../../tasks/common';
import { getEndpointIntegrationVersion } from '../../tasks/fleet';
import { createAgentPolicyTask, getEndpointIntegrationVersion } from '../../tasks/fleet';
import { createEndpointHost } from '../../tasks/create_endpoint_host';
import type { IndexedFleetEndpointPolicyResponse } from '../../../../../common/endpoint/data_loaders/index_fleet_endpoint_policy';
import { enableAllPolicyProtections } from '../../tasks/endpoint_policy';
import type { PolicyData, ResponseActionApiResponse } from '../../../../../common/endpoint/types';
Expand All @@ -25,29 +26,15 @@ describe('Endpoint generated alerts', () => {

before(() => {
getEndpointIntegrationVersion().then((version) => {
const policyName = `alerts test ${Math.random().toString(36).substring(2, 7)}`;

cy.task<IndexedFleetEndpointPolicyResponse>('indexFleetEndpointPolicy', {
policyName,
endpointPackageVersion: version,
agentPolicyName: policyName,
}).then((data) => {
createAgentPolicyTask(version, 'alerts test').then((data) => {
indexedPolicy = data;
policy = indexedPolicy.integrationPolicies[0];

return enableAllPolicyProtections(policy.id).then(() => {
// Create and enroll a new Endpoint host
return cy
.task(
'createEndpointHost',
{
agentPolicyId: policy.policy_id,
},
{ timeout: 180000 }
)
.then((host) => {
createdHost = host as CreateAndEnrollEndpointHostResponse;
});
return createEndpointHost(policy.policy_id).then((host) => {
createdHost = host as CreateAndEnrollEndpointHostResponse;
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { Agent } from '@kbn/fleet-plugin/common';
import { APP_ENDPOINTS_PATH } from '../../../../../common/constants';
import { ENDPOINT_VM_NAME } from '../../tasks/common';
import {
createAgentPolicyTask,
getAgentByHostName,
getEndpointIntegrationVersion,
reassignAgentPolicy,
Expand Down Expand Up @@ -48,13 +49,7 @@ describe('Endpoints page', () => {
initialAgentData = agentData;
});
getEndpointIntegrationVersion().then((version) => {
const policyName = `Reassign ${Math.random().toString(36).substring(2, 7)}`;

cy.task<IndexedFleetEndpointPolicyResponse>('indexFleetEndpointPolicy', {
policyName,
endpointPackageVersion: version,
agentPolicyName: policyName,
}).then((data) => {
createAgentPolicyTask(version).then((data) => {
response = data;
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
checkEndpointListForOnlyIsolatedHosts,
checkEndpointListForOnlyUnIsolatedHosts,
checkFlyoutEndpointIsolation,
createAgentPolicyTask,
filterOutEndpoints,
filterOutIsolatedHosts,
isolateHostWithComment,
Expand All @@ -28,6 +27,7 @@ import { ENDPOINT_VM_NAME } from '../../tasks/common';
import { login } from '../../tasks/login';
import type { IndexedFleetEndpointPolicyResponse } from '../../../../../common/endpoint/data_loaders/index_fleet_endpoint_policy';
import {
createAgentPolicyTask,
getAgentByHostName,
getEndpointIntegrationVersion,
reassignAgentPolicy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/

import type { Agent } from '@kbn/fleet-plugin/common';
import type { PolicyData } from '../../../../../common/endpoint/types';
import type { CreateAndEnrollEndpointHostResponse } from '../../../../../scripts/endpoint/common/endpoint_host_services';
import {
inputConsoleCommand,
openResponseConsoleFromEndpointList,
Expand All @@ -15,18 +16,17 @@ import {
waitForEndpointListPageToBeLoaded,
} from '../../tasks/response_console';
import type { IndexedFleetEndpointPolicyResponse } from '../../../../../common/endpoint/data_loaders/index_fleet_endpoint_policy';
import {
getAgentByHostName,
getEndpointIntegrationVersion,
reassignAgentPolicy,
} from '../../tasks/fleet';
import { getEndpointIntegrationVersion, createAgentPolicyTask } from '../../tasks/fleet';
import {
checkEndpointListForOnlyIsolatedHosts,
checkEndpointListForOnlyUnIsolatedHosts,
createAgentPolicyTask,
} from '../../tasks/isolate';

import { login } from '../../tasks/login';
import { ENDPOINT_VM_NAME } from '../../tasks/common';
import { enableAllPolicyProtections } from '../../tasks/endpoint_policy';
import { createEndpointHost } from '../../tasks/create_endpoint_host';
import { deleteAllLoadedEndpointData } from '../../tasks/delete_all_endpoint_data';

describe('Response console', () => {
const endpointHostname = Cypress.env(ENDPOINT_VM_NAME);
Expand All @@ -36,77 +36,100 @@ describe('Response console', () => {
});

describe('User journey for Isolate command: isolate and release an endpoint', () => {
let response: IndexedFleetEndpointPolicyResponse;
let initialAgentData: Agent;
let indexedPolicy: IndexedFleetEndpointPolicyResponse;
let policy: PolicyData;
let createdHost: CreateAndEnrollEndpointHostResponse;

before(() => {
getAgentByHostName(endpointHostname).then((agentData) => {
initialAgentData = agentData;
});

getEndpointIntegrationVersion().then((version) =>
createAgentPolicyTask(version).then((data) => {
response = data;
indexedPolicy = data;
policy = indexedPolicy.integrationPolicies[0];

return enableAllPolicyProtections(policy.id).then(() => {
// Create and enroll a new Endpoint host
return createEndpointHost(policy.policy_id).then((host) => {
createdHost = host as CreateAndEnrollEndpointHostResponse;
});
});
})
);
});

after(() => {
if (initialAgentData?.policy_id) {
reassignAgentPolicy(initialAgentData.id, initialAgentData.policy_id);
if (createdHost) {
cy.task('destroyEndpointHost', createdHost).then(() => {});
}
if (response) {
cy.task('deleteIndexedFleetEndpointPolicies', response);

if (indexedPolicy) {
cy.task('deleteIndexedFleetEndpointPolicies', indexedPolicy);
}

if (createdHost) {
deleteAllLoadedEndpointData({ endpointAgentIds: [createdHost.agentId] });
}
});

it('should isolate host from response console', () => {
const command = 'isolate';
waitForEndpointListPageToBeLoaded(endpointHostname);
checkEndpointListForOnlyUnIsolatedHosts();
openResponseConsoleFromEndpointList();
performCommandInputChecks('isolate');
performCommandInputChecks(command);
submitCommand();
waitForCommandToBeExecuted();
waitForCommandToBeExecuted(command);
waitForEndpointListPageToBeLoaded(endpointHostname);
checkEndpointListForOnlyIsolatedHosts();
});

it('should release host from response console', () => {
const command = 'release';
waitForEndpointListPageToBeLoaded(endpointHostname);
checkEndpointListForOnlyIsolatedHosts();
openResponseConsoleFromEndpointList();
performCommandInputChecks('release');
performCommandInputChecks(command);
submitCommand();
waitForCommandToBeExecuted();
waitForCommandToBeExecuted(command);
waitForEndpointListPageToBeLoaded(endpointHostname);
checkEndpointListForOnlyUnIsolatedHosts();
});
});

describe('User journey for Processes commands: list, kill and suspend process.', () => {
let response: IndexedFleetEndpointPolicyResponse;
let initialAgentData: Agent;
describe('User journey for Processes operations: list, kill and suspend process', () => {
let cronPID: string;
let newCronPID: string;

before(() => {
getAgentByHostName(endpointHostname).then((agentData) => {
initialAgentData = agentData;
});
let indexedPolicy: IndexedFleetEndpointPolicyResponse;
let policy: PolicyData;
let createdHost: CreateAndEnrollEndpointHostResponse;

before(() => {
getEndpointIntegrationVersion().then((version) =>
createAgentPolicyTask(version).then((data) => {
response = data;
indexedPolicy = data;
policy = indexedPolicy.integrationPolicies[0];

return enableAllPolicyProtections(policy.id).then(() => {
// Create and enroll a new Endpoint host
return createEndpointHost(policy.policy_id).then((host) => {
createdHost = host as CreateAndEnrollEndpointHostResponse;
});
});
})
);
});

after(() => {
if (initialAgentData?.policy_id) {
reassignAgentPolicy(initialAgentData.id, initialAgentData.policy_id);
if (createdHost) {
cy.task('destroyEndpointHost', createdHost).then(() => {});
}
if (response) {
cy.task('deleteIndexedFleetEndpointPolicies', response);

if (indexedPolicy) {
cy.task('deleteIndexedFleetEndpointPolicies', indexedPolicy);
}

if (createdHost) {
deleteAllLoadedEndpointData({ endpointAgentIds: [createdHost.agentId] });
}
});

Expand Down Expand Up @@ -140,7 +163,7 @@ describe('Response console', () => {
openResponseConsoleFromEndpointList();
inputConsoleCommand(`kill-process --pid ${cronPID}`);
submitCommand();
waitForCommandToBeExecuted();
waitForCommandToBeExecuted('kill-process');

performCommandInputChecks('processes');
submitCommand();
Expand All @@ -164,43 +187,47 @@ describe('Response console', () => {
openResponseConsoleFromEndpointList();
inputConsoleCommand(`suspend-process --pid ${newCronPID}`);
submitCommand();
waitForCommandToBeExecuted();
waitForCommandToBeExecuted('suspend-process');
});
});

describe('User journey for Get file command', () => {
let response: IndexedFleetEndpointPolicyResponse;
let initialAgentData: Agent;
describe('File operations: get-file and execute', () => {
const homeFilePath = `/home/ubuntu`;

const fileContent = 'This is a test file for the get-file command.';
const filePath = `/home/ubuntu/test_file.txt`;

before(() => {
getAgentByHostName(endpointHostname).then((agentData) => {
initialAgentData = agentData;
});
let indexedPolicy: IndexedFleetEndpointPolicyResponse;
let policy: PolicyData;
let createdHost: CreateAndEnrollEndpointHostResponse;

before(() => {
getEndpointIntegrationVersion().then((version) =>
createAgentPolicyTask(version).then((data) => {
response = data;
indexedPolicy = data;
policy = indexedPolicy.integrationPolicies[0];

return enableAllPolicyProtections(policy.id).then(() => {
// Create and enroll a new Endpoint host
return createEndpointHost(policy.policy_id).then((host) => {
createdHost = host as CreateAndEnrollEndpointHostResponse;
});
});
})
);

cy.task('installPackagesOnEndpoint', { hostname: endpointHostname, packages: ['unzip'] });

cy.task('createFileOnEndpoint', {
hostname: endpointHostname,
path: filePath,
content: fileContent,
});
});

after(() => {
if (initialAgentData?.policy_id) {
reassignAgentPolicy(initialAgentData.id, initialAgentData.policy_id);
if (createdHost) {
cy.task('destroyEndpointHost', createdHost).then(() => {});
}
if (response) {
cy.task('deleteIndexedFleetEndpointPolicies', response);

if (indexedPolicy) {
cy.task('deleteIndexedFleetEndpointPolicies', indexedPolicy);
}

if (createdHost) {
deleteAllLoadedEndpointData({ endpointAgentIds: [createdHost.agentId] });
}
});

Expand Down Expand Up @@ -234,30 +261,48 @@ describe('Response console', () => {
});
});
});

it('"execute --command" - should execute a command', async () => {
waitForEndpointListPageToBeLoaded(endpointHostname);
openResponseConsoleFromEndpointList();
inputConsoleCommand(`execute --command "ls -al ${homeFilePath}"`);
submitCommand();
waitForCommandToBeExecuted('execute');
});
});

describe('document signing', () => {
let response: IndexedFleetEndpointPolicyResponse;
let initialAgentData: Agent;
let indexedPolicy: IndexedFleetEndpointPolicyResponse;
let policy: PolicyData;
let createdHost: CreateAndEnrollEndpointHostResponse;

before(() => {
getAgentByHostName(endpointHostname).then((agentData) => {
initialAgentData = agentData;
});

getEndpointIntegrationVersion().then((version) =>
createAgentPolicyTask(version).then((data) => {
response = data;
indexedPolicy = data;
policy = indexedPolicy.integrationPolicies[0];

return enableAllPolicyProtections(policy.id).then(() => {
// Create and enroll a new Endpoint host
return createEndpointHost(policy.policy_id).then((host) => {
createdHost = host as CreateAndEnrollEndpointHostResponse;
});
});
})
);
});

after(() => {
if (initialAgentData?.policy_id) {
reassignAgentPolicy(initialAgentData.id, initialAgentData.policy_id);
if (createdHost) {
cy.task('destroyEndpointHost', createdHost).then(() => {});
}

if (indexedPolicy) {
cy.task('deleteIndexedFleetEndpointPolicies', indexedPolicy);
}
if (response) {
cy.task('deleteIndexedFleetEndpointPolicies', response);

if (createdHost) {
deleteAllLoadedEndpointData({ endpointAgentIds: [createdHost.agentId] });
}
});

Expand Down
Loading

0 comments on commit 9b50ae9

Please sign in to comment.