Skip to content
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
4 changes: 2 additions & 2 deletions src/core/MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1197,8 +1197,8 @@ In server code, `core` can be accessed from either `server.newPlatform` or `kbnS
| `server.route` | [`core.http.createRouter`](/docs/development/core/server/kibana-plugin-server.httpservicesetup.createrouter.md) | [Examples](./MIGRATION_EXAMPLES.md#route-registration) |
| `server.renderApp()` / `server.renderAppWithDefaultConfig()` | [`context.rendering.render()`](/docs/development/core/server/kibana-plugin-server.iscopedrenderingclient.render.md) | [Examples](./MIGRATION_EXAMPLES.md#render-html-content) |
| `request.getBasePath()` | [`core.http.basePath.get`](/docs/development/core/server/kibana-plugin-server.httpservicesetup.basepath.md) | |
| `server.plugins.elasticsearch.getCluster('data')` | [`context.elasticsearch.dataClient`](/docs/development/core/server/kibana-plugin-server.iscopedclusterclient.md) | |
| `server.plugins.elasticsearch.getCluster('admin')` | [`context.elasticsearch.adminClient`](/docs/development/core/server/kibana-plugin-server.iscopedclusterclient.md) | |
| `server.plugins.elasticsearch.getCluster('data')` | [`context.core.elasticsearch.dataClient`](/docs/development/core/server/kibana-plugin-server.iscopedclusterclient.md) | |
| `server.plugins.elasticsearch.getCluster('admin')` | [`context.core.elasticsearch.adminClient`](/docs/development/core/server/kibana-plugin-server.iscopedclusterclient.md) | |
| `server.plugins.elasticsearch.createCluster(...)` | [`core.elasticsearch.createClient`](/docs/development/core/server/kibana-plugin-server.elasticsearchservicesetup.createclient.md) | |
| `server.savedObjects.setScopedSavedObjectsClientFactory` | [`core.savedObjects.setClientFactory`](/docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.setclientfactory.md) | |
| `server.savedObjects.addScopedSavedObjectsClientWrapperFactory` | [`core.savedObjects.addClientWrapper`](/docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.addclientwrapper.md) | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

import sinon from 'sinon';
import expect from '@kbn/expect';
import { estimateBucketSpanFactory } from '../bucket_span_estimator';

Expand Down Expand Up @@ -32,9 +31,11 @@ const callWithRequest = method => {
});
};

// mock callWithInternalUserFactory
// we replace the return value of the factory with the above mocked callWithRequest
import * as mockModule from '../../../client/call_with_internal_user_factory';
const callWithInternalUser = () => {
return new Promise(resolve => {
resolve({});
});
};

// mock xpack_main plugin
function mockXpackMainPluginFactory(isEnabled = false, licenseType = 'platinum') {
Expand All @@ -51,7 +52,6 @@ function mockXpackMainPluginFactory(isEnabled = false, licenseType = 'platinum')
};
}

const mockElasticsearchPlugin = {};
// mock configuration to be passed to the estimator
const formConfig = {
aggTypes: ['count'],
Expand All @@ -67,34 +67,23 @@ const formConfig = {
};

describe('ML - BucketSpanEstimator', () => {
let mockCallWithInternalUserFactory;

beforeEach(() => {
mockCallWithInternalUserFactory = sinon.mock(mockModule);
mockCallWithInternalUserFactory
.expects('callWithInternalUserFactory')
.once()
.returns(callWithRequest);
});

it('call factory', () => {
expect(function() {
estimateBucketSpanFactory(callWithRequest);
mockCallWithInternalUserFactory.verify();
estimateBucketSpanFactory(callWithRequest, callWithInternalUser);
}).to.not.throwError('Not initialized.');
});

it('call factory and estimator with security disabled', done => {
expect(function() {
const estimateBucketSpan = estimateBucketSpanFactory(
callWithRequest,
mockElasticsearchPlugin,
callWithInternalUser,
mockXpackMainPluginFactory()
);

estimateBucketSpan(formConfig).catch(catchData => {
expect(catchData).to.be('Unable to retrieve cluster setting search.max_buckets');
mockCallWithInternalUserFactory.verify();

done();
});
}).to.not.throwError('Not initialized.');
Expand All @@ -104,12 +93,12 @@ describe('ML - BucketSpanEstimator', () => {
expect(function() {
const estimateBucketSpan = estimateBucketSpanFactory(
callWithRequest,
mockElasticsearchPlugin,
callWithInternalUser,
mockXpackMainPluginFactory(true)
);
estimateBucketSpan(formConfig).catch(catchData => {
expect(catchData).to.be('Unable to retrieve cluster setting search.max_buckets');
mockCallWithInternalUserFactory.verify();

done();
});
}).to.not.throwError('Not initialized.');
Expand All @@ -119,19 +108,14 @@ describe('ML - BucketSpanEstimator', () => {
expect(function() {
const estimateBucketSpan = estimateBucketSpanFactory(
callWithRequest,
mockElasticsearchPlugin,
callWithInternalUser,
mockXpackMainPluginFactory(true)
);

estimateBucketSpan(formConfig).catch(catchData => {
expect(catchData).to.be('Insufficient permissions to call bucket span estimation.');
mockCallWithInternalUserFactory.verify();
done();
});
}).to.not.throwError('Not initialized.');
});

afterEach(() => {
mockCallWithInternalUserFactory.restore();
});
});
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 { APICaller } from 'src/core/server';
import { BucketSpanEstimatorData } from '../../../public/application/services/ml_api_service';

export function estimateBucketSpanFactory(
callAsCurrentUser: APICaller,
callAsInternalUser: APICaller,
xpackMainPlugin: any
): (config: BucketSpanEstimatorData) => Promise<any>;
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ import { INTERVALS } from './intervals';
import { singleSeriesCheckerFactory } from './single_series_checker';
import { polledDataCheckerFactory } from './polled_data_checker';

import { callWithInternalUserFactory } from '../../client/call_with_internal_user_factory';
import { isSecurityDisabled } from '../../lib/security_utils';

export function estimateBucketSpanFactory(callWithRequest, elasticsearchPlugin, xpackMainPlugin) {
const callWithInternalUser = callWithInternalUserFactory(elasticsearchPlugin);
const PolledDataChecker = polledDataCheckerFactory(callWithRequest);
const SingleSeriesChecker = singleSeriesCheckerFactory(callWithRequest);
export function estimateBucketSpanFactory(callAsCurrentUser, callAsInternalUser, xpackMainPlugin) {
const PolledDataChecker = polledDataCheckerFactory(callAsCurrentUser);
const SingleSeriesChecker = singleSeriesCheckerFactory(callAsCurrentUser);

class BucketSpanEstimator {
constructor(
Expand Down Expand Up @@ -245,7 +243,7 @@ export function estimateBucketSpanFactory(callWithRequest, elasticsearchPlugin,

const getFieldCardinality = function(index, field) {
return new Promise((resolve, reject) => {
callWithRequest('search', {
callAsCurrentUser('search', {
index,
size: 0,
body: {
Expand Down Expand Up @@ -277,7 +275,7 @@ export function estimateBucketSpanFactory(callWithRequest, elasticsearchPlugin,
getFieldCardinality(index, field)
.then(value => {
const numPartitions = Math.floor(value / NUM_PARTITIONS) || 1;
callWithRequest('search', {
callAsCurrentUser('search', {
index,
size: 0,
body: {
Expand Down Expand Up @@ -337,7 +335,7 @@ export function estimateBucketSpanFactory(callWithRequest, elasticsearchPlugin,
function getBucketSpanEstimation() {
// fetch the `search.max_buckets` cluster setting so we're able to
// adjust aggregations to not exceed that limit.
callWithInternalUser('cluster.getSettings', {
callAsInternalUser('cluster.getSettings', {
flatSettings: true,
includeDefaults: true,
filterPath: '*.*max_buckets',
Expand Down Expand Up @@ -402,7 +400,7 @@ export function estimateBucketSpanFactory(callWithRequest, elasticsearchPlugin,
'cluster:monitor/xpack/ml/datafeeds/stats/get',
],
};
callWithRequest('ml.privilegeCheck', { body })
callAsCurrentUser('ml.privilegeCheck', { body })
.then(resp => {
if (
resp.cluster['cluster:monitor/xpack/ml/job/get'] &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import _ from 'lodash';

export function polledDataCheckerFactory(callWithRequest) {
export function polledDataCheckerFactory(callAsCurrentUser) {
class PolledDataChecker {
constructor(index, timeField, duration, query) {
this.index = index;
Expand Down Expand Up @@ -68,7 +68,7 @@ export function polledDataCheckerFactory(callWithRequest) {
performSearch(intervalMs) {
const body = this.createSearch(intervalMs);

return callWithRequest('search', {
return callAsCurrentUser('search', {
index: this.index,
size: 0,
body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import { mlLog } from '../../client/log';
import { INTERVALS, LONG_INTERVALS } from './intervals';

export function singleSeriesCheckerFactory(callWithRequest) {
export function singleSeriesCheckerFactory(callAsCurrentUser) {
const REF_DATA_INTERVAL = { name: '1h', ms: 3600000 };

class SingleSeriesChecker {
Expand Down Expand Up @@ -187,7 +187,7 @@ export function singleSeriesCheckerFactory(callWithRequest) {
performSearch(intervalMs) {
const body = this.createSearch(intervalMs);

return callWithRequest('search', {
return callAsCurrentUser('search', {
index: this.index,
size: 0,
body,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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 { APICaller } from 'src/core/server';

export function calculateModelMemoryLimitProvider(
callAsCurrentUser: APICaller
): (
indexPattern: string,
splitFieldName: string,
query: any,
fieldNames: any,
influencerNames: any, // string[] ?
timeFieldName: string,
earliestMs: number,
latestMs: number
) => Promise<any>;
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import numeral from '@elastic/numeral';
import { fieldsServiceProvider } from '../fields_service';

export function calculateModelMemoryLimitProvider(callWithRequest) {
const fieldsService = fieldsServiceProvider(callWithRequest);
export function calculateModelMemoryLimitProvider(callAsCurrentUser) {
const fieldsService = fieldsServiceProvider(callAsCurrentUser);

return function calculateModelMemoryLimit(
indexPattern,
Expand All @@ -26,7 +26,7 @@ export function calculateModelMemoryLimitProvider(callWithRequest) {
) {
return new Promise((response, reject) => {
const limits = {};
callWithRequest('ml.info')
callAsCurrentUser('ml.info')
.then(resp => {
if (resp.limits !== undefined && resp.limits.max_model_memory_limit !== undefined) {
limits.max_model_memory_limit = resp.limits.max_model_memory_limit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { CallAPIOptions, RequestHandlerContext } from 'kibana/server';
import { CallAPIOptions, IScopedClusterClient } from 'src/core/server';
import _ from 'lodash';
import { ML_JOB_FIELD_TYPES } from '../../../common/constants/field_types';
import { getSafeAggregationName } from '../../../common/util/job_utils';
Expand Down Expand Up @@ -113,9 +113,8 @@ export class DataVisualizer {
options?: CallAPIOptions
) => Promise<any>;

constructor(client: RequestHandlerContext | (() => any)) {
this.callAsCurrentUser =
typeof client === 'object' ? client.ml!.mlClient.callAsCurrentUser : client;
constructor(callAsCurrentUser: IScopedClusterClient['callAsCurrentUser']) {
this.callAsCurrentUser = callAsCurrentUser;
}

// Obtains overall stats on the fields in the supplied index pattern, returning an object
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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 { APICaller } from 'src/core/server';
import { TypeOf } from '@kbn/config-schema';
import { validateJobSchema } from '../../new_platform/job_validation_schema';

type ValidateJobPayload = TypeOf<typeof validateJobSchema>;

export function validateJob(
callAsCurrentUser: APICaller,
payload: ValidateJobPayload,
kbnVersion: string,
callAsInternalUser: APICaller,
xpackMainPlugin: any
): string[];
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function validateJob(
callWithRequest,
payload,
kbnVersion = 'current',
elasticsearchPlugin,
callAsInternalUser,
xpackMainPlugin
) {
const messages = getMessages();
Expand Down Expand Up @@ -111,7 +111,7 @@ export async function validateJob(
callWithRequest,
job,
duration,
elasticsearchPlugin,
callAsInternalUser,
xpackMainPlugin
))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function validateBucketSpan(
callWithRequest,
job,
duration,
elasticsearchPlugin,
callAsInternalUser,
xpackMainPlugin
) {
validateJobObject(job);
Expand Down Expand Up @@ -123,7 +123,7 @@ export async function validateBucketSpan(
return new Promise(resolve => {
estimateBucketSpanFactory(
callWithRequest,
elasticsearchPlugin,
callAsInternalUser,
xpackMainPlugin
)(data)
.then(resolve)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 { APICaller } from 'src/core/server';
import { Job, Datafeed } from '../../../public/application/jobs/new_job/common/job_creator/configs';

interface ValidateCardinalityConfig extends Job {
datafeed_config?: Datafeed;
}

export function validateCardinality(
callAsCurrentUser: APICaller,
job: ValidateCardinalityConfig
): any[];
Loading