-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[EDR Workflows] The host isolation exception tab is hidden on the basic license if no artifacts #192562
Merged
szwarckonrad
merged 14 commits into
elastic:main
from
szwarckonrad:hide-hie-tab-when-no-access
Sep 23, 2024
Merged
[EDR Workflows] The host isolation exception tab is hidden on the basic license if no artifacts #192562
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
de7bc89
depreciated const removal
szwarckonrad 21740a0
depreciated const removal
szwarckonrad e65294e
do not display the tab under certain conditions
szwarckonrad 56d0482
Merge branch 'refs/heads/main' into hide-hie-tab-when-no-access
szwarckonrad 03e6fab
Merge branch 'main' into hide-hie-tab-when-no-access
szwarckonrad 9b55c4e
Merge branch 'refs/heads/main' into hide-hie-tab-when-no-access
szwarckonrad 0d736cd
cr
szwarckonrad c08dbc9
Merge branch 'refs/heads/main' into hide-hie-tab-when-no-access
szwarckonrad 7dcf947
tests aligned
szwarckonrad 776a7f2
Merge branch 'main' into hide-hie-tab-when-no-access
szwarckonrad c554f89
Update x-pack/plugins/security_solution/public/management/pages/polic…
szwarckonrad 5dc3427
added test coverage for loading state
szwarckonrad f6104eb
Merge branch 'main' into hide-hie-tab-when-no-access
szwarckonrad dc425f7
Merge branch 'main' into hide-hie-tab-when-no-access
szwarckonrad 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
84 changes: 84 additions & 0 deletions
84
..._solution/public/management/hooks/artifacts/use_host_isolation_exceptions_access.test.tsx
This file contains 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,84 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useHostIsolationExceptionsAccess } from './use_host_isolation_exceptions_access'; | ||
import { checkArtifactHasData } from '../../services/exceptions_list/check_artifact_has_data'; | ||
|
||
jest.mock('../../services/exceptions_list/check_artifact_has_data', () => ({ | ||
checkArtifactHasData: jest.fn(), | ||
})); | ||
|
||
const mockArtifactHasData = (hasData = true) => { | ||
(checkArtifactHasData as jest.Mock).mockResolvedValueOnce(hasData); | ||
}; | ||
|
||
describe('useHostIsolationExceptionsAccess', () => { | ||
const mockApiClient = jest.fn(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const setupHook = (canAccess: boolean, canRead: boolean) => { | ||
return renderHook(() => useHostIsolationExceptionsAccess(canAccess, canRead, mockApiClient)); | ||
}; | ||
|
||
test('should set access to true if canAccessHostIsolationExceptions is true', async () => { | ||
const { result, waitFor } = setupHook(true, false); | ||
|
||
await waitFor(() => expect(result.current.hasAccessToHostIsolationExceptions).toBe(true)); | ||
}); | ||
|
||
test('should check for artifact data if canReadHostIsolationExceptions is true and canAccessHostIsolationExceptions is false', async () => { | ||
mockArtifactHasData(); | ||
|
||
const { result, waitFor } = setupHook(false, true); | ||
|
||
await waitFor(() => { | ||
expect(checkArtifactHasData).toHaveBeenCalledWith(mockApiClient()); | ||
expect(result.current.hasAccessToHostIsolationExceptions).toBe(true); | ||
}); | ||
}); | ||
|
||
test('should set access to false if canReadHostIsolationExceptions is true but no artifact data exists', async () => { | ||
mockArtifactHasData(false); | ||
|
||
const { result, waitFor } = setupHook(false, true); | ||
|
||
await waitFor(() => { | ||
expect(checkArtifactHasData).toHaveBeenCalledWith(mockApiClient()); | ||
expect(result.current.hasAccessToHostIsolationExceptions).toBe(false); | ||
}); | ||
}); | ||
|
||
test('should set access to false if neither canAccessHostIsolationExceptions nor canReadHostIsolationExceptions is true', async () => { | ||
const { result, waitFor } = setupHook(false, false); | ||
await waitFor(() => { | ||
expect(result.current.hasAccessToHostIsolationExceptions).toBe(false); | ||
}); | ||
}); | ||
|
||
test('should not call checkArtifactHasData if canAccessHostIsolationExceptions is true', async () => { | ||
const { result, waitFor } = setupHook(true, true); | ||
|
||
await waitFor(() => { | ||
expect(checkArtifactHasData).not.toHaveBeenCalled(); | ||
expect(result.current.hasAccessToHostIsolationExceptions).toBe(true); | ||
}); | ||
}); | ||
|
||
test('should set loading state correctly while checking access', async () => { | ||
const { result, waitFor } = setupHook(false, true); | ||
|
||
expect(result.current.isHostIsolationExceptionsAccessLoading).toBe(true); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isHostIsolationExceptionsAccessLoading).toBe(false); | ||
}); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
...urity_solution/public/management/hooks/artifacts/use_host_isolation_exceptions_access.tsx
This file contains 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,43 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { useEffect, useState } from 'react'; | ||
import { checkArtifactHasData } from '../../services/exceptions_list/check_artifact_has_data'; | ||
import type { ExceptionsListApiClient } from '../../services/exceptions_list/exceptions_list_api_client'; | ||
|
||
export const useHostIsolationExceptionsAccess = ( | ||
canAccessHostIsolationExceptions: boolean, | ||
canReadHostIsolationExceptions: boolean, | ||
getApiClient: () => ExceptionsListApiClient | ||
): { | ||
hasAccessToHostIsolationExceptions: boolean; | ||
isHostIsolationExceptionsAccessLoading: boolean; | ||
} => { | ||
const [hasAccess, setHasAccess] = useState<boolean | null>(null); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
// Host isolation exceptions is a paid feature and therefore: | ||
// canAccessHostIsolationExceptions signifies if the user has required license to access the feature. | ||
// canReadHostIsolationExceptions, however, is a privilege that allows the user to read and delete the data even if the license is not sufficient (downgrade scenario). | ||
// In such cases, the tab should be visible only if there is existing data. | ||
if (canAccessHostIsolationExceptions) { | ||
setHasAccess(true); | ||
} else if (canReadHostIsolationExceptions) { | ||
const result = await checkArtifactHasData(getApiClient()); | ||
setHasAccess(result); | ||
} else { | ||
setHasAccess(false); | ||
} | ||
})(); | ||
}, [canAccessHostIsolationExceptions, canReadHostIsolationExceptions, getApiClient]); | ||
|
||
return { | ||
hasAccessToHostIsolationExceptions: !!hasAccess, | ||
isHostIsolationExceptionsAccessLoading: hasAccess === null, | ||
}; | ||
}; |
This file contains 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 |
---|---|---|
|
@@ -10,7 +10,7 @@ import type { | |
ExceptionListItemSchema, | ||
UpdateExceptionListItemSchema, | ||
} from '@kbn/securitysolution-io-ts-list-types'; | ||
import { ENDPOINT_BLOCKLISTS_LIST_ID } from '@kbn/securitysolution-list-constants'; | ||
import { ENDPOINT_ARTIFACT_LISTS } from '@kbn/securitysolution-list-constants'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for cleaning up the deprecations 🥇 |
||
|
||
import type { HttpStart } from '@kbn/core/public'; | ||
import type { ConditionEntry } from '../../../../../common/endpoint/types'; | ||
|
@@ -46,7 +46,7 @@ export class BlocklistsApiClient extends ExceptionsListApiClient { | |
constructor(http: HttpStart) { | ||
super( | ||
http, | ||
ENDPOINT_BLOCKLISTS_LIST_ID, | ||
ENDPOINT_ARTIFACT_LISTS.blocklists.id, | ||
BLOCKLISTS_LIST_DEFINITION, | ||
readTransform, | ||
writeTransform | ||
|
@@ -56,7 +56,7 @@ export class BlocklistsApiClient extends ExceptionsListApiClient { | |
public static getInstance(http: HttpStart): ExceptionsListApiClient { | ||
return super.getInstance( | ||
http, | ||
ENDPOINT_BLOCKLISTS_LIST_ID, | ||
ENDPOINT_ARTIFACT_LISTS.blocklists.id, | ||
BLOCKLISTS_LIST_DEFINITION, | ||
readTransform, | ||
writeTransform | ||
|
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think a test case for
isHostIsolationExceptionsAccessLoading: true
would be useful, both here and inpolicy_details.test.tsx
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree!