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

Fixes conflict checks for sharing saved objects #168655

Merged
merged 10 commits into from
Oct 16, 2023

Conversation

jeramysoucy
Copy link
Contributor

@jeramysoucy jeramysoucy commented Oct 11, 2023

Closes #168049

Summary

This PR adjusts the KQL filters when collecting references to saved objects for the purpose of updating their spaces (i.e. sharing saved objects to spaces). An additional filter is added to specifically exclude ID -> ID matches - an ID match would mean the object has already been shared to the destination space and there is no conflict. Filters to match the shared object's ID or origin ID to the destination space's objects' origin ID, and to match the shared object's origin ID to the destination space's objects' IDs remain in place to properly check for conflicts with potential copies.

Manual Testing

  • Create 2 spaces: A and B
  • Add a sample data set (e.g. flight) to space A
  • In Discover, create a saved query called "s1" with a filter pill that uses the sample data logs data view
  • Create another saved query called "s2" with a filter pill that uses the sample data logs data view
  • Go to Stack Management->Saved Objects and share the "s1" query to space B
  • Now share the "s2" query to space B. From the main branch you will see that there is a conflict that disallows sharing the second query. This is because it is also attempting to share the referenced data view, which is already in space B. However, this should not be a conflict - from this PR you will be able to successfully share both queries.

Automated Testing

  • packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/utils/find_shared_origin_objects.test.ts
  • packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/internals/collect_multi_namespace_references.test.ts
  • x-pack/test/spaces_api_integration/common/suites/get_shareable_references.ts

@jeramysoucy jeramysoucy added Team:Security Team focused on: Auth, Users, Roles, Spaces, Audit Logging, and more! release_note:skip Skip the PR/issue when compiling release notes backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) v8.11.0 v8.12.0 labels Oct 11, 2023
@jeramysoucy jeramysoucy marked this pull request as ready for review October 12, 2023 20:47
@jeramysoucy jeramysoucy requested review from a team as code owners October 12, 2023 20:47
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-security (Team:Security)

@kc13greiner kc13greiner self-requested a review October 12, 2023 20:50
const match1 = buildNode('is', `${type}.id`, esKuery.escapeKuery(`${type}:${origin}`)); // here we are looking for the raw document `_id` field, which has a `type:` prefix
const match2 = buildNode('is', `${type}.originId`, esKuery.escapeKuery(origin)); // here we are looking for the saved object's `originId` field, which does not have a `type:` prefix
acc.push([match1, match2]);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To reviewers: there are a few ways we can structure this logic. I am not 100% set on the current implementation. Alternatively, we could have an explicit case for updateObjectsSpaces that distinctly handles when the shared object has an origin ID, rather than relying on re-using the initial two kuery nodes. This may make it more readable. Let me know what you think.

Copy link
Contributor

@TinaHeiligers TinaHeiligers left a comment

Choose a reason for hiding this comment

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

The code looks fine for fixing the bug.
I pulled the PR, tested locally following the instructions and it works as intended.

BTW, I couldn't reproduce the bug when using search queries to create saved queries.

@TinaHeiligers TinaHeiligers self-requested a review October 12, 2023 22:54
Copy link
Contributor

@TinaHeiligers TinaHeiligers left a comment

Choose a reason for hiding this comment

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

LGTM

@jeramysoucy
Copy link
Contributor Author

jeramysoucy commented Oct 13, 2023

I couldn't reproduce the bug when using search queries to create saved queries.

@TinaHeiligers You could not reproduce the issue in the main branch? If the issue was being able to create a saved query, I just slightly updated the instructions. The save query option doesn't appear unless there is a "filter pill" defined.

Copy link
Contributor

@kc13greiner kc13greiner left a comment

Choose a reason for hiding this comment

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

LGTM; ++ nice comments!

@kibana-ci
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

Metrics [docs]

Public APIs missing exports

Total count of every type that is part of your API that should be exported but is not. This will cause broken links in the API documentation system. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats exports for more detailed information.

id before after diff
@kbn/core-saved-objects-api-server 1 2 +1

History

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

@jeramysoucy jeramysoucy requested a review from nickpeihl October 16, 2023 14:47
Copy link
Member

@nickpeihl nickpeihl 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 resolving this!

verified several scenarios of sharing and copying saved objects across spaces with users having varying levels of permissions.

@jeramysoucy jeramysoucy merged commit 460f5e3 into elastic:main Oct 16, 2023
kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Oct 16, 2023
Closes elastic#168049

## Summary

