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

[SecuritySolution] Register AI Assistant management settings according to productFeatureKeys #213105

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React from 'react';
import { i18n } from '@kbn/i18n';
import { Subject, combineLatestWith } from 'rxjs';
import { Subject, withLatestFrom, combineLatestWith } from 'rxjs';
import type * as H from 'history';
import type {
AppMountParameters,
Expand All @@ -24,6 +24,7 @@ import type {
SecuritySolutionAppWrapperFeature,
SecuritySolutionCellRendererFeature,
} from '@kbn/discover-shared-plugin/public/services/discover_features';
import { ProductFeatureAssistantKey } from '@kbn/security-solution-features/src/product_features_keys';
import { getLazyCloudSecurityPosturePliAuthBlockExtension } from './cloud_security_posture/lazy_cloud_security_posture_pli_auth_block_extension';
import { getLazyEndpointAgentTamperProtectionExtension } from './management/pages/policy/view/ingest_manager_integration/lazy_endpoint_agent_tamper_protection_extension';
import type {
Expand Down Expand Up @@ -99,6 +100,7 @@ export class Plugin implements IPlugin<PluginSetup, PluginStart, SetupPlugins, S
this.services.setup(core, plugins);

const { home, usageCollection, management, cases } = plugins;
const { productFeatureKeys$ } = this.contract;

// Lazily instantiate subPlugins and initialize services
const mountDependencies = async (params?: AppMountParameters) => {
Expand Down Expand Up @@ -202,6 +204,21 @@ export class Plugin implements IPlugin<PluginSetup, PluginStart, SetupPlugins, S
},
});

productFeatureKeys$
.pipe(withLatestFrom(plugins.licensing.license$))
.subscribe(([productFeatureKeys, license]) => {
const isAssistantAvailable =
productFeatureKeys?.has(ProductFeatureAssistantKey.assistant) &&
license?.hasAtLeast('enterprise');
Comment on lines +214 to +216
Copy link
Contributor

@semd semd Mar 5, 2025

Choose a reason for hiding this comment

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

I just realized that if productFeatureKeys is still null, or the license is not defined yet, this code will behave as if the assistant is not available, disabling the assistant management.
Can we add a check for at the beginning?

Suggested change
const isAssistantAvailable =
productFeatureKeys?.has(ProductFeatureAssistantKey.assistant) &&
license?.hasAtLeast('enterprise');
if (!productFeatureKeys || !license) {
return;
}
const isAssistantAvailable =
productFeatureKeys.has(ProductFeatureAssistantKey.assistant) &&
license.hasAtLeast('enterprise');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems that as long as we use BehaviorSubject with an initial value, it will always emit the initial value as the first emission. To avoid that, I'd like to replace BehaviorSubject with Subject so the first emission will be the productFeatureKeys from ESS/serverless.
combineLatestWith waits for both productFeatureKeys$ and license$ to emit at least once before emitting a combined value.
In this case, we should be able to always receive the non null values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using Subject the subscribe handler will be called only if it is registered before the first next call. If the next happens before the handler is subscribed, it will never be called.
To avoid that, I'm sticking to BehaviorSubject and check if productFeatureKeys and license exists.

const assistantManagementApp = management?.sections.section.kibana.getApp(
'securityAiAssistantManagement'
);

if (!isAssistantAvailable) {
assistantManagementApp?.disable();
}
});

cases?.attachmentFramework.registerExternalReference(
getExternalReferenceAttachmentEndpointRegular()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { BehaviorSubject } from 'rxjs';
import { UpsellingService } from '@kbn/security-solution-upselling/service';
import type { CoreStart } from '@kbn/core/public';
import type {
ProductFeatureKeyType,
ProductFeatureKeys,
} from '@kbn/security-solution-features/src/types';
import type { ContractStartServices, PluginSetup, PluginStart } from './types';
import type { ExperimentalFeatures } from '../common/experimental_features';
import { navLinks$, updateNavLinks } from './common/links/nav_links';
Expand All @@ -20,18 +23,23 @@ export class PluginContract {
public upsellingService: UpsellingService;
public onboardingService: OnboardingService;
public isSolutionNavigationEnabled$: BehaviorSubject<boolean>;
public productFeatureKeys$: BehaviorSubject<Set<ProductFeatureKeyType> | null>;

constructor(private readonly experimentalFeatures: ExperimentalFeatures) {
this.onboardingService = new OnboardingService();
this.componentsService = new ContractComponentsService();
this.upsellingService = new UpsellingService();
this.isSolutionNavigationEnabled$ = new BehaviorSubject<boolean>(false); // defaults to classic navigation
this.productFeatureKeys$ = new BehaviorSubject<Set<ProductFeatureKeyType> | null>(null);
}

public getSetupContract(): PluginSetup {
return {
resolver: lazyResolver,
experimentalFeatures: { ...this.experimentalFeatures },
setProductFeatureKeys: (productFeatureKeys: ProductFeatureKeys) => {
this.productFeatureKeys$.next(new Set(productFeatureKeys));
},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import type { MapsStartApi } from '@kbn/maps-plugin/public';
import type { ServerlessPluginStart } from '@kbn/serverless/public';
import type { DiscoverSharedPublicStart } from '@kbn/discover-shared-plugin/public';
import type { AutomaticImportPluginStart } from '@kbn/automatic-import-plugin/public';
import type { ProductFeatureKeys } from '@kbn/security-solution-features';
import type { ResolverPluginSetup } from './resolver/types';
import type { Inspect } from '../common/search_strategy';
import type { Detections } from './detections';
Expand Down Expand Up @@ -213,6 +214,7 @@ export type StartRenderServices = Pick<
export interface PluginSetup {
resolver: () => Promise<ResolverPluginSetup>;
experimentalFeatures: ExperimentalFeatures;
setProductFeatureKeys: (productFeatureKeys: ProductFeatureKeys) => void;
}

export interface PluginStart {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
SecuritySolutionEssPluginStartDeps,
} from './types';
import { setOnboardingSettings } from './onboarding';
import { DEFAULT_PRODUCT_FEATURES } from '../server/constants';

export class SecuritySolutionEssPlugin
implements
Expand All @@ -28,8 +29,12 @@ export class SecuritySolutionEssPlugin
{
public setup(
_core: CoreSetup,
_setupDeps: SecuritySolutionEssPluginSetupDeps
setupDeps: SecuritySolutionEssPluginSetupDeps
): SecuritySolutionEssPluginSetup {
const { securitySolution } = setupDeps;

securitySolution.setProductFeatureKeys(DEFAULT_PRODUCT_FEATURES);

return {};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from '../common/experimental_features';
import { setOnboardingSettings } from './onboarding';
import { getAdditionalChargesMessage } from './components/additional_charges_message';
import { getProductProductFeatures } from '../common/pli/pli_features';

export class SecuritySolutionServerlessPlugin
implements
Expand All @@ -47,12 +48,14 @@ export class SecuritySolutionServerlessPlugin
setupDeps: SecuritySolutionServerlessPluginSetupDeps
): SecuritySolutionServerlessPluginSetup {
const { securitySolution } = setupDeps;
const { productTypes } = this.config;

this.experimentalFeatures = parseExperimentalConfigValue(
this.config.enableExperimental,
securitySolution.experimentalFeatures
).features;

securitySolution.setProductFeatureKeys(getProductProductFeatures(productTypes));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It setProductFeatureKeys correctly for complete and essential tiers.

return {};
}

Expand Down