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

[8.x] [Stream] Fix callout privileges (#198030) #198262

Open
wants to merge 2 commits into
base: 8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import { EuiCallOut } from '@elastic/eui';
import React 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,
} 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 { useKibanaContextForPlugin } from '../hooks/use_kibana';

const pageConfigurations = {
Expand Down Expand Up @@ -44,14 +48,22 @@ interface LogsDeprecationCalloutProps {

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

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

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

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

if (isDismissed || !(allDatasetLocator && discover?.show && fleet?.read)) {
return null;
}

Expand All @@ -71,7 +83,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 +93,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
148 changes: 78 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,7 @@ import {
DEFAULT_APP_CATEGORIES,
PluginInitializerContext,
AppDeepLinkLocations,
AppStatus,
} from '@kbn/core/public';
import { i18n } from '@kbn/i18n';
import { enableInfrastructureHostsView } from '@kbn/observability-plugin/public';
Expand All @@ -35,6 +36,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 @@ -141,66 +143,57 @@ export class Plugin implements InfraClientPluginClass {
/** !! 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',
},
]
: []),
],
},
]
: []),
];
}
)
map(([[{ application }], isInfrastructureHostsViewEnabled]) => {
const { infrastructure, logs } = application.capabilities;
return [
...(logs.show
? [
{
label: logsTitle,
sortKey: 200,
entries: getLogsNavigationEntries({
application,
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 @@ -408,26 +401,28 @@ export class Plugin implements InfraClientPluginClass {
}

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

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

if (capabilities.discover?.show && capabilities.fleet?.read) {
entries.push({
label: 'Explorer',
app: 'observability-logs-explorer',
path: '/',
isBetaFeature: true,
});
}
getLogsExplorerAccessibility$(application).subscribe((isAccessible) => {
if (isAccessible) {
entries.push({
label: 'Explorer',
app: 'observability-logs-explorer',
path: '/',
isBetaFeature: true,
});
}
mohamedhamed-ahmed marked this conversation as resolved.
Show resolved Hide resolved
});

// Display Stream nav entry when Logs Stream is enabled
if (routes.stream) entries.push(createNavEntryFromRoute(routes.stream));
Expand All @@ -440,6 +435,19 @@ const getLogsNavigationEntries = ({
return entries;
};

const getLogsExplorerAccessibility$ = (application: CoreStart['application']) => {
const { capabilities, applications$ } = application;
return applications$.pipe(
map(
(apps) =>
(apps.get(OBSERVABILITY_LOGS_EXPLORER_APP_ID)?.status ?? AppStatus.inaccessible) ===
AppStatus.accessible &&
capabilities.discover?.show &&
capabilities.fleet?.read
)
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
);
),
distinctUntilChanged()
);
// `import { map, distinctUntilChanged } from 'rxjs';`

to avoid duplicate values. Otherwise the subscription may be called multiple of times resulting in the following:

198262-multi-nav-02

The above is observed on 8.x branch and wasn't happening on main, but would be ideal to adjust everywhere.

Also see if we need capabilities.discover?.show && capabilities.fleet?.read explicitly here as the following already includes both.

if (!(discover?.show && fleet?.read && logs?.show)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes the check is still needed as its the one responsible for pushing the entry to the side nav

};

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