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

[alerting] add mustache variable for Kibana's publicly exposed base URL #90525

Merged
merged 1 commit into from
Feb 16, 2021
Merged
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
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;
Copy link
Member Author

Choose a reason for hiding this comment

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

new instance variable so we can capture the path in setup and use it in start


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;
Copy link
Member Author

Choose a reason for hiding this comment

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

there were a couple of paths of threading this new variable into the right place, this required the fewest number of changes

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 {
Copy link
Member Author

Choose a reason for hiding this comment

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

I double-checked the variables we're making available, alertActionSubgroup should have been here, but was not, so added it.

"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'] : []),
Copy link
Member Author

Choose a reason for hiding this comment

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

changed so security_and_spaces runs WITH the configured url, but spaces_only does not

`--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