This PR adjusts the KQL filters when collecting references to saved
objects for the purpose of updating their spaces (i.e. sharing saved
objects to spaces). An additional filter is added to specifically
exclude ID -> ID matches - an ID match would mean the object has already
been shared to the destination space and there is no conflict. Filters
to match the shared object's ID or origin ID to the destination space's
objects' origin ID, and to match the shared object's origin ID to the
destination space's objects' IDs remain in place to properly check for
conflicts with potential copies.

### Manual Testing
- Create 2 spaces: A and B
- Add a sample data set (e.g. flight) to space A
- In Discover, create a saved query called "s1" with a filter pill that
uses the sample data logs data view
- Create another saved query called "s2" with a filter pill that uses
the sample data logs data view
- Go to `Stack Management->Saved` Objects and share the "s1" query to
space B
- Now share the "s2" query to space B. From the main branch you will see
that there is a conflict that disallows sharing the second query. This
is because it is also attempting to share the referenced data view,
which is already in space B. However, this should not be a conflict -
from this PR you will be able to successfully share both queries.

### Automated Testing
-
packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/utils/find_shared_origin_objects.test.ts
-
packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/internals/collect_multi_namespace_references.test.ts
-
x-pack/test/spaces_api_integration/common/suites/get_shareable_references.ts

---------

Co-authored-by: kibanamachine <[email protected]>
(cherry picked from commit 460f5e3)
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.11

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

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Oct 16, 2023
…9000)

# Backport

