Skip to content

Commit

Permalink
Address pr comments
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Mar 1, 2024
1 parent e5430b0 commit 898fe4c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ export {
Principals,
TransformedPermission,
PrincipalType,
Permissions,
} from './saved_objects';

export {
Expand Down
31 changes: 17 additions & 14 deletions src/plugins/workspace/server/permission_control/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Principals,
SavedObject,
WORKSPACE_TYPE,
Permissions,
} from '../../../../core/server';
import { WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID } from '../../common/constants';
import { getPrincipalsFromRequest } from '../utils';
Expand Down Expand Up @@ -84,7 +85,7 @@ export class SavedObjectsPermissionControl {
SavedObject<unknown>,
'id' | 'type' | 'workspaces' | 'permissions'
>> = [];
const hasAllPermission = savedObjects.every((savedObject) => {
const hasPermissionToAllObjects = savedObjects.every((savedObject) => {
// for object that doesn't contain ACL like config, return true
if (!savedObject.permissions) {
return true;
Expand All @@ -97,10 +98,10 @@ export class SavedObjectsPermissionControl {
}
return hasPermission;
});
if (!hasAllPermission) {
if (!hasPermissionToAllObjects) {
this.logNotPermitted(notPermittedSavedObjects, principals, permissionModes);
}
return hasAllPermission;
return hasPermissionToAllObjects;
}

/**
Expand Down Expand Up @@ -136,7 +137,12 @@ export class SavedObjectsPermissionControl {
}

const principals = getPrincipalsFromRequest(request);
let savedObjectsBasicInfo: any[] = [];
const deniedObjects: Array<
Pick<SavedObjectsBulkGetObject, 'id' | 'type'> & {
workspaces?: string[];
permissions?: Permissions;
}
> = [];
const hasAllPermission = savedObjectsGet.every((item) => {
// for object that doesn't contain ACL like config, return true
if (!item.permissions) {
Expand All @@ -145,15 +151,12 @@ export class SavedObjectsPermissionControl {
const aclInstance = new ACL(item.permissions);
const hasPermission = aclInstance.hasPermission(permissionModes, principals);
if (!hasPermission) {
savedObjectsBasicInfo = [
...savedObjectsBasicInfo,
{
id: item.id,
type: item.type,
workspaces: item.workspaces,
permissions: item.permissions,
},
];
deniedObjects.push({
id: item.id,
type: item.type,
workspaces: item.workspaces,
permissions: item.permissions,
});
}
return hasPermission;
});
Expand All @@ -162,7 +165,7 @@ export class SavedObjectsPermissionControl {
`Authorization failed, principals: ${JSON.stringify(
principals
)} has no [${permissionModes}] permissions on the requested saved object: ${JSON.stringify(
savedObjectsBasicInfo
deniedObjects
)}`
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ export class WorkspacePlugin implements Plugin<{}, {}> {

await this.client.setup(core);

this.proxyWorkspaceTrafficToRealHandler(core);
this.logger.info('Workspace permission control enabled:' + isPermissionControlEnabled);
if (isPermissionControlEnabled) {
this.proxyWorkspaceTrafficToRealHandler(core);
this.permissionControl = new SavedObjectsPermissionControl(this.logger);

this.workspaceSavedObjectsClientWrapper = new WorkspaceSavedObjectsClientWrapper(
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/workspace/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { schema } from '@osd/config-schema';
import { CoreSetup, Logger, ensureRawRequest } from '../../../../core/server';
import { WorkspacePermissionMode } from '../../common/constants';
import { IWorkspaceClientImpl, WorkspacePermissionItem } from '../types';
import { AuthInfo, IWorkspaceClientImpl, WorkspacePermissionItem } from '../types';

const WORKSPACES_API_BASE_URL = '/api/workspaces';

Expand Down Expand Up @@ -125,7 +125,7 @@ export function registerRoutes({
router.handleLegacyErrors(async (context, req, res) => {
const { attributes, permissions: permissionsInRequest } = req.body;
const rawRequest = ensureRawRequest(req);
const authInfo = rawRequest?.auth?.credentials?.authInfo as { user_name?: string } | null;
const authInfo = rawRequest?.auth?.credentials?.authInfo as AuthInfo | null;
let permissions: WorkspacePermissionItem[] = [];
if (permissionsInRequest) {
permissions = Array.isArray(permissionsInRequest)
Expand Down

0 comments on commit 898fe4c

Please sign in to comment.