Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[data views] Disable scripted fields in serverless environment #163228

Merged
merged 36 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
82f0c9d
scripted fields disabled in serverless
mattkime Aug 6, 2023
5230848
fix functional test
mattkime Aug 6, 2023
df98c1b
fix functional test
mattkime Aug 6, 2023
ae6652a
Merge branch 'main' into disable_scripted_fields_serverless
mattkime Aug 8, 2023
c2863d8
Merge branch 'main' into disable_scripted_fields_serverless
mattkime Aug 8, 2023
3ade3a7
added todo
mattkime Aug 9, 2023
52c0897
Merge branch 'main' into disable_scripted_fields_serverless
mattkime Aug 12, 2023
631c4af
pass scripted fields enabled through service
mattkime Aug 13, 2023
f04b383
fix tests
mattkime Aug 13, 2023
e001c75
fix jest test
mattkime Aug 14, 2023
f5283cd
add scripted fields disabled test
mattkime Aug 14, 2023
ee7609e
comment cleanup
mattkime Aug 14, 2023
40e75ba
Merge branch 'main' into disable_scripted_fields_serverless
mattkime Aug 14, 2023
665e161
fix test
mattkime Aug 15, 2023
f8f475d
fix test
mattkime Aug 15, 2023
903d40a
Merge branch 'main' into disable_scripted_fields_serverless
mattkime Aug 15, 2023
b2af589
Merge branch 'main' into disable_scripted_fields_serverless
mattkime Aug 15, 2023
b4d87cd
supply default for scripted_fields
mattkime Aug 15, 2023
d69b750
Merge branch 'disable_scripted_fields_serverless' of github.com:mattk…
mattkime Aug 15, 2023
173f32b
better function names
mattkime Aug 16, 2023
e048c13
Merge branch 'main' into disable_scripted_fields_serverless
mattkime Aug 16, 2023
0e6982f
Merge branch 'main' into disable_scripted_fields_serverless
mattkime Aug 16, 2023
8c2ad07
Merge branch 'main' into disable_scripted_fields_serverless
mattkime Aug 17, 2023
94cc660
better code reuse, disable path
mattkime Aug 17, 2023
2de5de8
Merge branch 'disable_scripted_fields_serverless' of github.com:mattk…
mattkime Aug 17, 2023
82271f6
Update x-pack/test_serverless/api_integration/test_suites/common/scri…
mattkime Aug 17, 2023
d5e4d3e
Merge branch 'main' into disable_scripted_fields_serverless
mattkime Aug 19, 2023
37eff5e
add functional test for scripted fields tab
mattkime Aug 20, 2023
4f3ed1b
Merge branch 'disable_scripted_fields_serverless' of github.com:mattk…
mattkime Aug 20, 2023
b0d78f3
add functional test for scripted fields tab
mattkime Aug 20, 2023
3bfb4f1
second attempt at navigating to kibana dat aviews
mattkime Aug 21, 2023
0037388
fix functional test
mattkime Aug 21, 2023
f3208c0
fix functional test
mattkime Aug 21, 2023
d5ccd00
simplify type
mattkime Aug 21, 2023
3480c52
add unit test
mattkime Aug 21, 2023
1887f29
Merge branch 'main' into disable_scripted_fields_serverless
mattkime Aug 21, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('createSearchSource', () => {
getConfig: jest.fn(),
search: jest.fn(),
onResponse: (req, res) => res,
scriptedFieldsEnabled: true,
};

indexPatternContractMock = {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/search/search_source/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ export const createSearchSourceMock = (
)
),
onResponse: jest.fn().mockImplementation((req, res) => res),
scriptedFieldsEnabled: true,
});
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ describe('SearchSource', () => {
getConfig: getConfigMock,
search: mockSearchMethod,
onResponse: jest.fn().mockImplementation((_, res) => res),
scriptedFieldsEnabled: true,
};

searchSource = new SearchSource({}, searchSourceDependencies);
Expand Down
12 changes: 8 additions & 4 deletions src/plugins/data/common/search/search_source/search_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export const searchSourceRequiredUiSettings = [
export interface SearchSourceDependencies extends FetchHandlers {
aggs: AggsStart;
search: ISearchGeneric;
scriptedFieldsEnabled: boolean;
}

interface ExpressionAstOptions {
Expand Down Expand Up @@ -798,10 +799,13 @@ export class SearchSource {
// set defaults
let fieldsFromSource = searchRequest.fieldsFromSource || [];
body.fields = body.fields || [];
body.script_fields = {
...body.script_fields,
...scriptFields,
};

if (this.dependencies.scriptedFieldsEnabled) {
body.script_fields = {
...body.script_fields,
...scriptFields,
};
}
body.stored_fields = storedFields;
body.runtime_mappings = runtimeFields || {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('SearchSource service', () => {
getConfig: jest.fn(),
search: jest.fn(),
onResponse: jest.fn(),
scriptedFieldsEnabled: true,
};
});

Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export class DataPublicPlugin
fieldFormats,
indexPatterns: dataViews,
screenshotMode,
scriptedFieldsEnabled: dataViews.scriptedFieldsEnabled,
});
setSearchService(search);

Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/public/search/search_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('Search service', () => {
fieldFormats: {} as FieldFormatsStart,
indexPatterns: {} as DataViewsContract,
screenshotMode: screenshotModePluginMock.createStartContract(),
scriptedFieldsEnabled: true,
});
});

