[8.8] Handles non-existing objects in _copy_saved_objects API call (#158036) (#158399)

# Backport

This will backport the following commits from `main` to `8.8`:
- [Handles non-existing objects in _copy_saved_objects API call
(#158036)](https://github.com/elastic/kibana/pull/158036)

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

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

<!--BACKPORT [{"author":{"name":"Jeramy
Soucy","email":"jeramy.soucy@elastic.co"},"sourceCommit":{"committedDate":"2023-05-24T16:20:46Z","message":"Handles
non-existing objects in _copy_saved_objects API call (#158036)\n\nCloses
#156791\r\n\r\n## Summary\r\n\r\nThis PR implements catching the error
thrown by the\r\nsaved_objects_exporter when an object is not found, and
responding with\r\na detailed 404 (\"Not Found\") rather than a generic
500 (\"Internal Server\r\nError\") response message.\r\n\r\n### Example
Response:\r\n```\r\n{\r\n \"statusCode\": 404,\r\n \"error\": \"Not
Found\",\r\n \"message\": \"Saved objects not found\",\r\n
\"attributes\": {\r\n \"objects\": [\r\n {\r\n \"id\":
\"7adfa750-4c81-11e8-b3d7-01146121b73d\",\r\n \"type\":
\"dashboard\"\r\n },\r\n {\r\n \"id\":
\"571aaf70-4c88-11e8-b3d7-01146121b73d\",\r\n \"type\": \"search\"\r\n
},\r\n {\r\n \"id\": \"d3d7af60-4c81-11e8-b3d7-01146121b73d\",\r\n
\"type\": \"index-pattern\"\r\n }\r\n ]\r\n }\r\n}\r\n```\r\n\r\n###
Testing\r\n\r\n1. Install sample flight data, find the id of the flights
dashboard SO\r\n2. Create an additional space 'b'\r\n3. Issue a request
to copy saved objects to space b\r\n```\r\nPOST
kbn:/api/spaces/_copy_saved_objects\r\n{\r\n \"spaces\": [\r\n \"b\"\r\n
],\r\n \"objects\": [\r\n {\r\n \"type\": \"dashboard\",\r\n \"id\":
\"7adfa750-4c81-11e8-b3d7-01146121b73e\"\r\n },\r\n {\r\n \"type\":
\"dashboard\",\r\n \"id\": \"7adfa750-4c81-11e8-b3d7-01146121b73f\"\r\n
},\r\n {\r\n \"type\": \"dashboard\",\r\n \"id\":
\"7adfa750-4c81-11e8-b3d7-01146121b73g\"\r\n }\r\n ]\r\n}\r\n```\r\n4.
Verify response\r\n```\r\n{\r\n \"statusCode\": 404,\r\n \"error\":
\"Not Found\",\r\n \"message\": \"Saved objects not found\",\r\n
\"attributes\": {\r\n \"objects\": [\r\n {\r\n \"type\":
\"dashboard\",\r\n \"id\": \"7adfa750-4c81-11e8-b3d7-01146121b73e\"\r\n
},\r\n {\r\n \"type\": \"dashboard\",\r\n \"id\":
\"7adfa750-4c81-11e8-b3d7-01146121b73f\"\r\n },\r\n {\r\n \"type\":
\"dashboard\",\r\n \"id\": \"7adfa750-4c81-11e8-b3d7-01146121b73g\"\r\n
}\r\n ]\r\n }\r\n}\r\n```\r\n5. Issue a request to copy the flights
dashboard SO\r\n6. Verify the usual response (200, missing
references)\r\n7. Issue a malformed request to copy an SO\r\n8. Verify
status 400 response with
details","sha":"1589547b6f143c45c782ed857902026af2e7e38a","branchLabelMapping":{"^v8.9.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Security","Feature:Saved
Objects","Feature:Security/Spaces","backport:all-open","v8.9.0"],"number":158036,"url":"https://github.com/elastic/kibana/pull/158036","mergeCommit":{"message":"Handles
non-existing objects in _copy_saved_objects API call (#158036)\n\nCloses
#156791\r\n\r\n## Summary\r\n\r\nThis PR implements catching the error
thrown by the\r\nsaved_objects_exporter when an object is not found, and
responding with\r\na detailed 404 (\"Not Found\") rather than a generic
500 (\"Internal Server\r\nError\") response message.\r\n\r\n### Example
Response:\r\n```\r\n{\r\n \"statusCode\": 404,\r\n \"error\": \"Not
Found\",\r\n \"message\": \"Saved objects not found\",\r\n
\"attributes\": {\r\n \"objects\": [\r\n {\r\n \"id\":
\"7adfa750-4c81-11e8-b3d7-01146121b73d\",\r\n \"type\":
\"dashboard\"\r\n },\r\n {\r\n \"id\":
\"571aaf70-4c88-11e8-b3d7-01146121b73d\",\r\n \"type\": \"search\"\r\n
},\r\n {\r\n \"id\": \"d3d7af60-4c81-11e8-b3d7-01146121b73d\",\r\n
\"type\": \"index-pattern\"\r\n }\r\n ]\r\n }\r\n}\r\n```\r\n\r\n###
Testing\r\n\r\n1. Install sample flight data, find the id of the flights
dashboard SO\r\n2. Create an additional space 'b'\r\n3. Issue a request
to copy saved objects to space b\r\n```\r\nPOST
kbn:/api/spaces/_copy_saved_objects\r\n{\r\n \"spaces\": [\r\n \"b\"\r\n
],\r\n \"objects\": [\r\n {\r\n \"type\": \"dashboard\",\r\n \"id\":
\"7adfa750-4c81-11e8-b3d7-01146121b73e\"\r\n },\r\n {\r\n \"type\":
\"dashboard\",\r\n \"id\": \"7adfa750-4c81-11e8-b3d7-01146121b73f\"\r\n
},\r\n {\r\n \"type\": \"dashboard\",\r\n \"id\":
\"7adfa750-4c81-11e8-b3d7-01146121b73g\"\r\n }\r\n ]\r\n}\r\n```\r\n4.
Verify response\r\n```\r\n{\r\n \"statusCode\": 404,\r\n \"error\":
\"Not Found\",\r\n \"message\": \"Saved objects not found\",\r\n
\"attributes\": {\r\n \"objects\": [\r\n {\r\n \"type\":
\"dashboard\",\r\n \"id\": \"7adfa750-4c81-11e8-b3d7-01146121b73e\"\r\n
},\r\n {\r\n \"type\": \"dashboard\",\r\n \"id\":
\"7adfa750-4c81-11e8-b3d7-01146121b73f\"\r\n },\r\n {\r\n \"type\":
\"dashboard\",\r\n \"id\": \"7adfa750-4c81-11e8-b3d7-01146121b73g\"\r\n
}\r\n ]\r\n }\r\n}\r\n```\r\n5. Issue a request to copy the flights
dashboard SO\r\n6. Verify the usual response (200, missing
references)\r\n7. Issue a malformed request to copy an SO\r\n8. Verify
status 400 response with
details","sha":"1589547b6f143c45c782ed857902026af2e7e38a"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.9.0","labelRegex":"^v8.9.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/158036","number":158036,"mergeCommit":{"message":"Handles
non-existing objects in _copy_saved_objects API call (#158036)\n\nCloses
#156791\r\n\r\n## Summary\r\n\r\nThis PR implements catching the error
thrown by the\r\nsaved_objects_exporter when an object is not found, and
responding with\r\na detailed 404 (\"Not Found\") rather than a generic
500 (\"Internal Server\r\nError\") response message.\r\n\r\n### Example
Response:\r\n```\r\n{\r\n \"statusCode\": 404,\r\n \"error\": \"Not
Found\",\r\n \"message\": \"Saved objects not found\",\r\n
\"attributes\": {\r\n \"objects\": [\r\n {\r\n \"id\":
\"7adfa750-4c81-11e8-b3d7-01146121b73d\",\r\n \"type\":
\"dashboard\"\r\n },\r\n {\r\n \"id\":
\"571aaf70-4c88-11e8-b3d7-01146121b73d\",\r\n \"type\": \"search\"\r\n
},\r\n {\r\n \"id\": \"d3d7af60-4c81-11e8-b3d7-01146121b73d\",\r\n
\"type\": \"index-pattern\"\r\n }\r\n ]\r\n }\r\n}\r\n```\r\n\r\n###
Testing\r\n\r\n1. Install sample flight data, find the id of the flights
dashboard SO\r\n2. Create an additional space 'b'\r\n3. Issue a request
to copy saved objects to space b\r\n```\r\nPOST
kbn:/api/spaces/_copy_saved_objects\r\n{\r\n \"spaces\": [\r\n \"b\"\r\n
],\r\n \"objects\": [\r\n {\r\n \"type\": \"dashboard\",\r\n \"id\":
\"7adfa750-4c81-11e8-b3d7-01146121b73e\"\r\n },\r\n {\r\n \"type\":
\"dashboard\",\r\n \"id\": \"7adfa750-4c81-11e8-b3d7-01146121b73f\"\r\n
},\r\n {\r\n \"type\": \"dashboard\",\r\n \"id\":
\"7adfa750-4c81-11e8-b3d7-01146121b73g\"\r\n }\r\n ]\r\n}\r\n```\r\n4.
Verify response\r\n```\r\n{\r\n \"statusCode\": 404,\r\n \"error\":
\"Not Found\",\r\n \"message\": \"Saved objects not found\",\r\n
\"attributes\": {\r\n \"objects\": [\r\n {\r\n \"type\":
\"dashboard\",\r\n \"id\": \"7adfa750-4c81-11e8-b3d7-01146121b73e\"\r\n
},\r\n {\r\n \"type\": \"dashboard\",\r\n \"id\":
\"7adfa750-4c81-11e8-b3d7-01146121b73f\"\r\n },\r\n {\r\n \"type\":
\"dashboard\",\r\n \"id\": \"7adfa750-4c81-11e8-b3d7-01146121b73g\"\r\n
}\r\n ]\r\n }\r\n}\r\n```\r\n5. Issue a request to copy the flights
dashboard SO\r\n6. Verify the usual response (200, missing
references)\r\n7. Issue a malformed request to copy an SO\r\n8. Verify
status 400 response with
details","sha":"1589547b6f143c45c782ed857902026af2e7e38a"}}]}]
BACKPORT-->

Co-authored-by: Jeramy Soucy <jeramy.soucy@elastic.co>
This commit is contained in:
Kibana Machine 2023-05-24 13:32:34 -04:00 committed by GitHub
parent 8bc6826e1e
commit 2135cf0ff9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 13 deletions

View file

@ -36,6 +36,15 @@ Updates one or more saved objects to add and/or remove them from specified space
`spacesToRemove`::
(Required, string array) The IDs of the spaces the specified objects should be removed from.
[[spaces-api-update-objects-spaces-response-codes]]
==== Response codes
`200`::
Indicates a successful call.
`404`::
Indicates that the request failed because one or more of the objects specified could not be found. A list of the unresolved objects are included in the 404 response attributes.
[role="child_attributes"]
[[spaces-api-update-objects-spaces-response-body]]
==== {api-response-body-title}

View file

@ -105,19 +105,35 @@ export function initCopyToSpacesApi(deps: ExternalRouteDeps) {
})
);
const copySavedObjectsToSpaces = copySavedObjectsToSpacesFactory(
startServices.savedObjects,
request
);
const sourceSpaceId = getSpacesService().getSpaceId(request);
const copyResponse = await copySavedObjectsToSpaces(sourceSpaceId, destinationSpaceIds, {
objects,
includeReferences,
overwrite,
createNewCopies,
compatibilityMode,
});
return response.ok({ body: copyResponse });
try {
const copySavedObjectsToSpaces = copySavedObjectsToSpacesFactory(
startServices.savedObjects,
request
);
const sourceSpaceId = getSpacesService().getSpaceId(request);
const copyResponse = await copySavedObjectsToSpaces(sourceSpaceId, destinationSpaceIds, {
objects,
includeReferences,
overwrite,
createNewCopies,
compatibilityMode,
});
return response.ok({ body: copyResponse });
} catch (e) {
if (e.type === 'object-fetch-error' && e.attributes?.objects) {
return response.notFound({
body: {
message: 'Saved objects not found',
attributes: {
objects: e.attributes?.objects.map((obj: SavedObjectIdentifier) => ({
id: obj.id,
type: obj.type,
})),
},
},
});
} else throw e;
}
})
);