This will backport the following commits from `main` to `8.11`:
- [Fixes conflict checks for sharing saved objects
(#168655)](#168655)

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

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

<!--BACKPORT [{"author":{"name":"Jeramy
Soucy","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-10-16T16:18:19Z","message":"Fixes
conflict checks for sharing saved objects (#168655)\n\nCloses
#168049\r\n\r\n## Summary\r\n\r\nThis PR adjusts the KQL filters when
collecting references to saved\r\nobjects for the purpose of updating
their spaces (i.e. sharing saved\r\nobjects to spaces). An additional
filter is added to specifically\r\nexclude ID -> ID matches - an ID
match would mean the object has already\r\nbeen shared to the
destination space and there is no conflict. Filters\r\nto match the
shared object's ID or origin ID to the destination space's\r\nobjects'
origin ID, and to match the shared object's origin ID to
the\r\ndestination space's objects' IDs remain in place to properly
check for\r\nconflicts with potential copies.\r\n\r\n### Manual
Testing\r\n- Create 2 spaces: A and B\r\n- Add a sample data set (e.g.
flight) to space A\r\n- In Discover, create a saved query called \"s1\"
with a filter pill that\r\nuses the sample data logs data view\r\n-
Create another saved query called \"s2\" with a filter pill that
uses\r\nthe sample data logs data view\r\n- Go to `Stack
Management->Saved` Objects and share the \"s1\" query to\r\nspace B\r\n-
Now share the \"s2\" query to space B. From the main branch you will
see\r\nthat there is a conflict that disallows sharing the second query.
This\r\nis because it is also attempting to share the referenced data
view,\r\nwhich is already in space B. However, this should not be a
conflict -\r\nfrom this PR you will be able to successfully share both
queries.\r\n\r\n### Automated
Testing\r\n-\r\npackages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/utils/find_shared_origin_objects.test.ts\r\n-\r\npackages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/internals/collect_multi_namespace_references.test.ts\r\n-\r\nx-pack/test/spaces_api_integration/common/suites/get_shareable_references.ts\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"460f5e333631500b2d7984c212ab4d15f5ca4d8e","branchLabelMapping":{"^v8.12.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Security","release_note:skip","backport:prev-minor","v8.11.0","v8.12.0"],"number":168655,"url":"https://github.com/elastic/kibana/pull/168655","mergeCommit":{"message":"Fixes
conflict checks for sharing saved objects (#168655)\n\nCloses
#168049\r\n\r\n## Summary\r\n\r\nThis PR adjusts the KQL filters when
collecting references to saved\r\nobjects for the purpose of updating
their spaces (i.e. sharing saved\r\nobjects to spaces). An additional
filter is added to specifically\r\nexclude ID -> ID matches - an ID
match would mean the object has already\r\nbeen shared to the
destination space and there is no conflict. Filters\r\nto match the
shared object's ID or origin ID to the destination space's\r\nobjects'
origin ID, and to match the shared object's origin ID to
the\r\ndestination space's objects' IDs remain in place to properly
check for\r\nconflicts with potential copies.\r\n\r\n### Manual
Testing\r\n- Create 2 spaces: A and B\r\n- Add a sample data set (e.g.
flight) to space A\r\n- In Discover, create a saved query called \"s1\"
with a filter pill that\r\nuses the sample data logs data view\r\n-
Create another saved query called \"s2\" with a filter pill that
uses\r\nthe sample data logs data view\r\n- Go to `Stack
Management->Saved` Objects and share the \"s1\" query to\r\nspace B\r\n-
Now share the \"s2\" query to space B. From the main branch you will
see\r\nthat there is a conflict that disallows sharing the second query.
This\r\nis because it is also attempting to share the referenced data
view,\r\nwhich is already in space B. However, this should not be a
conflict -\r\nfrom this PR you will be able to successfully share both
queries.\r\n\r\n### Automated
Testing\r\n-\r\npackages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/utils/find_shared_origin_objects.test.ts\r\n-\r\npackages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/internals/collect_multi_namespace_references.test.ts\r\n-\r\nx-pack/test/spaces_api_integration/common/suites/get_shareable_references.ts\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"460f5e333631500b2d7984c212ab4d15f5ca4d8e"}},"sourceBranch":"main","suggestedTargetBranches":["8.11"],"targetPullRequestStates":[{"branch":"8.11","label":"v8.11.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.12.0","labelRegex":"^v8.12.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/168655","number":168655,"mergeCommit":{"message":"Fixes
conflict checks for sharing saved objects (#168655)\n\nCloses
#168049\r\n\r\n## Summary\r\n\r\nThis PR adjusts the KQL filters when
collecting references to saved\r\nobjects for the purpose of updating
their spaces (i.e. sharing saved\r\nobjects to spaces). An additional
filter is added to specifically\r\nexclude ID -> ID matches - an ID
match would mean the object has already\r\nbeen shared to the
destination space and there is no conflict. Filters\r\nto match the
shared object's ID or origin ID to the destination space's\r\nobjects'
origin ID, and to match the shared object's origin ID to
the\r\ndestination space's objects' IDs remain in place to properly
check for\r\nconflicts with potential copies.\r\n\r\n### Manual
Testing\r\n- Create 2 spaces: A and B\r\n- Add a sample data set (e.g.
flight) to space A\r\n- In Discover, create a saved query called \"s1\"
with a filter pill that\r\nuses the sample data logs data view\r\n-
Create another saved query called \"s2\" with a filter pill that
uses\r\nthe sample data logs data view\r\n- Go to `Stack
Management->Saved` Objects and share the \"s1\" query to\r\nspace B\r\n-
Now share the \"s2\" query to space B. From the main branch you will
see\r\nthat there is a conflict that disallows sharing the second query.
This\r\nis because it is also attempting to share the referenced data
view,\r\nwhich is already in space B. However, this should not be a
conflict -\r\nfrom this PR you will be able to successfully share both
queries.\r\n\r\n### Automated
Testing\r\n-\r\npackages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/utils/find_shared_origin_objects.test.ts\r\n-\r\npackages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/internals/collect_multi_namespace_references.test.ts\r\n-\r\nx-pack/test/spaces_api_integration/common/suites/get_shareable_references.ts\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<[email protected]>","sha":"460f5e333631500b2d7984c212ab4d15f5ca4d8e"}}]}]
BACKPORT-->

Co-authored-by: Jeramy Soucy <[email protected]>
dej611 pushed a commit to dej611/kibana that referenced this pull request Oct 17, 2023
Closes elastic#168049

## Summary

This PR adjusts the KQL filters when collecting references to saved
objects for the purpose of updating their spaces (i.e. sharing saved
objects to spaces). An additional filter is added to specifically
exclude ID -> ID matches - an ID match would mean the object has already
been shared to the destination space and there is no conflict. Filters
to match the shared object's ID or origin ID to the destination space's
objects' origin ID, and to match the shared object's origin ID to the
destination space's objects' IDs remain in place to properly check for
conflicts with potential copies.

### Manual Testing
- Create 2 spaces: A and B
- Add a sample data set (e.g. flight) to space A
- In Discover, create a saved query called "s1" with a filter pill that
uses the sample data logs data view
- Create another saved query called "s2" with a filter pill that uses
the sample data logs data view
- Go to `Stack Management->Saved` Objects and share the "s1" query to
space B
- Now share the "s2" query to space B. From the main branch you will see
that there is a conflict that disallows sharing the second query. This
is because it is also attempting to share the referenced data view,
which is already in space B. However, this should not be a conflict -
from this PR you will be able to successfully share both queries.

### Automated Testing
-
packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/utils/find_shared_origin_objects.test.ts
-
packages/core/saved-objects/core-saved-objects-api-server-internal/src/lib/apis/internals/collect_multi_namespace_references.test.ts
-
x-pack/test/spaces_api_integration/common/suites/get_shareable_references.ts

---------

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
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) release_note:skip Skip the PR/issue when compiling release notes Team:Security Team focused on: Auth, Users, Roles, Spaces, Audit Logging, and more! v8.11.0 v8.12.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can not share multiple saved queries that use the same data view
7 participants