From e7f9d497ae0c1ae650d2f12d4bdd6289957bf9d3 Mon Sep 17 00:00:00 2001 From: Chang Liu Date: Wed, 2 Nov 2022 14:37:40 -0700 Subject: [PATCH] Move tenant-related utils to common folder (#1184) Signed-off-by: Chang Liu (cherry picked from commit f815e3c) --- common/index.ts | 19 +++++++++ .../apps/configuration/utils/tenant-utils.tsx | 42 +++++++------------ .../utils/test/tenant-utils.test.tsx | 2 +- server/auth/types/authentication_type.ts | 2 +- server/multitenancy/tenant_resolver.ts | 5 +-- server/saved_objects/saved_objects_wrapper.ts | 6 +-- 6 files changed, 41 insertions(+), 35 deletions(-) diff --git a/common/index.ts b/common/index.ts index b820a3992..535d94972 100644 --- a/common/index.ts +++ b/common/index.ts @@ -29,6 +29,13 @@ export const API_AUTH_LOGOUT = '/auth/logout'; export const ERROR_MISSING_ROLE_PATH = '/missing-role'; +export const GLOBAL_TENANT_SYMBOL = ''; +export const PRIVATE_TENANT_SYMBOL = '__user__'; +export const DEFAULT_TENANT = 'default'; +export const GLOBAL_TENANT_RENDERING_TEXT = 'Global'; +export const PRIVATE_TENANT_RENDERING_TEXT = 'Private'; +export const globalTenantName = 'global_tenant'; + export enum AuthType { BASIC = 'basicauth', OPEN_ID = 'openid', @@ -47,3 +54,15 @@ export function isValidResourceName(resourceName: string): boolean { const exp = new RegExp('[\\p{C}%]', 'u'); return !exp.test(resourceName) && resourceName.length > 0; } + +export function isPrivateTenant(selectedTenant: string | null) { + return selectedTenant !== null && selectedTenant === PRIVATE_TENANT_SYMBOL; +} + +export function isRenderingPrivateTenant(selectedTenant: string | null) { + return selectedTenant !== null && selectedTenant?.startsWith(PRIVATE_TENANT_SYMBOL); +} + +export function isGlobalTenant(selectedTenant: string | null) { + return selectedTenant !== null && selectedTenant === GLOBAL_TENANT_SYMBOL; +} diff --git a/public/apps/configuration/utils/tenant-utils.tsx b/public/apps/configuration/utils/tenant-utils.tsx index 8c50ffcda..3b782143f 100644 --- a/public/apps/configuration/utils/tenant-utils.tsx +++ b/public/apps/configuration/utils/tenant-utils.tsx @@ -18,31 +18,33 @@ import { map } from 'lodash'; import React from 'react'; import { i18n } from '@osd/i18n'; import { - API_ENDPOINT_TENANTS, API_ENDPOINT_MULTITENANCY, + API_ENDPOINT_TENANTS, RoleViewTenantInvalidText, + TENANT_READ_PERMISSION, + TENANT_WRITE_PERMISSION, } from '../constants'; import { DataObject, ObjectsMessage, - Tenant, - TenantUpdate, - TenantSelect, - RoleTenantPermissionView, + RoleTenantPermission, RoleTenantPermissionDetail, + RoleTenantPermissionView, + Tenant, TenantPermissionType, - RoleTenantPermission, + TenantSelect, + TenantUpdate, } from '../types'; -import { TENANT_READ_PERMISSION, TENANT_WRITE_PERMISSION } from '../constants'; import { httpDelete, httpGet, httpPost } from './request-utils'; import { getResourceUrl } from './resource-utils'; - -export const globalTenantName = 'global_tenant'; -export const GLOBAL_TENANT_SYMBOL = ''; -export const PRIVATE_TENANT_SYMBOL = '__user__'; -export const DEFAULT_TENANT = 'default'; -export const GLOBAL_TENANT_RENDERING_TEXT = 'Global'; -export const PRIVATE_TENANT_RENDERING_TEXT = 'Private'; +import { + GLOBAL_TENANT_RENDERING_TEXT, + GLOBAL_TENANT_SYMBOL, + globalTenantName, + isGlobalTenant, + isRenderingPrivateTenant, + PRIVATE_TENANT_RENDERING_TEXT, +} from '../../../../common'; export const GLOBAL_USER_DICT: { [key: string]: string } = { Label: 'Global', @@ -179,18 +181,6 @@ export function transformRoleTenantPermissions( })); } -export function isPrivateTenant(selectedTenant: string | null) { - return selectedTenant !== null && selectedTenant === PRIVATE_TENANT_SYMBOL; -} - -export function isRenderingPrivateTenant(selectedTenant: string | null) { - return selectedTenant !== null && selectedTenant?.startsWith(PRIVATE_TENANT_SYMBOL); -} - -export function isGlobalTenant(selectedTenant: string | null) { - return selectedTenant !== null && selectedTenant === GLOBAL_TENANT_SYMBOL; -} - export const tenantColumn = { id: 'tenant_column', euiColumn: { diff --git a/public/apps/configuration/utils/test/tenant-utils.test.tsx b/public/apps/configuration/utils/test/tenant-utils.test.tsx index c079026b5..4e61f510d 100644 --- a/public/apps/configuration/utils/test/tenant-utils.test.tsx +++ b/public/apps/configuration/utils/test/tenant-utils.test.tsx @@ -20,7 +20,6 @@ import { resolveTenantName, RESOLVED_GLOBAL_TENANT, RESOLVED_PRIVATE_TENANT, - globalTenantName, formatTenantName, transformRoleTenantPermissionData, getTenantPermissionType, @@ -32,6 +31,7 @@ import { TENANT_WRITE_PERMISSION, } from '../../constants'; import { TenantPermissionType } from '../../types'; +import { globalTenantName } from '../../../../../common'; describe('Tenant list utils', () => { const expectedGlobalTenantListing = { diff --git a/server/auth/types/authentication_type.ts b/server/auth/types/authentication_type.ts index cf7f5b8c0..b097435b8 100755 --- a/server/auth/types/authentication_type.ts +++ b/server/auth/types/authentication_type.ts @@ -31,7 +31,7 @@ import { SecuritySessionCookie } from '../../session/security_cookie'; import { SecurityClient } from '../../backend/opensearch_security_client'; import { resolveTenant, isValidTenant } from '../../multitenancy/tenant_resolver'; import { UnauthenticatedError } from '../../errors'; -import { GLOBAL_TENANT_SYMBOL } from '../../../public/apps/configuration/utils/tenant-utils'; +import { GLOBAL_TENANT_SYMBOL } from '../../../common'; export interface IAuthenticationType { type: string; diff --git a/server/multitenancy/tenant_resolver.ts b/server/multitenancy/tenant_resolver.ts index 2fa735013..9a2ad989d 100755 --- a/server/multitenancy/tenant_resolver.ts +++ b/server/multitenancy/tenant_resolver.ts @@ -17,10 +17,7 @@ import { isEmpty, findKey, cloneDeep } from 'lodash'; import { OpenSearchDashboardsRequest } from '../../../../src/core/server'; import { SecuritySessionCookie } from '../session/security_cookie'; import { SecurityPluginConfigType } from '..'; -import { - GLOBAL_TENANT_SYMBOL, - PRIVATE_TENANT_SYMBOL, -} from '../../public/apps/configuration/utils/tenant-utils'; +import { GLOBAL_TENANT_SYMBOL, PRIVATE_TENANT_SYMBOL } from '../../common'; export const PRIVATE_TENANTS: string[] = [PRIVATE_TENANT_SYMBOL, 'private']; export const GLOBAL_TENANTS: string[] = ['global', GLOBAL_TENANT_SYMBOL]; diff --git a/server/saved_objects/saved_objects_wrapper.ts b/server/saved_objects/saved_objects_wrapper.ts index 0cf767ebe..992b064dc 100644 --- a/server/saved_objects/saved_objects_wrapper.ts +++ b/server/saved_objects/saved_objects_wrapper.ts @@ -36,14 +36,14 @@ import { } from 'opensearch-dashboards/server'; import { Config } from 'packages/osd-config/target'; import { SecurityPluginConfigType } from '..'; +import { OpenSearchDashboardsAuthState } from '../auth/types/authentication_type'; import { DEFAULT_TENANT, - globalTenantName, GLOBAL_TENANT_SYMBOL, + globalTenantName, isPrivateTenant, PRIVATE_TENANT_SYMBOL, -} from '../../public/apps/configuration/utils/tenant-utils'; -import { OpenSearchDashboardsAuthState } from '../auth/types/authentication_type'; +} from '../../common'; export class SecuritySavedObjectsClientWrapper { public httpStart?: HttpServiceStart;