Skip to content

Commit

Permalink
fix export
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierM committed Jan 31, 2020
1 parent f3fb462 commit a1b8c3e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 41 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,32 @@ export const createExportRulesRoute = (server: ServerFacade): Hapi.ServerRoute =
return headers.response().code(404);
}

const exportSizeLimit = server.config().get<number>('savedObjects.maxImportExportSize');
if (request.payload?.objects != null && request.payload.objects.length > exportSizeLimit) {
return Boom.badRequest(`Can't export more than ${exportSizeLimit} rules`);
} else {
const nonPackagedRulesCount = await getNonPackagedRulesCount({ alertsClient });
if (nonPackagedRulesCount > exportSizeLimit) {
try {
const exportSizeLimit = server.config().get<number>('savedObjects.maxImportExportSize');
if (request.payload?.objects != null && request.payload.objects.length > exportSizeLimit) {
return Boom.badRequest(`Can't export more than ${exportSizeLimit} rules`);
} else {
const nonPackagedRulesCount = await getNonPackagedRulesCount({ alertsClient });
if (nonPackagedRulesCount > exportSizeLimit) {
return Boom.badRequest(`Can't export more than ${exportSizeLimit} rules`);
}
}
}

const exported =
request.payload?.objects != null
? await getExportByObjectIds(alertsClient, request.payload.objects)
: await getExportAll(alertsClient);
const exported =
request.payload?.objects != null
? await getExportByObjectIds(alertsClient, request.payload.objects)
: await getExportAll(alertsClient);

const response = request.query.exclude_export_details
? headers.response(exported.rulesNdjson)
: headers.response(`${exported.rulesNdjson}${exported.exportDetails}`);
const response = request.query.exclude_export_details
? headers.response(exported.rulesNdjson)
: headers.response(`${exported.rulesNdjson}${exported.exportDetails}`);

return response
.header('Content-Disposition', `attachment; filename="${request.query.file_name}"`)
.header('Content-Type', 'application/ndjson');
return response
.header('Content-Disposition', `attachment; filename="${request.query.file_name}"`)
.header('Content-Type', 'application/ndjson');
} catch {
return Boom.badRequest(`Sorry, something went wrong to export rules`);
}
},
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('get_export_by_object_ids', () => {
const objects = [{ rule_id: 'rule-1' }];
const exports = await getRulesFromObjects(unsafeCast, objects);
const expected: RulesErrors = {
exportedCount: 1,
missingRules: [],
rules: [
{
Expand Down Expand Up @@ -141,6 +142,7 @@ describe('get_export_by_object_ids', () => {
const objects = [{ rule_id: 'rule-1' }];
const exports = await getRulesFromObjects(unsafeCast, objects);
const expected: RulesErrors = {
exportedCount: 0,
missingRules: [{ rule_id: 'rule-1' }],
rules: [],
};
Expand All @@ -164,6 +166,7 @@ describe('get_export_by_object_ids', () => {
const objects = [{ rule_id: 'rule-1' }];
const exports = await getRulesFromObjects(unsafeCast, objects);
const expected: RulesErrors = {
exportedCount: 0,
missingRules: [{ rule_id: 'rule-1' }],
rules: [],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@ import { readRules } from './read_rules';
import { transformRulesToNdjson, transformAlertToRule } from '../routes/rules/utils';
import { OutputRuleAlertRest } from '../types';

interface ExportSuccesRule {
statusCode: 200;
rule: Partial<OutputRuleAlertRest>;
}

interface ExportFailedRule {
statusCode: 404;
missingRuleId: { rule_id: string };
}

type ExportRules = ExportSuccesRule | ExportFailedRule;

export interface RulesErrors {
exportedCount: number;
missingRules: Array<{ rule_id: string }>;
rules: Array<Partial<OutputRuleAlertRest>>;
}
Expand All @@ -33,28 +46,44 @@ export const getRulesFromObjects = async (
alertsClient: AlertsClient,
objects: Array<{ rule_id: string }>
): Promise<RulesErrors> => {
const alertsAndErrors = await objects.reduce<Promise<RulesErrors>>(
async (accumPromise, object) => {
const accum = await accumPromise;
const rule = await readRules({ alertsClient, ruleId: object.rule_id });
if (rule != null && isAlertType(rule) && rule.params.immutable !== true) {
const transformedRule = transformAlertToRule(rule);
return {
missingRules: accum.missingRules,
rules: [...accum.rules, transformedRule],
};
} else {
return {
missingRules: [...accum.missingRules, { rule_id: object.rule_id }],
rules: accum.rules,
};
}
},
Promise.resolve({
exportedCount: 0,
missingRules: [],
rules: [],
})
const alertsAndErrors = await Promise.all(
objects.reduce<Array<Promise<ExportRules>>>((accumPromise, object) => {
const exportWorkerPromise = new Promise<ExportRules>(async resolve => {
try {
const rule = await readRules({ alertsClient, ruleId: object.rule_id });
if (rule != null && isAlertType(rule) && rule.params.immutable !== true) {
const transformedRule = transformAlertToRule(rule);
resolve({
statusCode: 200,
rule: transformedRule,
});
} else {
resolve({
statusCode: 404,
missingRuleId: { rule_id: object.rule_id },
});
}
} catch {
resolve({
statusCode: 404,
missingRuleId: { rule_id: object.rule_id },
});
}
});
return [...accumPromise, exportWorkerPromise];
}, [])
);
return alertsAndErrors;

const missingRules = alertsAndErrors.filter(
resp => resp.statusCode === 404
) as ExportFailedRule[];
const exportedRules = alertsAndErrors.filter(
resp => resp.statusCode === 200
) as ExportSuccesRule[];

return {
exportedCount: exportedRules.length,
missingRules: missingRules.map(mr => mr.missingRuleId),
rules: exportedRules.map(er => er.rule),
};
};

0 comments on commit a1b8c3e

Please sign in to comment.