Expand Down
9 changes: 8 additions & 1 deletion src/plugins/data/public/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface SearchServiceStartDependencies {
fieldFormats: FieldFormatsStart;
indexPatterns: DataViewsContract;
screenshotMode: ScreenshotModePluginStart;
scriptedFieldsEnabled: boolean;
}

export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
Expand Down Expand Up @@ -216,7 +217,12 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {

public start(
{ http, theme, uiSettings, chrome, application }: CoreStart,
{ fieldFormats, indexPatterns, screenshotMode }: SearchServiceStartDependencies
{
fieldFormats,
indexPatterns,
screenshotMode,
scriptedFieldsEnabled,
}: SearchServiceStartDependencies
): ISearchStart {
const search = ((request, options = {}) => {
return this.searchInterceptor.search(request, options);
Expand Down Expand Up @@ -245,6 +251,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
}
return response;
},
scriptedFieldsEnabled,
};

const config = this.initializerContext.config.get();
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/server/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
getConfig: <T = any>(key: string): T => uiSettingsCache[key],
search: this.asScoped(request).search,
onResponse: (req, res) => res,
scriptedFieldsEnabled: true,
};

return this.searchSourceService.start(scopedIndexPatterns, searchSourceDependencies);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,15 +569,15 @@ export function Tabs({

const euiTabs: EuiTabbedContentTab[] = useMemo(
() =>
getTabs(indexPattern, fieldFilter, relationships.length).map(
getTabs(indexPattern, fieldFilter, relationships.length, dataViews.scriptedFieldsEnabled).map(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great to add a functional test for tabs

(tab: Pick<EuiTabbedContentTab, 'name' | 'id'>) => {
return {
...tab,
content: getContent(tab.id),
};
}
),
[fieldFilter, getContent, indexPattern, relationships]
[fieldFilter, getContent, indexPattern, relationships, dataViews.scriptedFieldsEnabled]
);

const [selectedTabId, setSelectedTabId] = useState(euiTabs[0].id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ function getTitle(type: string, filteredCount: Dictionary<number>, totalCount: D
return title + count;
}

export function getTabs(indexPattern: DataView, fieldFilter: string, relationshipCount = 0) {
export function getTabs(
indexPattern: DataView,
fieldFilter: string,
relationshipCount = 0,
scriptedFieldsEnabled: boolean
) {
const totalCount = getCounts(indexPattern.fields.getAll(), indexPattern.getSourceFiltering());
const filteredCount = getCounts(
indexPattern.fields.getAll(),
Expand All @@ -89,7 +94,7 @@ export function getTabs(indexPattern: DataView, fieldFilter: string, relationshi
'data-test-subj': 'tab-indexedFields',
});

if (areScriptedFieldsEnabled(indexPattern)) {
if (areScriptedFieldsEnabled(indexPattern) && scriptedFieldsEnabled) {
tabs.push({
name: getTitle('scripted', filteredCount, totalCount),
id: TAB_SCRIPTED_FIELDS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const getTags = (indexPattern: DataViewListItem | DataView, isDefault: bo
return tags;
};

// todo add to this
export const areScriptedFieldsEnabled = (indexPattern: DataViewListItem | DataView) => {
return !isRollup(indexPattern.type);
};
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data_views/common/data_views/data_views.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ describe('IndexPatterns', () => {
onRedirectNoIndexPattern: () => {},
getCanSave: () => Promise.resolve(true),
getCanSaveAdvancedSettings: () => Promise.resolve(true),
scriptedFieldsEnabled: true,
});

indexPatternsNoAccess = new DataViewsService({
Expand All @@ -127,6 +128,7 @@ describe('IndexPatterns', () => {
onRedirectNoIndexPattern: () => {},
getCanSave: () => Promise.resolve(false),
getCanSaveAdvancedSettings: () => Promise.resolve(false),
scriptedFieldsEnabled: true,
});
});

Expand Down
14 changes: 12 additions & 2 deletions src/plugins/data_views/common/data_views/data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export interface DataViewsServiceDeps {
* Determines whether the user can save advancedSettings (used for defaultIndex)
*/
getCanSaveAdvancedSettings: () => Promise<boolean>;

scriptedFieldsEnabled: boolean;
}

/**
Expand Down Expand Up @@ -325,6 +327,8 @@ export class DataViewsService {
* Can the user save data views?
*/
public getCanSave: () => Promise<boolean>;

public readonly scriptedFieldsEnabled: boolean;
/**
* DataViewsService constructor
* @param deps Service dependencies
Expand All @@ -339,6 +343,7 @@ export class DataViewsService {
onError,
getCanSave = () => Promise.resolve(false),
getCanSaveAdvancedSettings,
scriptedFieldsEnabled,
} = deps;
this.apiClient = apiClient;
this.config = uiSettings;
Expand All @@ -350,6 +355,7 @@ export class DataViewsService {
this.getCanSaveAdvancedSettings = getCanSaveAdvancedSettings;

this.dataViewCache = createDataViewCache();
this.scriptedFieldsEnabled = scriptedFieldsEnabled;
}

/**
Expand Down Expand Up @@ -591,7 +597,9 @@ export class DataViewsService {
private refreshFieldsFn = async (indexPattern: DataView) => {
const { fields, indices } = await this.getFieldsAndIndicesForDataView(indexPattern);
fields.forEach((field) => (field.isMapped = true));
const scripted = indexPattern.getScriptedFields().map((field) => field.spec);
const scripted = this.scriptedFieldsEnabled
? indexPattern.getScriptedFields().map((field) => field.spec)
: [];
const fieldAttrs = indexPattern.getFieldAttrs();
const fieldsWithSavedAttrs = Object.values(
this.fieldArrayToMap([...fields, ...scripted], fieldAttrs)
Expand Down Expand Up @@ -655,7 +663,9 @@ export class DataViewsService {
displayErrors: boolean = true
) => {
const fieldsAsArr = Object.values(fields);
const scriptedFields = fieldsAsArr.filter((field) => field.scripted);
const scriptedFields = this.scriptedFieldsEnabled
? fieldsAsArr.filter((field) => field.scripted)
: [];
try {
let updatedFieldList: FieldSpec[];
const { fields: newFields, indices } = await this.getFieldsAndIndicesForWildcard(options);
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/data_views/public/data_views_service_public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface DataViewsServicePublicDeps extends DataViewsServiceDeps {
showAllIndices?: boolean;
isRollupIndex: (indexName: string) => boolean;
}) => Promise<MatchedItem[]>;
scriptedFieldsEnabled: boolean;
}

/**
Expand All @@ -44,6 +45,7 @@ export class DataViewsServicePublic extends DataViewsService {
isRollupIndex: (indexName: string) => boolean;
}) => Promise<MatchedItem[]>;
public hasData: HasDataService;
public readonly scriptedFieldsEnabled: boolean;

/**
* Constructor
Expand All @@ -55,5 +57,6 @@ export class DataViewsServicePublic extends DataViewsService {
this.getCanSaveSync = deps.getCanSaveSync;
this.hasData = deps.hasData;
this.getIndices = deps.getIndices;
this.scriptedFieldsEnabled = deps.scriptedFieldsEnabled;
}
}
6 changes: 4 additions & 2 deletions src/plugins/data_views/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Side Public License, v 1.
*/

import { PluginInitializerContext } from '@kbn/core/public';

export {
ILLEGAL_CHARACTERS_KEY,
CONTAINS_SPACES_KEY,
Expand Down Expand Up @@ -55,8 +57,8 @@ export { UiSettingsPublicToCommon } from './ui_settings_wrapper';

import { DataViewsPublicPlugin } from './plugin';

export function plugin() {
return new DataViewsPublicPlugin();
export function plugin(initializerContext: PluginInitializerContext) {
return new DataViewsPublicPlugin(initializerContext);
}

export type {
Expand Down
11 changes: 10 additions & 1 deletion src/plugins/data_views/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import { CoreSetup, CoreStart, Plugin } from '@kbn/core/public';
import { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from '@kbn/core/public';
import { i18n } from '@kbn/i18n';
import { getIndexPatternLoad } from './expressions';
import {
Expand All @@ -16,6 +16,10 @@ import {
DataViewsPublicStartDependencies,
} from './types';

interface ClientConfigType {
scriptedFieldsEnabled?: boolean;
}

import { DataViewsApiClient } from '.';
import { ContentMagementWrapper } from './content_management_wrapper';

Expand All @@ -40,6 +44,8 @@ export class DataViewsPublicPlugin
{
private readonly hasData = new HasData();

constructor(private readonly initializerContext: PluginInitializerContext) {}

public setup(
core: CoreSetup<DataViewsPublicStartDependencies, DataViewsPublicPluginStart>,
{ expressions, contentManagement }: DataViewsPublicSetupDependencies
Expand Down Expand Up @@ -74,6 +80,8 @@ export class DataViewsPublicPlugin
10000
);

const config = this.initializerContext.config.get<ClientConfigType>();

return new DataViewsServicePublic({
hasData: this.hasData.start(core),
uiSettings: new UiSettingsPublicToCommon(uiSettings),
Expand All @@ -91,6 +99,7 @@ export class DataViewsPublicPlugin
getCanSaveAdvancedSettings: () =>
Promise.resolve(application.capabilities.advancedSettings.save === true),
getIndices: (props) => getIndices({ ...props, http: core.http }),
scriptedFieldsEnabled: config.scriptedFieldsEnabled === false ? false : true, // accounting for null value
});
}

Expand Down
1 change: 1 addition & 0 deletions src/plugins/data_views/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export interface DataViewsServicePublic extends DataViewsServicePublicMethods {
showAllIndices?: boolean;
isRollupIndex: (indexName: string) => boolean;
}) => Promise<MatchedItem[]>;
scriptedFieldsEnabled: boolean;
}

export type DataViewsContract = DataViewsServicePublic;
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data_views/server/data_views_service_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface DataViewsServiceFactoryDeps {
uiSettings: UiSettingsServiceStart;
fieldFormats: FieldFormatsStart;
capabilities: CoreStart['capabilities'];
scriptedFieldsEnabled: boolean;
rollupsEnabled: boolean;
}

Expand Down Expand Up @@ -70,5 +71,6 @@ export const dataViewsServiceFactory = (deps: DataViewsServiceFactoryDeps) =>
: request
? (await capabilities.resolveCapabilities(request)).advancedSettings.save === true
: false,
scriptedFieldsEnabled: deps.scriptedFieldsEnabled,
});
};
20 changes: 20 additions & 0 deletions src/plugins/data_views/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Side Public License, v 1.
*/

import { schema, TypeOf } from '@kbn/config-schema';
import type { PluginConfigDescriptor } from '@kbn/core/server';
export { getFieldByName, findIndexPatternById } from './utils';
export type { FieldDescriptor, RollupIndexCapability } from './fetcher';
export { IndexPatternsFetcher, getCapabilitiesForRollupIndices } from './fetcher';
Expand Down Expand Up @@ -36,6 +38,24 @@ export type {
};
export { DataViewsServerPlugin as Plugin };

const configSchema = schema.object({
scriptedFieldsEnabled: schema.conditional(
schema.contextRef('serverless'),
true,
schema.boolean({ defaultValue: false }),
schema.never()
),
});

type ConfigType = TypeOf<typeof configSchema>;

export const config: PluginConfigDescriptor<ConfigType> = {
schema: configSchema,
exposeToBrowser: {
scriptedFieldsEnabled: true,
},
};

export {
SERVICE_PATH,
SERVICE_PATH_LEGACY,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data_views/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function createIndexPatternsStartMock() {
const dataViewsServiceFactory = jest.fn().mockResolvedValue({ get: jest.fn() });
return {
dataViewsServiceFactory,
getScriptedFieldsEnabled: jest.fn().mockReturnValue(true),
};
}

Expand Down
Loading