From 619adf62e54e23c8d0ef224436aa9286d83ca589 Mon Sep 17 00:00:00 2001 From: Udara Pathum <46132469+hwupathum@users.noreply.github.com> Date: Tue, 22 Oct 2024 17:12:59 +0530 Subject: [PATCH 1/8] Support filter query to tenant API --- .../common/TenantManagementConstants.java | 25 +++++++ .../tenant/management/v1/TenantsApi.java | 2 +- .../core/ServerTenantManagementService.java | 75 +++++++++++++------ .../src/main/resources/tenant-management.yaml | 6 +- 4 files changed, 81 insertions(+), 27 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java index 1c34f94170..d8a10261e5 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java @@ -35,6 +35,25 @@ private TenantManagementConstants() { public static final String PAGE_LINK_REL_PREVIOUS = "previous"; public static final String NON_EXISTING_USER_CODE = "30007 - "; + /** + * Supported filter attributes. + */ + public static class FilterAttributes { + + public static final String DOMAIN = "domain"; + } + + /** + * Supported filter operations. + */ + public static class FilterOperations { + + public static final String EQ = "eq"; + public static final String SW = "sw"; + public static final String EW = "ew"; + public static final String CO = "co"; + } + /** * Enum for error messages. */ @@ -52,6 +71,12 @@ public enum ErrorMessage { ERROR_CODE_PARTIALLY_CREATED_OR_UPDATED("TM-60021", "Tenant creation / update was completed with errors.", "Tenant creation / update was completed with error: %s"), + ERROR_CODE_INVALID_FILTER_FORMAT("TM-60022", "Invalid format used for filtering.", + "Filter needs to be in the format ++."), + ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE("TM-60023", "Unsupported filter attribute.", + "The filter attribute '%s' is not supported."), + ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_ATTRIBUTE("TM-60024", "Unsupported filter operation.", + "Unsupported filter operation for filter attribute '%s'."), ERROR_CODE_ERROR_LISTING_TENANTS("TM-65001", "Unable to list existing tenants.", "Server encountered an error while listing the tenants."), diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java index 67411f774d..ae095bf952 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java @@ -244,7 +244,7 @@ public Response isDomainExist(@ApiParam(value = "tenant domain",required=true) @ @ApiResponse(code = 500, message = "Internal Server Error", response = Error.class), @ApiResponse(code = 501, message = "Not Implemented", response = Error.class) }) - public Response retrieveTenants( @Valid @Min(0)@ApiParam(value = "Maximum number of records to return.") @QueryParam("limit") Integer limit, @Valid @Min(0)@ApiParam(value = "Number of records to skip for pagination.") @QueryParam("offset") Integer offset, @Valid@ApiParam(value = "Define the order in which the retrieved tenants should be sorted.", allowableValues="asc, desc") @QueryParam("sortOrder") String sortOrder, @Valid@ApiParam(value = "Attribute by which the retrieved records should be sorted. Currently sorting through _domainName_ only supported.") @QueryParam("sortBy") String sortBy, @Valid@ApiParam(value = "Condition to filter the retrival of records. Supports 'sw', 'co', 'ew' and 'eq' operations and also complex queries with 'and' operations. E.g. /tenants?filter=domain+sw+\"wso2\". _This option is not yet supported._ ") @QueryParam("filter") String filter) { + public Response retrieveTenants( @Valid @Min(0)@ApiParam(value = "Maximum number of records to return.") @QueryParam("limit") Integer limit, @Valid @Min(0)@ApiParam(value = "Number of records to skip for pagination.") @QueryParam("offset") Integer offset, @Valid@ApiParam(value = "Define the order in which the retrieved tenants should be sorted.", allowableValues="asc, desc") @QueryParam("sortOrder") String sortOrder, @Valid@ApiParam(value = "Attribute by which the retrieved records should be sorted. Currently sorting through _domainName_ only supported.") @QueryParam("sortBy") String sortBy, @Valid@ApiParam(value = "Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew' and 'eq' operations. E.g. /tenants?filter=domain+sw+wso2. ") @QueryParam("filter") String filter) { return delegate.retrieveTenants(limit, offset, sortOrder, sortBy, filter ); } diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java index 23f98cdeb6..de1b5e3f36 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java @@ -67,8 +67,16 @@ import static org.wso2.carbon.identity.api.server.common.Constants.ERROR_CODE_RESOURCE_LIMIT_REACHED; import static org.wso2.carbon.identity.api.server.common.Constants.V1_API_PATH_COMPONENT; +import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.ErrorMessage.ERROR_CODE_INVALID_FILTER_FORMAT; import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.ErrorMessage.ERROR_CODE_PARTIALLY_CREATED_OR_UPDATED; import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.ErrorMessage.ERROR_CODE_TENANT_LIMIT_REACHED; +import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE; +import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_ATTRIBUTE; +import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterAttributes.DOMAIN; +import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterOperations.CO; +import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterOperations.EQ; +import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterOperations.EW; +import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterOperations.SW; import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.TENANT_MANAGEMENT_PATH_COMPONENT; import static org.wso2.carbon.stratos.common.constants.TenantConstants.ErrorMessage.ERROR_CODE_INVALID_EMAIL; import static org.wso2.carbon.stratos.common.constants.TenantConstants.ErrorMessage.ERROR_CODE_MISSING_REQUIRED_PARAMETER; @@ -119,12 +127,12 @@ public String addTenant(TenantModel tenantModel) { public TenantsListResponse listTenants(Integer limit, Integer offset, String sortOrder, String sortBy, String filter) { - handleNotImplementedCapabilities(filter); TenantMgtService tenantMgtService = TenantManagementServiceHolder.getTenantMgtService(); + String filterFormatted = buildFilter(filter); try { TenantSearchResult tenantSearchResult = tenantMgtService.listTenants(limit, offset, sortOrder, sortBy, - filter); + filterFormatted); return createTenantListResponse(tenantSearchResult); } catch (TenantMgtException e) { throw handleTenantManagementException(e, TenantManagementConstants.ErrorMessage @@ -605,26 +613,6 @@ private static String includeData(TenantManagementConstants.ErrorMessage error, return message; } - /** - * Return Not Implemented error response for tenant List filtering which are not yet supported by the server. - * - * @param filter Filter string. - */ - private void handleNotImplementedCapabilities(String filter) { - - TenantManagementConstants.ErrorMessage errorEnum = null; - - if (filter != null) { - errorEnum = TenantManagementConstants.ErrorMessage.ERROR_CODE_FILTER_NOT_IMPLEMENTED; - } - - if (errorEnum != null) { - ErrorResponse errorResponse = getErrorBuilder(errorEnum, null).build(log, errorEnum.getDescription()); - Response.Status status = Response.Status.NOT_IMPLEMENTED; - throw new APIError(status, errorResponse); - } - } - public String addTenant(ChannelVerifiedTenantModel channelVerifiedTenantModel) { String resourceId; TenantMgtService tenantMgtService = TenantManagementServiceHolder.getTenantMgtService(); @@ -736,4 +724,47 @@ private String getISOFormatDate(Date date) { .withZoneSameInstant(ZoneId.of("UTC")); return ISO_OFFSET_DATE_TIME.format(zonedDateTime); } + + + private String buildFilter(String filter) { + + if (StringUtils.isNotBlank(filter)) { + String[] filterArgs = filter.split(" "); + if (filterArgs.length == 3) { + + String filterAttribute = filterArgs[0]; + + if (StringUtils.equalsIgnoreCase(filterAttribute, DOMAIN)) { + String operation = filterArgs[1]; + String attributeValue = filterArgs[2]; + return generateFilterStringForBackend(operation, attributeValue); + } else { + throw handleException(Response.Status.BAD_REQUEST, ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE, + filterAttribute); + } + } else { + throw handleException(Response.Status.BAD_REQUEST, ERROR_CODE_INVALID_FILTER_FORMAT, null); + } + } else { + return null; + } + } + + private String generateFilterStringForBackend(String operation, String attributeValue) { + + String formattedFilter = null; + if (StringUtils.equalsIgnoreCase(operation, SW)) { + formattedFilter = attributeValue + "*"; + } else if (StringUtils.equalsIgnoreCase(operation, EW)) { + formattedFilter = "*" + attributeValue; + } else if (StringUtils.equalsIgnoreCase(operation, EQ)) { + formattedFilter = attributeValue; + } else if (StringUtils.equalsIgnoreCase(operation, CO)) { + formattedFilter = "*" + attributeValue + "*"; + } else { + throw handleException(Response.Status.BAD_REQUEST, + ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_ATTRIBUTE, attributeValue); + } + return formattedFilter; + } } diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml index 42e23745c8..3dc53ba878 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml @@ -497,10 +497,8 @@ components: name: filter required: false description: > - Condition to filter the retrival of records. Supports 'sw', 'co', 'ew' - and 'eq' operations and also complex queries with 'and' operations. E.g. - /tenants?filter=domain+sw+"wso2". _This - option is not yet supported._ + Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew' and 'eq' operations. + E.g. /tenants?filter=domain+sw+wso2. schema: type: string additionalClaimsQueryParam: From e4f9f1a02aa500df382015f04e0bbc3c9add8a96 Mon Sep 17 00:00:00 2001 From: Udara Pathum <46132469+hwupathum@users.noreply.github.com> Date: Wed, 23 Oct 2024 11:47:39 +0530 Subject: [PATCH 2/8] Extract domain from filter in kernel level --- .../common/TenantManagementConstants.java | 8 --- .../tenant/management/v1/TenantsApi.java | 2 +- .../core/ServerTenantManagementService.java | 51 +++++++------------ .../src/main/resources/tenant-management.yaml | 2 +- 4 files changed, 20 insertions(+), 43 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java index d8a10261e5..d982d258d9 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java @@ -35,14 +35,6 @@ private TenantManagementConstants() { public static final String PAGE_LINK_REL_PREVIOUS = "previous"; public static final String NON_EXISTING_USER_CODE = "30007 - "; - /** - * Supported filter attributes. - */ - public static class FilterAttributes { - - public static final String DOMAIN = "domain"; - } - /** * Supported filter operations. */ diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java index ae095bf952..8ee5ca53c6 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java @@ -244,7 +244,7 @@ public Response isDomainExist(@ApiParam(value = "tenant domain",required=true) @ @ApiResponse(code = 500, message = "Internal Server Error", response = Error.class), @ApiResponse(code = 501, message = "Not Implemented", response = Error.class) }) - public Response retrieveTenants( @Valid @Min(0)@ApiParam(value = "Maximum number of records to return.") @QueryParam("limit") Integer limit, @Valid @Min(0)@ApiParam(value = "Number of records to skip for pagination.") @QueryParam("offset") Integer offset, @Valid@ApiParam(value = "Define the order in which the retrieved tenants should be sorted.", allowableValues="asc, desc") @QueryParam("sortOrder") String sortOrder, @Valid@ApiParam(value = "Attribute by which the retrieved records should be sorted. Currently sorting through _domainName_ only supported.") @QueryParam("sortBy") String sortBy, @Valid@ApiParam(value = "Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew' and 'eq' operations. E.g. /tenants?filter=domain+sw+wso2. ") @QueryParam("filter") String filter) { + public Response retrieveTenants( @Valid @Min(0)@ApiParam(value = "Maximum number of records to return.") @QueryParam("limit") Integer limit, @Valid @Min(0)@ApiParam(value = "Number of records to skip for pagination.") @QueryParam("offset") Integer offset, @Valid@ApiParam(value = "Define the order in which the retrieved tenants should be sorted.", allowableValues="asc, desc") @QueryParam("sortOrder") String sortOrder, @Valid@ApiParam(value = "Attribute by which the retrieved records should be sorted. Currently sorting through _domainName_ only supported.") @QueryParam("sortBy") String sortBy, @Valid@ApiParam(value = "Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew' and 'eq' operations. E.g. /tenants?filter=domainName+sw+wso2. ") @QueryParam("filter") String filter) { return delegate.retrieveTenants(limit, offset, sortOrder, sortBy, filter ); } diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java index de1b5e3f36..305fb93503 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java @@ -45,6 +45,7 @@ import org.wso2.carbon.stratos.common.exception.TenantManagementServerException; import org.wso2.carbon.stratos.common.exception.TenantMgtException; import org.wso2.carbon.stratos.common.util.ClaimsMgtUtil; +import org.wso2.carbon.tenant.mgt.services.TenantMgtImpl; import org.wso2.carbon.tenant.mgt.services.TenantMgtService; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.core.common.User; @@ -72,7 +73,6 @@ import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.ErrorMessage.ERROR_CODE_TENANT_LIMIT_REACHED; import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE; import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_ATTRIBUTE; -import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterAttributes.DOMAIN; import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterOperations.CO; import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterOperations.EQ; import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterOperations.EW; @@ -129,10 +129,10 @@ public TenantsListResponse listTenants(Integer limit, Integer offset, String sor TenantMgtService tenantMgtService = TenantManagementServiceHolder.getTenantMgtService(); - String filterFormatted = buildFilter(filter); + verifyFilter(filter); try { TenantSearchResult tenantSearchResult = tenantMgtService.listTenants(limit, offset, sortOrder, sortBy, - filterFormatted); + filter); return createTenantListResponse(tenantSearchResult); } catch (TenantMgtException e) { throw handleTenantManagementException(e, TenantManagementConstants.ErrorMessage @@ -726,45 +726,30 @@ private String getISOFormatDate(Date date) { } - private String buildFilter(String filter) { + private void verifyFilter(String filter) { if (StringUtils.isNotBlank(filter)) { String[] filterArgs = filter.split(" "); - if (filterArgs.length == 3) { + if (filterArgs.length != 3) { + throw handleException(Response.Status.BAD_REQUEST, ERROR_CODE_INVALID_FILTER_FORMAT, null); + } - String filterAttribute = filterArgs[0]; + String filterAttribute = filterArgs[0]; + String operation = filterArgs[1]; + String attributeValue = filterArgs[2]; - if (StringUtils.equalsIgnoreCase(filterAttribute, DOMAIN)) { - String operation = filterArgs[1]; - String attributeValue = filterArgs[2]; - return generateFilterStringForBackend(operation, attributeValue); - } else { - throw handleException(Response.Status.BAD_REQUEST, ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE, - filterAttribute); + if (StringUtils.equalsIgnoreCase(filterAttribute, TenantMgtImpl.DOMAIN_NAME)) { + if (!StringUtils.equalsIgnoreCase(operation, SW) && !StringUtils.equalsIgnoreCase(operation, EW) + && !StringUtils.equalsIgnoreCase(operation, EQ) + && !StringUtils.equalsIgnoreCase(operation, CO)) { + throw handleException(Response.Status.BAD_REQUEST, + ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_ATTRIBUTE, attributeValue); } } else { - throw handleException(Response.Status.BAD_REQUEST, ERROR_CODE_INVALID_FILTER_FORMAT, null); + throw handleException(Response.Status.BAD_REQUEST, ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE, + filterAttribute); } - } else { - return null; - } - } - private String generateFilterStringForBackend(String operation, String attributeValue) { - - String formattedFilter = null; - if (StringUtils.equalsIgnoreCase(operation, SW)) { - formattedFilter = attributeValue + "*"; - } else if (StringUtils.equalsIgnoreCase(operation, EW)) { - formattedFilter = "*" + attributeValue; - } else if (StringUtils.equalsIgnoreCase(operation, EQ)) { - formattedFilter = attributeValue; - } else if (StringUtils.equalsIgnoreCase(operation, CO)) { - formattedFilter = "*" + attributeValue + "*"; - } else { - throw handleException(Response.Status.BAD_REQUEST, - ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_ATTRIBUTE, attributeValue); } - return formattedFilter; } } diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml index 3dc53ba878..1d28d56cf3 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml @@ -498,7 +498,7 @@ components: required: false description: > Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew' and 'eq' operations. - E.g. /tenants?filter=domain+sw+wso2. + E.g. /tenants?filter=domainName+sw+wso2. schema: type: string additionalClaimsQueryParam: From 775eae5fdc7580373ccf1c0e2d0e646b6d51f456 Mon Sep 17 00:00:00 2001 From: Udara Pathum <46132469+hwupathum@users.noreply.github.com> Date: Wed, 23 Oct 2024 14:14:48 +0530 Subject: [PATCH 3/8] Check tenant domain contains any illegal characters --- .../v1/core/ServerTenantManagementService.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java index 305fb93503..655aadfb0a 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java @@ -78,6 +78,7 @@ import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterOperations.EW; import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterOperations.SW; import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.TENANT_MANAGEMENT_PATH_COMPONENT; +import static org.wso2.carbon.stratos.common.constants.TenantConstants.ErrorMessage.ERROR_CODE_ILLEGAL_CHARACTERS_IN_DOMAIN; import static org.wso2.carbon.stratos.common.constants.TenantConstants.ErrorMessage.ERROR_CODE_INVALID_EMAIL; import static org.wso2.carbon.stratos.common.constants.TenantConstants.ErrorMessage.ERROR_CODE_MISSING_REQUIRED_PARAMETER; @@ -93,6 +94,7 @@ public class ServerTenantManagementService { private static final String INLINE_PASSWORD = "inline-password"; private static final String CODE = "code"; private static final String PURPOSE = "purpose"; + private static final String ILLEGAL_CHARACTERS_FOR_TENANT_DOMAIN = ".*[^a-z0-9\\._\\-].*"; /** * Add a tenant. @@ -129,8 +131,8 @@ public TenantsListResponse listTenants(Integer limit, Integer offset, String sor TenantMgtService tenantMgtService = TenantManagementServiceHolder.getTenantMgtService(); - verifyFilter(filter); try { + verifyFilter(filter); TenantSearchResult tenantSearchResult = tenantMgtService.listTenants(limit, offset, sortOrder, sortBy, filter); return createTenantListResponse(tenantSearchResult); @@ -726,7 +728,7 @@ private String getISOFormatDate(Date date) { } - private void verifyFilter(String filter) { + private void verifyFilter(String filter) throws TenantMgtException { if (StringUtils.isNotBlank(filter)) { String[] filterArgs = filter.split(" "); @@ -739,6 +741,11 @@ private void verifyFilter(String filter) { String attributeValue = filterArgs[2]; if (StringUtils.equalsIgnoreCase(filterAttribute, TenantMgtImpl.DOMAIN_NAME)) { + // Check tenant domain contains any illegal characters. + if (attributeValue.matches(ILLEGAL_CHARACTERS_FOR_TENANT_DOMAIN)) { + throw new TenantManagementClientException(ERROR_CODE_ILLEGAL_CHARACTERS_IN_DOMAIN.getCode(), + String.format(ERROR_CODE_ILLEGAL_CHARACTERS_IN_DOMAIN.getMessage(), attributeValue)); + } if (!StringUtils.equalsIgnoreCase(operation, SW) && !StringUtils.equalsIgnoreCase(operation, EW) && !StringUtils.equalsIgnoreCase(operation, EQ) && !StringUtils.equalsIgnoreCase(operation, CO)) { From 3d0837f094bcaa76ab0b2fe6f8790a10989f163f Mon Sep 17 00:00:00 2001 From: Udara Pathum <46132469+hwupathum@users.noreply.github.com> Date: Wed, 23 Oct 2024 14:35:31 +0530 Subject: [PATCH 4/8] Update API description --- .../identity/api/server/tenant/management/v1/TenantsApi.java | 2 +- .../src/main/resources/tenant-management.yaml | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java index 8ee5ca53c6..c12647d44b 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java @@ -244,7 +244,7 @@ public Response isDomainExist(@ApiParam(value = "tenant domain",required=true) @ @ApiResponse(code = 500, message = "Internal Server Error", response = Error.class), @ApiResponse(code = 501, message = "Not Implemented", response = Error.class) }) - public Response retrieveTenants( @Valid @Min(0)@ApiParam(value = "Maximum number of records to return.") @QueryParam("limit") Integer limit, @Valid @Min(0)@ApiParam(value = "Number of records to skip for pagination.") @QueryParam("offset") Integer offset, @Valid@ApiParam(value = "Define the order in which the retrieved tenants should be sorted.", allowableValues="asc, desc") @QueryParam("sortOrder") String sortOrder, @Valid@ApiParam(value = "Attribute by which the retrieved records should be sorted. Currently sorting through _domainName_ only supported.") @QueryParam("sortBy") String sortBy, @Valid@ApiParam(value = "Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew' and 'eq' operations. E.g. /tenants?filter=domainName+sw+wso2. ") @QueryParam("filter") String filter) { + public Response retrieveTenants( @Valid @Min(0)@ApiParam(value = "Maximum number of records to return.") @QueryParam("limit") Integer limit, @Valid @Min(0)@ApiParam(value = "Number of records to skip for pagination.") @QueryParam("offset") Integer offset, @Valid@ApiParam(value = "Define the order in which the retrieved tenants should be sorted.", allowableValues="asc, desc") @QueryParam("sortOrder") String sortOrder, @Valid@ApiParam(value = "Attribute by which the retrieved records should be sorted. Currently sorting through domainName only supported.") @QueryParam("sortBy") String sortBy, @Valid@ApiParam(value = "Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew' and 'eq' operations. Currently filtering through domainName only supported. E.g. /tenants?filter=domainName+sw+wso2. ") @QueryParam("filter") String filter) { return delegate.retrieveTenants(limit, offset, sortOrder, sortBy, filter ); } diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml index 1d28d56cf3..d9bdb67183 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml @@ -488,7 +488,7 @@ components: name: sortBy required: false description: >- - Attribute by which the retrieved records should be sorted. Currently sorting through _domainName_ only + Attribute by which the retrieved records should be sorted. Currently sorting through domainName only supported. schema: type: string @@ -497,7 +497,8 @@ components: name: filter required: false description: > - Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew' and 'eq' operations. + Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew' and 'eq' operations. Currently filtering + through domainName only supported. E.g. /tenants?filter=domainName+sw+wso2. schema: type: string From b0612cfccf995f18d05a9011f613954bd4ed8594 Mon Sep 17 00:00:00 2001 From: Udara Pathum <46132469+hwupathum@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:26:39 +0530 Subject: [PATCH 5/8] Update API response codes --- .../api/server/tenant/management/v1/TenantsApi.java | 4 ++-- .../src/main/resources/tenant-management.yaml | 8 ++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java index c12647d44b..faa14957db 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java @@ -238,11 +238,11 @@ public Response isDomainExist(@ApiParam(value = "tenant domain",required=true) @ }, tags={ "Tenants", }) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = TenantsListResponse.class), + @ApiResponse(code = 400, message = "Invalid Input Request", response = Error.class), @ApiResponse(code = 401, message = "Unauthorized", response = Void.class), @ApiResponse(code = 403, message = "Resource Forbidden", response = Void.class), @ApiResponse(code = 404, message = "The specified resource is not found", response = Error.class), - @ApiResponse(code = 500, message = "Internal Server Error", response = Error.class), - @ApiResponse(code = 501, message = "Not Implemented", response = Error.class) + @ApiResponse(code = 500, message = "Internal Server Error", response = Error.class) }) public Response retrieveTenants( @Valid @Min(0)@ApiParam(value = "Maximum number of records to return.") @QueryParam("limit") Integer limit, @Valid @Min(0)@ApiParam(value = "Number of records to skip for pagination.") @QueryParam("offset") Integer offset, @Valid@ApiParam(value = "Define the order in which the retrieved tenants should be sorted.", allowableValues="asc, desc") @QueryParam("sortOrder") String sortOrder, @Valid@ApiParam(value = "Attribute by which the retrieved records should be sorted. Currently sorting through domainName only supported.") @QueryParam("sortBy") String sortBy, @Valid@ApiParam(value = "Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew' and 'eq' operations. Currently filtering through domainName only supported. E.g. /tenants?filter=domainName+sw+wso2. ") @QueryParam("filter") String filter) { diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml index d9bdb67183..515a2ca192 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml @@ -43,6 +43,8 @@ paths: 'application/json': schema: $ref: '#/components/schemas/TenantsListResponse' + '400': + $ref: '#/components/responses/InvalidInput' '401': $ref: '#/components/responses/Unauthorized' '403': @@ -51,12 +53,6 @@ paths: $ref: '#/components/responses/NotFound' '500': $ref: '#/components/responses/ServerError' - '501': - description: Not Implemented - content: - application/json: - schema: - $ref: '#/components/schemas/Error' post: tags: - Tenants From b11d66c18d4d65837071b9554d156e3234f8dff5 Mon Sep 17 00:00:00 2001 From: Udara Pathum <46132469+hwupathum@users.noreply.github.com> Date: Wed, 23 Oct 2024 18:35:22 +0530 Subject: [PATCH 6/8] Add space separator --- .../management/v1/core/ServerTenantManagementService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java index 655aadfb0a..acd5654f37 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java @@ -95,6 +95,7 @@ public class ServerTenantManagementService { private static final String CODE = "code"; private static final String PURPOSE = "purpose"; private static final String ILLEGAL_CHARACTERS_FOR_TENANT_DOMAIN = ".*[^a-z0-9\\._\\-].*"; + private static final String SPACE_SEPARATOR = " "; /** * Add a tenant. @@ -731,7 +732,7 @@ private String getISOFormatDate(Date date) { private void verifyFilter(String filter) throws TenantMgtException { if (StringUtils.isNotBlank(filter)) { - String[] filterArgs = filter.split(" "); + String[] filterArgs = filter.split(SPACE_SEPARATOR); if (filterArgs.length != 3) { throw handleException(Response.Status.BAD_REQUEST, ERROR_CODE_INVALID_FILTER_FORMAT, null); } From 9d5d8f59f91ddcd59f5e9d0abf441de491254fda Mon Sep 17 00:00:00 2001 From: Udara Pathum <46132469+hwupathum@users.noreply.github.com> Date: Thu, 24 Oct 2024 12:40:13 +0530 Subject: [PATCH 7/8] Move verify method to multi-tenancy --- .../common/TenantManagementConstants.java | 6 --- .../tenant/management/v1/TenantsApi.java | 2 +- .../core/ServerTenantManagementService.java | 45 ------------------- .../src/main/resources/tenant-management.yaml | 4 +- 4 files changed, 3 insertions(+), 54 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java index d982d258d9..b9fc693ea1 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java @@ -63,12 +63,6 @@ public enum ErrorMessage { ERROR_CODE_PARTIALLY_CREATED_OR_UPDATED("TM-60021", "Tenant creation / update was completed with errors.", "Tenant creation / update was completed with error: %s"), - ERROR_CODE_INVALID_FILTER_FORMAT("TM-60022", "Invalid format used for filtering.", - "Filter needs to be in the format ++."), - ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE("TM-60023", "Unsupported filter attribute.", - "The filter attribute '%s' is not supported."), - ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_ATTRIBUTE("TM-60024", "Unsupported filter operation.", - "Unsupported filter operation for filter attribute '%s'."), ERROR_CODE_ERROR_LISTING_TENANTS("TM-65001", "Unable to list existing tenants.", "Server encountered an error while listing the tenants."), diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java index faa14957db..bf77e72cd4 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/gen/java/org/wso2/carbon/identity/api/server/tenant/management/v1/TenantsApi.java @@ -244,7 +244,7 @@ public Response isDomainExist(@ApiParam(value = "tenant domain",required=true) @ @ApiResponse(code = 404, message = "The specified resource is not found", response = Error.class), @ApiResponse(code = 500, message = "Internal Server Error", response = Error.class) }) - public Response retrieveTenants( @Valid @Min(0)@ApiParam(value = "Maximum number of records to return.") @QueryParam("limit") Integer limit, @Valid @Min(0)@ApiParam(value = "Number of records to skip for pagination.") @QueryParam("offset") Integer offset, @Valid@ApiParam(value = "Define the order in which the retrieved tenants should be sorted.", allowableValues="asc, desc") @QueryParam("sortOrder") String sortOrder, @Valid@ApiParam(value = "Attribute by which the retrieved records should be sorted. Currently sorting through domainName only supported.") @QueryParam("sortBy") String sortBy, @Valid@ApiParam(value = "Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew' and 'eq' operations. Currently filtering through domainName only supported. E.g. /tenants?filter=domainName+sw+wso2. ") @QueryParam("filter") String filter) { + public Response retrieveTenants( @Valid @Min(0)@ApiParam(value = "Maximum number of records to return.") @QueryParam("limit") Integer limit, @Valid @Min(0)@ApiParam(value = "Number of records to skip for pagination.") @QueryParam("offset") Integer offset, @Valid@ApiParam(value = "Define the order in which the retrieved tenants should be sorted.", allowableValues="asc, desc") @QueryParam("sortOrder") String sortOrder, @Valid@ApiParam(value = "Attribute by which the retrieved records should be sorted. Currently sorting through domainName only supported.") @QueryParam("sortBy") String sortBy, @Valid@ApiParam(value = "Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew' and 'eq' operations. Currently, filtering is supported only by the domainName. E.g. /tenants?filter=domainName+sw+wso2. ") @QueryParam("filter") String filter) { return delegate.retrieveTenants(limit, offset, sortOrder, sortBy, filter ); } diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java index acd5654f37..37c5ebcb44 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/v1/core/ServerTenantManagementService.java @@ -45,7 +45,6 @@ import org.wso2.carbon.stratos.common.exception.TenantManagementServerException; import org.wso2.carbon.stratos.common.exception.TenantMgtException; import org.wso2.carbon.stratos.common.util.ClaimsMgtUtil; -import org.wso2.carbon.tenant.mgt.services.TenantMgtImpl; import org.wso2.carbon.tenant.mgt.services.TenantMgtService; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.core.common.User; @@ -68,17 +67,9 @@ import static org.wso2.carbon.identity.api.server.common.Constants.ERROR_CODE_RESOURCE_LIMIT_REACHED; import static org.wso2.carbon.identity.api.server.common.Constants.V1_API_PATH_COMPONENT; -import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.ErrorMessage.ERROR_CODE_INVALID_FILTER_FORMAT; import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.ErrorMessage.ERROR_CODE_PARTIALLY_CREATED_OR_UPDATED; import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.ErrorMessage.ERROR_CODE_TENANT_LIMIT_REACHED; -import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE; -import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.ErrorMessage.ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_ATTRIBUTE; -import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterOperations.CO; -import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterOperations.EQ; -import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterOperations.EW; -import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.FilterOperations.SW; import static org.wso2.carbon.identity.api.server.tenant.management.common.TenantManagementConstants.TENANT_MANAGEMENT_PATH_COMPONENT; -import static org.wso2.carbon.stratos.common.constants.TenantConstants.ErrorMessage.ERROR_CODE_ILLEGAL_CHARACTERS_IN_DOMAIN; import static org.wso2.carbon.stratos.common.constants.TenantConstants.ErrorMessage.ERROR_CODE_INVALID_EMAIL; import static org.wso2.carbon.stratos.common.constants.TenantConstants.ErrorMessage.ERROR_CODE_MISSING_REQUIRED_PARAMETER; @@ -94,8 +85,6 @@ public class ServerTenantManagementService { private static final String INLINE_PASSWORD = "inline-password"; private static final String CODE = "code"; private static final String PURPOSE = "purpose"; - private static final String ILLEGAL_CHARACTERS_FOR_TENANT_DOMAIN = ".*[^a-z0-9\\._\\-].*"; - private static final String SPACE_SEPARATOR = " "; /** * Add a tenant. @@ -133,7 +122,6 @@ public TenantsListResponse listTenants(Integer limit, Integer offset, String sor TenantMgtService tenantMgtService = TenantManagementServiceHolder.getTenantMgtService(); try { - verifyFilter(filter); TenantSearchResult tenantSearchResult = tenantMgtService.listTenants(limit, offset, sortOrder, sortBy, filter); return createTenantListResponse(tenantSearchResult); @@ -727,37 +715,4 @@ private String getISOFormatDate(Date date) { .withZoneSameInstant(ZoneId.of("UTC")); return ISO_OFFSET_DATE_TIME.format(zonedDateTime); } - - - private void verifyFilter(String filter) throws TenantMgtException { - - if (StringUtils.isNotBlank(filter)) { - String[] filterArgs = filter.split(SPACE_SEPARATOR); - if (filterArgs.length != 3) { - throw handleException(Response.Status.BAD_REQUEST, ERROR_CODE_INVALID_FILTER_FORMAT, null); - } - - String filterAttribute = filterArgs[0]; - String operation = filterArgs[1]; - String attributeValue = filterArgs[2]; - - if (StringUtils.equalsIgnoreCase(filterAttribute, TenantMgtImpl.DOMAIN_NAME)) { - // Check tenant domain contains any illegal characters. - if (attributeValue.matches(ILLEGAL_CHARACTERS_FOR_TENANT_DOMAIN)) { - throw new TenantManagementClientException(ERROR_CODE_ILLEGAL_CHARACTERS_IN_DOMAIN.getCode(), - String.format(ERROR_CODE_ILLEGAL_CHARACTERS_IN_DOMAIN.getMessage(), attributeValue)); - } - if (!StringUtils.equalsIgnoreCase(operation, SW) && !StringUtils.equalsIgnoreCase(operation, EW) - && !StringUtils.equalsIgnoreCase(operation, EQ) - && !StringUtils.equalsIgnoreCase(operation, CO)) { - throw handleException(Response.Status.BAD_REQUEST, - ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_ATTRIBUTE, attributeValue); - } - } else { - throw handleException(Response.Status.BAD_REQUEST, ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE, - filterAttribute); - } - - } - } } diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml index 515a2ca192..de10bebe24 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.v1/src/main/resources/tenant-management.yaml @@ -493,8 +493,8 @@ components: name: filter required: false description: > - Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew' and 'eq' operations. Currently filtering - through domainName only supported. + Condition to filter the retrieval of records. Supports 'sw', 'co', 'ew' and 'eq' operations. + Currently, filtering is supported only by the domainName. E.g. /tenants?filter=domainName+sw+wso2. schema: type: string From 54d60e5b1a8c057f661e99b7e9e255b69a64a5c1 Mon Sep 17 00:00:00 2001 From: Udara Pathum <46132469+hwupathum@users.noreply.github.com> Date: Thu, 24 Oct 2024 12:41:01 +0530 Subject: [PATCH 8/8] Remove unused constant --- .../management/common/TenantManagementConstants.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java index b9fc693ea1..1c34f94170 100644 --- a/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java +++ b/components/org.wso2.carbon.identity.api.server.tenant.management/org.wso2.carbon.identity.api.server.tenant.management.common/src/main/java/org/wso2/carbon/identity/api/server/tenant/management/common/TenantManagementConstants.java @@ -35,17 +35,6 @@ private TenantManagementConstants() { public static final String PAGE_LINK_REL_PREVIOUS = "previous"; public static final String NON_EXISTING_USER_CODE = "30007 - "; - /** - * Supported filter operations. - */ - public static class FilterOperations { - - public static final String EQ = "eq"; - public static final String SW = "sw"; - public static final String EW = "ew"; - public static final String CO = "co"; - } - /** * Enum for error messages. */