Skip to content

Commit

Permalink
[Saved Object Aggregation View] Use namespace registry to add tenant …
Browse files Browse the repository at this point in the history
…filter (#1169)

Signed-off-by: Craig Perkins <[email protected]>
Signed-off-by: Chang Liu <[email protected]>
Co-authored-by: Chang Liu <[email protected]>
Co-authored-by: Darshit Chanpura <[email protected]>
  • Loading branch information
3 people authored Nov 2, 2022
1 parent f815e3c commit 24807bb
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 14 deletions.
28 changes: 28 additions & 0 deletions public/apps/configuration/utils/tenant-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
import { httpDelete, httpGet, httpPost } from './request-utils';
import { getResourceUrl } from './resource-utils';
import {
DEFAULT_TENANT,
GLOBAL_TENANT_RENDERING_TEXT,
GLOBAL_TENANT_SYMBOL,
globalTenantName,
Expand Down Expand Up @@ -181,6 +182,33 @@ export function transformRoleTenantPermissions(
}));
}

export function getNamespacesToRegister(accountInfo: any) {
const tenants = accountInfo.tenants || {};
const availableTenantNames = Object.keys(tenants!);
const namespacesToRegister = availableTenantNames.map((tenant) => {
if (tenant === globalTenantName) {
return {
id: GLOBAL_USER_DICT.Value,
name: GLOBAL_USER_DICT.Label,
};
} else if (tenant === accountInfo.user_name) {
return {
id: `${PRIVATE_USER_DICT.Value}${accountInfo.user_name}`,
name: PRIVATE_USER_DICT.Label,
};
}
return {
id: tenant,
name: tenant,
};
});
namespacesToRegister.push({
id: DEFAULT_TENANT,
name: DEFAULT_TENANT,
});
return namespacesToRegister;
}

export const tenantColumn = {
id: 'tenant_column',
euiColumn: {
Expand Down
60 changes: 60 additions & 0 deletions public/apps/configuration/utils/test/tenant-utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
transformRoleTenantPermissionData,
getTenantPermissionType,
transformRoleTenantPermissions,
getNamespacesToRegister,
} from '../tenant-utils';
import {
RoleViewTenantInvalidText,
Expand Down Expand Up @@ -282,4 +283,63 @@ describe('Tenant list utils', () => {
expect(result[0]).toMatchObject(expectedRoleTenantPermissionView);
});
});

describe('get list of namespaces to register', () => {
it('resolves to list of namespaces with a custom tenant', () => {
const authInfo = {
user_name: 'user1',
tenants: {
global_tenant: true,
user1_tenant: true,
user1: true,
},
};
const expectedNamespaces = [
{
id: GLOBAL_USER_DICT.Value,
name: GLOBAL_USER_DICT.Label,
},
{
id: 'user1_tenant',
name: 'user1_tenant',
},
{
id: `${PRIVATE_USER_DICT.Value}user1`,
name: PRIVATE_USER_DICT.Label,
},
{
id: 'default',
name: 'default',
},
];
const result = getNamespacesToRegister(authInfo);
expect(result).toMatchObject(expectedNamespaces);
});

it('resolves to list of namespaces without a custom tenant', () => {
const authInfo = {
user_name: 'user1',
tenants: {
global_tenant: true,
user1: true,
},
};
const expectedNamespaces = [
{
id: GLOBAL_USER_DICT.Value,
name: GLOBAL_USER_DICT.Label,
},
{
id: `${PRIVATE_USER_DICT.Value}user1`,
name: PRIVATE_USER_DICT.Label,
},
{
id: 'default',
name: 'default',
},
];
const result = getNamespacesToRegister(authInfo);
expect(result).toMatchObject(expectedNamespaces);
});
});
});
9 changes: 8 additions & 1 deletion public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {
} from './types';
import { addTenantToShareURL } from './services/shared-link';
import { interceptError } from './utils/logout-utils';
import { tenantColumn } from './apps/configuration/utils/tenant-utils';
import { tenantColumn, getNamespacesToRegister } from './apps/configuration/utils/tenant-utils';

async function hasApiPermission(core: CoreSetup): Promise<boolean | undefined> {
try {
Expand Down Expand Up @@ -157,6 +157,13 @@ export class SecurityPlugin
deps.savedObjectsManagement.columns.register(
(tenantColumn as unknown) as SavedObjectsManagementColumn<string>
);
if (!!accountInfo) {
const namespacesToRegister = getNamespacesToRegister(accountInfo);
deps.savedObjectsManagement.namespaces.registerAlias('Tenant');
namespacesToRegister.forEach((ns) => {
deps.savedObjectsManagement.namespaces.register(ns as SavedObjectsManagementNamespace);
});
}
}

// Return methods that should be available to other plugins
Expand Down
34 changes: 21 additions & 13 deletions server/saved_objects/saved_objects_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,32 @@ export class SecuritySavedObjectsClientWrapper {
availableTenantNames.splice(index, 1);
}
}
const typeToNamespacesMap: any = {};
if (isPrivateTenant(selectedTenant!)) {
namespaceValue = selectedTenant! + username;
}
const searchTypes = Array.isArray(options.type) ? options.type : [options.type];
searchTypes.forEach((t) => {
if ('namespaces' in options) {
typeToNamespacesMap[t] = options.namespaces;
} else {
typeToNamespacesMap[t] = availableTenantNames;
if (!!options.namespaces) {
const namespacesToInclude = Array.isArray(options.namespaces)
? options.namespaces
: [options.namespaces];
const typeToNamespacesMap: any = {};
const searchTypes = Array.isArray(options.type) ? options.type : [options.type];
searchTypes.forEach((t) => {
typeToNamespacesMap[t] = namespacesToInclude;
});
if (searchTypes.includes('config')) {
if (namespacesToInclude.includes(namespaceValue)) {
typeToNamespacesMap.config = [namespaceValue];
} else {
delete typeToNamespacesMap.config;
}
}
});
if ('config' in typeToNamespacesMap) {
typeToNamespacesMap.config = [namespaceValue];

options.typeToNamespacesMap = new Map(Object.entries(typeToNamespacesMap));
options.type = '';
options.namespaces = [];
} else {
options.namespaces = [namespaceValue];
}
options.typeToNamespacesMap = new Map(Object.entries(typeToNamespacesMap));
options.type = '';
options.namespaces = [];

return await wrapperOptions.client.find(options);
};
Expand Down

0 comments on commit 24807bb

Please sign in to comment.