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] Use authc.getCurrentUser from core.security in browser #187042

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();
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed the else because it would have been dead code. SecurityServiceStart comes from CoreStart and is always present.

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
1 change: 1 addition & 0 deletions x-pack/plugins/security_solution/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,6 @@
"@kbn/core-analytics-browser",
"@kbn/core-i18n-browser",
"@kbn/core-theme-browser",
"@kbn/core-security-browser",
]
}