From c66509cd2ad97079ee5518e2cc87b461a696f527 Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Thu, 14 Mar 2024 15:10:33 -0400 Subject: [PATCH 1/5] Check if `licenseManagement` is available before linking from Synthetics plugin. --- .../plugins/observability_solution/synthetics/kibana.jsonc | 3 ++- .../apps/synthetics/hooks/use_synthetics_priviliges.tsx | 7 +++++++ .../synthetics/public/apps/synthetics/routes.tsx | 6 +++--- .../observability_solution/synthetics/public/plugin.ts | 2 ++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/observability_solution/synthetics/kibana.jsonc b/x-pack/plugins/observability_solution/synthetics/kibana.jsonc index ac894c024e5cd..6a5ce0209da2e 100644 --- a/x-pack/plugins/observability_solution/synthetics/kibana.jsonc +++ b/x-pack/plugins/observability_solution/synthetics/kibana.jsonc @@ -42,7 +42,8 @@ "ml", "serverless", "spaces", - "telemetry" + "telemetry", + "licenseManagement" ], "requiredBundles": [ "data", diff --git a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx index 812b540eea3ae..75793e1fc79b7 100644 --- a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx +++ b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx @@ -16,6 +16,7 @@ import { EuiMarkdownFormat, } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n-react'; +import { useKibana } from '@kbn/kibana-react-plugin/public'; import { css } from '@emotion/react'; import { i18n } from '@kbn/i18n'; import { selectOverviewStatus } from '../state/overview_status'; @@ -27,6 +28,7 @@ import { SYNTHETICS_INDEX_PATTERN, } from '../../../../common/constants'; import { useSyntheticsSettingsContext } from '../contexts'; +import { ClientPluginsStart } from '../../../plugin'; export const useSyntheticsPrivileges = () => { const { canRead: canReadSyntheticsIndex, loading: isCanReadLoading } = @@ -107,7 +109,11 @@ const Unprivileged = ({ unprivilegedIndices }: { unprivilegedIndices: string[] } ); const LicenseExpired = () => { + const plugins = useKibana(); + + const licenseManagementEnabled = plugins.services.licenseManagement?.enabled; const { basePath } = useSyntheticsSettingsContext(); + return ( { actions={[ {i18n.translate('xpack.synthetics.invalidLicense.licenseManagementLink', { diff --git a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/routes.tsx b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/routes.tsx index 154ad7e35c82f..69e077ae82e7e 100644 --- a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/routes.tsx +++ b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/routes.tsx @@ -188,7 +188,7 @@ export const PageRouter: FC = () => { apiService.addInspectorRequest = addInspectorRequest; - const isUnPrivileged = useSyntheticsPrivileges(); + const isUnprivileged = useSyntheticsPrivileges(); return ( @@ -205,12 +205,12 @@ export const PageRouter: FC = () => {
- {isUnPrivileged || } + {isUnprivileged || }
diff --git a/x-pack/plugins/observability_solution/synthetics/public/plugin.ts b/x-pack/plugins/observability_solution/synthetics/public/plugin.ts index d4758952e63fe..88d0071ef1f13 100644 --- a/x-pack/plugins/observability_solution/synthetics/public/plugin.ts +++ b/x-pack/plugins/observability_solution/synthetics/public/plugin.ts @@ -50,6 +50,7 @@ import type { ObservabilitySharedPluginSetup, ObservabilitySharedPluginStart, } from '@kbn/observability-shared-plugin/public'; +import { LicenseManagementUIPluginSetup } from '@kbn/license-management-plugin/public/plugin'; import { ObservabilityAIAssistantPublicSetup, ObservabilityAIAssistantPublicStart, @@ -100,6 +101,7 @@ export interface ClientPluginsStart { uiSettings: CoreStart['uiSettings']; usageCollection: UsageCollectionStart; serverless: ServerlessPluginStart; + licenseManagement?: LicenseManagementUIPluginSetup; } export interface UptimePluginServices extends Partial { From dfc292505238ed913ea04a9f2a24b066b169de33 Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Thu, 14 Mar 2024 17:07:38 -0400 Subject: [PATCH 2/5] Add unit tests. --- .../hooks/use_synthetics_priviliges.test.tsx | 57 +++++++++++++++++++ .../hooks/use_synthetics_priviliges.tsx | 3 + 2 files changed, 60 insertions(+) create mode 100644 x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.test.tsx diff --git a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.test.tsx b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.test.tsx new file mode 100644 index 0000000000000..7d496b3940da8 --- /dev/null +++ b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.test.tsx @@ -0,0 +1,57 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { renderHook } from '@testing-library/react-hooks'; +import React from 'react'; +import { render, WrappedHelper } from '../utils/testing'; +import { useSyntheticsPrivileges } from './use_synthetics_priviliges'; + +jest.mock('../../../hooks/use_capabilities', () => ({ + useCanReadSyntheticsIndex: jest.fn().mockReturnValue({ canRead: true, loading: false }), +})); + +jest.mock('react-redux', () => { + const actual = jest.requireActual('react-redux'); + return { + ...actual, + useSelector: jest.fn().mockReturnValue({ error: { body: { message: 'License not active' } } }), + }; +}); + +function wrapper({ children }: { children: React.ReactElement }) { + return {children}; +} + +describe('useSyntheticsPrivileges', () => { + it.each([ + [true, null], + [false, ''], + ])( + 'should correctly set the disabled prop of the license nav button if `licenseManagement` enabled is %s', + (enabled, expectedDisabledAttribute) => { + const { + result: { current }, + } = renderHook(() => useSyntheticsPrivileges(), { wrapper }); + + expect(current).not.toBeUndefined(); + + const { getByLabelText } = render(current!, { + core: { + licenseManagement: { enabled }, + }, + }); + + const licenseNavButton = getByLabelText('Navigate to license management'); + + expect(licenseNavButton); + + // there should only be an href if the license management is enabled, otherwise we render a disabled button with no handler + expect(!!licenseNavButton.getAttribute('href')).toEqual(enabled); + expect(licenseNavButton.getAttribute('disabled')).toEqual(expectedDisabledAttribute); + } + ); +}); diff --git a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx index 75793e1fc79b7..c43b1deb9bee7 100644 --- a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx +++ b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx @@ -137,6 +137,9 @@ const LicenseExpired = () => { } actions={[ Date: Thu, 14 Mar 2024 17:10:28 -0400 Subject: [PATCH 3/5] Simplify. --- .../apps/synthetics/hooks/use_synthetics_priviliges.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx index c43b1deb9bee7..4e15c41e36d06 100644 --- a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx +++ b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx @@ -109,9 +109,9 @@ const Unprivileged = ({ unprivilegedIndices }: { unprivilegedIndices: string[] } ); const LicenseExpired = () => { - const plugins = useKibana(); + const { services } = useKibana(); + const licenseManagementEnabled = services.licenseManagement?.enabled; - const licenseManagementEnabled = plugins.services.licenseManagement?.enabled; const { basePath } = useSyntheticsSettingsContext(); return ( From 1d42fecb890697fa72d484df21947958127846f6 Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Thu, 21 Mar 2024 14:51:48 -0400 Subject: [PATCH 4/5] Simplify. --- .../apps/synthetics/hooks/use_synthetics_priviliges.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx index 4e15c41e36d06..3de71d4470fdf 100644 --- a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx +++ b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/hooks/use_synthetics_priviliges.tsx @@ -109,8 +109,8 @@ const Unprivileged = ({ unprivilegedIndices }: { unprivilegedIndices: string[] } ); const LicenseExpired = () => { - const { services } = useKibana(); - const licenseManagementEnabled = services.licenseManagement?.enabled; + const licenseManagementEnabled = + useKibana().services.licenseManagement?.enabled; const { basePath } = useSyntheticsSettingsContext(); From 7ac745037c415d7a63fd6cfd37eda57eac465e7b Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Fri, 22 Mar 2024 15:41:10 +0000 Subject: [PATCH 5/5] [CI] Auto-commit changed files from 'node scripts/lint_ts_projects --fix' --- x-pack/plugins/observability_solution/synthetics/tsconfig.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/observability_solution/synthetics/tsconfig.json b/x-pack/plugins/observability_solution/synthetics/tsconfig.json index 9e1f64d07acea..2c2b2a11f08c9 100644 --- a/x-pack/plugins/observability_solution/synthetics/tsconfig.json +++ b/x-pack/plugins/observability_solution/synthetics/tsconfig.json @@ -84,7 +84,8 @@ "@kbn/code-editor-mock", "@kbn/serverless", "@kbn/repo-info", - "@kbn/index-management-plugin" + "@kbn/index-management-plugin", + "@kbn/license-management-plugin" ], "exclude": ["target/**/*"] }