Skip to content

Commit

Permalink
Make API route validation more strict. Incorporate bugfix from elasti…
Browse files Browse the repository at this point in the history
…c#63653.

- Fix route validation errors and API request.
  • Loading branch information
cjcenizal committed Apr 19, 2020
1 parent 2652482 commit a38fc93
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,23 @@ const mapStateToProps = state => ({
const mapDispatchToProps = dispatch => ({
getAutoFollowPattern: id => dispatch(getAutoFollowPattern(id)),
selectAutoFollowPattern: id => dispatch(selectEditAutoFollowPattern(id)),
saveAutoFollowPattern: (id, autoFollowPattern) =>
dispatch(saveAutoFollowPattern(id, autoFollowPattern, true)),
saveAutoFollowPattern: (id, autoFollowPattern) => {
// Strip out errors.
const { active, remoteCluster, leaderIndexPatterns, followIndexPattern } = autoFollowPattern;

dispatch(
saveAutoFollowPattern(
id,
{
active,
remoteCluster,
leaderIndexPatterns,
followIndexPattern,
},
true
)
);
},
clearApiError: () => {
dispatch(clearApiError(`${scope}-get`));
dispatch(clearApiError(`${scope}-save`));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export const resumeFollowerIndex = id =>
scope,
handler: async () => resumeFollowerIndexRequest(id),
onSuccess(response, dispatch) {
console.log('response', response);
/**
* We can have 1 or more follower index resume operation
* that can fail or succeed. We will show 1 toast notification for each.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export const registerCreateRoute = ({
license,
lib: { isEsError, formatEsError },
}: RouteDependencies) => {
const bodySchema = schema.object(
{
id: schema.string(),
},
{ unknowns: 'allow' }
);
const bodySchema = schema.object({
id: schema.string(),
remoteCluster: schema.string(),
leaderIndexPatterns: schema.arrayOf(schema.string()),
followIndexPattern: schema.string(),
});

router.post(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ export const registerUpdateRoute = ({
id: schema.string(),
});

const bodySchema = schema.object({
active: schema.boolean(),
remoteCluster: schema.string(),
leaderIndexPatterns: schema.arrayOf(schema.string()),
followIndexPattern: schema.string(),
});

router.put(
{
path: addBasePath('/auto_follow_patterns/{id}'),
validate: {
params: paramsSchema,
body: schema.object({}, { unknowns: 'allow' }),
body: bodySchema,
},
},
license.guardApiRoute(async (context, request, response) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ export const registerCreateRoute = ({
license,
lib: { isEsError, formatEsError },
}: RouteDependencies) => {
const bodySchema = schema.object(
{
name: schema.string(),
},
{ unknowns: 'allow' }
);
const bodySchema = schema.object({
name: schema.string(),
remoteCluster: schema.string(),
leaderIndex: schema.string(),
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
});

router.post(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,45 @@ export const registerUpdateRoute = ({
}: RouteDependencies) => {
const paramsSchema = schema.object({ id: schema.string() });

const bodySchema = 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
});

router.put(
{
path: addBasePath('/follower_indices/{id}'),
validate: {
params: paramsSchema,
body: bodySchema,
},
},
license.guardApiRoute(async (context, request, response) => {
const { id } = request.params as typeof paramsSchema.type;

// We need to first pause the follower and then resume it passing the advanced settings
// We need to first pause the follower and then resume it by passing the advanced settings
try {
const {
follower_indices: followerIndices,
} = await context.crossClusterReplication!.client.callAsCurrentUser('ccr.info', { id });

const followerIndexInfo = followerIndices && followerIndices[0];

if (!followerIndexInfo) {
return response.notFound({ body: `The follower index "${id}" does not exist.` });
}

// Retrieve paused state instead of pulling it from the payload to ensure it's not stale.
const isPaused = followerIndexInfo.status === 'paused';

// Pause follower if not already paused
if (!isPaused) {
await context.crossClusterReplication!.client.callAsCurrentUser(
Expand All @@ -57,6 +74,7 @@ export const registerUpdateRoute = ({
const body = removeEmptyFields(
serializeAdvancedSettings(request.body as FollowerIndexAdvancedSettings)
);

return response.ok({
body: await context.crossClusterReplication!.client.callAsCurrentUser(
'ccr.resumeFollowerIndex',
Expand Down

0 comments on commit a38fc93

Please sign in to comment.