[SECURITY_SOLUTION] delete advanced Policy fields when they are empty (#84368)

This commit is contained in:
Kevin Logan 2020-12-03 10:57:27 -05:00 committed by GitHub
parent 0bd928184c
commit ac71d2e941
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 313 additions and 0 deletions

View file

@ -15,6 +15,8 @@ import { AdvancedPolicySchema } from '../models/advanced_policy_schema';
function setValue(obj: Record<string, unknown>, value: string, path: string[]) {
let newPolicyConfig = obj;
// First set the value.
for (let i = 0; i < path.length - 1; i++) {
if (!newPolicyConfig[path[i]]) {
newPolicyConfig[path[i]] = {} as Record<string, unknown>;
@ -22,6 +24,36 @@ function setValue(obj: Record<string, unknown>, value: string, path: string[]) {
newPolicyConfig = newPolicyConfig[path[i]] as Record<string, unknown>;
}
newPolicyConfig[path[path.length - 1]] = value;
// Then, if the user is deleting the value, we need to ensure we clean up the config.
// We delete any sections that are empty, whether that be an empty string, empty object, or undefined.
if (value === '' || value === undefined) {
newPolicyConfig = obj;
for (let k = path.length; k >= 0; k--) {
const nextPath = path.slice(0, k);
for (let i = 0; i < nextPath.length - 1; i++) {
// Traverse and find the next section
newPolicyConfig = newPolicyConfig[nextPath[i]] as Record<string, unknown>;
}
if (
newPolicyConfig[nextPath[nextPath.length - 1]] === undefined ||
newPolicyConfig[nextPath[nextPath.length - 1]] === '' ||
Object.keys(newPolicyConfig[nextPath[nextPath.length - 1]] as object).length === 0
) {
// If we're looking at the `advanced` field, we leave it undefined as opposed to deleting it.
// This is because the UI looks for this field to begin rendering.
if (nextPath[nextPath.length - 1] === 'advanced') {
newPolicyConfig[nextPath[nextPath.length - 1]] = undefined;
// In all other cases, if field is empty, we'll delete it to clean up.
} else {
delete newPolicyConfig[nextPath[nextPath.length - 1]];
}
newPolicyConfig = obj;
} else {
break; // We are looking at a non-empty section, so we can terminate.
}
}
}
}
function getValue(obj: Record<string, unknown>, path: string[]) {

View file

@ -254,6 +254,287 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
},
]);
});
it('should have cleared the advanced section when the user deletes the value', async () => {
const advancedPolicyButton = await pageObjects.policy.findAdvancedPolicyButton();
await advancedPolicyButton.click();
const advancedPolicyField = await pageObjects.policy.findAdvancedPolicyField();
await advancedPolicyField.clearValue();
await advancedPolicyField.click();
await advancedPolicyField.type('true');
await pageObjects.policy.confirmAndSave();
await testSubjects.existOrFail('policyDetailsSuccessMessage');
const agentFullPolicy = await policyTestResources.getFullAgentPolicy(
policyInfo.agentPolicy.id
);
expect(agentFullPolicy.inputs).to.eql([
{
id: policyInfo.packagePolicy.id,
revision: 2,
data_stream: { namespace: 'default' },
name: 'Protect East Coast',
meta: {
package: {
name: 'endpoint',
version: policyInfo.packageInfo.version,
},
},
artifact_manifest: {
artifacts: {
'endpoint-exceptionlist-macos-v1': {
compression_algorithm: 'zlib',
decoded_sha256:
'd801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
decoded_size: 14,
encoded_sha256:
'f8e6afa1d5662f5b37f83337af774b5785b5b7f1daee08b7b00c2d6813874cda',
encoded_size: 22,
encryption_algorithm: 'none',
relative_url:
'/api/endpoint/artifacts/download/endpoint-exceptionlist-macos-v1/d801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
},
'endpoint-exceptionlist-windows-v1': {
compression_algorithm: 'zlib',
decoded_sha256:
'd801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
decoded_size: 14,
encoded_sha256:
'f8e6afa1d5662f5b37f83337af774b5785b5b7f1daee08b7b00c2d6813874cda',
encoded_size: 22,
encryption_algorithm: 'none',
relative_url:
'/api/endpoint/artifacts/download/endpoint-exceptionlist-windows-v1/d801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
},
'endpoint-trustlist-linux-v1': {
compression_algorithm: 'zlib',
decoded_sha256:
'd801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
decoded_size: 14,
encoded_sha256:
'f8e6afa1d5662f5b37f83337af774b5785b5b7f1daee08b7b00c2d6813874cda',
encoded_size: 22,
encryption_algorithm: 'none',
relative_url:
'/api/endpoint/artifacts/download/endpoint-trustlist-linux-v1/d801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
},
'endpoint-trustlist-macos-v1': {
compression_algorithm: 'zlib',
decoded_sha256:
'd801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
decoded_size: 14,
encoded_sha256:
'f8e6afa1d5662f5b37f83337af774b5785b5b7f1daee08b7b00c2d6813874cda',
encoded_size: 22,
encryption_algorithm: 'none',
relative_url:
'/api/endpoint/artifacts/download/endpoint-trustlist-macos-v1/d801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
},
'endpoint-trustlist-windows-v1': {
compression_algorithm: 'zlib',
decoded_sha256:
'd801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
decoded_size: 14,
encoded_sha256:
'f8e6afa1d5662f5b37f83337af774b5785b5b7f1daee08b7b00c2d6813874cda',
encoded_size: 22,
encryption_algorithm: 'none',
relative_url:
'/api/endpoint/artifacts/download/endpoint-trustlist-windows-v1/d801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
},
},
// The manifest version could have changed when the Policy was updated because the
// policy details page ensures that a save action applies the udpated policy on top
// of the latest Package Policy. So we just ignore the check against this value by
// forcing it to be the same as the value returned in the full agent policy.
manifest_version: agentFullPolicy.inputs[0].artifact_manifest.manifest_version,
schema_version: 'v1',
},
policy: {
linux: {
events: { file: true, network: true, process: true },
logging: { file: 'info' },
advanced: { agent: { connection_delay: 'true' } },
},
mac: {
events: { file: true, network: true, process: true },
logging: { file: 'info' },
malware: { mode: 'prevent' },
popup: {
malware: {
enabled: true,
message: 'Elastic Security { action } { filename }',
},
},
},
windows: {
events: {
dll_and_driver_load: true,
dns: true,
file: true,
network: true,
process: true,
registry: true,
security: true,
},
logging: { file: 'info' },
malware: { mode: 'prevent' },
popup: {
malware: {
enabled: true,
message: 'Elastic Security { action } { filename }',
},
},
antivirus_registration: {
enabled: false,
},
},
},
type: 'endpoint',
use_output: 'default',
},
]);
// Clear the value
await advancedPolicyField.click();
await advancedPolicyField.clearValueWithKeyboard();
await pageObjects.policy.confirmAndSave();
await testSubjects.existOrFail('policyDetailsSuccessMessage');
const agentFullPolicyUpdated = await policyTestResources.getFullAgentPolicy(
policyInfo.agentPolicy.id
);
expect(agentFullPolicyUpdated.inputs).to.eql([
{
id: policyInfo.packagePolicy.id,
revision: 3,
data_stream: { namespace: 'default' },
name: 'Protect East Coast',
meta: {
package: {
name: 'endpoint',
version: policyInfo.packageInfo.version,
},
},
artifact_manifest: {
artifacts: {
'endpoint-exceptionlist-macos-v1': {
compression_algorithm: 'zlib',
decoded_sha256:
'd801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
decoded_size: 14,
encoded_sha256:
'f8e6afa1d5662f5b37f83337af774b5785b5b7f1daee08b7b00c2d6813874cda',
encoded_size: 22,
encryption_algorithm: 'none',
relative_url:
'/api/endpoint/artifacts/download/endpoint-exceptionlist-macos-v1/d801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
},
'endpoint-exceptionlist-windows-v1': {
compression_algorithm: 'zlib',
decoded_sha256:
'd801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
decoded_size: 14,
encoded_sha256:
'f8e6afa1d5662f5b37f83337af774b5785b5b7f1daee08b7b00c2d6813874cda',
encoded_size: 22,
encryption_algorithm: 'none',
relative_url:
'/api/endpoint/artifacts/download/endpoint-exceptionlist-windows-v1/d801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
},
'endpoint-trustlist-linux-v1': {
compression_algorithm: 'zlib',
decoded_sha256:
'd801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
decoded_size: 14,
encoded_sha256:
'f8e6afa1d5662f5b37f83337af774b5785b5b7f1daee08b7b00c2d6813874cda',
encoded_size: 22,
encryption_algorithm: 'none',
relative_url:
'/api/endpoint/artifacts/download/endpoint-trustlist-linux-v1/d801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
},
'endpoint-trustlist-macos-v1': {
compression_algorithm: 'zlib',
decoded_sha256:
'd801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
decoded_size: 14,
encoded_sha256:
'f8e6afa1d5662f5b37f83337af774b5785b5b7f1daee08b7b00c2d6813874cda',
encoded_size: 22,
encryption_algorithm: 'none',
relative_url:
'/api/endpoint/artifacts/download/endpoint-trustlist-macos-v1/d801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
},
'endpoint-trustlist-windows-v1': {
compression_algorithm: 'zlib',
decoded_sha256:
'd801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
decoded_size: 14,
encoded_sha256:
'f8e6afa1d5662f5b37f83337af774b5785b5b7f1daee08b7b00c2d6813874cda',
encoded_size: 22,
encryption_algorithm: 'none',
relative_url:
'/api/endpoint/artifacts/download/endpoint-trustlist-windows-v1/d801aa1fb7ddcc330a5e3173372ea6af4a3d08ec58074478e85aa5603e926658',
},
},
// The manifest version could have changed when the Policy was updated because the
// policy details page ensures that a save action applies the udpated policy on top
// of the latest Package Policy. So we just ignore the check against this value by
// forcing it to be the same as the value returned in the full agent policy.
manifest_version: agentFullPolicy.inputs[0].artifact_manifest.manifest_version,
schema_version: 'v1',
},
policy: {
linux: {
events: { file: true, network: true, process: true },
logging: { file: 'info' },
},
mac: {
events: { file: true, network: true, process: true },
logging: { file: 'info' },
malware: { mode: 'prevent' },
popup: {
malware: {
enabled: true,
message: 'Elastic Security { action } { filename }',
},
},
},
windows: {
events: {
dll_and_driver_load: true,
dns: true,
file: true,
network: true,
process: true,
registry: true,
security: true,
},
logging: { file: 'info' },
malware: { mode: 'prevent' },
popup: {
malware: {
enabled: true,
message: 'Elastic Security { action } { filename }',
},
},
antivirus_registration: {
enabled: false,
},
},
},
type: 'endpoint',
use_output: 'default',
},
]);
});
});
describe('when on Ingest Policy Edit Package Policy page', async () => {