-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Data Source]Add data source permission wrapper and dataSourceAdmin r…
…ole (#7959) * Add data source permission wrapper Signed-off-by: yubonluo <[email protected]> * Changeset file for PR #7959 created/updated * optimize the config schema Signed-off-by: yubonluo <[email protected]> * optimize the code Signed-off-by: yubonluo <[email protected]> * optimize the code Signed-off-by: yubonluo <[email protected]> * add some coments and optimize the logic Signed-off-by: yubonluo <[email protected]> * optimize the code Signed-off-by: yubonluo <[email protected]> * add unit tests Signed-off-by: yubonluo <[email protected]> * fix test error Signed-off-by: yubonluo <[email protected]> * optimize the code Signed-off-by: yubonluo <[email protected]> * optimize the code Signed-off-by: yubonluo <[email protected]> * Move some logic to workspace wrapper Signed-off-by: yubonluo <[email protected]> * delete useless code Signed-off-by: yubonluo <[email protected]> * delete useless code Signed-off-by: yubonluo <[email protected]> --------- Signed-off-by: yubonluo <[email protected]> Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> (cherry picked from commit bc49b8c) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
c47faf2
commit bbaf5aa
Showing
23 changed files
with
846 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
feat: | ||
- [Data source] Add data source permission wrapper and dataSourceAdmin role ([#7959](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7959)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { AuthStatus } from '../http/auth_state_storage'; | ||
import { httpServerMock, httpServiceMock } from '../mocks'; | ||
import { getPrincipalsFromRequest } from './auth_info'; | ||
|
||
describe('utils', () => { | ||
const mockAuth = httpServiceMock.createAuth(); | ||
it('should return empty map when request do not have authentication', () => { | ||
const mockRequest = httpServerMock.createOpenSearchDashboardsRequest(); | ||
mockAuth.get.mockReturnValueOnce({ | ||
status: AuthStatus.unknown, | ||
state: { | ||
authInfo: { | ||
user_name: 'bar', | ||
backend_roles: ['foo'], | ||
}, | ||
}, | ||
}); | ||
const result = getPrincipalsFromRequest(mockRequest, mockAuth); | ||
expect(result).toEqual({}); | ||
}); | ||
|
||
it('should return normally when request has authentication', () => { | ||
const mockRequest = httpServerMock.createOpenSearchDashboardsRequest(); | ||
mockAuth.get.mockReturnValueOnce({ | ||
status: AuthStatus.authenticated, | ||
state: { | ||
authInfo: { | ||
user_name: 'bar', | ||
backend_roles: ['foo'], | ||
}, | ||
}, | ||
}); | ||
const result = getPrincipalsFromRequest(mockRequest, mockAuth); | ||
expect(result.users).toEqual(['bar']); | ||
expect(result.groups).toEqual(['foo']); | ||
}); | ||
|
||
it('should throw error when request is not authenticated', () => { | ||
const mockRequest = httpServerMock.createOpenSearchDashboardsRequest(); | ||
mockAuth.get.mockReturnValueOnce({ | ||
status: AuthStatus.unauthenticated, | ||
state: {}, | ||
}); | ||
expect(() => getPrincipalsFromRequest(mockRequest, mockAuth)).toThrow('NOT_AUTHORIZED'); | ||
}); | ||
|
||
it('should throw error when authentication status is not expected', () => { | ||
const mockRequest = httpServerMock.createOpenSearchDashboardsRequest(); | ||
mockAuth.get.mockReturnValueOnce({ | ||
// @ts-expect-error | ||
status: 'foo', | ||
state: {}, | ||
}); | ||
expect(() => getPrincipalsFromRequest(mockRequest, mockAuth)).toThrow( | ||
'UNEXPECTED_AUTHORIZATION_STATUS' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { AuthStatus } from '../http/auth_state_storage'; | ||
import { OpenSearchDashboardsRequest } from '../http/router'; | ||
import { HttpAuth } from '../http/types'; | ||
import { PrincipalType, Principals } from '../saved_objects/permission_control/acl'; | ||
|
||
export interface AuthInfo { | ||
backend_roles?: string[]; | ||
user_name?: string; | ||
} | ||
|
||
export const getPrincipalsFromRequest = ( | ||
request: OpenSearchDashboardsRequest, | ||
auth?: HttpAuth | ||
): Principals => { | ||
const payload: Principals = {}; | ||
const authInfoResp = auth?.get(request); | ||
if (authInfoResp?.status === AuthStatus.unknown) { | ||
/** | ||
* Login user have access to all the workspaces when no authentication is presented. | ||
*/ | ||
return payload; | ||
} | ||
|
||
if (authInfoResp?.status === AuthStatus.authenticated) { | ||
const authState = authInfoResp?.state as { authInfo: AuthInfo } | null; | ||
if (authState?.authInfo?.backend_roles) { | ||
payload[PrincipalType.Groups] = authState.authInfo.backend_roles; | ||
} | ||
if (authState?.authInfo?.user_name) { | ||
payload[PrincipalType.Users] = [authState.authInfo.user_name]; | ||
} | ||
return payload; | ||
} | ||
|
||
if (authInfoResp?.status === AuthStatus.unauthenticated) { | ||
throw new Error('NOT_AUTHORIZED'); | ||
} | ||
|
||
throw new Error('UNEXPECTED_AUTHORIZATION_STATUS'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.