forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.14] [Cases] Populate user info from fake requests (elastic#180671) (…
…elastic#181572) # Backport This will backport the following commits from `main` to `8.14`: - [[Cases] Populate user info from fake requests (elastic#180671)](elastic#180671) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Christos Nasikas","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-04-24T13:38:24Z","message":"[Cases] Populate user info from fake requests (elastic#180671)\n\n## Summary\r\n\r\nSet a system user name when the request is fake. This is helpful to show\r\nuser information to the case created by the case action.\r\n\r\n## Testing\r\n\r\n1. Create a rule with a case action\r\n2. In the created case verify that the `elastic/kibana` user is shown\r\nand not the `Unknown` user\r\n\r\n<img width=\"2297\" alt=\"Screenshot 2024-04-22 at 10 37 46 PM\"\r\nsrc=\"https://github.com/elastic/kibana/assets/7871006/abfcec4c-f2a4-4663-84e0-1816ada69167\">\r\n\r\n\r\n### Checklist\r\n\r\nDelete any items that are not applicable to this PR.\r\n\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\n### For maintainers\r\n\r\n- [x] This was checked for breaking API changes and was [labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine <[email protected]>","sha":"42fa118b7dd4e0ec9108ca056d5f705d6f5617e9","branchLabelMapping":{"^v8.15.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:skip","Team:ResponseOps","Feature:Cases","v8.14.0","v8.15.0"],"title":"[Cases] Populate user info from fake requests","number":180671,"url":"https://github.com/elastic/kibana/pull/180671","mergeCommit":{"message":"[Cases] Populate user info from fake requests (elastic#180671)\n\n## Summary\r\n\r\nSet a system user name when the request is fake. This is helpful to show\r\nuser information to the case created by the case action.\r\n\r\n## Testing\r\n\r\n1. Create a rule with a case action\r\n2. In the created case verify that the `elastic/kibana` user is shown\r\nand not the `Unknown` user\r\n\r\n<img width=\"2297\" alt=\"Screenshot 2024-04-22 at 10 37 46 PM\"\r\nsrc=\"https://github.com/elastic/kibana/assets/7871006/abfcec4c-f2a4-4663-84e0-1816ada69167\">\r\n\r\n\r\n### Checklist\r\n\r\nDelete any items that are not applicable to this PR.\r\n\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\n### For maintainers\r\n\r\n- [x] This was checked for breaking API changes and was [labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine <[email protected]>","sha":"42fa118b7dd4e0ec9108ca056d5f705d6f5617e9"}},"sourceBranch":"main","suggestedTargetBranches":["8.14"],"targetPullRequestStates":[{"branch":"8.14","label":"v8.14.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.15.0","branchLabelMappingKey":"^v8.15.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/180671","number":180671,"mergeCommit":{"message":"[Cases] Populate user info from fake requests (elastic#180671)\n\n## Summary\r\n\r\nSet a system user name when the request is fake. This is helpful to show\r\nuser information to the case created by the case action.\r\n\r\n## Testing\r\n\r\n1. Create a rule with a case action\r\n2. In the created case verify that the `elastic/kibana` user is shown\r\nand not the `Unknown` user\r\n\r\n<img width=\"2297\" alt=\"Screenshot 2024-04-22 at 10 37 46 PM\"\r\nsrc=\"https://github.com/elastic/kibana/assets/7871006/abfcec4c-f2a4-4663-84e0-1816ada69167\">\r\n\r\n\r\n### Checklist\r\n\r\nDelete any items that are not applicable to this PR.\r\n\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios\r\n\r\n### For maintainers\r\n\r\n- [x] This was checked for breaking API changes and was [labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine <[email protected]>","sha":"42fa118b7dd4e0ec9108ca056d5f705d6f5617e9"}}]}] BACKPORT--> Co-authored-by: Christos Nasikas <[email protected]>
- Loading branch information
1 parent
65cf0de
commit e98ed18
Showing
10 changed files
with
176 additions
and
9 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* 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 { coreMock, httpServerMock, loggingSystemMock } from '@kbn/core/server/mocks'; | ||
import { CasesClientFactory } from './factory'; | ||
import { createCasesClientFactoryMockArgs } from './mocks'; | ||
import { createCasesClient } from './client'; | ||
import type { FakeRawRequest } from '@kbn/core-http-server'; | ||
import { CoreKibanaRequest } from '@kbn/core-http-router-server-internal'; | ||
|
||
jest.mock('./client'); | ||
|
||
describe('CasesClientFactory', () => { | ||
const coreStart = coreMock.createStart(); | ||
const request = httpServerMock.createKibanaRequest(); | ||
|
||
const rawRequest: FakeRawRequest = { | ||
headers: {}, | ||
path: '/', | ||
}; | ||
|
||
const fakeRequest = CoreKibanaRequest.from(rawRequest); | ||
const createCasesClientMocked = createCasesClient as jest.Mock; | ||
const logger = loggingSystemMock.createLogger(); | ||
const args = createCasesClientFactoryMockArgs(); | ||
let casesClientFactory: CasesClientFactory; | ||
|
||
args.featuresPluginStart.getKibanaFeatures.mockReturnValue([]); | ||
|
||
beforeEach(() => { | ||
casesClientFactory = new CasesClientFactory(logger); | ||
casesClientFactory.initialize(args); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('user info', () => { | ||
it('constructs the user info from user profiles', async () => { | ||
const scopedClusterClient = coreStart.elasticsearch.client.asScoped(request).asCurrentUser; | ||
args.securityPluginStart.userProfiles.getCurrent.mockResolvedValueOnce({ | ||
// @ts-expect-error: not all fields are needed | ||
user: { username: 'my_user', full_name: 'My user', email: '[email protected]' }, | ||
}); | ||
|
||
await casesClientFactory.create({ | ||
request, | ||
savedObjectsService: coreStart.savedObjects, | ||
scopedClusterClient, | ||
}); | ||
|
||
expect(args.securityPluginStart.userProfiles.getCurrent).toHaveBeenCalled(); | ||
expect(args.securityPluginStart.authc.getCurrentUser).not.toHaveBeenCalled(); | ||
expect(createCasesClientMocked.mock.calls[0][0].user).toEqual({ | ||
username: 'my_user', | ||
full_name: 'My user', | ||
email: '[email protected]', | ||
}); | ||
}); | ||
|
||
it('constructs the user info from the authc service if the user profile is not available', async () => { | ||
const scopedClusterClient = coreStart.elasticsearch.client.asScoped(request).asCurrentUser; | ||
// @ts-expect-error: not all fields are needed | ||
args.securityPluginStart.authc.getCurrentUser.mockReturnValueOnce({ | ||
username: 'my_user_2', | ||
full_name: 'My user 2', | ||
email: '[email protected]', | ||
}); | ||
|
||
await casesClientFactory.create({ | ||
request, | ||
savedObjectsService: coreStart.savedObjects, | ||
scopedClusterClient, | ||
}); | ||
|
||
expect(args.securityPluginStart.userProfiles.getCurrent).toHaveBeenCalled(); | ||
expect(args.securityPluginStart.authc.getCurrentUser).toHaveBeenCalled(); | ||
expect(createCasesClientMocked.mock.calls[0][0].user).toEqual({ | ||
username: 'my_user_2', | ||
full_name: 'My user 2', | ||
email: '[email protected]', | ||
}); | ||
}); | ||
|
||
it('constructs the user info from fake requests correctly', async () => { | ||
const scopedClusterClient = | ||
coreStart.elasticsearch.client.asScoped(fakeRequest).asCurrentUser; | ||
|
||
await casesClientFactory.create({ | ||
request: fakeRequest, | ||
savedObjectsService: coreStart.savedObjects, | ||
scopedClusterClient, | ||
}); | ||
|
||
expect(args.securityPluginStart.userProfiles.getCurrent).toHaveBeenCalled(); | ||
expect(args.securityPluginStart.authc.getCurrentUser).toHaveBeenCalled(); | ||
expect(createCasesClientMocked.mock.calls[0][0].user).toEqual({ | ||
username: 'elastic/kibana', | ||
full_name: null, | ||
email: null, | ||
}); | ||
}); | ||
|
||
it('return null for all user fields if it cannot find the user info', async () => { | ||
const scopedClusterClient = coreStart.elasticsearch.client.asScoped(request).asCurrentUser; | ||
|
||
await casesClientFactory.create({ | ||
request, | ||
savedObjectsService: coreStart.savedObjects, | ||
scopedClusterClient, | ||
}); | ||
|
||
expect(args.securityPluginStart.userProfiles.getCurrent).toHaveBeenCalled(); | ||
expect(args.securityPluginStart.authc.getCurrentUser).toHaveBeenCalled(); | ||
expect(createCasesClientMocked.mock.calls[0][0].user).toEqual({ | ||
username: null, | ||
full_name: null, | ||
email: 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
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
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