Skip to content

Commit

Permalink
[alerting] add mustache variable for Kibana's publicly exposed base URL
Browse files Browse the repository at this point in the history
resolves elastic#49392

Adds the top-level mustache variable `kibanaBaseUrl` for action parameter
mustache templates.  The value comes from Kibana config, which, if not set
will result in this variable having the value `undefined`.
  • Loading branch information
pmuellr committed Feb 10, 2021
1 parent b18e4ec commit a3bc837
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
13 changes: 11 additions & 2 deletions x-pack/plugins/alerts/server/alert_type_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface ConstructorOptions {
taskRunnerFactory: TaskRunnerFactory;
licenseState: ILicenseState;
licensing: LicensingPluginSetup;
kibanaBaseUrl: string | undefined;
}

export interface RegistryAlertType
Expand Down Expand Up @@ -113,12 +114,20 @@ export class AlertTypeRegistry {
private readonly taskRunnerFactory: TaskRunnerFactory;
private readonly licenseState: ILicenseState;
private readonly licensing: LicensingPluginSetup;
private readonly kibanaBaseUrl: string | undefined;

constructor({ taskManager, taskRunnerFactory, licenseState, licensing }: ConstructorOptions) {
constructor({
taskManager,
taskRunnerFactory,
licenseState,
licensing,
kibanaBaseUrl,
}: ConstructorOptions) {
this.taskManager = taskManager;
this.taskRunnerFactory = taskRunnerFactory;
this.licenseState = licenseState;
this.licensing = licensing;
this.kibanaBaseUrl = kibanaBaseUrl;
}

public has(id: string) {
Expand Down Expand Up @@ -183,7 +192,7 @@ export class AlertTypeRegistry {
InstanceContext,
ActionGroupIds,
RecoveryActionGroupId | RecoveredActionGroupId
>(normalizedAlertType, context),
>(normalizedAlertType, context, this.kibanaBaseUrl),
},
});
// No need to notify usage on basic alert types
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/alerts/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export class AlertingPlugin {
core: CoreSetup<AlertingPluginsStart, unknown>,
plugins: AlertingPluginsSetup
): PluginSetupContract {
const kibanaBaseUrl = core.http.basePath.publicBaseUrl;
this.licenseState = new LicenseState(plugins.licensing.license$);
this.security = plugins.security;

Expand Down Expand Up @@ -211,6 +212,7 @@ export class AlertingPlugin {
taskRunnerFactory: this.taskRunnerFactory,
licenseState: this.licenseState,
licensing: plugins.licensing,
kibanaBaseUrl,
});
this.alertTypeRegistry = alertTypeRegistry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface CreateExecutionHandlerOptions<
actions: AlertAction[];
spaceId: string;
apiKey: RawAlert['apiKey'];
kibanaBaseUrl: string | undefined;
alertType: NormalizedAlertType<
Params,
State,
Expand Down Expand Up @@ -82,6 +83,7 @@ export function createExecutionHandler<
spaceId,
apiKey,
alertType,
kibanaBaseUrl,
eventLogger,
request,
alertParams,
Expand Down Expand Up @@ -126,6 +128,7 @@ export function createExecutionHandler<
context,
actionParams: action.params,
state,
kibanaBaseUrl,
alertParams,
}),
};
Expand Down
13 changes: 10 additions & 3 deletions x-pack/plugins/alerts/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class TaskRunner<
private context: TaskRunnerContext;
private logger: Logger;
private taskInstance: AlertTaskInstance;
private kibanaBaseUrl: string | undefined;
private alertType: NormalizedAlertType<
Params,
State,
Expand All @@ -94,13 +95,15 @@ export class TaskRunner<
RecoveryActionGroupId
>,
taskInstance: ConcreteTaskInstance,
context: TaskRunnerContext
context: TaskRunnerContext,
kibanaBaseUrl: string | undefined
) {
this.context = context;
this.logger = context.logger;
this.alertType = alertType;
this.taskInstance = taskInstanceToAlertTaskInstance(taskInstance);
this.alertTypeRegistry = context.alertTypeRegistry;
this.kibanaBaseUrl = kibanaBaseUrl;
}

async getApiKeyForAlertPermissions(alertId: string, spaceId: string) {
Expand Down Expand Up @@ -160,6 +163,7 @@ export class TaskRunner<
tags: string[] | undefined,
spaceId: string,
apiKey: RawAlert['apiKey'],
kibanaBaseUrl: string | undefined,
actions: Alert<Params>['actions'],
alertParams: Params
) {
Expand All @@ -180,6 +184,7 @@ export class TaskRunner<
actions,
spaceId,
alertType: this.alertType,
kibanaBaseUrl,
eventLogger: this.context.eventLogger,
request: this.getFakeKibanaRequest(spaceId, apiKey),
alertParams,
Expand Down Expand Up @@ -373,7 +378,8 @@ export class TaskRunner<
services: Services,
apiKey: RawAlert['apiKey'],
alert: SanitizedAlert<Params>,
event: Event
event: Event,
kibanaBaseUrl: string | undefined
) {
const {
params: { alertId, spaceId },
Expand All @@ -387,6 +393,7 @@ export class TaskRunner<
alert.tags,
spaceId,
apiKey,
kibanaBaseUrl,
alert.actions,
alert.params
);
Expand Down Expand Up @@ -428,7 +435,7 @@ export class TaskRunner<
}
return {
state: await promiseResult<AlertTaskState, Error>(
this.validateAndExecuteAlert(services, apiKey, alert, event)
this.validateAndExecuteAlert(services, apiKey, alert, event, this.kibanaBaseUrl)
),
schedule: asOk(
// fetch the alert again to ensure we return the correct schedule as it may have
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export class TaskRunnerFactory {
ActionGroupIds,
RecoveryActionGroupId
>,
{ taskInstance }: RunContext
{ taskInstance }: RunContext,
kibanaBaseUrl: string | undefined
) {
if (!this.isInitialized) {
throw new Error('TaskRunnerFactory not initialized');
Expand All @@ -83,6 +84,6 @@ export class TaskRunnerFactory {
InstanceContext,
ActionGroupIds,
RecoveryActionGroupId
>(alertType, taskInstance, this.taskRunnerContext!);
>(alertType, taskInstance, this.taskRunnerContext!, kibanaBaseUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface TransformActionParamsOptions {
actionParams: AlertActionParams;
alertParams: AlertTypeParams;
state: AlertInstanceState;
kibanaBaseUrl?: string;
context: AlertInstanceContext;
}

Expand All @@ -44,6 +45,7 @@ export function transformActionParams({
context,
actionParams,
state,
kibanaBaseUrl,
alertParams,
}: TransformActionParamsOptions): AlertActionParams {
// when the list of variables we pass in here changes,
Expand All @@ -61,6 +63,7 @@ export function transformActionParams({
context,
date: new Date().toISOString(),
state,
kibanaBaseUrl,
params: alertParams,
};
return actionsPlugin.renderActionParameterTemplates(actionTypeId, actionParams, variables);
Expand Down

0 comments on commit a3bc837

Please sign in to comment.