[8.11] [Encrypted Saved Object] Fix namespaces option in decryption (#168588) (#168811)

# Backport

This will backport the following commits from `main` to `8.11`:
- [[Encrypted Saved Object] Fix namespaces option in decryption
(#168588)](https://github.com/elastic/kibana/pull/168588)

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

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

<!--BACKPORT
[{"author":{"name":"Shahzad","email":"shahzad31comp@gmail.com"},"sourceCommit":{"committedDate":"2023-10-13T08:04:57Z","message":"[Encrypted
Saved Object] Fix namespaces option in decryption
(#168588)\n\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"cdc77c9a8baadd2b75d82fd0726aef8d03c79d3b","branchLabelMapping":{"^v8.12.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Feature:Security/Encrypted
Saved
Objects","v8.11.0","v8.12.0"],"number":168588,"url":"https://github.com/elastic/kibana/pull/168588","mergeCommit":{"message":"[Encrypted
Saved Object] Fix namespaces option in decryption
(#168588)\n\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"cdc77c9a8baadd2b75d82fd0726aef8d03c79d3b"}},"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/168588","number":168588,"mergeCommit":{"message":"[Encrypted
Saved Object] Fix namespaces option in decryption
(#168588)\n\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"cdc77c9a8baadd2b75d82fd0726aef8d03c79d3b"}}]}]
BACKPORT-->

Co-authored-by: Shahzad <shahzad31comp@gmail.com>
This commit is contained in:
Kibana Machine 2023-10-13 07:34:57 -04:00 committed by GitHub
parent 10ab162d38
commit 6b005dd19c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 2 deletions

View file

@ -68,6 +68,7 @@ describe('#setupSavedObjects', () => {
type: 'known-type',
attributes: { attrOne: 'one', attrSecret: '*secret*' },
references: [],
namespaces: ['some-ns'],
};
mockSavedObjectsRepository.get.mockResolvedValue(mockSavedObject);
mockSavedObjectTypeRegistry.isSingleNamespace.mockReturnValue(true);
@ -101,6 +102,7 @@ describe('#setupSavedObjects', () => {
type: 'known-type',
attributes: { attrOne: 'one', attrSecret: '*secret*' },
references: [],
namespaces: ['some-ns2', 'some-ns'],
};
mockSavedObjectsRepository.get.mockResolvedValue(mockSavedObject);
mockSavedObjectTypeRegistry.isSingleNamespace.mockReturnValue(false);
@ -154,6 +156,7 @@ describe('#setupSavedObjects', () => {
type: 'known-type',
attributes: { attrOne: 'one', attrSecret: '*secret*' },
references: [],
namespaces: ['some-ns'],
};
mockSavedObjectsRepository.createPointInTimeFinder = jest.fn().mockReturnValue({
close: jest.fn(),
@ -320,5 +323,51 @@ describe('#setupSavedObjects', () => {
await finder.close();
expect(mockClose).toHaveBeenCalledTimes(1);
});
it('includes `namespace` for * find option', async () => {
const mockSavedObject: SavedObject = {
id: 'some-id',
type: 'known-type',
attributes: { attrOne: 'one', attrSecret: '*secret*' },
references: [],
namespaces: ['some-ns'],
};
mockSavedObjectsRepository.createPointInTimeFinder = jest.fn().mockReturnValue({
close: jest.fn(),
find: function* asyncGenerator() {
yield { saved_objects: [mockSavedObject] };
},
});
mockSavedObjectTypeRegistry.isSingleNamespace.mockReturnValue(true);
const finder = await setupContract().createPointInTimeFinderDecryptedAsInternalUser({
type: 'known-type',
namespaces: ['*'],
});
for await (const res of finder.find()) {
expect(res).toEqual({
saved_objects: [
{
...mockSavedObject,
attributes: { attrOne: 'one', attrSecret: 'secret' },
},
],
});
}
expect(mockEncryptedSavedObjectsService.decryptAttributes).toHaveBeenCalledTimes(1);
expect(mockEncryptedSavedObjectsService.decryptAttributes).toHaveBeenCalledWith(
{ type: mockSavedObject.type, id: mockSavedObject.id, namespace: 'some-ns' },
mockSavedObject.attributes
);
expect(mockSavedObjectsRepository.createPointInTimeFinder).toHaveBeenCalledTimes(1);
expect(mockSavedObjectsRepository.createPointInTimeFinder).toHaveBeenCalledWith(
{ type: 'known-type', namespaces: ['*'] },
undefined
);
});
});
});

View file

@ -118,7 +118,7 @@ export function setupSavedObjects({
{
type,
id,
namespace: getDescriptorNamespace(typeRegistry, type, options?.namespace),
namespace: getDescriptorNamespace(typeRegistry, type, savedObject.namespaces),
},
savedObject.attributes as Record<string, unknown>
)) as T,
@ -148,7 +148,7 @@ export function setupSavedObjects({
namespace: getDescriptorNamespace(
typeRegistry,
savedObject.type,
findOptions.namespaces
savedObject.namespaces
),
};