Skip to content

Commit

Permalink
Np migration tsvb route validation (elastic#51850)
Browse files Browse the repository at this point in the history
  • Loading branch information
TinaHeiligers authored and jkelastic committed Jan 17, 2020
1 parent c15f7ad commit d005b90
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export class VisualizeEmbeddable extends Embeddable<VisualizeInput, VisualizeOut
this.expression = await buildPipeline(this.vis, {
searchSource: this.savedVisualization.searchSource,
timeRange: this.timeRange,
savedObjectId: this.savedVisualization.id,
});

this.vis.filters = { timeRange: this.timeRange };
Expand Down
14 changes: 14 additions & 0 deletions src/legacy/core_plugins/vis_type_timeseries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ const metricsPluginInitializer: LegacyPluginInitializer = ({ Plugin }: LegacyPlu
styleSheetPaths: resolve(__dirname, 'public/index.scss'),
hacks: [resolve(__dirname, 'public/legacy')],
injectDefaultVars: server => ({}),
mappings: {
'tsvb-validation-telemetry': {
properties: {
failedRequests: {
type: 'long',
},
},
},
},
savedObjectSchemas: {
'tsvb-validation-telemetry': {
isNamespaceAgnostic: true,
},
},
},
init: (server: Legacy.Server) => {
const visTypeTimeSeriesPlugin = server.newPlatform.setup.plugins
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Context = KibanaContext | null;
interface Arguments {
params: string;
uiState: string;
savedObjectId: string | null;
}

type VisParams = Required<Arguments>;
Expand Down Expand Up @@ -64,10 +65,16 @@ export const createMetricsFn = (): ExpressionFunction<typeof name, Context, Argu
default: '"{}"',
help: '',
},
savedObjectId: {
types: ['null', 'string'],
default: null,
help: '',
},
},
async fn(context: Context, args: Arguments) {
const params = JSON.parse(args.params);
const uiStateParams = JSON.parse(args.uiState);
const savedObjectId = args.savedObjectId;
const uiState = new PersistedState(uiStateParams);

const response = await metricsRequestHandler({
Expand All @@ -76,6 +83,7 @@ export const createMetricsFn = (): ExpressionFunction<typeof name, Context, Argu
filters: get(context, 'filters', null),
visParams: params,
uiState,
savedObjectId,
});

response.visType = 'metrics';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ import { timefilter } from 'ui/timefilter';
import { kfetch } from 'ui/kfetch';
import { getUISettings } from './services';

export const metricsRequestHandler = async ({ uiState, timeRange, filters, query, visParams }) => {
export const metricsRequestHandler = async ({
uiState,
timeRange,
filters,
query,
visParams,
savedObjectId,
}) => {
const config = getUISettings();
const timezone = timezoneProvider(config)();
const uiStateObj = uiState.get(visParams.type, {});
Expand All @@ -49,6 +56,7 @@ export const metricsRequestHandler = async ({ uiState, timeRange, filters, query
filters,
panels: [visParams],
state: uiStateObj,
savedObjectId: savedObjectId || 'unsaved',
}),
});

Expand Down
9 changes: 7 additions & 2 deletions src/legacy/core_plugins/vis_type_timeseries/server/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ import { SearchStrategiesRegister } from './lib/search_strategies/search_strateg
// @ts-ignore
import { getVisData } from './lib/get_vis_data';
import { Framework } from '../../../../plugins/vis_type_timeseries/server';
import { ValidationTelemetryServiceSetup } from '../../../../plugins/vis_type_timeseries/server';

export const init = async (framework: Framework, __LEGACY: any) => {
export const init = async (
framework: Framework,
__LEGACY: any,
validationTelemetry: ValidationTelemetryServiceSetup
) => {
const { core } = framework;
const router = core.http.createRouter();

visDataRoutes(router, framework);
visDataRoutes(router, framework, validationTelemetry);

// [LEGACY_TODO]
fieldsRoutes(__LEGACY.server);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import Joi from 'joi';
const stringOptionalNullable = Joi.string()
.allow('', null)
.optional();
const stringRequired = Joi.string()
.allow('')
.required();
const arrayNullable = Joi.array().allow(null);
const numberIntegerOptional = Joi.number()
.integer()
.optional();
const numberIntegerRequired = Joi.number()
.integer()
.required();
const numberOptional = Joi.number().optional();
const numberRequired = Joi.number().required();
const queryObject = Joi.object({
language: Joi.string().allow(''),
query: Joi.string().allow(''),
});

const annotationsItems = Joi.object({
color: stringOptionalNullable,
fields: stringOptionalNullable,
hidden: Joi.boolean().optional(),
icon: stringOptionalNullable,
id: stringOptionalNullable,
ignore_global_filters: numberIntegerOptional,
ignore_panel_filters: numberIntegerOptional,
index_pattern: stringOptionalNullable,
query_string: queryObject.optional(),
template: stringOptionalNullable,
time_field: stringOptionalNullable,
});

const backgroundColorRulesItems = Joi.object({
value: Joi.number()
.allow(null)
.optional(),
id: stringOptionalNullable,
background_color: stringOptionalNullable,
color: stringOptionalNullable,
});

const gaugeColorRulesItems = Joi.object({
gauge: stringOptionalNullable,
id: stringOptionalNullable,
operator: stringOptionalNullable,
value: Joi.number(),
});
const metricsItems = Joi.object({
field: stringOptionalNullable,
id: stringRequired,
metric_agg: stringOptionalNullable,
numerator: stringOptionalNullable,
denominator: stringOptionalNullable,
sigma: stringOptionalNullable,
function: stringOptionalNullable,
script: stringOptionalNullable,
variables: Joi.array()
.items(
Joi.object({
field: stringOptionalNullable,
id: stringRequired,
name: stringOptionalNullable,
})
)
.optional(),
type: stringRequired,
value: stringOptionalNullable,
values: Joi.array()
.items(Joi.string().allow('', null))
.allow(null)
.optional(),
});

const splitFiltersItems = Joi.object({
id: stringOptionalNullable,
color: stringOptionalNullable,
filter: Joi.object({
language: Joi.string().allow(''),
query: Joi.string().allow(''),
}).optional(),
label: stringOptionalNullable,
});

const seriesItems = Joi.object({
aggregate_by: stringOptionalNullable,
aggregate_function: stringOptionalNullable,
axis_position: stringRequired,
axis_max: stringOptionalNullable,
axis_min: stringOptionalNullable,
chart_type: stringRequired,
color: stringRequired,
color_rules: Joi.array()
.items(
Joi.object({
value: numberOptional,
id: stringRequired,
text: stringOptionalNullable,
operator: stringOptionalNullable,
})
)
.optional(),
fill: numberOptional,
filter: Joi.object({
query: stringRequired,
language: stringOptionalNullable,
}).optional(),
formatter: stringRequired,
hide_in_legend: numberIntegerOptional,
hidden: Joi.boolean().optional(),
id: stringRequired,
label: stringOptionalNullable,
line_width: numberOptional,
metrics: Joi.array().items(metricsItems),
offset_time: stringOptionalNullable,
override_index_pattern: numberOptional,
point_size: numberRequired,
separate_axis: numberIntegerOptional,
seperate_axis: numberIntegerOptional,
series_index_pattern: stringOptionalNullable,
series_time_field: stringOptionalNullable,
series_interval: stringOptionalNullable,
series_drop_last_bucket: numberIntegerOptional,
split_color_mode: stringOptionalNullable,
split_filters: Joi.array()
.items(splitFiltersItems)
.optional(),
split_mode: stringRequired,
stacked: stringRequired,
steps: numberIntegerOptional,
terms_field: stringOptionalNullable,
terms_order_by: stringOptionalNullable,
terms_size: stringOptionalNullable,
terms_direction: stringOptionalNullable,
terms_include: stringOptionalNullable,
terms_exclude: stringOptionalNullable,
time_range_mode: stringOptionalNullable,
trend_arrows: numberOptional,
type: stringOptionalNullable,
value_template: stringOptionalNullable,
var_name: stringOptionalNullable,
});

export const visPayloadSchema = Joi.object({
filters: arrayNullable,
panels: Joi.array().items(
Joi.object({
annotations: Joi.array()
.items(annotationsItems)
.optional(),
axis_formatter: stringRequired,
axis_position: stringRequired,
axis_scale: stringRequired,
axis_min: stringOptionalNullable,
axis_max: stringOptionalNullable,
bar_color_rules: arrayNullable.optional(),
background_color: stringOptionalNullable,
background_color_rules: Joi.array()
.items(backgroundColorRulesItems)
.optional(),
default_index_pattern: stringOptionalNullable,
default_timefield: stringOptionalNullable,
drilldown_url: stringOptionalNullable,
drop_last_bucket: numberIntegerOptional,
filter: Joi.alternatives(
stringOptionalNullable,
Joi.object({
language: stringOptionalNullable,
query: stringOptionalNullable,
})
),
gauge_color_rules: Joi.array()
.items(gaugeColorRulesItems)
.optional(),
gauge_width: [stringOptionalNullable, numberOptional],
gauge_inner_color: stringOptionalNullable,
gauge_inner_width: Joi.alternatives(stringOptionalNullable, numberIntegerOptional),
gauge_style: stringOptionalNullable,
gauge_max: stringOptionalNullable,
id: stringRequired,
ignore_global_filters: numberOptional,
ignore_global_filter: numberOptional,
index_pattern: stringRequired,
interval: stringRequired,
isModelInvalid: Joi.boolean().optional(),
legend_position: stringOptionalNullable,
markdown: stringOptionalNullable,
markdown_scrollbars: numberIntegerOptional,
markdown_openLinksInNewTab: numberIntegerOptional,
markdown_vertical_align: stringOptionalNullable,
markdown_less: stringOptionalNullable,
markdown_css: stringOptionalNullable,
pivot_id: stringOptionalNullable,
pivot_label: stringOptionalNullable,
pivot_type: stringOptionalNullable,
pivot_rows: stringOptionalNullable,
series: Joi.array()
.items(seriesItems)
.required(),
show_grid: numberIntegerRequired,
show_legend: numberIntegerRequired,
time_field: stringOptionalNullable,
time_range_mode: stringOptionalNullable,
type: stringRequired,
})
),
// general
query: Joi.array()
.items(queryObject)
.allow(null)
.required(),
state: Joi.object({
sort: Joi.object({
column: stringRequired,
order: Joi.string()
.valid(['asc', 'desc'])
.required(),
}).optional(),
}).required(),
savedObjectId: Joi.string().optional(),
timerange: Joi.object({
timezone: stringRequired,
min: stringRequired,
max: stringRequired,
}).required(),
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@
* under the License.
*/

import { IRouter } from 'kibana/server';
import { schema } from '@kbn/config-schema';
import { getVisData } from '../lib/get_vis_data';
import { visPayloadSchema } from './post_vis_schema';
import {
Framework,
ValidationTelemetryServiceSetup,
} from '../../../../../plugins/vis_type_timeseries/server';

const escapeHatch = schema.object({}, { allowUnknowns: true });

export const visDataRoutes = (router, framework) => {
export const visDataRoutes = (
router: IRouter,
framework: Framework,
{ logFailedValidation }: ValidationTelemetryServiceSetup
) => {
router.post(
{
path: '/api/metrics/vis/data',
Expand All @@ -31,6 +41,16 @@ export const visDataRoutes = (router, framework) => {
},
},
async (requestContext, request, response) => {
const { error: validationError } = visPayloadSchema.validate(request.body);
if (validationError) {
logFailedValidation();
const savedObjectId =
(typeof request.body === 'object' && (request.body as any).savedObjectId) ||
'unavailable';
framework.logger.warn(
`Request validation error: ${validationError.message} (saved object id: ${savedObjectId}). This most likely means your TSVB visualization contains outdated configuration. You can report this problem under https://github.com/elastic/kibana/issues/new?template=Bug_report.md`
);
}
try {
const results = await getVisData(requestContext, request.body, framework);
return response.ok({ body: results });
Expand Down
Loading

0 comments on commit d005b90

Please sign in to comment.