Skip to content

Commit

Permalink
[Security Solution] Use authc.getCurrentUser from core.security in br…
Browse files Browse the repository at this point in the history
…owser
  • Loading branch information
tsullivan committed Jun 28, 2024
1 parent 55bbd76 commit aa82d28
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 31 deletions.
25 changes: 5 additions & 20 deletions x-pack/plugins/security_solution/public/common/lib/kibana/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,32 +88,17 @@ export const useCurrentUser = (): AuthenticatedElasticUser | null => {

const [, dispatchToaster] = useStateToaster();

const { security } = useKibana().services;
const { securityService: security } = useKibana().services;

const fetchUser = useCallback(
() => {
let didCancel = false;
const fetchData = async () => {
try {
if (security != null) {
const response = await security.authc.getCurrentUser();
if (!isMounted.current) return;
if (!didCancel) {
setUser(convertToCamelCase<AuthenticatedUser, AuthenticatedElasticUser>(response));
}
} else {
setUser({
username: i18n.translate('xpack.securitySolution.getCurrentUser.unknownUser', {
defaultMessage: 'Unknown',
}),
email: '',
fullName: '',
roles: [],
enabled: false,
authenticationRealm: { name: '', type: '' },
lookupRealm: { name: '', type: '' },
authenticationProvider: '',
});
const response = await security.authc.getCurrentUser();
if (!isMounted.current) return;
if (!didCancel) {
setUser(convertToCamelCase<AuthenticatedUser, AuthenticatedElasticUser>(response));
}
} catch (error) {
if (!didCancel) {
Expand Down
21 changes: 11 additions & 10 deletions x-pack/plugins/security_solution/public/management/links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,30 @@ describe('links', () => {
links: links.links?.filter((link) => !excludedLinks.includes(link.id)),
});

const getPlugins = (noUserAuthz: boolean = false): StartPlugins => {
const getPlugins = (): StartPlugins => {
return {
security: {
authc: {
getCurrentUser: noUserAuthz
? jest.fn().mockReturnValue(undefined)
: jest.fn().mockReturnValue([]),
},
},
fleet: {
authz: createFleetAuthzMock(),
},
} as unknown as StartPlugins;
};

const getCoreStart = (noUserAuthz: boolean = false) => {
const coreStart = coreMock.createStart();
coreStart.security.authc.getCurrentUser = noUserAuthz
? jest.fn().mockReturnValue(undefined)
: jest.fn().mockReturnValue([]);
return coreStart;
};

beforeAll(() => {
ExperimentalFeaturesService.init({
experimentalFeatures: { ...allowedExperimentalValues },
});
});

beforeEach(() => {
coreMockStarted = coreMock.createStart();
coreMockStarted = getCoreStart();
fakeHttpServices = coreMockStarted.http as jest.Mocked<HttpSetup>;
});

Expand All @@ -84,7 +85,7 @@ describe('links', () => {
});

it('should not return any endpoint management link for user with all sub-feature privileges when no user authz', async () => {
const filteredLinks = await getManagementFilteredLinks(coreMockStarted, getPlugins(true));
const filteredLinks = await getManagementFilteredLinks(getCoreStart(true), getPlugins());
expect(filteredLinks).toEqual(
getLinksWithout(
SecurityPageName.blocklist,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export const getManagementFilteredLinks = async (
plugins: StartPlugins
): Promise<LinkItem> => {
const fleetAuthz = plugins.fleet?.authz;
const currentUser = await plugins.security.authc.getCurrentUser();
const currentUser = await core.security.authc.getCurrentUser();
const {
canReadActionsLogManagement,
canAccessHostIsolationExceptions,
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/security_solution/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import type { ChartsPluginStart } from '@kbn/charts-plugin/public';
import type { SavedSearchPublicPluginStart } from '@kbn/saved-search-plugin/public';
import type { PluginStartContract } from '@kbn/alerting-plugin/public/plugin';
import type { MapsStartApi } from '@kbn/maps-plugin/public';
import type { SecurityServiceStart } from '@kbn/core-security-browser';
import type { ResolverPluginSetup } from './resolver/types';
import type { Inspect } from '../common/search_strategy';
import type { Detections } from './detections';
Expand Down Expand Up @@ -188,6 +189,11 @@ export type StartServices = CoreStart &
customDataService: DataPublicPluginStart;
topValuesPopover: TopValuesPopoverService;
timelineDataService: DataPublicPluginStart;
/**
* The "security" field name is used for both CoreStart['security'] and StartPlugins['security'] (deprecated).
* Use this field to ensure you are using the non-deprecated Security-In-Core APIs.
*/
securityService: SecurityServiceStart;
};

export type StartRenderServices = Pick<
Expand Down

0 comments on commit aa82d28

Please sign in to comment.