From abfae9aff41814af041156377eba7380bea522b0 Mon Sep 17 00:00:00 2001 From: Chang Liu Date: Wed, 2 Nov 2022 12:41:37 -0700 Subject: [PATCH 1/3] Move utils to common folder Signed-off-by: Chang Liu --- common/index.ts | 18 +++++++++ .../apps/configuration/utils/tenant-utils.tsx | 39 +++++++------------ public/index.ts | 1 + server/auth/types/authentication_type.ts | 2 +- server/multitenancy/tenant_resolver.ts | 5 +-- server/saved_objects/saved_objects_wrapper.ts | 6 +-- 6 files changed, 39 insertions(+), 32 deletions(-) diff --git a/common/index.ts b/common/index.ts index 44227240e..12d891ecd 100644 --- a/common/index.ts +++ b/common/index.ts @@ -42,6 +42,12 @@ export const AUTH_HEADER_NAME = 'authorization'; export const AUTH_GRANT_TYPE = 'authorization_code'; export const AUTH_RESPONSE_TYPE = 'code'; +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 enum AuthType { BASIC = 'basicauth', OPEN_ID = 'openid', @@ -61,3 +67,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..3fda9fcce 100644 --- a/public/apps/configuration/utils/tenant-utils.tsx +++ b/public/apps/configuration/utils/tenant-utils.tsx @@ -18,31 +18,34 @@ 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'; +import { + GLOBAL_TENANT_RENDERING_TEXT, + GLOBAL_TENANT_SYMBOL, + isGlobalTenant, + isRenderingPrivateTenant, + PRIVATE_TENANT_RENDERING_TEXT, +} from '../../../../common'; 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'; export const GLOBAL_USER_DICT: { [key: string]: string } = { Label: 'Global', @@ -179,18 +182,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/index.ts b/public/index.ts index d09f3dc49..82a6a2f39 100644 --- a/public/index.ts +++ b/public/index.ts @@ -21,3 +21,4 @@ export function plugin(initializerContext: PluginInitializerContext) { return new SecurityPlugin(initializerContext); } export { SecurityPluginSetup, SecurityPluginStart } from './types'; +export { tenantColumn } from './apps/configuration/utils/tenant-utils'; diff --git a/server/auth/types/authentication_type.ts b/server/auth/types/authentication_type.ts index 7c7c14154..b1ea1a208 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..c86ced8b0 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 { globalTenantName } from '../../public/apps/configuration/utils/tenant-utils'; +import { OpenSearchDashboardsAuthState } from '../auth/types/authentication_type'; import { DEFAULT_TENANT, - globalTenantName, GLOBAL_TENANT_SYMBOL, 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; From 0e1178bad7b09dad3c913743a05c7bfc9e0c2bfe Mon Sep 17 00:00:00 2001 From: Chang Liu Date: Wed, 2 Nov 2022 12:49:38 -0700 Subject: [PATCH 2/3] Remove the unnecessary export tenantColumn in public/index.js Signed-off-by: Chang Liu --- public/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/public/index.ts b/public/index.ts index 82a6a2f39..d09f3dc49 100644 --- a/public/index.ts +++ b/public/index.ts @@ -21,4 +21,3 @@ export function plugin(initializerContext: PluginInitializerContext) { return new SecurityPlugin(initializerContext); } export { SecurityPluginSetup, SecurityPluginStart } from './types'; -export { tenantColumn } from './apps/configuration/utils/tenant-utils'; From 30fd8f5a6402ad7c2d431dbe32bb279df90c18d8 Mon Sep 17 00:00:00 2001 From: Chang Liu Date: Wed, 2 Nov 2022 13:45:58 -0700 Subject: [PATCH 3/3] Move globalTenantName to common Signed-off-by: Chang Liu --- common/index.ts | 1 + public/apps/configuration/utils/tenant-utils.tsx | 3 +-- public/apps/configuration/utils/test/tenant-utils.test.tsx | 2 +- server/saved_objects/saved_objects_wrapper.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/common/index.ts b/common/index.ts index 12d891ecd..028d9d376 100644 --- a/common/index.ts +++ b/common/index.ts @@ -47,6 +47,7 @@ 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', diff --git a/public/apps/configuration/utils/tenant-utils.tsx b/public/apps/configuration/utils/tenant-utils.tsx index 3fda9fcce..3b782143f 100644 --- a/public/apps/configuration/utils/tenant-utils.tsx +++ b/public/apps/configuration/utils/tenant-utils.tsx @@ -40,13 +40,12 @@ import { getResourceUrl } from './resource-utils'; import { GLOBAL_TENANT_RENDERING_TEXT, GLOBAL_TENANT_SYMBOL, + globalTenantName, isGlobalTenant, isRenderingPrivateTenant, PRIVATE_TENANT_RENDERING_TEXT, } from '../../../../common'; -export const globalTenantName = 'global_tenant'; - export const GLOBAL_USER_DICT: { [key: string]: string } = { Label: 'Global', Value: GLOBAL_TENANT_SYMBOL, 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/saved_objects/saved_objects_wrapper.ts b/server/saved_objects/saved_objects_wrapper.ts index c86ced8b0..992b064dc 100644 --- a/server/saved_objects/saved_objects_wrapper.ts +++ b/server/saved_objects/saved_objects_wrapper.ts @@ -36,11 +36,11 @@ import { } from 'opensearch-dashboards/server'; import { Config } from 'packages/osd-config/target'; import { SecurityPluginConfigType } from '..'; -import { globalTenantName } from '../../public/apps/configuration/utils/tenant-utils'; import { OpenSearchDashboardsAuthState } from '../auth/types/authentication_type'; import { DEFAULT_TENANT, GLOBAL_TENANT_SYMBOL, + globalTenantName, isPrivateTenant, PRIVATE_TENANT_SYMBOL, } from '../../common';