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

[Security Solution + Files + Fleet] Add option to Files client to handle index alias and fix Endpoint/Fleet usage to set new option to true #153342

Merged

Conversation

paul-tavares
Copy link
Contributor

@paul-tavares paul-tavares commented Mar 20, 2023

Summary

  • Adds indexIsAlias to Files plugin client. Used when provided indexes are Aliases (changes how the documents are retrieved internally)
  • Changes security solution (endpoint) file service to use .search() instead of .get() when retrieving a file metadata via id
  • Changed Security Solution call to createEsFileClient() (Files plugin service) to set indexIsAlias to true
  • Changed Fleet call to createEsFileClient() (Files plugin service) to set indexIsAlias to true

Addresses the following Issues that were raised for 8.7:


olm-6234-fix-file-downloads

@paul-tavares paul-tavares added release_note:skip Skip the PR/issue when compiling release notes Team:Defend Workflows “EDR Workflows” sub-team of Security Solution v8.7.0 labels Mar 20, 2023
@paul-tavares paul-tavares self-assigned this Mar 20, 2023
: undefined;

// Because `asStream` was used in retrieving the document, errors will also not be processes
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI: this was something that had caused me some trouble in the past. When asStream is used, errors in ES are also not processed, so the call does not actually fail. We check here to see if retrieving the chunk was successful and error/log it if not.

