Skip to content
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

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

Merged

Conversation

kevinlog
Copy link
Contributor

@kevinlog kevinlog commented Nov 25, 2020

Summary

Fixes #84127

In the advanced Policy UI, we need to ensure that when fields are cleared out, we delete the relevant section in the advanced fields. Further, we need to ensure that we clean up any left over nested fields.

Initially, you will see no advanced section in the Policy yaml in Fleet:
image

When a user adds "false" to a field, we should see this in the Policy yaml in Fleet (note that the field added is nested):
image

image

After the user deletes the field, the path is empty, everything should be cleaned up before going to the Agent/Endpoint:
image

Checklist

Delete any items that are not applicable to this PR.

@kevinlog kevinlog marked this pull request as ready for review November 30, 2020 14:07
@kevinlog kevinlog requested review from a team as code owners November 30, 2020 14:07
@kevinlog kevinlog added v7.11.0 release_note:skip Skip the PR/issue when compiling release notes Team:Defend Workflows “EDR Workflows” sub-team of Security Solution labels Nov 30, 2020
@kevinlog
Copy link
Contributor Author

@elasticmachine merge upstream

break; // We are looking at a non-empty section, so we can terminate.
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be covered by testing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I can write a test case for it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pushed up a test

Copy link
Contributor

@paul-tavares paul-tavares left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Had only some questions for my own knowning :)

if (
newPolicyConfig[nextPath[nextPath.length - 1]] === undefined ||
newPolicyConfig[nextPath[nextPath.length - 1]] === '' ||
Object.keys(newPolicyConfig[nextPath[nextPath.length - 1]] as object).length === 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming this casting here is safe at runtime? will it always be an object if defined at this point?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'as object' ? Shouldn't it be 'as Object' ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both work. The type error I had on this went away after I added the cast

Object.keys(newPolicyConfig[nextPath[nextPath.length - 1]] as object).length === 0
) {
if (nextPath[nextPath.length - 1] === 'advanced') {
newPolicyConfig[nextPath[nextPath.length - 1]] = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm reading this right, it seems that if the key name is advanced we keep it, but set it to undefined, but any other key we delete it. Should the advanced key also be deleted? Or does that happen when you walk the path back to the prior level?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the UI looks for an advanced key which will be initialized to undefined to start making decisions on how to render the form. This is essentially resetting if it's completely emptied out

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm also a little confused about the if/else logic here; maybe more comments would be clarifying

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added more comments

} else {
delete newPolicyConfig[nextPath[nextPath.length - 1]];
}
newPolicyConfig = obj;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this assignment multiple times, what does it do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it updates the reference to the area in the config as we follow the given path. Say our path is linux.advanced.config - we start at linux and access advanced, then we need to update the reference to access config in the next iteration.

for (let i = 0; i < path.length - 1; i++) {
if (!newPolicyConfig[path[i]]) {
newPolicyConfig[path[i]] = {} as Record<string, unknown>;
}
newPolicyConfig = newPolicyConfig[path[i]] as Record<string, unknown>;
}
newPolicyConfig[path[path.length - 1]] = value;

// Then, if the user is deleting the value, then we need to ensure we clean up the config.
// We delete any sections are the empty, whether that be an empty string, empty object, or undefined.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/are the/that are

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Object.keys(newPolicyConfig[nextPath[nextPath.length - 1]] as object).length === 0
) {
if (nextPath[nextPath.length - 1] === 'advanced') {
newPolicyConfig[nextPath[nextPath.length - 1]] = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm also a little confused about the if/else logic here; maybe more comments would be clarifying

@kevinlog
Copy link
Contributor Author

kevinlog commented Dec 2, 2020

@elasticmachine merge upstream

@kibanamachine
Copy link
Contributor

expected head sha didn’t match current head ref.

@kevinlog
Copy link
Contributor Author

kevinlog commented Dec 3, 2020

@elasticmachine merge upstream

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
securitySolution 8.0MB 8.0MB +572.0B

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@kevinlog kevinlog merged commit ac71d2e into elastic:master Dec 3, 2020
@kevinlog kevinlog deleted the bug/delete-empty-advanced-policy-fields branch December 3, 2020 15:57
gmmorris added a commit to gmmorris/kibana that referenced this pull request Dec 4, 2020
* master: (28 commits)
  [Actions] fixes bug where severity is auto selected but not applied to the action in PagerDuty (elastic#84891)
  Only attempt to rollover signals index if version < builtin version (elastic#84982)
  skip flaky suite (elastic#84978)
  skip lens rollup tests
  Add geo containment tracking alert type (elastic#84151)
  Changed rollup tests to use test user rather than elastic super user. (elastic#79567)
  skip 'should allow creation of lens xy chart' elastic#84957
  [APM] Add log_level/sanitize_field_names config options to Python Agent (elastic#84810)
  [Maps] geo line source (elastic#76572)
  [data.search] Move search method inside session service and add tests (elastic#84715)
  skip lens drag and drop test.  elastic#84941
  [Ingest Node Pipelines] Integrate painless autocomplete (elastic#84554)
  [Lens] allow drag and drop reorder on xyChart for y dimension (elastic#84640)
  [Lens] Fix error when selecting the current field again (elastic#84817)
  [Metrics UI] Add metadata tab to node details flyout (elastic#84454)
  [CI] Enables APM collection (elastic#81731)
  [Workplace Search] Migrate Sources Schema tree (elastic#84847)
  Disable checking for conflicts when copying saved objects (elastic#83575)
  [SECURITY_SOLUTION] delete advanced Policy fields when they are empty (elastic#84368)
  y18n 4.0.0 -> 4.0.1 (elastic#84905)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_note:skip Skip the PR/issue when compiling release notes Team:Defend Workflows “EDR Workflows” sub-team of Security Solution v7.11.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Advanced Policy UI in Endpoint Security should delete empty entries in Package Policy
7 participants