Skip to content

Commit

Permalink
Merge pull request #400 from PasinduYeshan/fix/bulk-api
Browse files Browse the repository at this point in the history
Enforce maximum operation count for scim2/Bulk API
  • Loading branch information
piraveena authored Mar 4, 2024
2 parents 50b8497 + 8266fa6 commit afc366d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public class ResponseCodeConstants {

public static final int CODE_PAYLOAD_TOO_LARGE = 413;
public static final String DESC_PAYLOAD_TOO_LARGE = "{\"maxOperations\": 1000,\"maxPayloadSize\": 1048576}";
public static final String ERROR_DESC_MAX_OPERATIONS_EXCEEDED = "The number of operations in the bulk " +
"request: %d exceeds the maximum total number of operations count: %d.";

public static final int CODE_INTERNAL_ERROR = 500;
public static final String DESC_INTERNAL_ERROR = "An internal error.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.charon3.core.config.CharonConfiguration;
import org.wso2.charon3.core.config.SCIMConfigConstants;
import org.wso2.charon3.core.encoder.JSONDecoder;
import org.wso2.charon3.core.encoder.JSONEncoder;
import org.wso2.charon3.core.exceptions.BadRequestException;
import org.wso2.charon3.core.exceptions.CharonException;
import org.wso2.charon3.core.exceptions.InternalErrorException;
import org.wso2.charon3.core.exceptions.PayloadTooLargeException;
import org.wso2.charon3.core.extensions.RoleManager;
import org.wso2.charon3.core.extensions.RoleV2Manager;
import org.wso2.charon3.core.extensions.UserManager;
Expand Down Expand Up @@ -80,6 +83,24 @@ public SCIMResponse processBulkData(String data, UserManager userManager) {
bulkRequestProcessor.setFailOnError(bulkRequestDataObject.getFailOnErrors());
bulkRequestProcessor.setUserManager(userManager);

int maxOperationCount =
(Integer) CharonConfiguration.getInstance().getConfig().get(SCIMConfigConstants.MAX_OPERATIONS);
int totalOperationCount = bulkRequestDataObject.getUserOperationRequests().size() +
bulkRequestDataObject.getGroupOperationRequests().size() +
bulkRequestDataObject.getRoleOperationRequests().size() +
bulkRequestDataObject.getRoleV2OperationRequests().size();
if (totalOperationCount > maxOperationCount) {
if (logger.isDebugEnabled()) {
logger.debug(String.format(ResponseCodeConstants.ERROR_DESC_MAX_OPERATIONS_EXCEEDED,
totalOperationCount,
maxOperationCount));
}
throw new PayloadTooLargeException(
String.format(ResponseCodeConstants.ERROR_DESC_MAX_OPERATIONS_EXCEEDED,
totalOperationCount,
maxOperationCount));
}

// Get bulk response data.
bulkResponseData = bulkRequestProcessor.processBulkRequests(bulkRequestDataObject);
//encode the BulkResponseData object
Expand All @@ -93,7 +114,7 @@ public SCIMResponse processBulkData(String data, UserManager userManager) {
// Create the final response.
return new SCIMResponse(ResponseCodeConstants.CODE_OK, finalEncodedResponse, responseHeaders);

} catch (CharonException | BadRequestException | InternalErrorException e) {
} catch (CharonException | BadRequestException | InternalErrorException | PayloadTooLargeException e) {
return AbstractResourceManager.encodeSCIMException(e);
}
}
Expand Down

0 comments on commit afc366d

Please sign in to comment.