Skip to content

Commit

Permalink
Merge branch 'master' into ie11/death
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticmachine authored May 14, 2020
2 parents be83382 + 02a2d07 commit 1eac7b1
Show file tree
Hide file tree
Showing 34 changed files with 453 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,13 @@ export class IndexPattern implements IIndexPattern {
}

async popularizeField(fieldName: string, unit = 1) {
/**
* This function is just used by Discover and it's high likely to be removed in the near future
* It doesn't use the save function to skip the error message that's displayed when
* a user adds several columns in a higher frequency that the changes can be persisted to ES
* resulting in 409 errors
*/
if (!this.id) return;
const field = this.fields.getByName(fieldName);
if (!field) {
return;
Expand All @@ -308,7 +315,15 @@ export class IndexPattern implements IIndexPattern {
return;
}
field.count = count;
await this.save();

try {
const res = await this.savedObjectsClient.update(type, this.id, this.prepBody(), {
version: this.version,
});
this.version = res._version;
} catch (e) {
// no need for an error message here
}
}

getNonScriptedFields() {
Expand Down
13 changes: 5 additions & 8 deletions x-pack/plugins/apm/server/routes/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import * as t from 'io-ts';
import Boom from 'boom';
import { unique } from 'lodash';
import { ScopedAnnotationsClient } from '../../../observability/server';
import { setupRequest } from '../lib/helpers/setup_request';
import { getServiceAgentName } from '../lib/services/get_service_agent_name';
import { getServices } from '../lib/services/get_services';
Expand Down Expand Up @@ -95,13 +94,10 @@ export const serviceAnnotationsRoute = createRoute(() => ({
const { serviceName } = context.params.path;
const { environment } = context.params.query;

let annotationsClient: ScopedAnnotationsClient | undefined;

if (context.plugins.observability) {
annotationsClient = await context.plugins.observability.getScopedAnnotationsClient(
request
);
}
const annotationsClient = await context.plugins.observability?.getScopedAnnotationsClient(
context,
request
);

return getServiceAnnotations({
setup,
Expand Down Expand Up @@ -143,6 +139,7 @@ export const serviceAnnotationsCreateRoute = createRoute(() => ({
},
handler: async ({ request, context }) => {
const annotationsClient = await context.plugins.observability?.getScopedAnnotationsClient(
context,
request
);

Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/observability/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"xpack",
"observability"
],
"optionalPlugins": [
"licensing"
],
"ui": true,
"server": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { CoreSetup, PluginInitializerContext, KibanaRequest } from 'kibana/server';
import {
CoreSetup,
PluginInitializerContext,
KibanaRequest,
RequestHandlerContext,
} from 'kibana/server';
import { PromiseReturnType } from '../../../typings/common';
import { createAnnotationsClient } from './create_annotations_client';
import { registerAnnotationAPIs } from './register_annotation_apis';
Expand Down Expand Up @@ -31,11 +36,12 @@ export async function bootstrapAnnotations({ index, core, context }: Params) {
});

return {
getScopedAnnotationsClient: (request: KibanaRequest) => {
getScopedAnnotationsClient: (requestContext: RequestHandlerContext, request: KibanaRequest) => {
return createAnnotationsClient({
index,
apiCaller: core.elasticsearch.dataClient.asScoped(request).callAsCurrentUser,
logger,
license: requestContext.licensing?.license,
});
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import { APICaller, Logger } from 'kibana/server';
import * as t from 'io-ts';
import { Client } from 'elasticsearch';
import Boom from 'boom';
import { ILicense } from '../../../../licensing/server';
import {
createAnnotationRt,
deleteAnnotationRt,
Expand Down Expand Up @@ -40,8 +42,9 @@ export function createAnnotationsClient(params: {
index: string;
apiCaller: APICaller;
logger: Logger;
license?: ILicense;
}) {
const { index, apiCaller, logger } = params;
const { index, apiCaller, logger, license } = params;

const initIndex = () =>
createOrUpdateIndex({
Expand All @@ -51,48 +54,59 @@ export function createAnnotationsClient(params: {
logger,
});

function ensureGoldLicense<T extends (...args: any[]) => any>(fn: T): T {
return ((...args) => {
if (!license?.hasAtLeast('gold')) {
throw Boom.forbidden('Annotations require at least a gold license or a trial license.');
}
return fn(...args);
}) as T;
}

return {
get index() {
return index;
},
create: async (
createParams: CreateParams
): Promise<{ _id: string; _index: string; _source: Annotation }> => {
const indexExists = await apiCaller('indices.exists', {
index,
});
create: ensureGoldLicense(
async (
createParams: CreateParams
): Promise<{ _id: string; _index: string; _source: Annotation }> => {
const indexExists = await apiCaller('indices.exists', {
index,
});

if (!indexExists) {
await initIndex();
}
if (!indexExists) {
await initIndex();
}

const annotation = {
...createParams,
event: {
created: new Date().toISOString(),
},
};
const annotation = {
...createParams,
event: {
created: new Date().toISOString(),
},
};

const response = (await apiCaller('index', {
index,
body: annotation,
refresh: 'wait_for',
})) as IndexDocumentResponse;
const response = (await apiCaller('index', {
index,
body: annotation,
refresh: 'wait_for',
})) as IndexDocumentResponse;

return apiCaller('get', {
index,
id: response._id,
});
},
getById: async (getByIdParams: GetByIdParams) => {
return apiCaller('get', {
index,
id: response._id,
});
}
),
getById: ensureGoldLicense(async (getByIdParams: GetByIdParams) => {
const { id } = getByIdParams;

return apiCaller('get', {
id,
index,
});
},
delete: async (deleteParams: DeleteParams) => {
}),
delete: ensureGoldLicense(async (deleteParams: DeleteParams) => {
const { id } = deleteParams;

const response = (await apiCaller('delete', {
Expand All @@ -101,6 +115,6 @@ export function createAnnotationsClient(params: {
refresh: 'wait_for',
})) as PromiseReturnType<Client['delete']>;
return response;
},
}),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function registerAnnotationAPIs({
handler: (params: { data: t.TypeOf<TType>; client: ScopedAnnotationsClient }) => Promise<any>
): RequestHandler {
return async (...args: Parameters<RequestHandler>) => {
const [, request, response] = args;
const [context, request, response] = args;

const rt = types;

Expand All @@ -56,16 +56,26 @@ export function registerAnnotationAPIs({
index,
apiCaller,
logger,
license: context.licensing?.license,
});

const res = await handler({
data: validation.right,
client,
});
try {
const res = await handler({
data: validation.right,
client,
});

return response.ok({
body: res,
});
return response.ok({
body: res,
});
} catch (err) {
return response.custom({
statusCode: err.output?.statusCode ?? 500,
body: {
message: err.output?.payload?.message ?? 'An internal server error occured',
},
});
}
};
}

Expand Down
6 changes: 2 additions & 4 deletions x-pack/plugins/uptime/server/lib/alerts/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ import { updateState } from './common';
import { ACTION_GROUP_DEFINITIONS, DYNAMIC_SETTINGS_DEFAULTS } from '../../../common/constants';
import { Cert, CertResult } from '../../../common/runtime_types';
import { commonStateTranslations, tlsTranslations } from './translations';
import { DEFAULT_FROM, DEFAULT_TO } from '../../rest_api/certs/certs';

const { TLS } = ACTION_GROUP_DEFINITIONS;

const DEFAULT_FROM = 'now-1d';
const DEFAULT_TO = 'now';
const DEFAULT_INDEX = 0;
const DEFAULT_SIZE = 20;

interface TlsAlertState {
Expand Down Expand Up @@ -113,7 +111,7 @@ export const tlsAlertFactory: UptimeAlertTypeFactory = (_server, libs) => ({
dynamicSettings,
from: DEFAULT_FROM,
to: DEFAULT_TO,
index: DEFAULT_INDEX,
index: 0,
size: DEFAULT_SIZE,
notValidAfter: `now+${dynamicSettings?.certExpirationThreshold ??
DYNAMIC_SETTINGS_DEFAULTS.certExpirationThreshold}d`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('getCerts', () => {
},
Object {
"range": Object {
"@timestamp": Object {
"monitor.timespan": Object {
"gte": "now-2d",
"lte": "now+1h",
},
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/uptime/server/lib/requests/get_certs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const getCerts: UMElasticsearchQueryFn<GetCertsParams, CertResult> = asyn
},
{
range: {
'@timestamp': {
'monitor.timespan': {
gte: from,
lte: to,
},
Expand Down
8 changes: 4 additions & 4 deletions x-pack/plugins/uptime/server/rest_api/certs/certs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { API_URLS } from '../../../common/constants';
import { UMServerLibs } from '../../lib/lib';
import { UMRestApiRouteFactory } from '../types';

const DEFAULT_INDEX = 0;
export const DEFAULT_FROM = 'now-5m';
export const DEFAULT_TO = 'now';

const DEFAULT_SIZE = 25;
const DEFAULT_FROM = 'now-1d';
const DEFAULT_TO = 'now';
const DEFAULT_SORT = 'not_after';
const DEFAULT_DIRECTION = 'asc';

Expand All @@ -31,7 +31,7 @@ export const createGetCertsRoute: UMRestApiRouteFactory = (libs: UMServerLibs) =
}),
},
handler: async ({ callES, dynamicSettings }, _context, request, response): Promise<any> => {
const index = request.query?.index ?? DEFAULT_INDEX;
const index = request.query?.index ?? 0;
const size = request.query?.size ?? DEFAULT_SIZE;
const from = request.query?.from ?? DEFAULT_FROM;
const to = request.query?.to ?? DEFAULT_TO;
Expand Down
4 changes: 4 additions & 0 deletions x-pack/scripts/functional_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const onlyNotInCoverageTests = [
require.resolve('../test/alerting_api_integration/basic/config.ts'),
require.resolve('../test/alerting_api_integration/spaces_only/config.ts'),
require.resolve('../test/alerting_api_integration/security_and_spaces/config.ts'),
require.resolve('../test/apm_api_integration/basic/config.ts'),
require.resolve('../test/apm_api_integration/trial/config.ts'),
require.resolve('../test/detection_engine_api_integration/security_and_spaces/config.ts'),
require.resolve('../test/detection_engine_api_integration/basic/config.ts'),
require.resolve('../test/plugin_api_integration/config.ts'),
Expand All @@ -29,6 +31,8 @@ const onlyNotInCoverageTests = [
require.resolve('../test/token_api_integration/config.js'),
require.resolve('../test/oidc_api_integration/config.ts'),
require.resolve('../test/oidc_api_integration/implicit_flow.config.ts'),
require.resolve('../test/observability_api_integration/basic/config.ts'),
require.resolve('../test/observability_api_integration/trial/config.ts'),
require.resolve('../test/pki_api_integration/config.ts'),
require.resolve('../test/login_selector_api_integration/config.ts'),
require.resolve('../test/encrypted_saved_objects_api_integration/config.ts'),
Expand Down
2 changes: 0 additions & 2 deletions x-pack/test/api_integration/apis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ export default function({ loadTestFile }) {
loadTestFile(require.resolve('./management'));
loadTestFile(require.resolve('./uptime'));
loadTestFile(require.resolve('./maps'));
loadTestFile(require.resolve('./apm'));
loadTestFile(require.resolve('./siem'));
loadTestFile(require.resolve('./short_urls'));
loadTestFile(require.resolve('./lens'));
loadTestFile(require.resolve('./fleet'));
loadTestFile(require.resolve('./ingest'));
loadTestFile(require.resolve('./endpoint'));
loadTestFile(require.resolve('./ml'));
loadTestFile(require.resolve('./observability'));
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const makePing = async (
refresh: boolean = true,
tls: boolean | TlsProps = false
) => {
const timestamp = new Date();
const baseDoc: any = {
tcp: {
rtt: {
Expand All @@ -40,7 +41,7 @@ export const makePing = async (
ephemeral_id: '0d9a8dc6-f604-49e3-86a0-d8f9d6f2cbad',
version: '8.0.0',
},
'@timestamp': new Date().toISOString(),
'@timestamp': timestamp.toISOString(),
resolve: {
rtt: {
us: 350,
Expand Down Expand Up @@ -88,6 +89,10 @@ export const makePing = async (
check_group: uuid.v4(),
type: 'http',
status: 'up',
timespan: {
gte: timestamp.toISOString(),
lt: new Date(timestamp.getTime() + 5000).toISOString,
},
},
event: {
dataset: 'uptime',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const makeTls = ({ valid = true, commonName = '*.elastic.co', expiry, sha
server: {
x509: {
not_before: '2020-03-01T00:00:00.000Z',
not_after: '2020-05-30T12:00:00.000Z',
not_after: expiryDate,
issuer: {
distinguished_name:
'CN=DigiCert SHA2 High Assurance Server CA,OU=www.digicert.com,O=DigiCert Inc,C=US',
Expand Down
Loading

0 comments on commit 1eac7b1

Please sign in to comment.