Skip to content

Commit

Permalink
[SIEM][Detection Engine] Fixes return codes where some were rule_id i…
Browse files Browse the repository at this point in the history
…nstead of id

## Summary

Fixes some return error codes where they were `rule_id` when they should have been `id`

- [x] [Unit or functional tests](https://github.com/elastic/kibana/blob/master/CONTRIBUTING.md#cross-browser-compatibility) were updated or added to match the most common scenarios
  • Loading branch information
FrankHassanabad authored Feb 20, 2020
1 parent 1464706 commit 8097bd8
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('import_rules_route', () => {
message: 'Unexpected token : in JSON at position 8',
status_code: 400,
},
rule_id: '(unknown)',
rule_id: '(unknown id)',
},
],
success: false,
Expand Down Expand Up @@ -329,7 +329,7 @@ describe('import_rules_route', () => {
message: 'Unexpected token : in JSON at position 8',
status_code: 400,
},
rule_id: '(unknown)',
rule_id: '(unknown id)',
},
],
success: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ export const createImportRulesRoute = (
// early with the error and an (unknown) for the ruleId
resolve(
createBulkErrorObject({
ruleId: '(unknown)',
statusCode: 400,
message: parsedRule.message,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,10 +794,20 @@ describe('utils', () => {
});

describe('getIdBulkError', () => {
test('outputs message about id and rule_id not being found if both are not null', () => {
const error = getIdBulkError({ id: '123', ruleId: '456' });
const expected: BulkError = {
id: '123',
rule_id: '456',
error: { message: 'id: "123" and rule_id: "456" not found', status_code: 404 },
};
expect(error).toEqual(expected);
});

test('outputs message about id not being found if only id is defined and ruleId is undefined', () => {
const error = getIdBulkError({ id: '123', ruleId: undefined });
const expected: BulkError = {
rule_id: '123',
id: '123',
error: { message: 'id: "123" not found', status_code: 404 },
};
expect(error).toEqual(expected);
Expand All @@ -806,7 +816,7 @@ describe('utils', () => {
test('outputs message about id not being found if only id is defined and ruleId is null', () => {
const error = getIdBulkError({ id: '123', ruleId: null });
const expected: BulkError = {
rule_id: '123',
id: '123',
error: { message: 'id: "123" not found', status_code: 404 },
};
expect(error).toEqual(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ export const getIdBulkError = ({
id: string | undefined | null;
ruleId: string | undefined | null;
}): BulkError => {
if (id != null) {
if (id != null && ruleId != null) {
return createBulkErrorObject({
id,
ruleId,
statusCode: 404,
message: `id: "${id}" and rule_id: "${ruleId}" not found`,
});
} else if (id != null) {
return createBulkErrorObject({
ruleId: id,
id,
statusCode: 404,
message: `id: "${id}" not found`,
});
Expand All @@ -76,7 +83,6 @@ export const getIdBulkError = ({
});
} else {
return createBulkErrorObject({
ruleId: '(unknown id)',
statusCode: 404,
message: `id or rule_id should have been defined`,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export const transformError = (err: Error & { statusCode?: number }): OutputErro
};

export interface BulkError {
rule_id: string;
id?: string;
rule_id?: string;
error: {
status_code: number;
message: string;
Expand All @@ -53,24 +54,54 @@ export interface BulkError {

export const createBulkErrorObject = ({
ruleId,
id,
statusCode,
message,
}: {
ruleId: string;
ruleId?: string;
id?: string;
statusCode: number;
message: string;
}): BulkError => {
return {
rule_id: ruleId,
error: {
status_code: statusCode,
message,
},
};
if (id != null && ruleId != null) {
return {
id,
rule_id: ruleId,
error: {
status_code: statusCode,
message,
},
};
} else if (id != null) {
return {
id,
error: {
status_code: statusCode,
message,
},
};
} else if (ruleId != null) {
return {
rule_id: ruleId,
error: {
status_code: statusCode,
message,
},
};
} else {
return {
rule_id: '(unknown id)',
error: {
status_code: statusCode,
message,
},
};
}
};

export interface ImportRuleResponse {
rule_id: string;
rule_id?: string;
id?: string;
status_code?: number;
message?: string;
error?: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default ({ getService }: FtrProviderContext): void => {
message: 'id: "fake_id" not found',
status_code: 404,
},
rule_id: 'fake_id', // TODO This is a known issue where it should be id and not rule_id
id: 'fake_id',
},
]);
});
Expand All @@ -152,7 +152,7 @@ export default ({ getService }: FtrProviderContext): void => {
const bodyToCompare = removeServerGeneratedPropertiesIncludingRuleId(body[0]);
expect([bodyToCompare, body[1]]).to.eql([
getSimpleRuleOutputWithoutRuleId(),
{ rule_id: 'fake_id', error: { status_code: 404, message: 'id: "fake_id" not found' } },
{ id: 'fake_id', error: { status_code: 404, message: 'id: "fake_id" not found' } },
]);
});
});
Expand Down Expand Up @@ -262,7 +262,7 @@ export default ({ getService }: FtrProviderContext): void => {
message: 'id: "fake_id" not found',
status_code: 404,
},
rule_id: 'fake_id', // TODO This is a known issue where it should be id and not rule_id
id: 'fake_id',
},
]);
});
Expand All @@ -285,7 +285,7 @@ export default ({ getService }: FtrProviderContext): void => {
const bodyToCompare = removeServerGeneratedPropertiesIncludingRuleId(body[0]);
expect([bodyToCompare, body[1]]).to.eql([
getSimpleRuleOutputWithoutRuleId(),
{ rule_id: 'fake_id', error: { status_code: 404, message: 'id: "fake_id" not found' } },
{ id: 'fake_id', error: { status_code: 404, message: 'id: "fake_id" not found' } },
]);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export default ({ getService }: FtrProviderContext) => {
.expect(200);

expect(body).to.eql([
{ rule_id: 'fake_id', error: { status_code: 404, message: 'id: "fake_id" not found' } },
{ id: 'fake_id', error: { status_code: 404, message: 'id: "fake_id" not found' } },
]);
});

Expand Down Expand Up @@ -347,7 +347,7 @@ export default ({ getService }: FtrProviderContext) => {
message: 'id: "fake_id" not found',
status_code: 404,
},
rule_id: 'fake_id', // TODO: This should be id and not rule_id in the codebase
id: 'fake_id',
},
]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export default ({ getService }: FtrProviderContext) => {
.expect(200);

expect(body).to.eql([
{ rule_id: 'fake_id', error: { status_code: 404, message: 'id: "fake_id" not found' } },
{ id: 'fake_id', error: { status_code: 404, message: 'id: "fake_id" not found' } },
]);
});

Expand Down Expand Up @@ -377,7 +377,7 @@ export default ({ getService }: FtrProviderContext) => {
message: 'id: "fake_id" not found',
status_code: 404,
},
rule_id: 'fake_id', // TODO: This should be id and not rule_id in the codebase
id: 'fake_id',
},
]);
});
Expand Down

0 comments on commit 8097bd8

Please sign in to comment.