[UA] Protects against Cloud plugin being disabled (#31637) (#31832)

Currently, if the Cloud plugin is disabled the server will fail to start

Fixes #31612

Signed-off-by: Tyler Smalley <tyler.smalley@elastic.co>
This commit is contained in:
Tyler Smalley 2019-02-22 12:54:41 -08:00 committed by GitHub
parent c2eab6cb8a
commit 2e5f32dc11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 20 deletions

View file

@ -5,5 +5,7 @@
*/
export interface CloudPlugin {
isCloudEnabled: boolean;
config: {
isCloudEnabled: boolean;
};
}

View file

@ -16,29 +16,80 @@ import { registerClusterCheckupRoutes } from './cluster_checkup';
const MigrationApis = require('../lib/es_migration_apis');
MigrationApis.getUpgradeAssistantStatus = jest.fn();
function register(plugins = {}) {
const server = new Server();
server.plugins = {
elasticsearch: {
getCluster: () => ({ callWithRequest: jest.fn() } as any),
} as any,
...plugins,
} as any;
server.config = () => ({ get: () => '' } as any);
registerClusterCheckupRoutes(server);
return server;
}
/**
* Since these route callbacks are so thin, these serve simply as integration tests
* to ensure they're wired up to the lib functions correctly. Business logic is tested
* more thoroughly in the es_migration_apis test.
*/
describe('cluster checkup API', () => {
const server = new Server();
server.plugins = {
elasticsearch: {
getCluster: () => ({ callWithRequest: jest.fn() } as any),
} as any,
apm_oss: {
indexPatterns: ['apm-*'],
},
cloud: {
isCloudEnabled: false,
},
} as any;
server.config = () => ({ get: () => '' } as any);
const spy = jest.spyOn(MigrationApis, 'getUpgradeAssistantStatus');
registerClusterCheckupRoutes(server);
afterEach(() => jest.clearAllMocks());
describe('with cloud enabled', () => {
it('is provided to getUpgradeAssistantStatus', async () => {
const server = register({
cloud: {
config: {
isCloudEnabled: true,
},
},
});
MigrationApis.getUpgradeAssistantStatus.mockResolvedValue({
cluster: [],
indices: [],
nodes: [],
});
await server.inject({
method: 'GET',
url: '/api/upgrade_assistant/status',
});
expect(spy.mock.calls[0][2]).toBe(true);
});
});
describe('with APM enabled', () => {
it('is provided to getUpgradeAssistantStatus', async () => {
const server = register({
apm_oss: {
indexPatterns: ['apm-*'],
},
});
MigrationApis.getUpgradeAssistantStatus.mockResolvedValue({
cluster: [],
indices: [],
nodes: [],
});
await server.inject({
method: 'GET',
url: '/api/upgrade_assistant/status',
});
expect(spy.mock.calls[0][3]).toEqual(['apm-*']);
});
});
describe('GET /api/upgrade_assistant/reindex/{indexName}.json', () => {
const server = register();
it('returns state', async () => {
MigrationApis.getUpgradeAssistantStatus.mockResolvedValue({
cluster: [],

View file

@ -6,14 +6,15 @@
import Boom from 'boom';
import { Legacy } from 'kibana';
import { get } from 'lodash';
import _ from 'lodash';
import { getUpgradeAssistantStatus } from '../lib/es_migration_apis';
import { EsVersionPrecheck } from '../lib/es_version_precheck';
export function registerClusterCheckupRoutes(server: Legacy.Server) {
const { callWithRequest } = server.plugins.elasticsearch.getCluster('admin');
const { isCloudEnabled } = server.plugins.cloud;
const isCloudEnabled = _.get(server.plugins, 'cloud.config.isCloudEnabled', false);
const apmIndexPatterns = _.get(server, 'plugins.apm_oss.indexPatterns', []);
server.route({
path: '/api/upgrade_assistant/status',
@ -23,8 +24,6 @@ export function registerClusterCheckupRoutes(server: Legacy.Server) {
},
async handler(request) {
try {
const apmIndexPatterns = get(server, 'plugins.apm_oss.indexPatterns', []);
return await getUpgradeAssistantStatus(
callWithRequest,
request,

View file

@ -11,7 +11,7 @@ import { XPackMainPlugin } from 'x-pack/plugins/xpack_main/xpack_main';
declare module 'hapi' {
interface PluginProperties {
cloud: CloudPlugin;
cloud?: CloudPlugin;
xpack_main: XPackMainPlugin;
}
}