diff --git a/common/index.ts b/common/index.ts index 805008b1e..6de69e8e3 100644 --- a/common/index.ts +++ b/common/index.ts @@ -29,6 +29,8 @@ export const API_AUTH_LOGOUT = '/auth/logout'; export const ERROR_MISSING_ROLE_PATH = '/missing-role'; +export const MAX_INTEGER = 2147483647; + export enum AuthType { BASIC = 'basicauth', OPEN_ID = 'openid', diff --git a/server/multitenancy/tenant_index.ts b/server/multitenancy/tenant_index.ts index f80d1770d..ab30e0aaf 100644 --- a/server/multitenancy/tenant_index.ts +++ b/server/multitenancy/tenant_index.ts @@ -29,6 +29,7 @@ import { import { createIndexMap } from '../../../../src/core/server/saved_objects/migrations/core/build_index_map'; import { mergeTypes } from '../../../../src/core/server/saved_objects/migrations/kibana/kibana_migrator'; import { SecurityClient } from '../backend/opendistro_security_client'; +import { MAX_INTEGER } from '../../common'; export async function setupIndexTemplate( esClient: ElasticsearchClient, @@ -38,9 +39,11 @@ export async function setupIndexTemplate( ) { const mappings: IndexMapping = buildActiveMappings(mergeTypes(typeRegistry.getAllTypes())); try { - await esClient.indices.putTemplate({ + await esClient.indices.putIndexTemplate({ name: 'tenant_template', body: { + // Setting priority to the max value to avoid being overridden by custom index templates. + priority: MAX_INTEGER, index_patterns: [ kibanaIndex + '_-*_*', kibanaIndex + '_0*_*', @@ -54,10 +57,12 @@ export async function setupIndexTemplate( kibanaIndex + '_8*_*', kibanaIndex + '_9*_*', ], - settings: { - number_of_shards: 1, + template: { + settings: { + number_of_shards: 1, + }, + mappings, }, - mappings, }, }); } catch (error) { diff --git a/server/multitenancy/test/tenant_index.test.ts b/server/multitenancy/test/tenant_index.test.ts new file mode 100644 index 000000000..2a14c8277 --- /dev/null +++ b/server/multitenancy/test/tenant_index.test.ts @@ -0,0 +1,44 @@ +/* + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the "license" file accompanying this file. This file 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 { MAX_INTEGER } from '../../../common'; + +describe('Tenant index template', () => { + const mockESClient = { + indices: { + putIndexTemplate: jest.fn().mockImplementation((template) => { + return template; + }), + }, + }; + + const priority = MAX_INTEGER; + + it('put template', () => { + const result = mockESClient.indices.putIndexTemplate({ + name: 'test_index_template_a', + body: { + priority, + index_patterns: 'test_index_patterns_a', + template: { + settings: { + number_of_shards: 1, + }, + }, + }, + }); + expect(result.body.priority).toEqual(priority); + }); +});