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

[Cases] Validate attributes before create SOs #158590

Merged
merged 15 commits into from
Jun 7, 2023

Conversation

cnasikas
Copy link
Member

@cnasikas cnasikas commented May 26, 2023

Summary

This PR uses io-ts's decode utitlity function to validate the attributes before creating cases saved objects.

Checklist

Delete any items that are not applicable to this PR.

For maintainers

@cnasikas cnasikas added release_note:skip Skip the PR/issue when compiling release notes Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) Feature:Cases Cases feature v8.9.0 labels May 26, 2023
@cnasikas cnasikas self-assigned this May 26, 2023
@cnasikas cnasikas marked this pull request as ready for review May 29, 2023 08:33
@cnasikas cnasikas requested a review from a team as a code owner May 29, 2023 08:33
@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops (Team:ResponseOps)

@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops-cases (Feature:Cases)

@cnasikas
Copy link
Member Author

@elasticmachine merge upstream


export const CommentUserActionRt = rt.strict({
type: rt.literal(ActionTypes.comment),
payload: CommentUserActionPayloadRt,
});

export const CommentUserActionWithoutIdsRt = rt.strict({
Copy link
Member Author

Choose a reason for hiding this comment

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

I needed this type so I can validate the attributes passed to the so client when you creating a user action. The attributes do not contain any ids as they are in the references array.

@cnasikas cnasikas force-pushed the decode_cases_services_requests branch from 5bf2fde to 784dc56 Compare May 29, 2023 08:37
@@ -242,24 +241,6 @@ export const basicParams = {
...entity,
};

export const mappings: ConnectorMappings = [
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved on level up.

@cnasikas
Copy link
Member Author

buildkite test this

Copy link
Contributor

@jonathan-buttner jonathan-buttner left a comment

Choose a reason for hiding this comment

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

Great work, left a couple comments

@@ -167,9 +167,11 @@ export class AttachmentService {
try {
this.context.log.debug(`Attempting to POST a new comment`);

const decodedAttributes = decodeOrThrow(AttachmentTransformedAttributesRt)(attributes);
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about using CommentAttributesRt etc for the io-ts schema instead? When I read this, my first thought is why are we checking that the attributes match the "transformed" schema. I could see that being confusing if you didn't know they were the same.

@@ -583,7 +585,13 @@ export class CasesService {
}: PatchCaseArgs): Promise<SavedObjectsUpdateResponse<CaseTransformedAttributes>> {
try {
this.log.debug(`Attempting to UPDATE case ${caseId}`);
const transformedAttributes = transformAttributesToESModel(updatedAttributes);

const PartialCaseTransformedAttributesRt = getPartialCaseTransformedAttributesRt();
Copy link
Contributor

Choose a reason for hiding this comment

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

Forgot to mention when we created getPartialCaseTransformedAttributesRt() but we could probably move it to a global within this file or static variable within the class since it won't change. That way we don't have to create it every time.

await expect(persister.createUserAction(getRequest())).resolves.not.toThrow();
});

it('throws if closure_type is omitted', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: throws if fields is omitted

).resolves.not.toThrow();
});

it('throws if closure_type is omitted', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: throws if owner is omitted

PersistableStateAttachmentRt,
]);

export const CommentRequestRt = rt.union([...BasicCommentRequestRt.types, ExternalReferenceSORt]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it work to do:

export const CommentRequestRt = rt.union([BasicCommentRequestRt, ExternalReferenceSORt]);

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems so!

@cnasikas
Copy link
Member Author

buildkite test this

@cnasikas
Copy link
Member Author

cnasikas commented Jun 6, 2023

@elasticmachine merge upstream

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
cases 175.6KB 175.8KB +149.0B
Unknown metric groups

ESLint disabled line counts

id before after diff
enterpriseSearch 19 21 +2
securitySolution 413 417 +4
total +6

Total ESLint disabled count

id before after diff
enterpriseSearch 20 22 +2
securitySolution 497 501 +4
total +6

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @cnasikas

Copy link
Contributor

@adcoelho adcoelho left a comment

Choose a reason for hiding this comment

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

LGTM so far, I just want to take another look at the tests.

As discussed offline, I checked also other services in x-pack/plugins/cases/server/services/. The alerts service, in particular, felt like it could need a decode before calls to the SOClient. I took a look and it seemed fine because none of the methods there are exposed to the outside anyway, and none uses anything other than case or alert ids but perhaps you could double-check?

})
).resolves.not.toThrow();
});

Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if no attribute is passed?

Copy link
Contributor

@js-jankisalvi js-jankisalvi left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for adding tests to verify error scenarios. 👍

@cnasikas cnasikas merged commit b5adaec into elastic:main Jun 7, 2023
@cnasikas cnasikas deleted the decode_cases_services_requests branch June 7, 2023 15:58
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting Feature:Cases Cases feature release_note:skip Skip the PR/issue when compiling release notes Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) v8.9.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants