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

[Fleet] Catch errors on package install if saved object reference not found #142112

Closed
juliaElastic opened this issue Sep 28, 2022 · 1 comment · Fixed by #146274
Closed

[Fleet] Catch errors on package install if saved object reference not found #142112

juliaElastic opened this issue Sep 28, 2022 · 1 comment · Fixed by #146274
Assignees
Labels
bug Fixes for quality problems that affect the customer experience Team:Fleet Team label for Observability Data Collection Fleet team

Comments

@juliaElastic
Copy link
Contributor

juliaElastic commented Sep 28, 2022

In a recent sdh, Fleet setup triggered a package reinstall, which contained a reference to a saved object that did not exist anymore.
This caused an error from which Fleet could not recover.

The code should catch errors when looking up saved objects, logging out errors as a warning, and should not throw an error.

See usages of savedObjectsClient.get here: https://github.com/elastic/kibana/blob/main/x-pack/plugins/fleet/server/services/epm/archive/storage.ts

Stack trace:

[2022-09-15T02:37:13.054+00:00][WARN ][plugins.fleet] Fleet setup failed
[2022-09-15T02:37:13.054+00:00][WARN ][plugins.fleet] Error: Saved object [epm-packages-assets/26c38a49-1bee-5afd-8433-3ea503fddd86] not found
    at Function.createGenericNotFoundError (/usr/share/kibana/src/core/server/saved_objects/service/lib/errors.js:125:37)
    at SavedObjectsRepository.get (/usr/share/kibana/src/core/server/saved_objects/service/lib/repository.js:1165:46)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at SavedObjectsClient.get (/usr/share/kibana/src/core/server/saved_objects/service/saved_objects_client.js:73:12)
    at EncryptedSavedObjectsClientWrapper.get (/usr/share/kibana/x-pack/plugins/encrypted_saved_objects/server/saved_objects/encrypted_saved_objects_client_wrapper.js:114:59)
    at SpacesSavedObjectsClient.get (/usr/share/kibana/x-pack/plugins/spaces/server/saved_objects/spaces_saved_objects_client.js:194:12)
    at getEsPackage (/usr/share/kibana/x-pack/plugins/fleet/server/services/epm/archive/storage.js:234:25)
    at getPackageFromSource (/usr/share/kibana/x-pack/plugins/fleet/server/services/epm/packages/get.js:247:13)
    at getPackageInfo (/usr/share/kibana/x-pack/plugins/fleet/server/services/epm/packages/get.js:165:9)
    at storedPackagePoliciesToAgentInputs (/usr/share/kibana/x-pack/plugins/fleet/server/services/agent_policies/package_policies_to_agent_inputs.js:100:49)
    at getFullAgentPolicy (/usr/share/kibana/x-pack/plugins/fleet/server/services/agent_policies/full_agent_policy.js:82:13)
    at AgentPolicyService.deployPolicy (/usr/share/kibana/x-pack/plugins/fleet/server/services/agent_policy.js:559:24)
    at agentPolicyUpdateEventHandler (/usr/share/kibana/x-pack/plugins/fleet/server/services/agent_policy_update.js:54:5)
    at /usr/share/kibana/node_modules/p-map/index.js:57:22

Further improvements from the same SDH:

  • The package cleanup logic should delete the epm-packages saved object as well, not only epm-packages-assets. It looks like we leave orphaned references in epm-packages object now.
  • It looks like on upgrade from 8.3 to 8.4, the download source created is not set to default. This is causing the error: Unable to initialize Fleet Default download source host is not setup. Check the migration logic to 8.4 to make sure the created download source is set as default.
  • force deleting a package should work even if there are agents using its package policies. Now it gives "unable to remove package with existing package policy(s) in use by agent(s)" error.
  • improve error message to specify which package policy is the problematic one: Error: No package for package policy undefined at /usr/share/kibana/x-pack/plugins/fleet/server/services/agent_policies/package_policies_to_agent_permissions.js:37:13
  • Ran into another issue where a SO not found blocks the package installation:
[2022-11-24T13:51:35.001+01:00][ERROR][plugins.fleet] Error: Error installing system 1.20.4: Saved object [dashboard/system-01c54730-fee6-11e9-8405-516218e3d268] not found
    at AssignmentService.updateTagAssignments (/Users/juliabardi/kibana/kibana/x-pack/plugins/saved_objects_tagging/server/services/assignments/assignment_service.ts:106:13)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async tagKibanaAssets (/Users/juliabardi/kibana/kibana/x-pack/plugins/fleet/server/services/epm/kibana/assets/tag_assets.ts:34:3)
    at async installKibanaAssetsAndReferences (/Users/juliabardi/kibana/kibana/x-pack/plugins/fleet/server/services/epm/kibana/assets/install.ts:141:3)
@juliaElastic juliaElastic added enhancement New value added to drive a business result Team:Fleet Team label for Observability Data Collection Fleet team bug Fixes for quality problems that affect the customer experience and removed enhancement New value added to drive a business result labels Sep 28, 2022
@juliaElastic juliaElastic self-assigned this Nov 22, 2022
@juliaElastic
Copy link
Contributor Author

