Skip to content

Commit

Permalink
Improved service limit checking for services
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed Aug 26, 2022
1 parent 8620da5 commit 14502a3
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 50 deletions.
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.kapua.commons.configuration;

import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException;
import org.eclipse.kapua.commons.configuration.exception.ServiceConfigurationLimitExceededException;
import org.eclipse.kapua.commons.configuration.exception.ServiceConfigurationParentLimitExceededException;
import org.eclipse.kapua.commons.jpa.AbstractEntityCacheFactory;
Expand Down Expand Up @@ -128,6 +129,20 @@ protected boolean validateNewConfigValuesCoherence(KapuaTocd ocd, Map<String, Ob
return true;
}

/**
* Checks if the given scope {@link KapuaId} can have more entities for this {@link KapuaConfigurableService}.
*
* @param scopeId The scope {@link KapuaId} to check.
* @param entityType The entity type of this {@link KapuaConfigurableService}
* @throws KapuaException
* @since 2.0.0
*/
protected void checkAllowedEntities(KapuaId scopeId, String entityType) throws KapuaException {
if (allowedChildEntities(scopeId) <= 0) {
throw new KapuaMaxNumberOfItemsReachedException(entityType);
}
}

/**
* Gets the number of remaining allowed entity for the given scope, according to the {@link KapuaConfigurableService#getConfigValues(KapuaId)}
*
Expand Down Expand Up @@ -212,17 +227,4 @@ protected long allowedChildEntities(KapuaId scopeId, KapuaId targetScopeId, Map<
}
return Integer.MAX_VALUE;
}

//
// /**
// * Gets the {@link ServiceConfig} values for the given {@link Account}.
// * This method defaults to {@link Account#getId()}, but implementations can change it to use other attributes.
// *
// * @param account The account from which get the id.
// * @return The scoped configurations for the given {@link Account}.
// * @throws KapuaException
// */
// protected Map<String, Object> getConfigValues(Account account) throws KapuaException {
// return getConfigValues(account.getId());
// }
}
Expand Up @@ -20,7 +20,6 @@
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaIllegalAccessException;
import org.eclipse.kapua.KapuaIllegalArgumentException;
import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException;
import org.eclipse.kapua.commons.configuration.AbstractKapuaConfigurableResourceLimitedService;
import org.eclipse.kapua.commons.jpa.EntityManagerContainer;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
Expand Down Expand Up @@ -97,10 +96,8 @@ public Account create(AccountCreator accountCreator) throws KapuaException {
authorizationService.checkPermission(permissionFactory.newPermission(AccountDomains.ACCOUNT_DOMAIN, Actions.write, accountCreator.getScopeId()));

//
// Check child account policy
if (allowedChildEntities(accountCreator.getScopeId()) <= 0) {
throw new KapuaMaxNumberOfItemsReachedException("Accounts");
}
// Check entity limit
checkAllowedEntities(accountCreator.getScopeId(), "Accounts");

//
// Check if the parent account exists
Expand Down
Expand Up @@ -16,7 +16,6 @@
import org.eclipse.kapua.KapuaDuplicateNameException;
import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException;
import org.eclipse.kapua.commons.configuration.AbstractKapuaConfigurableResourceLimitedService;
import org.eclipse.kapua.commons.jpa.EntityManagerContainer;
import org.eclipse.kapua.event.ServiceEvent;
Expand Down Expand Up @@ -70,10 +69,8 @@ public Device create(DeviceCreator deviceCreator) throws KapuaException {
DeviceValidation.validateCreatePreconditions(deviceCreator);

//
// Check limits
if (allowedChildEntities(deviceCreator.getScopeId()) <= 0) {
throw new KapuaMaxNumberOfItemsReachedException("Devices");
}
// Check entity limit
checkAllowedEntities(deviceCreator.getScopeId(), "Devices");

//
// Check duplicate clientId
Expand Down
Expand Up @@ -15,7 +15,6 @@
import org.eclipse.kapua.KapuaDuplicateNameException;
import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException;
import org.eclipse.kapua.commons.configuration.AbstractKapuaConfigurableResourceLimitedService;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.commons.util.ArgumentValidator;
Expand Down Expand Up @@ -91,10 +90,8 @@ public Job create(JobCreator creator) throws KapuaException {
authorizationService.checkPermission(permissionFactory.newPermission(JobDomains.JOB_DOMAIN, Actions.write, creator.getScopeId()));

//
// Check limits
if (allowedChildEntities(creator.getScopeId()) <= 0) {
throw new KapuaMaxNumberOfItemsReachedException("Jobs");
}
// Check entity limit
checkAllowedEntities(creator.getScopeId(), "Jobs");

//
// Check duplicate name
Expand Down
Expand Up @@ -15,7 +15,6 @@
import org.eclipse.kapua.KapuaDuplicateNameException;
import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException;
import org.eclipse.kapua.commons.configuration.AbstractKapuaConfigurableResourceLimitedService;
import org.eclipse.kapua.commons.util.ArgumentValidator;
import org.eclipse.kapua.event.ServiceEvent;
Expand Down Expand Up @@ -74,10 +73,8 @@ public Group create(GroupCreator groupCreator) throws KapuaException {
authorizationService.checkPermission(permissionFactory.newPermission(AuthorizationDomains.GROUP_DOMAIN, Actions.write, groupCreator.getScopeId()));

//
// Check limits
if (allowedChildEntities(groupCreator.getScopeId()) <= 0) {
throw new KapuaMaxNumberOfItemsReachedException("Groups");
}
// Check entity limit
checkAllowedEntities(groupCreator.getScopeId(), "Groups");

//
// Check duplicate name
Expand Down
Expand Up @@ -16,7 +16,6 @@
import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaErrorCodes;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException;
import org.eclipse.kapua.commons.configuration.AbstractKapuaConfigurableResourceLimitedService;
import org.eclipse.kapua.commons.jpa.EntityManagerContainer;
import org.eclipse.kapua.commons.util.ArgumentValidator;
Expand Down Expand Up @@ -85,10 +84,8 @@ public Role create(RoleCreator roleCreator) throws KapuaException {
authorizationService.checkPermission(permissionFactory.newPermission(AuthorizationDomains.ROLE_DOMAIN, Actions.write, roleCreator.getScopeId()));

//
// Check limits
if (allowedChildEntities(roleCreator.getScopeId()) <= 0) {
throw new KapuaMaxNumberOfItemsReachedException("Roles");
}
// Check entity limit
checkAllowedEntities(roleCreator.getScopeId(), "Roles");

//
// Check duplicate name
Expand Down
Expand Up @@ -15,7 +15,6 @@
import org.eclipse.kapua.KapuaDuplicateNameException;
import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException;
import org.eclipse.kapua.commons.configuration.AbstractKapuaConfigurableResourceLimitedService;
import org.eclipse.kapua.commons.util.ArgumentValidator;
import org.eclipse.kapua.locator.KapuaProvider;
Expand Down Expand Up @@ -69,10 +68,8 @@ public Tag create(TagCreator tagCreator) throws KapuaException {
authorizationService.checkPermission(permissionFactory.newPermission(TagDomains.TAG_DOMAIN, Actions.write, tagCreator.getScopeId()));

//
// Check limit
if (allowedChildEntities(tagCreator.getScopeId()) <= 0) {
throw new KapuaMaxNumberOfItemsReachedException("Tags");
}
// Check entity limit
checkAllowedEntities(tagCreator.getScopeId(), "Tags");

//
// Check duplicate name
Expand Down
Expand Up @@ -19,7 +19,6 @@
import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaIllegalArgumentException;
import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException;
import org.eclipse.kapua.commons.configuration.AbstractKapuaConfigurableResourceLimitedService;
import org.eclipse.kapua.commons.jpa.EntityManagerContainer;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
Expand Down Expand Up @@ -90,16 +89,14 @@ public User create(UserCreator userCreator) throws KapuaException {
ArgumentValidator.isEmptyOrNull(userCreator.getExternalId(), "userCreator.externalId");
}

long remainingChildEntities = allowedChildEntities(userCreator.getScopeId());
if (remainingChildEntities <= 0) {
LOGGER.info("Exceeded child limit - remaining: {}", remainingChildEntities);
throw new KapuaMaxNumberOfItemsReachedException("Users");
}

//
// Check Access
authorizationService.checkPermission(permissionFactory.newPermission(UserDomains.USER_DOMAIN, Actions.write, userCreator.getScopeId()));

//
// Check entity limit
checkAllowedEntities(userCreator.getScopeId(), "Users");

//
// Check duplicate name
UserQuery query = new UserQueryImpl(userCreator.getScopeId());
Expand Down

0 comments on commit 14502a3

Please sign in to comment.