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] Registering file attachment type #152399

Merged

Conversation

jonathan-buttner
Copy link
Contributor

@jonathan-buttner jonathan-buttner commented Feb 28, 2023

This PR registers the file attachment type .files within the cases attachment framework.

Notable changes:

  • Attachment framework accepts an optional validation function for use when a request is received for that attachment type, this allows validating that the file metadata is correct
  • Refactored the logic for enforcing that only 1000 alerts can be attached to a case and 100 files

Issue: #151933

cUrl request to create file attachments
curl --location --request POST 'http://elastic:changeme@localhost:5601/internal/cases/<case id>/attachments/_bulk_create' \
--header 'kbn-xsrf: hello' \
--header 'Content-Type: application/json' \
--data-raw '[
    {
        "type": "externalReference",
        "externalReferenceStorage": {
            "type": "savedObject",
            "soType": "file"
        },
        "externalReferenceId": "my-id",
        "externalReferenceAttachmentTypeId": ".files",
        "externalReferenceMetadata": {
            "file": {
                "name": "test_file",
                "extension": "png",
                "mimeType": "image/png",
                "createdAt": "2023-02-27T20:26:54.345Z"
            }
        },
        "owner": "cases"
    },
    {
        "type": "externalReference",
        "externalReferenceStorage": {
            "type": "savedObject",
            "soType": "file"
        },
        "externalReferenceId": "my-id",
        "externalReferenceAttachmentTypeId": ".files",
        "externalReferenceMetadata": {
            "file": {
                "name": "test_file",
                "extension": "png",
                "mimeType": "image/png",
                "createdAt": "2023-02-27T20:26:54.345Z"
            }
        },
        "owner": "cases"
    },
        {
        "type": "externalReference",
        "externalReferenceStorage": {
            "type": "savedObject",
            "soType": "file"
        },
        "externalReferenceId": "my-id",
        "externalReferenceAttachmentTypeId": ".files",
        "externalReferenceMetadata": {
            "file": {
                "name": "test_file",
                "extension": "jpeg",
                "mimeType": "image/jpeg",
                "createdAt": "2023-02-27T20:26:54.345Z"
            }
        },
        "owner": "cases"
    }
]
'

@jonathan-buttner jonathan-buttner added release_note:skip Skip the PR/issue when compiling release notes backport:skip This commit does not require backporting Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) Feature:Cases Cases feature v8.8.0 labels Feb 28, 2023
@jonathan-buttner jonathan-buttner marked this pull request as ready for review March 1, 2023 12:28
@jonathan-buttner jonathan-buttner requested a review from a team as a code owner March 1, 2023 12:28
@elasticmachine
Copy link
Contributor

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

@elasticmachine
Copy link
Contributor

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

@@ -104,6 +105,8 @@ export class CasePlugin {
)}] and plugins [${Object.keys(plugins)}]`
);

registerInternalAttachments(this.externalReferenceAttachmentTypeRegistry);
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is an "Internal attachment type" that should always exist maybe it would make more sense to register it when externalReferenceAttachmentTypeRegistry is initialized and not when the cases plugin is set up?

Copy link
Member

@cnasikas cnasikas Mar 1, 2023

Choose a reason for hiding this comment

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

The externalReferenceAttachmentTypeRegistry should not be considered as a singleton. It can be instantiated in various places where a clean state is needed and can lead to bugs if not. It will also make the testing much harder if the state of the class contains our internal attachments.

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 I'm open to either keeping the registration in the plugin or making it a part of the ExternalReferenceAttachmentTypeRegistry. If it makes testing easier to do registration in the plugin that's fine with me.

Copy link
Contributor

Choose a reason for hiding this comment

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

It will also make the testing much harder if the state of the class contains our internal attachments.

But if I understand it correctly the class is "ours" so wherever it is used it will need those internal attachments.

My reasoning was that it is why more error-prone to have to remember to register internal types every time we instantiate it. External ones though are fine because those are the responsibility of the "instantiator".

Copy link
Member

@cnasikas cnasikas Mar 2, 2023

Choose a reason for hiding this comment

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

My reasoning was that it is why more error-prone to have to remember to register internal types every time we instantiate it

I see what you mean. This should never happen. This class is instantiated only once in the setup lifecycle method of the cases plugin. The same instance is passed down to the cases code and lives throughout the whole lifecycle of the cases plugin (This can be a counterargument to my main point 🙂). If you need a new instance of the class you probably need it for testing or to do something else unrelated to the internal attachments.

I see the cases plugin as an external consumer of the externalReferenceAttachmentTypeRegistry and not as an internal consumer. By registering data in the constructor of the externalReferenceAttachmentTypeRegistry you tight couple the data with the logic of the class (representation). The class is now aware of the data which are implementation detail. The class has more concerns than it should have.

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, just some minor comments 👍

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
cases 564 565 +1

Page load bundle

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

id before after diff
cases 133.2KB 133.3KB +107.0B
Unknown metric groups

ESLint disabled line counts

id before after diff
securitySolution 428 430 +2

Total ESLint disabled count

id before after diff
securitySolution 506 508 +2

History

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

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.

Code LGMT! Tested without issues 🚀

@jonathan-buttner jonathan-buttner merged commit 94fbed0 into elastic:main Mar 6, 2023
@jonathan-buttner jonathan-buttner deleted the cases-file-attachment branch March 6, 2023 16:57
bmorelli25 pushed a commit to bmorelli25/kibana that referenced this pull request Mar 10, 2023
This PR registers the file attachment type `.files` within the cases
attachment framework.

Notable changes:
- Attachment framework accepts an optional validation function for use
when a request is received for that attachment type, this allows
validating that the file metadata is correct
- Refactored the logic for enforcing that only 1000 alerts can be
attached to a case and 100 files

Issue: elastic#151933

<details><summary>cUrl request to create file attachments</summary>

```
curl --location --request POST 'http://elastic:changeme@localhost:5601/internal/cases/<case id>/attachments/_bulk_create' \
--header 'kbn-xsrf: hello' \
--header 'Content-Type: application/json' \
--data-raw '[
    {
        "type": "externalReference",
        "externalReferenceStorage": {
            "type": "savedObject",
            "soType": "file"
        },
        "externalReferenceId": "my-id",
        "externalReferenceAttachmentTypeId": ".files",
        "externalReferenceMetadata": {
            "file": {
                "name": "test_file",
                "extension": "png",
                "mimeType": "image/png",
                "createdAt": "2023-02-27T20:26:54.345Z"
            }
        },
        "owner": "cases"
    },
    {
        "type": "externalReference",
        "externalReferenceStorage": {
            "type": "savedObject",
            "soType": "file"
        },
        "externalReferenceId": "my-id",
        "externalReferenceAttachmentTypeId": ".files",
        "externalReferenceMetadata": {
            "file": {
                "name": "test_file",
                "extension": "png",
                "mimeType": "image/png",
                "createdAt": "2023-02-27T20:26:54.345Z"
            }
        },
        "owner": "cases"
    },
        {
        "type": "externalReference",
        "externalReferenceStorage": {
            "type": "savedObject",
            "soType": "file"
        },
        "externalReferenceId": "my-id",
        "externalReferenceAttachmentTypeId": ".files",
        "externalReferenceMetadata": {
            "file": {
                "name": "test_file",
                "extension": "jpeg",
                "mimeType": "image/jpeg",
                "createdAt": "2023-02-27T20:26:54.345Z"
            }
        },
        "owner": "cases"
    }
]
'
```

</details>
@cnasikas cnasikas mentioned this pull request Apr 3, 2023
5 tasks
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.8.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants