Skip to content

Commit

Permalink
[7.8] [Ingest pipelines] Fix schema validation for simulate and updat…
Browse files Browse the repository at this point in the history
…e routes (#67199) (#67330)

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
alisonelizabeth and elasticmachine authored May 26, 2020
1 parent 4bc1105 commit 5c790b0
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 16 deletions.
6 changes: 2 additions & 4 deletions x-pack/plugins/ingest_pipelines/server/routes/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import { schema } from '@kbn/config-schema';
import { Pipeline } from '../../../common/types';
import { API_BASE_PATH } from '../../../common/constants';
import { RouteDependencies } from '../../types';
import { pipelineSchema } from './pipeline_schema';

const bodySchema = schema.object({
name: schema.string(),
description: schema.maybe(schema.string()),
processors: schema.arrayOf(schema.recordOf(schema.string(), schema.any())),
version: schema.maybe(schema.number()),
on_failure: schema.maybe(schema.arrayOf(schema.recordOf(schema.string(), schema.any()))),
...pipelineSchema,
});

export const registerCreateRoute = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { schema } from '@kbn/config-schema';

export const pipelineSchema = {
description: schema.maybe(schema.string()),
processors: schema.arrayOf(schema.recordOf(schema.string(), schema.any())),
version: schema.maybe(schema.number()),
on_failure: schema.maybe(schema.arrayOf(schema.recordOf(schema.string(), schema.any()))),
};
8 changes: 2 additions & 6 deletions x-pack/plugins/ingest_pipelines/server/routes/api/simulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ import { schema } from '@kbn/config-schema';

import { API_BASE_PATH } from '../../../common/constants';
import { RouteDependencies } from '../../types';
import { pipelineSchema } from './pipeline_schema';

const bodySchema = schema.object({
pipeline: schema.object({
description: schema.string(),
processors: schema.arrayOf(schema.recordOf(schema.string(), schema.any())),
version: schema.maybe(schema.number()),
on_failure: schema.maybe(schema.arrayOf(schema.recordOf(schema.string(), schema.any()))),
}),
pipeline: schema.object(pipelineSchema),
documents: schema.arrayOf(schema.recordOf(schema.string(), schema.any())),
verbose: schema.maybe(schema.boolean()),
});
Expand Down
8 changes: 2 additions & 6 deletions x-pack/plugins/ingest_pipelines/server/routes/api/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ import { schema } from '@kbn/config-schema';

import { API_BASE_PATH } from '../../../common/constants';
import { RouteDependencies } from '../../types';
import { pipelineSchema } from './pipeline_schema';

const bodySchema = schema.object({
description: schema.string(),
processors: schema.arrayOf(schema.recordOf(schema.string(), schema.any())),
version: schema.maybe(schema.number()),
on_failure: schema.maybe(schema.arrayOf(schema.recordOf(schema.string(), schema.any()))),
});
const bodySchema = schema.object(pipelineSchema);

const paramsSchema = schema.object({
name: schema.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ export default function ({ getService }: FtrProviderContext) {
},
],
version: 1,
on_failure: [
{
set: {
field: '_index',
value: 'failed-{{ _index }}',
},
},
],
};

before(() => createPipeline({ body: PIPELINE, id: PIPELINE_ID }));
Expand All @@ -139,6 +147,23 @@ export default function ({ getService }: FtrProviderContext) {
});
});

it('should allow optional fields to be removed', async () => {
const uri = `${API_BASE_PATH}/${PIPELINE_ID}`;

const { body } = await supertest
.put(uri)
.set('kbn-xsrf', 'xxx')
.send({
processors: PIPELINE.processors,
// removes description, version and on_failure
})
.expect(200);

expect(body).to.eql({
acknowledged: true,
});
});

it('should not allow a non-existing pipeline to be updated', async () => {
const uri = `${API_BASE_PATH}/pipeline_does_not_exist`;

Expand Down Expand Up @@ -313,6 +338,54 @@ export default function ({ getService }: FtrProviderContext) {
},
},
],
version: 1,
on_failure: [
{
set: {
field: '_index',
value: 'failed-{{ _index }}',
},
},
],
},
documents: [
{
_index: 'index',
_id: 'id',
_source: {
foo: 'bar',
},
},
{
_index: 'index',
_id: 'id',
_source: {
foo: 'rab',
},
},
],
})
.expect(200);

// The simulate ES response is quite long and includes timestamps
// so for now, we just confirm the docs array is returned with the correct length
expect(body.docs?.length).to.eql(2);
});

it('should successfully simulate a pipeline with only required pipeline fields', async () => {
const { body } = await supertest
.post(`${API_BASE_PATH}/simulate`)
.set('kbn-xsrf', 'xxx')
.send({
pipeline: {
processors: [
{
set: {
field: 'field2',
value: '_value',
},
},
],
},
documents: [
{
Expand Down

0 comments on commit 5c790b0

Please sign in to comment.