Skip to content

Commit

Permalink
[alerting] add mustache variable kibanaBaseUrl for Kibana's publicly …
Browse files Browse the repository at this point in the history
…exposed base URL (#90525)

resolves #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` which will be rendered
as an empty string.
  • Loading branch information
pmuellr authored Feb 16, 2021
1 parent 073cd4d commit 20e16bd
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 1 deletion.
3 changes: 3 additions & 0 deletions x-pack/plugins/alerts/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export class AlertingPlugin {
private eventLogService?: IEventLogService;
private eventLogger?: IEventLogger;
private readonly kibanaIndexConfig: Observable<{ kibana: { index: string } }>;
private kibanaBaseUrl: string | undefined;

constructor(initializerContext: PluginInitializerContext) {
this.config = initializerContext.config.create<AlertsConfig>().pipe(first()).toPromise();
Expand All @@ -176,6 +177,7 @@ export class AlertingPlugin {
core: CoreSetup<AlertingPluginsStart, unknown>,
plugins: AlertingPluginsSetup
): PluginSetupContract {
this.kibanaBaseUrl = core.http.basePath.publicBaseUrl;
this.licenseState = new LicenseState(plugins.licensing.license$);
this.security = plugins.security;

Expand Down Expand Up @@ -371,6 +373,7 @@ export class AlertingPlugin {
eventLogger: this.eventLogger!,
internalSavedObjectsRepository: core.savedObjects.createInternalRepository(['alert']),
alertTypeRegistry: this.alertTypeRegistry!,
kibanaBaseUrl: this.kibanaBaseUrl,
});

this.eventLogService!.registerSavedObjectProvider('alert', (request) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const createExecutionHandlerParams: jest.Mocked<
alertName: 'name-of-alert',
tags: ['tag-A', 'tag-B'],
apiKey: 'MTIzOmFiYw==',
kibanaBaseUrl: 'http://localhost:5601',
alertType,
logger: loggingSystemMock.create().get(),
eventLogger: mockEventLogger,
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ describe('Task Runner', () => {
eventLogger: eventLoggerMock.create(),
internalSavedObjectsRepository: savedObjectsRepositoryMock.create(),
alertTypeRegistry,
kibanaBaseUrl: 'https://localhost:5601',
};

const mockedAlertTypeSavedObject: Alert<AlertTypeParams> = {
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/alerts/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,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 +181,7 @@ export class TaskRunner<
actions,
spaceId,
alertType: this.alertType,
kibanaBaseUrl,
eventLogger: this.context.eventLogger,
request: this.getFakeKibanaRequest(spaceId, apiKey),
alertParams,
Expand Down Expand Up @@ -388,6 +390,7 @@ export class TaskRunner<
alert.tags,
spaceId,
apiKey,
this.context.kibanaBaseUrl,
alert.actions,
alert.params
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('Task Runner Factory', () => {
eventLogger: eventLoggerMock.create(),
internalSavedObjectsRepository: savedObjectsRepositoryMock.create(),
alertTypeRegistry: alertTypeRegistryMock.create(),
kibanaBaseUrl: 'https://localhost:5601',
};

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface TaskRunnerContext {
basePathService: IBasePath;
internalSavedObjectsRepository: ISavedObjectsRepository;
alertTypeRegistry: AlertTypeRegistry;
kibanaBaseUrl: string | undefined;
}

export class TaskRunnerFactory {
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,18 @@ describe('transformActionVariables', () => {
"description": "The alert action group that was used to scheduled actions for the alert.",
"name": "alertActionGroup",
},
Object {
"description": "The alert action subgroup that was used to scheduled actions for the alert.",
"name": "alertActionSubgroup",
},
Object {
"description": "The human readable name of the alert action group that was used to scheduled actions for the alert.",
"name": "alertActionGroupName",
},
Object {
"description": "The configured server.publicBaseUrl value or empty string if not configured.",
"name": "kibanaBaseUrl",
},
]
`);
});
Expand Down Expand Up @@ -91,10 +99,18 @@ describe('transformActionVariables', () => {
"description": "The alert action group that was used to scheduled actions for the alert.",
"name": "alertActionGroup",
},
Object {
"description": "The alert action subgroup that was used to scheduled actions for the alert.",
"name": "alertActionSubgroup",
},
Object {
"description": "The human readable name of the alert action group that was used to scheduled actions for the alert.",
"name": "alertActionGroupName",
},
Object {
"description": "The configured server.publicBaseUrl value or empty string if not configured.",
"name": "kibanaBaseUrl",
},
Object {
"description": "foo-description",
"name": "context.foo",
Expand Down Expand Up @@ -146,10 +162,18 @@ describe('transformActionVariables', () => {
"description": "The alert action group that was used to scheduled actions for the alert.",
"name": "alertActionGroup",
},
Object {
"description": "The alert action subgroup that was used to scheduled actions for the alert.",
"name": "alertActionSubgroup",
},
Object {
"description": "The human readable name of the alert action group that was used to scheduled actions for the alert.",
"name": "alertActionGroupName",
},
Object {
"description": "The configured server.publicBaseUrl value or empty string if not configured.",
"name": "kibanaBaseUrl",
},
Object {
"description": "foo-description",
"name": "state.foo",
Expand Down Expand Up @@ -204,10 +228,18 @@ describe('transformActionVariables', () => {
"description": "The alert action group that was used to scheduled actions for the alert.",
"name": "alertActionGroup",
},
Object {
"description": "The alert action subgroup that was used to scheduled actions for the alert.",
"name": "alertActionSubgroup",
},
Object {
"description": "The human readable name of the alert action group that was used to scheduled actions for the alert.",
"name": "alertActionGroupName",
},
Object {
"description": "The configured server.publicBaseUrl value or empty string if not configured.",
"name": "kibanaBaseUrl",
},
Object {
"description": "fooC-description",
"name": "context.fooC",
Expand Down Expand Up @@ -280,10 +312,18 @@ describe('transformActionVariables', () => {
"description": "The alert action group that was used to scheduled actions for the alert.",
"name": "alertActionGroup",
},
Object {
"description": "The alert action subgroup that was used to scheduled actions for the alert.",
"name": "alertActionSubgroup",
},
Object {
"description": "The human readable name of the alert action group that was used to scheduled actions for the alert.",
"name": "alertActionGroupName",
},
Object {
"description": "The configured server.publicBaseUrl value or empty string if not configured.",
"name": "kibanaBaseUrl",
},
Object {
"description": "fooC-description",
"name": "context.fooC",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ function getAlwaysProvidedActionVariables(): ActionVariable[] {
}),
});

result.push({
name: 'alertActionSubgroup',
description: i18n.translate(
'xpack.triggersActionsUI.actionVariables.alertActionSubgroupLabel',
{
defaultMessage:
'The alert action subgroup that was used to scheduled actions for the alert.',
}
),
});

result.push({
name: 'alertActionGroupName',
description: i18n.translate(
Expand All @@ -99,5 +110,13 @@ function getAlwaysProvidedActionVariables(): ActionVariable[] {
),
});

result.push({
name: 'kibanaBaseUrl',
description: i18n.translate('xpack.triggersActionsUI.actionVariables.kibanaBaseUrlLabel', {
defaultMessage:
'The configured server.publicBaseUrl value or empty string if not configured.',
}),
});

return result;
}
3 changes: 2 additions & 1 deletion x-pack/test/alerting_api_integration/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface CreateTestConfigOptions {
ssl?: boolean;
enableActionsProxy: boolean;
rejectUnauthorized?: boolean;
publicBaseUrl?: boolean;
}

// test.not-enabled is specifically not enabled
Expand Down Expand Up @@ -97,7 +98,7 @@ export function createTestConfig(name: string, options: CreateTestConfigOptions)
...xPackApiIntegrationTestsConfig.get('kbnTestServer'),
serverArgs: [
...xPackApiIntegrationTestsConfig.get('kbnTestServer.serverArgs'),
'--server.publicBaseUrl=https://localhost:5601',
...(options.publicBaseUrl ? ['--server.publicBaseUrl=https://localhost:5601'] : []),
`--xpack.actions.allowedHosts=${JSON.stringify(['localhost', 'some.non.existent.com'])}`,
'--xpack.encryptedSavedObjects.encryptionKey="wuGNaIhoMpk5sO4UBxgr3NyW1sFcLgIf"',
'--xpack.alerts.invalidateApiKeysTask.interval="15s"',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export default createTestConfig('security_and_spaces', {
license: 'trial',
ssl: true,
enableActionsProxy: true,
publicBaseUrl: true,
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default function alertingTests({ loadTestFile, getService }: FtrProviderC
loadTestFile(require.resolve('./update_api_key'));
loadTestFile(require.resolve('./alerts'));
loadTestFile(require.resolve('./event_log'));
loadTestFile(require.resolve('./mustache_templates'));
});
});
}
Loading

0 comments on commit 20e16bd

Please sign in to comment.