// to retrieve the chunk.
if (decodedChunkDoc && ('error' in decodedChunkDoc || !decodedChunkDoc.found)) {
const err = new Error(`Failed to retrieve chunk id [${id}] from index [${chunkIndex}]`);
this.logger.error(err);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI: I normally prefer .error() instead of .debug just because it will provide imediate feedback in the logs without requiring a customer to set debugging to debug. The code base seems to (for the most part) use .debug() so let me know if I should change it.

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM

@paul-tavares paul-tavares changed the title [Security Solution][Endpoint] Fix get-file server services to use .search() instead of .get() when retrieving file info [Security Solution + Files + Fleet] Add option to Files client to handle index alias and fix Endpoint/Fleet usage to set new option to true Mar 21, 2023
@paul-tavares paul-tavares marked this pull request as ready for review March 21, 2023 16:32
@paul-tavares paul-tavares requested review from a team as code owners March 21, 2023 16:32
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-defend-workflows (Team:Defend Workflows)

@vadimkibana vadimkibana requested a review from jloleysens March 21, 2023 16:35
Copy link
Member

@kpollich kpollich left a comment

Choose a reason for hiding this comment

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

Fleet change (one line/one file) LGTM 🚀

@@ -186,6 +186,7 @@ export async function getAgentUploadFile(
metadataIndex: FILE_STORAGE_METADATA_AGENT_INDEX,
elasticsearchClient: esClient,
logger: appContextService.getLogger(),
indexIsAlias: true,
Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

@botelastic botelastic bot added the Team:Fleet Team label for Observability Data Collection Fleet team label Mar 21, 2023
@elasticmachine
Copy link
Contributor

Pinging @elastic/fleet (Team:Fleet)

Copy link
Contributor

@vadimkibana vadimkibana 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! Files services code changes LGTM.


In Slack you mentioned about testing, there are integration tests in src/plugins/files/server/blob_storage_service/adapters/es/integration_tests/es.test.ts for this code.

You can run them with

yarn test:jest_integration src/plugins/files/server/blob_storage_service/adapters/es/integration_tests/es.test.ts

You can either add a test case for indexIsAlias there, or copy the whole test suite and execute it using indexIsAlias: true.

@vadimkibana
Copy link
Contributor

vadimkibana commented Mar 21, 2023

Nothing actionable, just curious, why did you decide instead of fetching the chunk by "search", to instead execute a query to find the index first and then fetch by "get"?

Copy link
Member

@ashokaditya ashokaditya left a comment

Choose a reason for hiding this comment

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

Thanks for the changes. 🔥 I only have a few nits but this is good to 🚢

@@ -30,6 +30,13 @@ export interface CreateEsFileClientArgs {
* An elasticsearch client that will be used to interact with the cluster.
*/
elasticsearchClient: ElasticsearchClient;
/**
* Tread the indexes provided as Aliases. If set to true, ES `search()` will be used to
Copy link
Member

Choose a reason for hiding this comment

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

nit: Treat the indices...

@@ -30,6 +30,13 @@ export interface CreateEsFileClientArgs {
* An elasticsearch client that will be used to interact with the cluster.
*/
elasticsearchClient: ElasticsearchClient;
/**
* Tread the indexes provided as Aliases. If set to true, ES `search()` will be used to
* retrieve the file info and content instead of `get()`. This is needed to ensurer the
Copy link
Member

Choose a reason for hiding this comment

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

nit: needed to ensure...

: undefined;

// Because `asStream` was used in retrieving the document, errors will also not be processes
Copy link
Member

Choose a reason for hiding this comment

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

nit: ...also not be processed...

Copy link
Contributor

@hop-dev hop-dev left a comment

Choose a reason for hiding this comment

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

Thanks for this Paul!

@paul-tavares
Copy link
Contributor Author

Hi @vadimkibana ,

Re:

Nothing actionable, just curious, why did you decide instead of fetching the chunk by "search", to instead execute a query to find the index first and then fetch by "get"?

I did not want to change to much in that function, so I opted to stick with the .get() and just ensure I got the correct (real) index for it. That being said, looks like asStream is also available in the .search(), so maybe I should use that instead?

@paul-tavares paul-tavares enabled auto-merge (squash) March 21, 2023 18:48
@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Unknown metric groups

API count

id before after diff
files 214 215 +1

ESLint disabled line counts

id before after diff
securitySolution 433 436 +3

Total ESLint disabled count

id before after diff
securitySolution 513 516 +3

History

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

cc @paul-tavares

@paul-tavares paul-tavares merged commit 50cc574 into elastic:main Mar 21, 2023
@paul-tavares paul-tavares deleted the task/olm-6234-fix-get-file-index-access branch March 21, 2023 20:40
@kibanamachine
Copy link
Contributor

💔 All backports failed

Status Branch Result
8.7 Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

node scripts/backport --pr 153342

Questions ?

Please refer to the Backport tool documentation

@paul-tavares
Copy link
Contributor Author

💚 All backports created successfully

Status Branch Result
8.7

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

paul-tavares added a commit to paul-tavares/kibana that referenced this pull request Mar 21, 2023
…dle index alias and fix Endpoint/Fleet usage to set new option to true (elastic#153342)

## Summary

- Adds `indexIsAlias` to `Files` plugin client. Used when provided
indexes are Aliases (changes how the documents are retrieved internally)
- Changes security solution (endpoint) file service to use `.search()`
instead of `.get()` when retrieving a file metadata via `id`
- Changed Security Solution call to `createEsFileClient()` (`Files`
plugin service) to set `indexIsAlias` to `true`
- Changed Fleet call to `createEsFileClient()` (`Files` plugin service)
to set `indexIsAlias` to `true`

Addresses the following Issues that were raised for 8.7:

- Fixes elastic#153322
- FIxes elastic#153334

(cherry picked from commit 50cc574)

# Conflicts:
#	src/plugins/files/server/blob_storage_service/adapters/es/content_stream/content_stream.ts
#	src/plugins/files/server/file_client/create_es_file_client.ts
paul-tavares added a commit that referenced this pull request Mar 22, 2023
…to handle index alias and fix Endpoint/Fleet usage to set new option to true (#153342) (#153403)

# Backport

This will backport the following commits from `main` to `8.7`:
- [[Security Solution + Files + Fleet] Add option to Files client to
handle index alias and fix Endpoint/Fleet usage to set new option to
true (#153342)](#153342)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Paul
Tavares","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-03-21T20:40:39Z","message":"[Security
Solution + Files + Fleet] Add option to Files client to handle index
alias and fix Endpoint/Fleet usage to set new option to true
(#153342)\n\n## Summary\r\n\r\n- Adds `indexIsAlias` to `Files` plugin
client. Used when provided\r\nindexes are Aliases (changes how the
documents are retrieved internally)\r\n- Changes security solution
(endpoint) file service to use `.search()`\r\ninstead of `.get()` when
retrieving a file metadata via `id`\r\n- Changed Security Solution call
to `createEsFileClient()` (`Files`\r\nplugin service) to set
`indexIsAlias` to `true`\r\n- Changed Fleet call to
`createEsFileClient()` (`Files` plugin service)\r\nto set `indexIsAlias`
to `true`\r\n\r\n\r\nAddresses the following Issues that were raised for
8.7:\r\n\r\n- Fixes #153322 \r\n- FIxes
#153334","sha":"50cc574c63a3837ce255878ee483b9cbc75d8277","branchLabelMapping":{"^v8.8.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","Team:Defend
Workflows","v8.7.0","v8.8.0"],"number":153342,"url":"https://github.com/elastic/kibana/pull/153342","mergeCommit":{"message":"[Security
Solution + Files + Fleet] Add option to Files client to handle index
alias and fix Endpoint/Fleet usage to set new option to true
(#153342)\n\n## Summary\r\n\r\n- Adds `indexIsAlias` to `Files` plugin
client. Used when provided\r\nindexes are Aliases (changes how the
documents are retrieved internally)\r\n- Changes security solution
(endpoint) file service to use `.search()`\r\ninstead of `.get()` when
retrieving a file metadata via `id`\r\n- Changed Security Solution call
to `createEsFileClient()` (`Files`\r\nplugin service) to set
`indexIsAlias` to `true`\r\n- Changed Fleet call to
`createEsFileClient()` (`Files` plugin service)\r\nto set `indexIsAlias`
to `true`\r\n\r\n\r\nAddresses the following Issues that were raised for
8.7:\r\n\r\n- Fixes #153322 \r\n- FIxes
#153334","sha":"50cc574c63a3837ce255878ee483b9cbc75d8277"}},"sourceBranch":"main","suggestedTargetBranches":["8.7"],"targetPullRequestStates":[{"branch":"8.7","label":"v8.7.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.8.0","labelRegex":"^v8.8.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/153342","number":153342,"mergeCommit":{"message":"[Security
Solution + Files + Fleet] Add option to Files client to handle index
alias and fix Endpoint/Fleet usage to set new option to true
(#153342)\n\n## Summary\r\n\r\n- Adds `indexIsAlias` to `Files` plugin
client. Used when provided\r\nindexes are Aliases (changes how the
documents are retrieved internally)\r\n- Changes security solution
(endpoint) file service to use `.search()`\r\ninstead of `.get()` when
retrieving a file metadata via `id`\r\n- Changed Security Solution call
to `createEsFileClient()` (`Files`\r\nplugin service) to set
`indexIsAlias` to `true`\r\n- Changed Fleet call to
`createEsFileClient()` (`Files` plugin service)\r\nto set `indexIsAlias`
to `true`\r\n\r\n\r\nAddresses the following Issues that were raised for
8.7:\r\n\r\n- Fixes #153322 \r\n- FIxes
#153334","sha":"50cc574c63a3837ce255878ee483b9cbc75d8277"}}]}]
BACKPORT-->
Copy link
Contributor

@jloleysens jloleysens left a comment

Choose a reason for hiding this comment

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

Left a few post hoc thoughts @paul-tavares . Otherwise this change LGTM!

Comment on lines +47 to +48
private readonly logger: Logger,
private readonly indexIsAlias: boolean = false
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: we usually try to follow the pattern of passing logger last. I understand this prevents indexIsAlias from using default and would make the refactor a little larger. Best solution may be proper DI 😄

return fileDocSearchResult.hits.hits[0] as GetResponse<TDocument>;
}

return esClient.get<TDocument>({
Copy link
Contributor

@jloleysens jloleysens Mar 22, 2023

Choose a reason for hiding this comment

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

With this change, we do not have the same error behaviour for non-existent docs. A call to .get will throw where a call to .search will not.

I think we should just copy what x-pack/plugins/security_solution/server/endpoint/services/actions/action_files.ts is doing in this case.

@paul-tavares
Copy link
Contributor Author

Thanks @jloleysens for the feedback.
I'm preparing another PR to add tests and will make these changes you suggested as well, now that I don't have to rush it to get into v8.7.

paul-tavares added a commit that referenced this pull request Mar 28, 2023
…tion and minor refactor (#153815)

## Summary

This PR builds on top of #153342
and:

- adds test for the `indexAsAlias` option that was added to
`createEsFileClient()`
- removes `fetchDoc()` utility (not needed)

---------

Co-authored-by: kibanamachine <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_note:skip Skip the PR/issue when compiling release notes Team:Defend Workflows “EDR Workflows” sub-team of Security Solution Team:Fleet Team label for Observability Data Collection Fleet team v8.7.0 v8.8.0
Projects
None yet