juliaElastic commented Nov 24, 2022

The download source error is already reported here: #139513
Couldn't reproduce it so far.

juliaElastic added a commit that referenced this issue Nov 28, 2022
…ge assets (#146274)

## Summary

Fixes #142112

Improvements coming out of SDHs, to handle Fleet errors more robustly.

### 1. Catching package assets not found error and logging a warning, to
prevent it blocking setup/install.
### 2. In assets cleanup logic, doing a check that the assets are not
referenced by packages before deleting them.

Verifying 1. and 2. hard, because I don't have a way to reproduce the
assets not found error. Tried to install/delete different versions of a
package, but didn't run into the issues.

### 3. Force removing a package deletes package policies as well

To verify:
- add at least one integration (e.g. system package)
- force delete integration with API
```
DELETE http://elastic:changeme@localhost:5601/julia/api/fleet/epm/packages/system/1.20.4
kbn-xsrf: kibana

{ "force": true }
```
- verify that package policies are deleted as well
- there is an info log added:
```
[2022-11-24T14:10:29.138+01:00][INFO ][plugins.fleet] deleting package policies of system package because force flag was enabled: 84a4e5d4-e363-4045-9240-9ab151f2b376,6d34a544-7467-48fd-a779-c2d30d808aa6
```

### 4. Catching saved object not found error when tagging package assets
To verify:
- follow the steps above to force delete a package
- add system integration again 
- verify that it succeeds, and there is a warning log
```
[2022-11-24T14:07:47.960+01:00][WARN ][plugins.fleet] Error: Saved object [dashboard/system-01c54730-fee6-11e9-8405-516218e3d268] not found
```

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
kibanamachine pushed a commit to kibanamachine/kibana that referenced this issue Nov 28, 2022
…ge assets (elastic#146274)

## Summary

Fixes elastic#142112

Improvements coming out of SDHs, to handle Fleet errors more robustly.

### 1. Catching package assets not found error and logging a warning, to
prevent it blocking setup/install.
### 2. In assets cleanup logic, doing a check that the assets are not
referenced by packages before deleting them.

Verifying 1. and 2. hard, because I don't have a way to reproduce the
assets not found error. Tried to install/delete different versions of a
package, but didn't run into the issues.

### 3. Force removing a package deletes package policies as well

To verify:
- add at least one integration (e.g. system package)
- force delete integration with API
```
DELETE http://elastic:changeme@localhost:5601/julia/api/fleet/epm/packages/system/1.20.4
kbn-xsrf: kibana

{ "force": true }
```
- verify that package policies are deleted as well
- there is an info log added:
```
[2022-11-24T14:10:29.138+01:00][INFO ][plugins.fleet] deleting package policies of system package because force flag was enabled: 84a4e5d4-e363-4045-9240-9ab151f2b376,6d34a544-7467-48fd-a779-c2d30d808aa6
```

### 4. Catching saved object not found error when tagging package assets
To verify:
- follow the steps above to force delete a package
- add system integration again
- verify that it succeeds, and there is a warning log
```
[2022-11-24T14:07:47.960+01:00][WARN ][plugins.fleet] Error: Saved object [dashboard/system-01c54730-fee6-11e9-8405-516218e3d268] not found
```

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit 7198faf)
kibanamachine added a commit that referenced this issue Nov 30, 2022
… package assets (#146274) (#146411)

# Backport

This will backport the following commits from `main` to `8.6`:
- [Catching saved object not found error, do not delete referenced
package assets (#146274)](#146274)

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

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

<!--BACKPORT [{"author":{"name":"Julia
Bardi","email":"90178898+juliaElastic@users.noreply.github.com"},"sourceCommit":{"committedDate":"2022-11-28T15:07:20Z","message":"Catching
saved object not found error, do not delete referenced package assets
(#146274)\n\n## Summary\r\n\r\nFixes
#142112
coming out of SDHs, to handle Fleet errors more robustly.\r\n\r\n### 1.
Catching package assets not found error and logging a warning,
to\r\nprevent it blocking setup/install.\r\n### 2. In assets cleanup
logic, doing a check that the assets are not\r\nreferenced by packages
before deleting them.\r\n\r\nVerifying 1. and 2. hard, because I don't
have a way to reproduce the\r\nassets not found error. Tried to
install/delete different versions of a\r\npackage, but didn't run into
the issues.\r\n\r\n### 3. Force removing a package deletes package
policies as well\r\n\r\nTo verify:\r\n- add at least one integration
(e.g. system package)\r\n- force delete integration with
API\r\n```\r\nDELETE
http://elastic:changeme@localhost:5601/julia/api/fleet/epm/packages/system/1.20.4\r\nkbn-xsrf:
kibana\r\n\r\n{ \"force\": true }\r\n```\r\n- verify that package
policies are deleted as well\r\n- there is an info log
added:\r\n```\r\n[2022-11-24T14:10:29.138+01:00][INFO ][plugins.fleet]
deleting package policies of system package because force flag was
enabled:
84a4e5d4-e363-4045-9240-9ab151f2b376,6d34a544-7467-48fd-a779-c2d30d808aa6\r\n```\r\n\r\n###
4. Catching saved object not found error when tagging package
assets\r\nTo verify:\r\n- follow the steps above to force delete a
package\r\n- add system integration again \r\n- verify that it succeeds,
and there is a warning
log\r\n```\r\n[2022-11-24T14:07:47.960+01:00][WARN ][plugins.fleet]
Error: Saved object
[dashboard/system-01c54730-fee6-11e9-8405-516218e3d268] not
found\r\n```\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"7198faf5ee61faf26c41c4549b48d83a7eff0069","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","v8.7.0","v8.6.1"],"number":146274,"url":"#146274
saved object not found error, do not delete referenced package assets
(#146274)\n\n## Summary\r\n\r\nFixes
#142112
coming out of SDHs, to handle Fleet errors more robustly.\r\n\r\n### 1.
Catching package assets not found error and logging a warning,
to\r\nprevent it blocking setup/install.\r\n### 2. In assets cleanup
logic, doing a check that the assets are not\r\nreferenced by packages
before deleting them.\r\n\r\nVerifying 1. and 2. hard, because I don't
have a way to reproduce the\r\nassets not found error. Tried to
install/delete different versions of a\r\npackage, but didn't run into
the issues.\r\n\r\n### 3. Force removing a package deletes package
policies as well\r\n\r\nTo verify:\r\n- add at least one integration
(e.g. system package)\r\n- force delete integration with
API\r\n```\r\nDELETE
http://elastic:changeme@localhost:5601/julia/api/fleet/epm/packages/system/1.20.4\r\nkbn-xsrf:
kibana\r\n\r\n{ \"force\": true }\r\n```\r\n- verify that package
policies are deleted as well\r\n- there is an info log
added:\r\n```\r\n[2022-11-24T14:10:29.138+01:00][INFO ][plugins.fleet]
deleting package policies of system package because force flag was
enabled:
84a4e5d4-e363-4045-9240-9ab151f2b376,6d34a544-7467-48fd-a779-c2d30d808aa6\r\n```\r\n\r\n###
4. Catching saved object not found error when tagging package
assets\r\nTo verify:\r\n- follow the steps above to force delete a
package\r\n- add system integration again \r\n- verify that it succeeds,
and there is a warning
log\r\n```\r\n[2022-11-24T14:07:47.960+01:00][WARN ][plugins.fleet]
Error: Saved object
[dashboard/system-01c54730-fee6-11e9-8405-516218e3d268] not
found\r\n```\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"7198faf5ee61faf26c41c4549b48d83a7eff0069"}},"sourceBranch":"main","suggestedTargetBranches":["8.6"],"targetPullRequestStates":[{"branch":"main","label":"v8.7.0","labelRegex":"^v8.7.0$","isSourceBranch":true,"state":"MERGED","url":"#146274
saved object not found error, do not delete referenced package assets
(#146274)\n\n## Summary\r\n\r\nFixes
#142112
coming out of SDHs, to handle Fleet errors more robustly.\r\n\r\n### 1.
Catching package assets not found error and logging a warning,
to\r\nprevent it blocking setup/install.\r\n### 2. In assets cleanup
logic, doing a check that the assets are not\r\nreferenced by packages
before deleting them.\r\n\r\nVerifying 1. and 2. hard, because I don't
have a way to reproduce the\r\nassets not found error. Tried to
install/delete different versions of a\r\npackage, but didn't run into
the issues.\r\n\r\n### 3. Force removing a package deletes package
policies as well\r\n\r\nTo verify:\r\n- add at least one integration
(e.g. system package)\r\n- force delete integration with
API\r\n```\r\nDELETE
http://elastic:changeme@localhost:5601/julia/api/fleet/epm/packages/system/1.20.4\r\nkbn-xsrf:
kibana\r\n\r\n{ \"force\": true }\r\n```\r\n- verify that package
policies are deleted as well\r\n- there is an info log
added:\r\n```\r\n[2022-11-24T14:10:29.138+01:00][INFO ][plugins.fleet]
deleting package policies of system package because force flag was
enabled:
84a4e5d4-e363-4045-9240-9ab151f2b376,6d34a544-7467-48fd-a779-c2d30d808aa6\r\n```\r\n\r\n###
4. Catching saved object not found error when tagging package
assets\r\nTo verify:\r\n- follow the steps above to force delete a
package\r\n- add system integration again \r\n- verify that it succeeds,
and there is a warning
log\r\n```\r\n[2022-11-24T14:07:47.960+01:00][WARN ][plugins.fleet]
Error: Saved object
[dashboard/system-01c54730-fee6-11e9-8405-516218e3d268] not
found\r\n```\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"7198faf5ee61faf26c41c4549b48d83a7eff0069"}},{"branch":"8.6","label":"v8.6.1","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Julia Bardi <90178898+juliaElastic@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Fixes for quality problems that affect the customer experience Team:Fleet Team label for Observability Data Collection Fleet team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant