Skip to content

Commit

Permalink
[8.16] [Stream] Fix callout privileges (#198030) (#198269)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.16`:
- [[Stream] Fix callout privileges
(#198030)](#198030)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"mohamedhamed-ahmed","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-30T08:43:35Z","message":"[Stream]
Fix callout privileges (#198030)\n\ncloses
https://github.com/elastic/kibana/issues/197044\r\ncloses
https://github.com/elastic/kibana/issues/197988\r\n\r\n## 📝
Summary\r\n\r\nThe PR targets fixing 3 bugs:\r\n\r\n1. The callout
shouldn't be shown if `Logs Explorer` isn't available,\r\notherwise the
link displayed in the callout will cause a problem.
[Fix\r\nhere](https://github.com/elastic/kibana/pull/198030/files#diff-8c5f793d1bf0d933c73047f31ded8a5a65f91e4f8d5230fc4e40dfb9b245fec1R66)\r\n2.
The callout shouldn't be shown if `Logs Explorer` is available
but\r\nthe user doesn't have privilege to access it, otherwise we end up
in\r\n`Application Not Found` page.
[Fix\r\nhere](https://github.com/elastic/kibana/pull/198030/files#diff-8c5f793d1bf0d933c73047f31ded8a5a65f91e4f8d5230fc4e40dfb9b245fec1R66)\r\n3.
The side nav entry for `Logs Explorer` should be hidden if
`Logs\r\nExplorer` is disabled.
[Fix\r\nhere](https://github.com/elastic/kibana/pull/198030/files#diff-ad4e42fbe4d2a7a99ec5d52fbf127ded12f0efe02a10e3afa0409b40be1f4bcfR397)","sha":"5576316abadc4115020f2e22bd823b413c8e98a2","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:skip","v9.0.0","ci:project-deploy-observability","Team:obs-ux-logs","Team:obs-ux-infra_services","v8.15.0","v8.16.0","backport:version","v8.17.0"],"number":198030,"url":"https://github.com/elastic/kibana/pull/198030","mergeCommit":{"message":"[Stream]
Fix callout privileges (#198030)\n\ncloses
https://github.com/elastic/kibana/issues/197044\r\ncloses
https://github.com/elastic/kibana/issues/197988\r\n\r\n## 📝
Summary\r\n\r\nThe PR targets fixing 3 bugs:\r\n\r\n1. The callout
shouldn't be shown if `Logs Explorer` isn't available,\r\notherwise the
link displayed in the callout will cause a problem.
[Fix\r\nhere](https://github.com/elastic/kibana/pull/198030/files#diff-8c5f793d1bf0d933c73047f31ded8a5a65f91e4f8d5230fc4e40dfb9b245fec1R66)\r\n2.
The callout shouldn't be shown if `Logs Explorer` is available
but\r\nthe user doesn't have privilege to access it, otherwise we end up
in\r\n`Application Not Found` page.
[Fix\r\nhere](https://github.com/elastic/kibana/pull/198030/files#diff-8c5f793d1bf0d933c73047f31ded8a5a65f91e4f8d5230fc4e40dfb9b245fec1R66)\r\n3.
The side nav entry for `Logs Explorer` should be hidden if
`Logs\r\nExplorer` is disabled.
[Fix\r\nhere](https://github.com/elastic/kibana/pull/198030/files#diff-ad4e42fbe4d2a7a99ec5d52fbf127ded12f0efe02a10e3afa0409b40be1f4bcfR397)","sha":"5576316abadc4115020f2e22bd823b413c8e98a2"}},"sourceBranch":"main","suggestedTargetBranches":["8.15","8.16","8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","labelRegex":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/198030","number":198030,"mergeCommit":{"message":"[Stream]
Fix callout privileges (#198030)\n\ncloses
https://github.com/elastic/kibana/issues/197044\r\ncloses
https://github.com/elastic/kibana/issues/197988\r\n\r\n## 📝
Summary\r\n\r\nThe PR targets fixing 3 bugs:\r\n\r\n1. The callout
shouldn't be shown if `Logs Explorer` isn't available,\r\notherwise the
link displayed in the callout will cause a problem.
[Fix\r\nhere](https://github.com/elastic/kibana/pull/198030/files#diff-8c5f793d1bf0d933c73047f31ded8a5a65f91e4f8d5230fc4e40dfb9b245fec1R66)\r\n2.
The callout shouldn't be shown if `Logs Explorer` is available
but\r\nthe user doesn't have privilege to access it, otherwise we end up
in\r\n`Application Not Found` page.
[Fix\r\nhere](https://github.com/elastic/kibana/pull/198030/files#diff-8c5f793d1bf0d933c73047f31ded8a5a65f91e4f8d5230fc4e40dfb9b245fec1R66)\r\n3.
The side nav entry for `Logs Explorer` should be hidden if
`Logs\r\nExplorer` is disabled.
[Fix\r\nhere](https://github.com/elastic/kibana/pull/198030/files#diff-ad4e42fbe4d2a7a99ec5d52fbf127ded12f0efe02a10e3afa0409b40be1f4bcfR397)","sha":"5576316abadc4115020f2e22bd823b413c8e98a2"}},{"branch":"8.15","label":"v8.15.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.16","label":"v8.16.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.x","label":"v8.17.0","labelRegex":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
mohamedhamed-ahmed and kibanamachine authored Oct 31, 2024
1 parent 365c0a3 commit 049ac4c
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
*/

import { History } from 'history';
import { CoreStart } from '@kbn/core/public';
import React from 'react';
import { AppStatus, CoreStart } from '@kbn/core/public';
import React, { useMemo } from 'react';
import ReactDOM from 'react-dom';
import { Router, Routes, Route } from '@kbn/shared-ux-router';
import { AppMountParameters } from '@kbn/core/public';
import { Storage } from '@kbn/kibana-utils-plugin/public';
import { AllDatasetsLocatorParams, ALL_DATASETS_LOCATOR_ID } from '@kbn/deeplinks-observability';
import {
AllDatasetsLocatorParams,
ALL_DATASETS_LOCATOR_ID,
OBSERVABILITY_LOGS_EXPLORER_APP_ID,
} from '@kbn/deeplinks-observability';
import useObservable from 'react-use/lib/useObservable';
import { map } from 'rxjs';
import { LinkToLogsPage } from '../pages/link_to/link_to_logs';
import { LogsPage } from '../pages/logs';
import { InfraClientStartDeps, InfraClientStartExports } from '../types';
Expand Down Expand Up @@ -57,7 +63,22 @@ const LogsApp: React.FC<{
storage: Storage;
theme$: AppMountParameters['theme$'];
}> = ({ core, history, pluginStart, plugins, setHeaderActionMenu, storage, theme$ }) => {
const { logs, discover, fleet } = core.application.capabilities;
const { logs } = core.application.capabilities;

const isLogsExplorerAppAccessible = useObservable(
useMemo(
() =>
core.application.applications$.pipe(
map(
(apps) =>
(apps.get(OBSERVABILITY_LOGS_EXPLORER_APP_ID)?.status ?? AppStatus.inaccessible) ===
AppStatus.accessible
)
),
[core.application.applications$]
),
false
);

return (
<CoreProviders core={core} pluginStart={pluginStart} plugins={plugins} theme$={theme$}>
Expand All @@ -74,7 +95,7 @@ const LogsApp: React.FC<{
toastsService={core.notifications.toasts}
>
<Routes>
{Boolean(discover?.show && fleet?.read) && (
{isLogsExplorerAppAccessible && (
<Route
path="/"
exact
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@
*/

import { EuiCallOut } from '@elastic/eui';
import React from 'react';
import React, { useMemo } from 'react';
import { i18n } from '@kbn/i18n';
import { EuiButton } from '@elastic/eui';
import { AllDatasetsLocatorParams, ALL_DATASETS_LOCATOR_ID } from '@kbn/deeplinks-observability';
import {
AllDatasetsLocatorParams,
ALL_DATASETS_LOCATOR_ID,
DatasetLocatorParams,
OBSERVABILITY_LOGS_EXPLORER_APP_ID,
} from '@kbn/deeplinks-observability';
import { getRouterLinkProps } from '@kbn/router-utils';
import useLocalStorage from 'react-use/lib/useLocalStorage';

import { euiThemeVars } from '@kbn/ui-theme';
import { css } from '@emotion/css';
import { SharePublicStart } from '@kbn/share-plugin/public/plugin';
import { LocatorPublic } from '@kbn/share-plugin/common';
import useObservable from 'react-use/lib/useObservable';
import { AppStatus } from '@kbn/core/public';
import { map } from 'rxjs';
import { useKibanaContextForPlugin } from '../hooks/use_kibana';

const pageConfigurations = {
Expand Down Expand Up @@ -44,17 +52,35 @@ interface LogsDeprecationCalloutProps {

export const LogsDeprecationCallout = ({ page }: LogsDeprecationCalloutProps) => {
const {
services: { share },
services: { share, application },
} = useKibanaContextForPlugin();

const isLogsExplorerAppAccessible = useObservable(
useMemo(
() =>
application.applications$.pipe(
map(
(apps) =>
(apps.get(OBSERVABILITY_LOGS_EXPLORER_APP_ID)?.status ?? AppStatus.inaccessible) ===
AppStatus.accessible
)
),
[application.applications$]
),
false
);

const { dismissalStorageKey, message } = pageConfigurations[page];

const [isDismissed, setDismissed] = useLocalStorage(dismissalStorageKey, false);

if (isDismissed) {
if (isDismissed || !isLogsExplorerAppAccessible) {
return null;
}

const allDatasetLocator =
share.url.locators.get<AllDatasetsLocatorParams>(ALL_DATASETS_LOCATOR_ID);

return (
<EuiCallOut
title={i18n.translate('xpack.infra.logsDeprecationCallout.euiCallOut.discoverANewLogLabel', {
Expand All @@ -71,7 +97,7 @@ export const LogsDeprecationCallout = ({ page }: LogsDeprecationCalloutProps) =>
fill
data-test-subj="infraLogsDeprecationCalloutTryLogsExplorerButton"
color="warning"
{...getLogsExplorerLinkProps(share)}
{...getLogsExplorerLinkProps(allDatasetLocator!)}
>
{i18n.translate('xpack.infra.logsDeprecationCallout.tryLogsExplorerButtonLabel', {
defaultMessage: 'Try Logs Explorer',
Expand All @@ -81,9 +107,7 @@ export const LogsDeprecationCallout = ({ page }: LogsDeprecationCalloutProps) =>
);
};

const getLogsExplorerLinkProps = (share: SharePublicStart) => {
const locator = share.url.locators.get<AllDatasetsLocatorParams>(ALL_DATASETS_LOCATOR_ID)!;

const getLogsExplorerLinkProps = (locator: LocatorPublic<DatasetLocatorParams>) => {
return getRouterLinkProps({
href: locator.getRedirectUrl({}),
onClick: () => locator.navigate({}),
Expand Down
155 changes: 85 additions & 70 deletions x-pack/plugins/observability_solution/infra/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
DEFAULT_APP_CATEGORIES,
PluginInitializerContext,
AppDeepLinkLocations,
AppStatus,
ApplicationStart,
} from '@kbn/core/public';
import { i18n } from '@kbn/i18n';
import { enableInfrastructureHostsView } from '@kbn/observability-plugin/public';
Expand All @@ -21,7 +23,7 @@ import {
MetricsExplorerLocatorParams,
ObservabilityTriggerId,
} from '@kbn/observability-shared-plugin/common';
import { BehaviorSubject, combineLatest, from } from 'rxjs';
import { BehaviorSubject, combineLatest, distinctUntilChanged, from, of, switchMap } from 'rxjs';
import { map } from 'rxjs';
import type { EmbeddableApiContext } from '@kbn/presentation-publishing';
import { apiCanAddNewPanel } from '@kbn/presentation-containers';
Expand All @@ -35,6 +37,7 @@ import {
} from '@kbn/observability-shared-plugin/common';
import { OBSERVABILITY_ENABLE_LOGS_STREAM } from '@kbn/management-settings-ids';
import { NavigationEntry } from '@kbn/observability-shared-plugin/public';
import { OBSERVABILITY_LOGS_EXPLORER_APP_ID } from '@kbn/deeplinks-observability/constants';
import type { InfraPublicConfig } from '../common/plugin_config_types';
import { createInventoryMetricRuleType } from './alerting/inventory';
import { createLogThresholdRuleType } from './alerting/log_threshold';
Expand Down Expand Up @@ -131,76 +134,75 @@ export class Plugin implements InfraClientPluginClass {
messageFields: this.config.sources?.default?.fields?.message,
});

const startDep$AndHostViewFlag$ = combineLatest([
const startDep$AndAccessibleFlag$ = combineLatest([
from(core.getStartServices()),
core.settings.client.get$<boolean>(enableInfrastructureHostsView),
]);
]).pipe(
switchMap(([[{ application }], isInfrastructureHostsViewEnabled]) =>
combineLatest([
of(application),
of(isInfrastructureHostsViewEnabled),
getLogsExplorerAccessible$(application),
])
)
);

const logRoutes = getLogsAppRoutes({ isLogsStreamEnabled });

/** !! Need to be kept in sync with the deepLinks in x-pack/plugins/observability_solution/infra/public/plugin.ts */
pluginsSetup.observabilityShared.navigation.registerSections(
startDep$AndHostViewFlag$.pipe(
map(
([
[
{
application: { capabilities },
},
],
isInfrastructureHostsViewEnabled,
]) => {
const { infrastructure, logs } = capabilities;
return [
...(logs.show
? [
{
label: logsTitle,
sortKey: 200,
entries: getLogsNavigationEntries({
capabilities,
config: this.config,
routes: logRoutes,
}),
},
]
: []),
...(infrastructure.show
? [
{
label: metricsTitle,
sortKey: 300,
entries: [
{
label: inventoryTitle,
app: 'metrics',
path: '/inventory',
},
...(this.config.featureFlags.metricsExplorerEnabled
? [
{
label: metricsExplorerTitle,
app: 'metrics',
path: '/explorer',
},
]
: []),
...(isInfrastructureHostsViewEnabled
? [
{
label: hostsTitle,
app: 'metrics',
path: '/hosts',
},
]
: []),
],
},
]
: []),
];
}
)
startDep$AndAccessibleFlag$.pipe(
map(([application, isInfrastructureHostsViewEnabled, isLogsExplorerAccessible]) => {
const { infrastructure, logs } = application.capabilities;
return [
...(logs.show
? [
{
label: logsTitle,
sortKey: 200,
entries: getLogsNavigationEntries({
isLogsExplorerAccessible,
config: this.config,
routes: logRoutes,
}),
},
]
: []),
...(infrastructure.show
? [
{
label: metricsTitle,
sortKey: 300,
entries: [
{
label: inventoryTitle,
app: 'metrics',
path: '/inventory',
},
...(this.config.featureFlags.metricsExplorerEnabled
? [
{
label: metricsExplorerTitle,
app: 'metrics',
path: '/explorer',
},
]
: []),
...(isInfrastructureHostsViewEnabled
? [
{
label: hostsTitle,
app: 'metrics',
path: '/hosts',
},
]
: []),
],
},
]
: []),
];
})
)
);

Expand Down Expand Up @@ -333,9 +335,10 @@ export class Plugin implements InfraClientPluginClass {
},
});

startDep$AndHostViewFlag$.subscribe(
([_startServices, isInfrastructureHostsViewEnabled]: [
[CoreStart, InfraClientStartDeps, InfraClientStartExports],
startDep$AndAccessibleFlag$.subscribe(
([_startServices, isInfrastructureHostsViewEnabled, _isLogsExplorerAccessible]: [
ApplicationStart,
boolean,
boolean
]) => {
this.appUpdater$.next(() => ({
Expand Down Expand Up @@ -408,19 +411,19 @@ export class Plugin implements InfraClientPluginClass {
}

const getLogsNavigationEntries = ({
capabilities,
isLogsExplorerAccessible,
config,
routes,
}: {
capabilities: CoreStart['application']['capabilities'];
isLogsExplorerAccessible: boolean;
config: InfraPublicConfig;
routes: LogsAppRoutes;
}) => {
const entries: NavigationEntry[] = [];

if (!config.featureFlags.logsUIEnabled) return entries;

if (capabilities.discover?.show && capabilities.fleet?.read) {
if (isLogsExplorerAccessible) {
entries.push({
label: 'Explorer',
app: 'observability-logs-explorer',
Expand All @@ -440,6 +443,18 @@ const getLogsNavigationEntries = ({
return entries;
};

const getLogsExplorerAccessible$ = (application: CoreStart['application']) => {
const { applications$ } = application;
return applications$.pipe(
map(
(apps) =>
(apps.get(OBSERVABILITY_LOGS_EXPLORER_APP_ID)?.status ?? AppStatus.inaccessible) ===
AppStatus.accessible
),
distinctUntilChanged()
);
};

const createNavEntryFromRoute = ({ path, title }: LogsRoute): NavigationEntry => ({
app: 'logs',
label: title,
Expand Down

0 comments on commit 049ac4c

Please sign in to comment.