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

Bug/update follower index validation cr #24

Open
wants to merge 5 commits into
base: bug/update-follower-index-validation
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
*/

import '../../public/np_ready/app/services/breadcrumbs.mock';
import { setupEnvironment, pageHelpers, nextTick } from './helpers';
import { setupEnvironment, pageHelpers, nextTick, getRandomString } from './helpers';
import { FollowerIndexForm } from '../../public/np_ready/app/components/follower_index_form/follower_index_form';
import { getEndpoint } from '../../common/routes_endpoints';
import { FOLLOWER_INDEX_EDIT } from './helpers/constants';

jest.mock('ui/new_platform');

const { setup } = pageHelpers.followerIndexEdit;
const { setup: setupFollowerIndexAdd } = pageHelpers.followerIndexAdd;

describe('Edit Auto-follow pattern', () => {
describe('Edit follower index', () => {
let server;
let httpRequestsMockHelpers;

Expand All @@ -29,16 +30,16 @@ describe('Edit Auto-follow pattern', () => {
describe('on component mount', () => {
let find;
let component;
let waitFor;

const remoteClusters = [{ name: 'new-york', seeds: ['localhost:123'], isConnected: true }];

beforeEach(async () => {
httpRequestsMockHelpers.setLoadRemoteClustersResponse(remoteClusters);
httpRequestsMockHelpers.setGetFollowerIndexResponse(FOLLOWER_INDEX_EDIT);
({ component, find } = setup());
({ component, find, waitFor } = setup());

await nextTick();
component.update();
await waitFor('followerIndexForm');
});

/**
Expand Down Expand Up @@ -90,6 +91,51 @@ describe('Edit Auto-follow pattern', () => {
});
});

describe('payload and API endpoint validation', () => {
const remoteClusters = [{ name: 'new-york', seeds: ['localhost:123'], isConnected: true }];
let testBed;

beforeEach(async () => {
httpRequestsMockHelpers.setLoadRemoteClustersResponse(remoteClusters);
httpRequestsMockHelpers.setGetFollowerIndexResponse(FOLLOWER_INDEX_EDIT);

testBed = await setup();
await testBed.waitFor('followerIndexForm');
});

test('should send the correct payload', async () => {
const { actions, form, component, find, waitFor } = testBed;
const maxRetryDelay = getRandomString();

form.setInputValue('maxRetryDelayInput', maxRetryDelay);

actions.clickSaveForm();
component.update(); // The modal to confirm the update opens
await waitFor('confirmModalTitleText');
find('confirmModalConfirmButton').simulate('click');

await nextTick(); // Make sure the Request went through

const { method, path } = getEndpoint('followerIndex', 'edit', {
id: FOLLOWER_INDEX_EDIT.name,
});

const expectedBody = { ...FOLLOWER_INDEX_EDIT, maxRetryDelay };
delete expectedBody.name;
delete expectedBody.remoteCluster;
delete expectedBody.leaderIndex;
delete expectedBody.status;

const latestRequest = server.requests[server.requests.length - 1];
const requestBody = JSON.parse(JSON.parse(latestRequest.requestBody).body);

// Validate the API endpoint called: method, path and payload
expect(latestRequest.method).toBe(method.toUpperCase());
expect(latestRequest.url).toBe(path);
expect(requestBody).toEqual(expectedBody);
});
});

describe('when the remote cluster is disconnected', () => {
let find;
let exists;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.
*/

/**
* --------------------------------------------------------
* Single source of truth for the CCR routes endpoints
* To be used in server, client and api integration tests
* --------------------------------------------------------
*/

import { API_BASE_PATH } from './constants';

type Section = 'followerIndex';
type Method = 'get' | 'post' | 'put' | 'delete' | 'head';

interface EndpointDefinition {
method: Method;
path: string;
}

// Follower indices
const followerIndexEndpoints = {
edit: {
method: 'put',
path: `${API_BASE_PATH}/follower_indices/{id}`,
} as EndpointDefinition,
};

// TODO: Add autoFollowPatternEndpoints & ccrEndpoints

const ccrRoutesEndpoints = {
followerIndex: followerIndexEndpoints,
};

type CcrRoutesEndPoints = typeof ccrRoutesEndpoints;

const replacePlaceholders = (path: string, data: { [key: string]: any }): string =>
Object.entries(data).reduce((updatedPath, [key, value]) => {
const regEx = new RegExp(`{${key}}`);
return updatedPath.replace(regEx, value);
}, path);

export const getEndpoint = <S extends Section, E extends keyof CcrRoutesEndPoints[S]>(
section: S,
endpoint: E,
data?: { [key: string]: any }
): EndpointDefinition => {
const endpointDefinition = (ccrRoutesEndpoints[section][
endpoint
] as unknown) as EndpointDefinition;

return data
? { ...endpointDefinition, path: replacePlaceholders(endpointDefinition.path, data) } // Replace placeholders with data
: endpointDefinition;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
API_REMOTE_CLUSTERS_BASE_PATH,
API_INDEX_MANAGEMENT_BASE_PATH,
} from '../../../../common/constants';
import { getEndpoint } from '../../../../common/routes_endpoints';
import { arrify } from '../../../../common/services/utils';
import {
UIM_FOLLOWER_INDEX_CREATE,
Expand Down Expand Up @@ -159,7 +160,9 @@ export const updateFollowerIndex = (id, followerIndex) => {
readPollTimeout,
} = followerIndex;

const request = httpClient.put(`${API_BASE_PATH}/follower_indices/${encodeURIComponent(id)}`, {
const { path, method } = getEndpoint('followerIndex', 'edit', { id: encodeURIComponent(id) });

const request = httpClient[method](path, {
body: JSON.stringify({
maxReadRequestOperationCount,
maxOutstandingReadRequests,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
// @ts-ignore
} from '../../../../common/services/follower_index_serialization';
import { API_BASE_PATH } from '../../../../common/constants';
import { getEndpoint } from '../../../../common/routes_endpoints';
// @ts-ignore
import { removeEmptyFields } from '../../../../common/services/utils';
// @ts-ignore
Expand All @@ -21,6 +22,34 @@ import { licensePreRoutingFactory } from '../../lib/license_pre_routing_factory'
import { RouteDependencies } from '../types';
import { mapErrorToKibanaHttpResponse } from '../map_to_kibana_http_error';

// ------------------
// Validation schemas
// ------------------
const advancedSettings = {
maxReadRequestOperationCount: schema.maybe(schema.number()),
maxOutstandingReadRequests: schema.maybe(schema.number()),
maxReadRequestSize: schema.maybe(schema.string()), // byte value
maxWriteRequestOperationCount: schema.maybe(schema.number()),
maxWriteRequestSize: schema.maybe(schema.string()), // byte value
maxOutstandingWriteRequests: schema.maybe(schema.number()),
maxWriteBufferCount: schema.maybe(schema.number()),
maxWriteBufferSize: schema.maybe(schema.string()), // byte value
maxRetryDelay: schema.maybe(schema.string()), // time value
readPollTimeout: schema.maybe(schema.string()), // time value
};

const basicSettingsSchema = {
name: schema.string(),
remoteCluster: schema.string(),
leaderIndex: schema.string(),
};

const followerIndexSchema = schema.object({ ...basicSettingsSchema, ...advancedSettings });
const advancedSettingsSchema = schema.object({ ...advancedSettings });

// ------------------
// Routes definition
// ------------------
export const registerFollowerIndexRoutes = ({ router, __LEGACY }: RouteDependencies) => {
/**
* Returns a list of all follower indices
Expand Down Expand Up @@ -130,12 +159,7 @@ export const registerFollowerIndexRoutes = ({ router, __LEGACY }: RouteDependenc
{
path: `${API_BASE_PATH}/follower_indices`,
validate: {
body: schema.object(
{
name: schema.string(),
},
{ unknowns: 'allow' }
),
body: followerIndexSchema,
},
},
licensePreRoutingFactory({
Expand All @@ -161,21 +185,10 @@ export const registerFollowerIndexRoutes = ({ router, __LEGACY }: RouteDependenc
*/
router.put(
{
path: `${API_BASE_PATH}/follower_indices/{id}`,
path: getEndpoint('followerIndex', 'edit').path,

Choose a reason for hiding this comment

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

I'm not sure I see the benefit of the added getEndpoint machinery.

It seems like another thing we need to build out (replacePlaceholders 👀) and share properly across apps and without a clear benefit over statically declaring these values (at least the path pattern, not convinced about the method) and importing them from a common folder.

If we issue a request (in tests or in app code) against a server on a path that does not exist we should be met with a 404 and so the server is already the source of truth, so this does not seem like a major current pain point.

My understanding of the bug we had here was that we required a body but the logic inside the route handler accepted a body with all undefined values - combined with NP route validation the oversight snuck in because body without route validation is {} (i.e., an acceptable value!).

The best way, in my view, to cover this specific case would be the addition of an API integration test that issues a request against elastic with a value set and checking the response (like what you have added here x-pack/test/api_integration/apis/management/cross_cluster_replication/follower_indices.js)

The major take away for me, from this bug, besides the added tests is that we need to be more thorough in our route validations and explicitly testing sending unexpected values to routes.

validate: {
params: schema.object({ id: schema.string() }),
body: schema.object({
maxReadRequestOperationCount: schema.maybe(schema.number()),
maxOutstandingReadRequests: schema.maybe(schema.number()),
maxReadRequestSize: schema.maybe(schema.string()), // byte value
maxWriteRequestOperationCount: schema.maybe(schema.number()),
maxWriteRequestSize: schema.maybe(schema.string()), // byte value
maxOutstandingWriteRequests: schema.maybe(schema.number()),
maxWriteBufferCount: schema.maybe(schema.number()),
maxWriteBufferSize: schema.maybe(schema.string()), // byte value
maxRetryDelay: schema.maybe(schema.string()), // time value
readPollTimeout: schema.maybe(schema.string()), // time value
}),
body: advancedSettingsSchema,
},
},
licensePreRoutingFactory({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { getEndpoint } from '../../../../../legacy/plugins/cross_cluster_replication/common/routes_endpoints';
import { API_BASE_PATH } from './constants';
import { getRandomString } from './lib';
import { getFollowerIndexPayload } from './fixtures';
Expand All @@ -13,7 +14,45 @@ export const registerHelpers = supertest => {

const loadFollowerIndices = () => supertest.get(`${API_BASE_PATH}/follower_indices`);

const getFollowerIndex = name => supertest.get(`${API_BASE_PATH}/follower_indices/${name}`);
const getFollowerIndex = (name, waitUntilIsActive = false) => {
const maxRetries = 10;
const delayBetweenRetries = 500;
let retryCount = 0;

const proceed = async () => {
const response = await supertest.get(`${API_BASE_PATH}/follower_indices/${name}`);

if (waitUntilIsActive && response.body.status !== 'active') {
retryCount += 1;

if (retryCount > maxRetries) {
throw new Error('Error waiting for follower index to be active.');
}

return new Promise(resolve => setTimeout(resolve, delayBetweenRetries)).then(proceed);
}

return response;
};

return {
expect: status =>
new Promise((resolve, reject) =>
proceed()
.then(response => {
if (status !== response.status) {
reject(new Error(`Expected status ${status} but got ${response.status}`));
}
return resolve(response);
})
.catch(reject)
),
then: (resolve, reject) =>
proceed()
.then(resolve)
.catch(reject),
};
};

const createFollowerIndex = (name = getRandomString(), payload = getFollowerIndexPayload()) => {
followerIndicesCreated.push(name);
Expand All @@ -24,6 +63,14 @@ export const registerHelpers = supertest => {
.send({ ...payload, name });
};

const updateFollowerIndex = (name, payload) => {
const { method, path } = getEndpoint('followerIndex', 'edit', { id: name });

return supertest[method](path)
.set('kbn-xsrf', 'xxx')
.send(payload);
};

const unfollowLeaderIndex = followerIndex => {
const followerIndices = Array.isArray(followerIndex) ? followerIndex : [followerIndex];
const followerIndicesToEncodedString = followerIndices
Expand Down Expand Up @@ -51,6 +98,7 @@ export const registerHelpers = supertest => {
loadFollowerIndices,
getFollowerIndex,
createFollowerIndex,
updateFollowerIndex,
unfollowAll,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function({ getService }) {
loadFollowerIndices,
getFollowerIndex,
createFollowerIndex,
updateFollowerIndex,
unfollowAll,
} = registerFollowerIndicesnHelpers(supertest);

Expand Down Expand Up @@ -92,6 +93,31 @@ export default function({ getService }) {
});
});

describe('update()', () => {
it('should update a follower index advanced settings', async () => {
// Create a follower index
const leaderIndex = await createIndex();
const followerIndex = getRandomString();
const initialValue = 1234;
const payload = getFollowerIndexPayload(leaderIndex, undefined, {
maxReadRequestOperationCount: initialValue,
});
await createFollowerIndex(followerIndex, payload);

// Verify that its advanced settings are correctly set
const { body } = await getFollowerIndex(followerIndex, true);
expect(body.maxReadRequestOperationCount).to.be(initialValue);

// Update the follower index
const updatedValue = 7777;
await updateFollowerIndex(followerIndex, { maxReadRequestOperationCount: updatedValue });

// Verify that the advanced settings are updated
const { body: updatedBody } = await getFollowerIndex(followerIndex, true);
expect(updatedBody.maxReadRequestOperationCount).to.be(updatedValue);
});
});

describe('Advanced settings', () => {
it('hard-coded values should match Elasticsearch default values', async () => {
/**
Expand Down