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
2 changes: 1 addition & 1 deletion x-pack/plugins/security_solution/public/plugin_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ export class PluginServices {
savedObjectsTagging: savedObjectsTaggingOss.getTaggingApi(),
storage: this.storage,
sessionStorage: this.sessionStorage,
security: startPlugins.security,
securityLayout: { getPluginWrapper: () => SecuritySolutionTemplateWrapper },
securityService: coreStart.security,
contentManagement: startPlugins.contentManagement,
telemetry: this.telemetry.start(),
customDataService,
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",
]
}
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -36327,7 +36327,6 @@
"xpack.securitySolution.formattedNumber.compactThousands": "k",
"xpack.securitySolution.formattedNumber.compactTrillions": "T",
"xpack.securitySolution.getCurrentUser.Error": "Erreur lors de l'obtention de l'utilisateur",
"xpack.securitySolution.getCurrentUser.unknownUser": "Inconnu",
"xpack.securitySolution.getFileAction.pendingMessage": "Récupération du fichier à partir de l'hôte.",
"xpack.securitySolution.getStarted.landingCards.box.cloudCard.desc": "Évaluez votre niveau de cloud et protégez vos charges de travail contre les attaques.",
"xpack.securitySolution.getStarted.landingCards.box.cloudCard.title": "Protection cloud de bout en bout",
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -36302,7 +36302,6 @@
"xpack.securitySolution.formattedNumber.compactThousands": "k",
"xpack.securitySolution.formattedNumber.compactTrillions": "T",
"xpack.securitySolution.getCurrentUser.Error": "ユーザーの取得エラー",
"xpack.securitySolution.getCurrentUser.unknownUser": "不明",
"xpack.securitySolution.getFileAction.pendingMessage": "ホストからファイルを取得しています。",
"xpack.securitySolution.getStarted.landingCards.box.cloudCard.desc": "クラウド態勢を評価し、ワークロードを攻撃から保護します。",
"xpack.securitySolution.getStarted.landingCards.box.cloudCard.title": "エンドツーエンドのクラウド保護",
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -36345,7 +36345,6 @@
"xpack.securitySolution.formattedNumber.compactThousands": "k",
"xpack.securitySolution.formattedNumber.compactTrillions": "T",
"xpack.securitySolution.getCurrentUser.Error": "获取用户时出错",
"xpack.securitySolution.getCurrentUser.unknownUser": "未知",
"xpack.securitySolution.getFileAction.pendingMessage": "正在从主机检索文件。",
"xpack.securitySolution.getStarted.landingCards.box.cloudCard.desc": "评估您的云态势并防止工作负载受到攻击。",
"xpack.securitySolution.getStarted.landingCards.box.cloudCard.title": "端到端云防护",
Expand Down