Skip to content

Commit

Permalink
Merge branch 'master' into refresh-uptime-app-after-data-added
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Apr 17, 2020
2 parents 2ef2fd4 + 7efe7e8 commit 2d40913
Show file tree
Hide file tree
Showing 196 changed files with 5,406 additions and 35,393 deletions.
4 changes: 2 additions & 2 deletions docs/api/saved-objects/create.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ any data that you send to the API is properly formed.

[source,sh]
--------------------------------------------------
$ curl -X POST "localhost:5601/api/saved_objects/index-pattern/my-pattern"
$ curl -X POST "localhost:5601/api/saved_objects/index-pattern/my-pattern" -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -d '
{
"attributes": {
"title": "my-pattern-*"
}
}
}'
--------------------------------------------------
// KIBANA

Expand Down
16 changes: 8 additions & 8 deletions docs/api/saved-objects/export.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -68,46 +68,46 @@ Export all index pattern saved objects:

[source,sh]
--------------------------------------------------
$ curl -X POST "localhost:5601/api/saved_objects/_export"
$ curl -X POST "localhost:5601/api/saved_objects/_export" -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -d '
{
"type": "index-pattern"
}
}'
--------------------------------------------------
// KIBANA

Export all index pattern saved objects and exclude the export summary from the stream:

[source,sh]
--------------------------------------------------
$ curl -X POST "localhost:5601/api/saved_objects/_export"
$ curl -X POST "localhost:5601/api/saved_objects/_export" -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -d '
{
"type": "index-pattern",
"excludeExportDetails": true
}
}'
--------------------------------------------------
// KIBANA

Export a specific saved object:

[source,sh]
--------------------------------------------------
$ curl -X POST "localhost:5601/api/saved_objects/_export"
$ curl -X POST "localhost:5601/api/saved_objects/_export" -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -d '
{
"objects": [
{
"type": "dashboard",
"id": "be3733a0-9efe-11e7-acb3-3dab96693fab"
}
]
}
}'
--------------------------------------------------
// KIBANA

Export a specific saved object and it's related objects :

[source,sh]
--------------------------------------------------
$ curl -X POST "localhost:5601/api/saved_objects/_export"
$ curl -X POST "localhost:5601/api/saved_objects/_export" -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -d '
{
"objects": [
{
Expand All @@ -116,6 +116,6 @@ $ curl -X POST "localhost:5601/api/saved_objects/_export"
}
],
"includeReferencesDeep": true
}
}'
--------------------------------------------------
// KIBANA
1 change: 1 addition & 0 deletions packages/kbn-config-schema/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
export { ObjectType, TypeOf, Type };
export { ByteSizeValue } from './byte_size_value';
export { SchemaTypeError, ValidationError } from './errors';
export { isConfigSchema } from './typeguards';

