Skip to content

Commit 6fd979e

Browse files
author
Daniel Augusto Veronezi Salvador
committed
Merge branch 'quota-enable-list-quota-for-disabled-quota-accounts' into '4.18.0.0-scclouds'
Permitir que contas com o _plugin_ Quota desabilitado continuem acessando as APIs do Quota Closes apache#1686 See merge request scclouds/scclouds!710
2 parents 20f49f4 + 6845ba8 commit 6fd979e

File tree

27 files changed

+61
-129
lines changed

27 files changed

+61
-129
lines changed

api/src/main/java/org/apache/cloudstack/api/BaseCmd.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,10 @@ public Map<String, String> getFullUrlParams() {
380380
}
381381

382382
/**
383-
* Method intended to execute command's specific permissions and parameters. Should be overwritten by any class who needs specific validation.
383+
* To be overwritten by any class who needs specific validation
384384
*/
385-
public void validateCommandSpecificPermissionsAndParameters(final Map<String, String> params){
385+
public void validateSpecificParameters(final Map<String, String> params){
386+
// To be overwritten by any class who needs specific validation
386387
}
387388

388389
/**

api/src/main/java/org/apache/cloudstack/api/BaseListCmd.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ public Long getStartIndex() {
118118
}
119119

120120
@Override
121-
public void validateCommandSpecificPermissionsAndParameters(final Map<String, String> params){
122-
super.validateCommandSpecificPermissionsAndParameters(params);
121+
public void validateSpecificParameters(final Map<String, String> params){
122+
super.validateSpecificParameters(params);
123123

124124
final Object pageSizeObj = params.get(ApiConstants.PAGE_SIZE);
125125
Long pageSize = null;

framework/config/src/main/java/org/apache/cloudstack/framework/config/PluginAccessConfigs.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

framework/quota/src/main/java/org/apache/cloudstack/quota/constant/QuotaConfig.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
package org.apache.cloudstack.quota.constant;
1919

2020
import org.apache.cloudstack.framework.config.ConfigKey;
21-
import org.apache.cloudstack.framework.config.PluginAccessConfigs;
2221

23-
public interface QuotaConfig extends PluginAccessConfigs {
22+
public interface QuotaConfig {
23+
24+
public static final ConfigKey<Boolean> QuotaPluginEnabled = new ConfigKey<Boolean>("Advanced", Boolean.class, "quota.enable.service", "false",
25+
"Indicates whether Quota plugin is enabled or not.", true);
2426

2527
public static final ConfigKey<String> QuotaEnableEnforcement = new ConfigKey<String>("Advanced", String.class, "quota.enable.enforcement", "false",
2628
"Enable the usage quota enforcement, i.e. on true when exceeding quota the respective account will be locked.", true);

plugins/api/discovery/src/main/java/org/apache/cloudstack/discovery/ApiDiscoveryServiceImpl.java

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Set;
27-
import java.util.stream.Collectors;
2827

2928
import javax.inject.Inject;
3029

@@ -43,7 +42,6 @@
4342
import org.apache.cloudstack.api.response.ApiParameterResponse;
4443
import org.apache.cloudstack.api.response.ApiResponseResponse;
4544
import org.apache.cloudstack.api.response.ListResponse;
46-
import org.apache.cloudstack.framework.config.PluginAccessConfigs;
4745
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
4846
import org.apache.commons.lang3.StringUtils;
4947
import org.apache.log4j.Logger;
@@ -68,8 +66,6 @@ public class ApiDiscoveryServiceImpl extends ComponentLifecycleBase implements A
6866
List<PluggableService> _services = null;
6967
protected static Map<String, ApiDiscoveryResponse> s_apiNameDiscoveryResponseMap = null;
7068

71-
List<String> quotaCmdList;
72-
7369
@Inject
7470
AccountService accountService;
7571

@@ -88,9 +84,6 @@ public boolean start() {
8884
Set<Class<?>> cmdClasses = new LinkedHashSet<Class<?>>();
8985
for (PluggableService service : _services) {
9086
s_logger.debug(String.format("getting api commands of service: %s", service.getClass().getName()));
91-
if (service.getClass().getSimpleName().equals("QuotaServiceImpl") && PluginAccessConfigs.QuotaPluginEnabled.value()) {
92-
quotaCmdList = service.getCommands().parallelStream().map(cmdClass -> cmdClass.getAnnotation(APICommand.class).name()).collect(Collectors.toList());
93-
}
9487
cmdClasses.addAll(service.getCommands());
9588
}
9689
cmdClasses.addAll(this.getCommands());
@@ -254,25 +247,13 @@ public ListResponse<? extends BaseResponse> listApis(User user, String name) {
254247
List<ApiDiscoveryResponse> responseList = new ArrayList<>();
255248
List<String> apisAllowed = new ArrayList<>(s_apiNameDiscoveryResponseMap.keySet());
256249

257-
if (user == null) {
250+
if (user == null)
258251
return null;
259-
}
260-
261-
Account account = accountService.getAccount(user.getAccountId());
262-
if (account == null) {
263-
throw new PermissionDeniedException(String.format("The account with id [%s] for user [%s] is null.", user.getAccountId(), user));
264-
}
265252

266253
if (name != null) {
267254
if (!s_apiNameDiscoveryResponseMap.containsKey(name))
268255
return null;
269256

270-
if (account.getType() != Account.Type.ADMIN && PluginAccessConfigs.QuotaPluginEnabled.value() &&
271-
!PluginAccessConfigs.QuotaAccountEnabled.valueIn(user.getAccountId()) && quotaCmdList.parallelStream().anyMatch(name::equalsIgnoreCase)) {
272-
273-
return null;
274-
}
275-
276257
for (APIChecker apiChecker : _apiAccessCheckers) {
277258
try {
278259
apiChecker.checkAccess(user, name);
@@ -284,6 +265,11 @@ public ListResponse<? extends BaseResponse> listApis(User user, String name) {
284265
responseList.add(s_apiNameDiscoveryResponseMap.get(name));
285266

286267
} else {
268+
Account account = accountService.getAccount(user.getAccountId());
269+
if (account == null) {
270+
throw new PermissionDeniedException(String.format("The account with id [%s] for user [%s] is null.", user.getAccountId(), user));
271+
}
272+
287273
final Role role = roleService.findRole(account.getRoleId());
288274
if (role == null || role.getId() < 1L) {
289275
throw new PermissionDeniedException(String.format("The account [%s] has role null or unknown.",
@@ -294,11 +280,6 @@ public ListResponse<? extends BaseResponse> listApis(User user, String name) {
294280
s_logger.info(String.format("Account [%s] is Root Admin, all APIs are allowed.",
295281
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(account, "accountName", "uuid")));
296282
} else {
297-
if (PluginAccessConfigs.QuotaPluginEnabled.value() && !PluginAccessConfigs.QuotaAccountEnabled.valueIn(user.getAccountId())) {
298-
apisAllowed.removeAll(quotaCmdList);
299-
apisAllowed.add("quotaIsEnabled");
300-
}
301-
302283
for (APIChecker apiChecker : _apiAccessCheckers) {
303284
apisAllowed = apiChecker.getApisAllowedToUser(role, user, apisAllowed);
304285
}

plugins/database/quota/src/main/java/org/apache/cloudstack/api/command/QuotaBalanceCmd.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
import com.cloud.exception.InvalidParameterValueException;
2424
import com.cloud.user.Account;
25-
import org.apache.log4j.Logger;
2625
import org.apache.cloudstack.api.APICommand;
2726
import org.apache.cloudstack.api.ApiConstants;
27+
import org.apache.cloudstack.api.BaseCmd;
2828
import org.apache.cloudstack.api.Parameter;
2929
import org.apache.cloudstack.api.response.AccountResponse;
3030
import org.apache.cloudstack.api.response.DomainResponse;
@@ -35,9 +35,8 @@
3535
import org.apache.cloudstack.quota.QuotaService;
3636

3737
@APICommand(name = "quotaBalance", responseObject = QuotaStatementItemResponse.class, description = "Create quota balance statements for the account.", since = "4.7.0",
38-
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
39-
public class QuotaBalanceCmd extends QuotaBaseCmd {
40-
public static final Logger s_logger = Logger.getLogger(QuotaBalanceCmd.class);
38+
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
39+
public class QuotaBalanceCmd extends BaseCmd {
4140

4241
@Parameter(name = ApiConstants.ACCOUNT, type = CommandType.STRING, description = "Account's name for which statement will be generated. Deprecated, please use " +
4342
ApiConstants.ACCOUNT_ID + " instead.")

plugins/database/quota/src/main/java/org/apache/cloudstack/api/command/QuotaBaseCmd.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
public abstract class QuotaBaseCmd extends BaseCmd {
2929

3030
@Override
31-
public void validateCommandSpecificPermissionsAndParameters(final Map<String, String> params) {
31+
public void validateSpecificParameters(final Map<String, String> params) {
3232
validateQuotaAccountEnabled(this);
33-
super.validateCommandSpecificPermissionsAndParameters(params);
33+
super.validateSpecificParameters(params);
3434
}
3535

3636
public static void validateQuotaAccountEnabled(BaseCmd cmd) {
@@ -40,7 +40,7 @@ public static void validateQuotaAccountEnabled(BaseCmd cmd) {
4040
return;
4141
}
4242

43-
if (!QuotaConfig.QuotaPluginEnabled.value() || !QuotaConfig.QuotaAccountEnabled.valueIn(caller.getAccountId())){
43+
if (!QuotaConfig.QuotaPluginEnabled.value()){
4444
throw new UnavailableCommandException(String.format("The API [%s] is not available for account %s.", cmd.getActualCommandName(),
4545
ReflectionToStringBuilderUtils.reflectOnlySelectedFields(caller, "accountName", "uuid")));
4646
}

plugins/database/quota/src/main/java/org/apache/cloudstack/api/command/QuotaBaseListCmd.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

plugins/database/quota/src/main/java/org/apache/cloudstack/api/command/QuotaConfigureEmailCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
@APICommand(name = QuotaConfigureEmailCmd.API_NAME, responseObject = QuotaConfigureEmailResponse.class, description = "Configure a quota email template", since = "4.16.0.11",
3232
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
33-
public class QuotaConfigureEmailCmd extends QuotaBaseCmd {
33+
public class QuotaConfigureEmailCmd extends BaseCmd {
3434

3535
public static final String API_NAME = "quotaConfigureEmail";
3636

plugins/database/quota/src/main/java/org/apache/cloudstack/api/command/QuotaCreditsCmd.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.cloudstack.api.APICommand;
2222
import org.apache.cloudstack.api.ApiConstants;
2323
import org.apache.cloudstack.api.ApiErrorCode;
24+
import org.apache.cloudstack.api.BaseCmd;
2425
import org.apache.cloudstack.api.Parameter;
2526
import org.apache.cloudstack.api.ServerApiException;
2627
import org.apache.cloudstack.api.response.AccountResponse;
@@ -36,7 +37,7 @@
3637
import java.util.Date;
3738

3839
@APICommand(name = "quotaCredits", responseObject = QuotaCreditsResponse.class, description = "Add +-credits to an account", since = "4.7.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
39-
public class QuotaCreditsCmd extends QuotaBaseCmd {
40+
public class QuotaCreditsCmd extends BaseCmd {
4041

4142
@Inject
4243
QuotaResponseBuilder _responseBuilder;

0 commit comments

Comments
 (0)