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] Add decode to attachment functionality #158424

Merged
merged 6 commits into from
May 26, 2023

Conversation

jonathan-buttner
Copy link
Contributor

@jonathan-buttner jonathan-buttner commented May 24, 2023

This PR adds io-ts decode calls to the service layer of the attachment functionality. This ensures that the results from elasticsearch do not violate the schema.

For bulk operations, error saved objects are not decoded.

@jonathan-buttner jonathan-buttner 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 24, 2023
@@ -172,7 +180,7 @@ const CommentAttributesWithoutRefsRt = rt.union([
AttributesTypeUserRt,
AttributesTypeAlertsRt,
AttributesTypeActionsRt,
ExternalReferenceWithoutRefsRt,
AttributesTypeExternalReferenceWithoutRefsRt,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this was a bug before. ExternalReferenceWithoutRefsRt. Doesn't include the common attributes like created_by etc.

@jonathan-buttner jonathan-buttner marked this pull request as ready for review May 25, 2023 16:13
@jonathan-buttner jonathan-buttner requested a review from a team as a code owner May 25, 2023 16:13
@elasticmachine
Copy link
Contributor

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

@elasticmachine
Copy link
Contributor

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

Copy link
Member

@cnasikas cnasikas left a comment

Choose a reason for hiding this comment

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

LGTM! I left some comments.

if (isSOError(attachment)) {
// Forcing the type here even though it is an error. The client is responsible for
// determining what to do with the errors
// TODO: we should fix the return type of this function so that it can return errors
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately the SavedObjectsBulkUpdateResponse is wrong so we need to cast it at some point somewhere. Similar to patchCases in x-pack/plugins/cases/server/services/cases/index.ts we had to cast.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I mean we can create our own type where we force it to be what we want.

Copy link
Member

Choose a reason for hiding this comment

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

ohh, ok!

): SavedObjectsBulkResponse<AttachmentTransformedAttributes> {
const validatedAttachments: AttachmentSavedObjectTransformed[] = [];

for (const so of res.saved_objects) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we check if it is a SO error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, good idea

): Array<SavedObject<AttributesTypeAlerts>> {
return response.saved_objects.map((so) => {
// We need a cast here because the limited attachment type conflicts with the expected result even though they
// should be the same
Copy link
Member

Choose a reason for hiding this comment

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

Where is the cast 🙂 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lol, oops, I'll remove it.

id: '1',
type: CASE_COMMENT_SAVED_OBJECT,
attributes: {
comment: 'Wow, good luck catching that bad meanie!',
Copy link
Member

Choose a reason for hiding this comment

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

Is funny how this follows us after 3 years 😆.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha, good ol' copy paste!

});

await expect(attachmentGetter.bulkGet(['1'])).rejects.toThrowErrorMatchingInlineSnapshot(
`"Invalid value \\"undefined\\" supplied to \\"comment\\",Invalid value \\"user\\" supplied to \\"type\\",Invalid value \\"undefined\\" supplied to \\"alertId\\",Invalid value \\"undefined\\" supplied to \\"index\\",Invalid value \\"undefined\\" supplied to \\"rule\\",Invalid value \\"undefined\\" supplied to \\"actions\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceAttachmentTypeId\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceMetadata\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceId\\",Invalid value \\"undefined\\" supplied to \\"externalReferenceStorage\\",Invalid value \\"undefined\\" supplied to \\"persistableStateAttachmentTypeId\\",Invalid value \\"undefined\\" supplied to \\"persistableStateAttachmentState\\""`
Copy link
Member

Choose a reason for hiding this comment

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

I would expect a message only about the comment. Is it because io-ts tries to find a matching definion on the other attachment types?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's what I thought originally too. Yeah I think the issue is that io-ts doesn't know what type to use from the union because it doesn't match any of them.

Copy link
Member

Choose a reason for hiding this comment

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

Make sense!

@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 170.0KB 170.0KB +26.0B
Unknown metric groups

ESLint disabled line counts

id before after diff
enterpriseSearch 19 21 +2
securitySolution 400 404 +4
total +6

Total ESLint disabled count

id before after diff
enterpriseSearch 20 22 +2
securitySolution 480 484 +4
total +6

History

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

@jonathan-buttner jonathan-buttner merged commit 97e122f into elastic:main May 26, 2023
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label May 26, 2023
@jonathan-buttner jonathan-buttner deleted the cases-comments-decode branch May 26, 2023 14:01
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.

5 participants