function any(options?: TypeOptions<any>) {
return new AnyType(options);
Expand Down
20 changes: 20 additions & 0 deletions packages/kbn-config-schema/src/typeguards/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/

export { isConfigSchema } from './is_config_schema';
56 changes: 56 additions & 0 deletions packages/kbn-config-schema/src/typeguards/is_config_schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 { schema } from '..';
import { isConfigSchema } from './is_config_schema';

describe('isConfigSchema', () => {
it('returns true for every sub classes of `Type`', () => {
expect(isConfigSchema(schema.any())).toBe(true);
expect(isConfigSchema(schema.arrayOf(schema.string()))).toBe(true);
expect(isConfigSchema(schema.boolean())).toBe(true);
expect(isConfigSchema(schema.buffer())).toBe(true);
expect(isConfigSchema(schema.byteSize())).toBe(true);
expect(isConfigSchema(schema.duration())).toBe(true);
expect(isConfigSchema(schema.literal(''))).toBe(true);
expect(isConfigSchema(schema.mapOf(schema.string(), schema.number()))).toBe(true);
expect(isConfigSchema(schema.nullable(schema.string()))).toBe(true);
expect(isConfigSchema(schema.number())).toBe(true);
expect(isConfigSchema(schema.object({}))).toBe(true);
expect(isConfigSchema(schema.oneOf([schema.string()]))).toBe(true);
expect(isConfigSchema(schema.recordOf(schema.string(), schema.object({})))).toBe(true);
expect(isConfigSchema(schema.string())).toBe(true);
expect(isConfigSchema(schema.stream())).toBe(true);
});

it('returns false for every javascript data type', () => {
expect(isConfigSchema('foo')).toBe(false);
expect(isConfigSchema(42)).toBe(false);
expect(isConfigSchema(new Date())).toBe(false);
expect(isConfigSchema(null)).toBe(false);
expect(isConfigSchema(undefined)).toBe(false);
expect(isConfigSchema([1, 2, 3])).toBe(false);
expect(isConfigSchema({ foo: 'bar' })).toBe(false);
expect(isConfigSchema(function() {})).toBe(false);
});

it('returns true as long as `__isKbnConfigSchemaType` is true', () => {
expect(isConfigSchema({ __isKbnConfigSchemaType: true })).toBe(true);
});
});
24 changes: 24 additions & 0 deletions packages/kbn-config-schema/src/typeguards/is_config_schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { Type } from '../types';

export function isConfigSchema(obj: any): obj is Type<any> {
return obj ? obj.__isKbnConfigSchemaType === true : false;
}
3 changes: 3 additions & 0 deletions packages/kbn-config-schema/src/types/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export abstract class Type<V> {
// sets the value to `null` while still keeping the type.
public readonly type: V = null! as V;

// used for the `isConfigSchema` typeguard
public readonly __isKbnConfigSchemaType = true;

/**
* Internal "schema" backed by Joi.
* @type {Schema}
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/http/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { Request, ResponseObject, ResponseToolkit } from 'hapi';
import Boom from 'boom';

import { Type } from '@kbn/config-schema';
import { isConfigSchema } from '@kbn/config-schema';
import { Logger } from '../../logging';
import { KibanaRequest } from './request';
import { KibanaResponseFactory, kibanaResponseFactory, IKibanaResponse } from './response';
Expand Down Expand Up @@ -139,7 +139,7 @@ function routeSchemasFromRouteConfig<P, Q, B>(

if (route.validate !== false) {
Object.entries(route.validate).forEach(([key, schema]) => {
if (!(schema instanceof Type || typeof schema === 'function')) {
if (!(isConfigSchema(schema) || typeof schema === 'function')) {
throw new Error(
`Expected a valid validation logic declared with '@kbn/config-schema' package or a RouteValidationFunction at key: [${key}].`
);
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/http/router/validator/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { ValidationError, Type, schema, ObjectType } from '@kbn/config-schema';
import { ValidationError, Type, schema, ObjectType, isConfigSchema } from '@kbn/config-schema';
import { Stream } from 'stream';
import { RouteValidationError } from './validator_error';

Expand Down Expand Up @@ -236,7 +236,7 @@ export class RouteValidator<P = {}, Q = {}, B = {}> {
data?: unknown,
namespace?: string
): RouteValidationResultType<typeof validationRule> {
if (validationRule instanceof Type) {
if (isConfigSchema(validationRule)) {
return validationRule.validate(data, {}, namespace);
} else if (typeof validationRule === 'function') {
return this.validateFunction(validationRule, data, namespace);
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { join } from 'path';
import typeDetect from 'type-detect';
import { Subject } from 'rxjs';
import { first } from 'rxjs/operators';
import { Type } from '@kbn/config-schema';
import { isConfigSchema } from '@kbn/config-schema';

import { Logger } from '../logging';
import {
Expand Down Expand Up @@ -150,7 +150,7 @@ export class PluginWrapper<
}

const configDescriptor = pluginDefinition.config;
if (!(configDescriptor.schema instanceof Type)) {
if (!isConfigSchema(configDescriptor.schema)) {
throw new Error('Configuration schema expected to be an instance of Type');
}
return configDescriptor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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 { duration } from 'moment';
import { getProxyRouteHandlerDeps } from './mocks';

import { kibanaResponseFactory } from '../../../../../core/server';
import { createHandler } from '../../routes/api/console/proxy/create_handler';
import * as requestModule from '../../lib/proxy_request';

describe('Console Proxy Route', () => {
afterEach(async () => {
jest.resetAllMocks();
});

describe('fallback behaviour', () => {
it('falls back to all configured endpoints regardless of error', async () => {
// Describe a situation where all three configured nodes reject
(requestModule.proxyRequest as jest.Mock).mockRejectedValueOnce(new Error('ECONNREFUSED'));
(requestModule.proxyRequest as jest.Mock).mockRejectedValueOnce(new Error('EHOSTUNREACH'));
(requestModule.proxyRequest as jest.Mock).mockRejectedValueOnce(new Error('ESOCKETTIMEDOUT'));

const handler = createHandler(
getProxyRouteHandlerDeps({
readLegacyESConfig: () => ({
requestTimeout: duration(30000),
customHeaders: {},
requestHeadersWhitelist: [],
hosts: ['http://localhost:9201', 'http://localhost:9202', 'http://localhost:9203'],
}),
})
);

const response = await handler(
{} as any,
{
headers: {},
query: { method: 'get', path: 'test' },
} as any,
kibanaResponseFactory
);

expect(response.status).toBe(502);
// Return the message from the ES node we attempted last.
expect(response.payload.message).toBe('ESOCKETTIMEDOUT');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,9 @@ export const createHandler = ({

break;
} catch (e) {
// If we reached here it means we hit a lower level network issue than just, for e.g., a 500.
// We try contacting another node in that case.
log.error(e);
if (e.code !== 'ECONNREFUSED') {
return response.internalError(e);
}
if (idx === hosts.length - 1) {
log.warn(`Could not connect to any configured ES node [${hosts.join(', ')}]`);
return response.customError({
Expand Down
Loading

0 comments on commit 2d40913

Please sign in to comment.