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][Endpoint] Users can filter trusted apps by policy name #106710

Conversation

dasansol92
Copy link
Contributor

@dasansol92 dasansol92 commented Jul 26, 2021

Summary

  • Adds dropdown that allows users filter trusted apps by policy.
  • It's displayed only when trustedAppsByPolicyEnabled FF is enabled.
  • Adds unit test and fixed existing ones.
  • Renames search component.

trustedd apps filter by policy final

Checklist

For maintainers

@dasansol92 dasansol92 added v8.0.0 release_note:skip Skip the PR/issue when compiling release notes Team:Defend Workflows “EDR Workflows” sub-team of Security Solution auto-backport Deprecated - use backport:version if exact versions are needed v7.15.0 labels Jul 26, 2021
@dasansol92 dasansol92 marked this pull request as ready for review July 26, 2021 15:11
@dasansol92 dasansol92 requested a review from a team as a code owner July 26, 2021 15:11
@dasansol92 dasansol92 requested review from joeypoon and parkiino July 26, 2021 15:11
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-onboarding-and-lifecycle-mgt (Team:Onboarding and Lifecycle Mgt)

Copy link
Member

@joeypoon joeypoon left a comment

Choose a reason for hiding this comment

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

just some nits but otherwise looks good.

};

expect(getTrustedAppsListPath(location)).toEqual(
'/administration/trusted_apps?page_index=2&page_size=20&view_type=list&show=create'
'/administration/trusted_apps?page_index=2&page_size=20&view_type=list&show=create&filter=test&included_policies=globally&excluded_policies=unassigned'
Copy link
Member

Choose a reason for hiding this comment

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

non-blocking nit: could use string templating here for location.filter and the location included/excluded policies.

const doEntriesExist = useTrustedAppsSelector(entriesExist) === true;
const navigationCallback = useTrustedAppsNavigateCallback((query: string) => ({ filter: query }));
const didEntriesExist = useTrustedAppsSelector(prevEntriesExist) === true;
Copy link
Member

Choose a reason for hiding this comment

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

can we just do useTrustedAppsSelector(prevEntriesExist) without the === true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OMG, totally right! Don't know why I write this, I think it came from a copy paste of line 58

@dasansol92
Copy link
Contributor Author

@elasticmachine merge upstream

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.

Left a few comments and suggestion. let me know if you have any questions. This (feature) is looking awesome

@@ -19,3 +21,31 @@ export const parseQueryFilterToKQL = (filter: string, fields: Readonly<string[]>

return `(${kuery})`;
};

const getPolicyQuery = (policy: string): string => {
Copy link
Contributor

Choose a reason for hiding this comment

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

policy (to me) makes it sound like its the actual endpoint policy. maybe rename it to policyId instead?

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [policies]);

const onButtonClick = () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

can you wrap this in useCallback()? Same for the closePopover below as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I thought that but following react docs: https://reactjs.org/docs/hooks-reference.html#usestate
It says:

React guarantees that setState function identity is stable and won’t change on re-renders. This is why it’s safe to omit from the useEffect or useCallback dependency list.

As this functions only has setState actions I think is one of those cases you don't need to wrap it in a useCallback as it wouldn't be better in terms on performance.

Note: I'm using the setState param function to avoid memoize the current value.

setIsPopoverOpen((prevIsPopoverOpen) => !prevIsPopoverOpen);

Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 - true. the setter function provided back by useState() is guaranteed to now change and thus you can omit that setter function from the dependencies of useCallback.

One of the benefits of using useCallback() is that the returned function is always the same until the dependencies change. Because onButtonClick is given as a prop to another component, and because its value (the function) is a new function instance (because its not wrapped in useCallback()), that will trigger a re-render of the downstream component (prop changed).

Copy link
Contributor

Choose a reason for hiding this comment

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

☝️ ( 😁 ) - still optional

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh right, I'm still wondering if it will improve something as there is no changing state/prop related? So the returned function still being the same right?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, every time this component is rendered, the onButtonClick will be a new instance (function) because it's not cached. So when that is used below as a prop value, it will cause that comment below to also re-render (because the prop value is new (oldOnButtonClick !== onButtonClick)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Argh! I see, got it. Thanks!!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in: 6e92a6b

onChangeSelection: (items: Item[]) => void;
}

export interface Item {
Copy link
Contributor

Choose a reason for hiding this comment

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

Naming suggestion: PolicySelectionItem (just because Item is so generic)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally agree, this was cp from eui docs

Comment on lines 22 to 25
const policy = generator.generatePolicyPackagePolicy();
policy.name = 'test policy A';
policy.id = 'abc123';
const getElement = (params: Partial<PoliciesSelectorProps>) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you set these on every test run so that we avoid mutable state being used between tests?


export const getPolicyResponse = (): GetPolicyListResponse => ({
items: [
{
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a generator for policy response? if so, I would suggest inializing a generator here with a seed and then getting the mocked policy response with it, rather than to define the entire response here.

@@ -922,6 +936,27 @@ describe('When on the Trusted Apps Page', () => {
backButtonUrl: '/fleet',
});
});

const priorMockImplementation = coreStart.http.get.getMockImplementation();
// @ts-ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

why the ts-ignore? is it due to the callback overload signatures? If so, I have gotten around this by just accepting options as the argument and doing a type assertion in the body of the function. see x-pack/plugins/security_solution/public/common/mock/endpoint/http_handler_mock_factory.ts:199 for reference

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing this by using the getLastLoadedResourceState solves that

Copy link
Contributor

Choose a reason for hiding this comment

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

☝️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I don't remember why I said that. This was copy&pasted from line 766.

…ourceState helper function to get the prev state. Use generated data for policies in tests
…47' of github.com:dasansol92/kibana into feat/olm-users_can_filter_Trusted_Apps_by_policy_name-747
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.

Approving 🔥 🔥 🔥

I did leave a few comments that might need follow up, but nothing that should hold this up for now.

@dasansol92
Copy link
Contributor Author

@elasticmachine merge upstream

@dasansol92
Copy link
Contributor Author

@elasticmachine merge upstream

@dasansol92 dasansol92 requested a review from a team as a code owner August 3, 2021 13:06
@dasansol92 dasansol92 requested a review from paul-tavares August 3, 2021 13:06
@dasansol92
Copy link
Contributor Author

@elasticmachine merge upstream

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.

🔥 🔥 🔥

🚢

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [policies]);

const onButtonClick = () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

☝️ ( 😁 ) - still optional

@@ -922,6 +936,27 @@ describe('When on the Trusted Apps Page', () => {
backButtonUrl: '/fleet',
});
});

const priorMockImplementation = coreStart.http.get.getMockImplementation();
// @ts-ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

☝️

@kibanamachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
securitySolution 2342 2345 +3

Async chunks

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

id before after diff
securitySolution 6.4MB 6.4MB +10.2KB

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
securitySolution 211.9KB 211.9KB +34.0B

History

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

@kibanamachine
Copy link
Contributor

💚 Backport successful

Status Branch Result
7.x

This backport PR will be merged automatically after passing CI.

kibanamachine added a commit to kibanamachine/kibana that referenced this pull request Aug 5, 2021
… name (elastic#106710)

* Allow users select policies from a dropdown

* Policy filters are passed throguh the API call and the results are now filtered by policy

* Moved policies selector inside search component and triggers search only when refresh button is clicked

* Fixes tests

* Triggers policy filter when policy is selected. Also fix unit test because now policies are loaded at the trusted apps list

* Renamed components and added an index.ts for the exports

* Adds unit tests for policies selector component

* Fix unit tests and changed camelcase by snack case for url params

* adds multilang

* Fixes i18n keys

* Move mock resonse to the mocks file

* Use string templating in test

* remove === true from boolean comparison

* Set function in useCallback. Renames some variables and types. Use reourceState helper function to get the prev state. Use generated data for policies in tests

* Fix ts errors

* Removes unused type and fix type name for Item

* Puts exclude clause on policy dropdown behind a feature flag

* Adds missing feature flags in some tests and in global reducer

* Fix test adding useExperimentalValua mock for FF

* Wrapp handlers in a useCallback in order to prevent useless rerenders

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
kibanamachine added a commit that referenced this pull request Aug 5, 2021
… name (#106710) (#107750)

* Allow users select policies from a dropdown

* Policy filters are passed throguh the API call and the results are now filtered by policy

* Moved policies selector inside search component and triggers search only when refresh button is clicked

* Fixes tests

* Triggers policy filter when policy is selected. Also fix unit test because now policies are loaded at the trusted apps list

* Renamed components and added an index.ts for the exports

* Adds unit tests for policies selector component

* Fix unit tests and changed camelcase by snack case for url params

* adds multilang

* Fixes i18n keys

* Move mock resonse to the mocks file

* Use string templating in test

* remove === true from boolean comparison

* Set function in useCallback. Renames some variables and types. Use reourceState helper function to get the prev state. Use generated data for policies in tests

* Fix ts errors

* Removes unused type and fix type name for Item

* Puts exclude clause on policy dropdown behind a feature flag

* Adds missing feature flags in some tests and in global reducer

* Fix test adding useExperimentalValua mock for FF

* Wrapp handlers in a useCallback in order to prevent useless rerenders

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: David Sánchez <davidsansol92@gmail.com>
streamich pushed a commit to vadimkibana/kibana that referenced this pull request Aug 8, 2021
… name (elastic#106710)

* Allow users select policies from a dropdown

* Policy filters are passed throguh the API call and the results are now filtered by policy

* Moved policies selector inside search component and triggers search only when refresh button is clicked

* Fixes tests

* Triggers policy filter when policy is selected. Also fix unit test because now policies are loaded at the trusted apps list

* Renamed components and added an index.ts for the exports

* Adds unit tests for policies selector component

* Fix unit tests and changed camelcase by snack case for url params

* adds multilang

* Fixes i18n keys

* Move mock resonse to the mocks file

* Use string templating in test

* remove === true from boolean comparison

* Set function in useCallback. Renames some variables and types. Use reourceState helper function to get the prev state. Use generated data for policies in tests

* Fix ts errors

* Removes unused type and fix type name for Item

* Puts exclude clause on policy dropdown behind a feature flag

* Adds missing feature flags in some tests and in global reducer

* Fix test adding useExperimentalValua mock for FF

* Wrapp handlers in a useCallback in order to prevent useless rerenders

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Deprecated - use backport:version if exact versions are needed release_note:skip Skip the PR/issue when compiling release notes Team:Defend Workflows “EDR Workflows” sub-team of Security Solution v7.15.0 v8.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants