Skip to content

Commit 63c708a

Browse files
committed
Unhide setting 'js.interpretation.enabled'
1 parent e2d18c0 commit 63c708a

File tree

9 files changed

+94
-63
lines changed

9 files changed

+94
-63
lines changed

api/src/main/java/com/cloud/server/ManagementService.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
import org.apache.cloudstack.api.command.user.vmgroup.UpdateVMGroupCmd;
7272
import org.apache.cloudstack.config.Configuration;
7373
import org.apache.cloudstack.config.ConfigurationGroup;
74-
import org.apache.cloudstack.framework.config.ConfigKey;
7574

7675
import com.cloud.alert.Alert;
7776
import com.cloud.capacity.Capacity;
@@ -108,14 +107,6 @@
108107
public interface ManagementService {
109108
static final String Name = "management-server";
110109

111-
ConfigKey<Boolean> JsInterpretationEnabled = new ConfigKey<>("Hidden"
112-
, Boolean.class
113-
, "js.interpretation.enabled"
114-
, "false"
115-
, "Enable/Disable all JavaScript interpretation related functionalities to create or update Javascript rules."
116-
, false
117-
, ConfigKey.Scope.Global);
118-
119110
/**
120111
* returns the a map of the names/values in the configuration table
121112
*
@@ -534,6 +525,4 @@ VirtualMachine upgradeSystemVM(ScaleSystemVMCmd cmd) throws ResourceUnavailableE
534525

535526
boolean removeManagementServer(RemoveManagementServerCmd cmd);
536527

537-
void checkJsInterpretationAllowedIfNeededForParameterValue(String paramName, boolean paramValue);
538-
539528
}

engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42210to42300.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
package com.cloud.upgrade.dao;
1818

1919
import java.io.InputStream;
20+
import java.sql.Connection;
21+
import java.sql.PreparedStatement;
22+
import java.sql.ResultSet;
23+
import java.sql.SQLException;
2024

25+
import com.cloud.utils.crypt.DBEncryptionUtil;
2126
import com.cloud.utils.exception.CloudRuntimeException;
2227

2328
public class Upgrade42210to42300 extends DbUpgradeAbstractImpl implements DbUpgrade, DbUpgradeSystemVmTemplate {
@@ -42,4 +47,46 @@ public InputStream[] getPrepareScripts() {
4247

4348
return new InputStream[] {script};
4449
}
50+
51+
@Override
52+
public void performDataMigration(Connection conn) {
53+
unhideJsInterpretationEnabled(conn);
54+
}
55+
56+
protected void unhideJsInterpretationEnabled(Connection conn) {
57+
String value = getJsInterpretationEnabled(conn);
58+
if (value != null) {
59+
updateJsInterpretationEnabledFields(conn, value);
60+
}
61+
}
62+
63+
protected String getJsInterpretationEnabled(Connection conn) {
64+
String query = "SELECT value FROM cloud.configuration WHERE name = 'js.interpretation.enabled' AND category = 'Hidden';";
65+
66+
try (PreparedStatement pstmt = conn.prepareStatement(query)) {
67+
ResultSet rs = pstmt.executeQuery();
68+
if (rs.next()) {
69+
return rs.getString("value");
70+
}
71+
logger.debug("Unable to retrieve value of hidden configuration 'js.interpretation.enabled'. The configuration may already be unhidden.");
72+
return null;
73+
} catch (SQLException e) {
74+
throw new CloudRuntimeException("Error while retrieving value of hidden configuration 'js.interpretation.enabled'.", e);
75+
}
76+
}
77+
78+
protected void updateJsInterpretationEnabledFields(Connection conn, String encryptedValue) {
79+
String query = "UPDATE cloud.configuration SET value = ?, category = 'System', component = 'JsInterpreter', is_dynamic = 1 WHERE name = 'js.interpretation.enabled';";
80+
81+
try (PreparedStatement pstmt = conn.prepareStatement(query)) {
82+
String decryptedValue = DBEncryptionUtil.decrypt(encryptedValue);
83+
logger.info("Updating setting 'js.interpretation.enabled' to decrypted value [{}], and category 'System', component 'JsInterpreter', and is_dynamic '1'.", decryptedValue);
84+
pstmt.setString(1, decryptedValue);
85+
pstmt.executeUpdate();
86+
} catch (SQLException e) {
87+
throw new CloudRuntimeException("Error while unhiding configuration 'js.interpretation.enabled'.", e);
88+
} catch (CloudRuntimeException e) {
89+
logger.warn("Error while decrypting configuration 'js.interpretation.enabled'. The configuration may already be decrypted.");
90+
}
91+
}
4592
}

plugins/database/quota/src/main/java/org/apache/cloudstack/api/response/QuotaResponseBuilderImpl.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.cloud.user.UserVO;
5050
import com.cloud.utils.DateUtil;
5151
import com.cloud.utils.exception.CloudRuntimeException;
52+
import org.apache.cloudstack.api.ApiConstants;
5253
import org.apache.cloudstack.api.ApiErrorCode;
5354
import org.apache.cloudstack.api.ServerApiException;
5455
import org.apache.cloudstack.api.command.QuotaBalanceCmd;
@@ -163,12 +164,6 @@ public class QuotaResponseBuilderImpl implements QuotaResponseBuilder {
163164

164165
private Set<Account.Type> accountTypesThatCanListAllQuotaSummaries = Sets.newHashSet(Account.Type.ADMIN, Account.Type.DOMAIN_ADMIN);
165166

166-
protected void checkActivationRulesAllowed(String activationRule) {
167-
if (!_quotaService.isJsInterpretationEnabled() && StringUtils.isNotEmpty(activationRule)) {
168-
throw new PermissionDeniedException("Quota Tariff Activation Rule cannot be set, as Javascript interpretation is disabled in the configuration.");
169-
}
170-
}
171-
172167
@Override
173168
public QuotaTariffResponse createQuotaTariffResponse(QuotaTariffVO tariff, boolean returnActivationRule) {
174169
final QuotaTariffResponse response = new QuotaTariffResponse();
@@ -501,15 +496,14 @@ public QuotaTariffVO updateQuotaTariffPlan(QuotaTariffUpdateCmd cmd) {
501496
Integer position = cmd.getPosition();
502497

503498
warnQuotaTariffUpdateDeprecatedFields(cmd);
499+
jsInterpreterHelper.ensureInterpreterEnabledIfParameterProvided(ApiConstants.ACTIVATION_RULE, StringUtils.isNotBlank(activationRule));
504500

505501
QuotaTariffVO currentQuotaTariff = _quotaTariffDao.findByName(name);
506502

507503
if (currentQuotaTariff == null) {
508504
throw new InvalidParameterValueException(String.format("There is no quota tariffs with name [%s].", name));
509505
}
510506

511-
checkActivationRulesAllowed(activationRule);
512-
513507
Date currentQuotaTariffStartDate = currentQuotaTariff.getEffectiveOn();
514508

515509
currentQuotaTariff.setRemoved(now);
@@ -758,14 +752,14 @@ public QuotaTariffVO createQuotaTariff(QuotaTariffCreateCmd cmd) {
758752
String activationRule = cmd.getActivationRule();
759753
Integer position = ObjectUtils.defaultIfNull(cmd.getPosition(), 1);
760754

755+
jsInterpreterHelper.ensureInterpreterEnabledIfParameterProvided(ApiConstants.ACTIVATION_RULE, StringUtils.isNotBlank(activationRule));
756+
761757
QuotaTariffVO currentQuotaTariff = _quotaTariffDao.findByName(name);
762758

763759
if (currentQuotaTariff != null) {
764760
throw new InvalidParameterValueException(String.format("A quota tariff with name [%s] already exist.", name));
765761
}
766762

767-
checkActivationRulesAllowed(activationRule);
768-
769763
if (startDate.compareTo(now) < 0) {
770764
throw new InvalidParameterValueException(String.format("The value passed as Quota tariff's start date is in the past: [%s]. " +
771765
"Please, inform a date in the future or do not pass the parameter to use the current date and time.", startDate));

plugins/database/quota/src/main/java/org/apache/cloudstack/quota/QuotaService.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,4 @@ public interface QuotaService extends PluggableService {
4040

4141
boolean saveQuotaAccount(AccountVO account, BigDecimal aggrUsage, Date endDate);
4242

43-
boolean isJsInterpretationEnabled();
44-
4543
}

plugins/database/quota/src/main/java/org/apache/cloudstack/quota/QuotaServiceImpl.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import com.cloud.domain.dao.DomainDao;
6565
import com.cloud.exception.InvalidParameterValueException;
6666
import com.cloud.exception.PermissionDeniedException;
67-
import com.cloud.server.ManagementService;
6867
import com.cloud.user.Account;
6968
import com.cloud.user.AccountVO;
7069
import com.cloud.user.dao.AccountDao;
@@ -95,8 +94,6 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
9594

9695
private TimeZone _usageTimezone;
9796

98-
private boolean jsInterpretationEnabled = false;
99-
10097
public QuotaServiceImpl() {
10198
super();
10299
}
@@ -108,8 +105,6 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
108105
String timeZoneStr = ObjectUtils.defaultIfNull(_configDao.getValue(Config.UsageAggregationTimezone.toString()), "GMT");
109106
_usageTimezone = TimeZone.getTimeZone(timeZoneStr);
110107

111-
jsInterpretationEnabled = ManagementService.JsInterpretationEnabled.value();
112-
113108
return true;
114109
}
115110

@@ -298,9 +293,4 @@ public void setMinBalance(Long accountId, Double balance) {
298293
_quotaAcc.updateQuotaAccount(accountId, acc);
299294
}
300295
}
301-
302-
@Override
303-
public boolean isJsInterpretationEnabled() {
304-
return jsInterpretationEnabled;
305-
}
306296
}

server/src/main/java/com/cloud/resource/ResourceManagerImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import org.apache.cloudstack.framework.extensions.vo.ExtensionResourceMapVO;
7979
import org.apache.cloudstack.framework.extensions.vo.ExtensionVO;
8080
import org.apache.cloudstack.gpu.GpuService;
81+
import org.apache.cloudstack.jsinterpreter.JsInterpreterHelper;
8182
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
8283
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
8384
import org.apache.cloudstack.utils.identity.ManagementServerNode;
@@ -310,6 +311,8 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager,
310311
private GpuService gpuService;
311312
@Inject
312313
ManagementService managementService;
314+
@Inject
315+
JsInterpreterHelper jsInterpreterHelper;
313316

314317
private List<? extends Discoverer> _discoverers;
315318

@@ -2818,9 +2821,6 @@ private void updateHostTags(HostVO host, Long hostId, List<String> hostTags, Boo
28182821

28192822
@Override
28202823
public Host updateHost(final UpdateHostCmd cmd) throws NoTransitionException {
2821-
managementService.checkJsInterpretationAllowedIfNeededForParameterValue(ApiConstants.IS_TAG_A_RULE,
2822-
Boolean.TRUE.equals(cmd.getIsTagARule()));
2823-
28242824
return updateHost(cmd.getId(), cmd.getName(), cmd.getOsCategoryId(),
28252825
cmd.getAllocationState(), cmd.getUrl(), cmd.getHostTags(), cmd.getIsTagARule(), cmd.getAnnotation(), false,
28262826
cmd.getExternalDetails(), cmd.isCleanupExternalDetails());
@@ -2830,6 +2830,8 @@ private Host updateHost(Long hostId, String name, Long guestOSCategoryId, String
28302830
String url, List<String> hostTags, Boolean isTagARule, String annotation,
28312831
boolean isUpdateFromHostHealthCheck, Map<String, String> externalDetails,
28322832
boolean cleanupExternalDetails) throws NoTransitionException {
2833+
jsInterpreterHelper.ensureInterpreterEnabledIfParameterProvided(ApiConstants.IS_TAG_A_RULE, Boolean.TRUE.equals(isTagARule));
2834+
28332835
// Verify that the host exists
28342836
final HostVO host = _hostDao.findById(hostId);
28352837
if (host == null) {

server/src/main/java/com/cloud/server/ManagementServerImpl.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,8 +1082,6 @@ public class ManagementServerImpl extends MutualExclusiveIdsManagerBase implemen
10821082

10831083
protected List<DeploymentPlanner> _planners;
10841084

1085-
private boolean jsInterpretationEnabled = false;
1086-
10871085
private final List<HypervisorType> supportedHypervisors = new ArrayList<>();
10881086

10891087
public List<DeploymentPlanner> getPlanners() {
@@ -1164,8 +1162,6 @@ public boolean configure(final String name, final Map<String, Object> params) th
11641162
supportedHypervisors.add(HypervisorType.KVM);
11651163
supportedHypervisors.add(HypervisorType.XenServer);
11661164

1167-
jsInterpretationEnabled = JsInterpretationEnabled.value();
1168-
11691165
return true;
11701166
}
11711167

@@ -4374,10 +4370,8 @@ public List<Class<?>> getCommands() {
43744370
cmdList.add(ListGuestVlansCmd.class);
43754371
cmdList.add(AssignVolumeCmd.class);
43764372
cmdList.add(ListSecondaryStorageSelectorsCmd.class);
4377-
if (jsInterpretationEnabled) {
4378-
cmdList.add(CreateSecondaryStorageSelectorCmd.class);
4379-
cmdList.add(UpdateSecondaryStorageSelectorCmd.class);
4380-
}
4373+
cmdList.add(CreateSecondaryStorageSelectorCmd.class);
4374+
cmdList.add(UpdateSecondaryStorageSelectorCmd.class);
43814375
cmdList.add(RemoveSecondaryStorageSelectorCmd.class);
43824376
cmdList.add(ListAffectedVmsForStorageScopeChangeCmd.class);
43834377
cmdList.add(ListGuiThemesCmd.class);
@@ -4435,8 +4429,7 @@ public String getConfigComponentName() {
44354429

44364430
@Override
44374431
public ConfigKey<?>[] getConfigKeys() {
4438-
return new ConfigKey<?>[] {exposeCloudStackVersionInApiXmlResponse, exposeCloudStackVersionInApiListCapabilities, vmPasswordLength, sshKeyLength, humanReadableSizes, customCsIdentifier,
4439-
JsInterpretationEnabled};
4432+
return new ConfigKey<?>[] {exposeCloudStackVersionInApiXmlResponse, exposeCloudStackVersionInApiListCapabilities, vmPasswordLength, sshKeyLength, humanReadableSizes, customCsIdentifier};
44404433
}
44414434

44424435
protected class EventPurgeTask extends ManagedContextRunnable {
@@ -6002,13 +5995,5 @@ public boolean removeManagementServer(RemoveManagementServerCmd cmd) {
60025995
public Answer getExternalVmConsole(VirtualMachine vm, Host host) {
60035996
return extensionsManager.getInstanceConsole(vm, host);
60045997
}
6005-
@Override
6006-
public void checkJsInterpretationAllowedIfNeededForParameterValue(String paramName, boolean paramValue) {
6007-
if (!paramValue || jsInterpretationEnabled) {
6008-
return;
6009-
}
6010-
throw new InvalidParameterValueException(String.format(
6011-
"The parameter %s cannot be set to true as JS interpretation is disabled",
6012-
paramName));
6013-
}
5998+
60145999
}

server/src/main/java/com/cloud/storage/StorageManagerImpl.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
import org.apache.cloudstack.framework.config.ConfigKey;
112112
import org.apache.cloudstack.framework.config.Configurable;
113113
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
114+
import org.apache.cloudstack.jsinterpreter.JsInterpreterHelper;
114115
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
115116
import org.apache.cloudstack.management.ManagementServerHost;
116117
import org.apache.cloudstack.resourcedetail.dao.DiskOfferingDetailsDao;
@@ -417,6 +418,8 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C
417418
StorageManager storageManager;
418419
@Inject
419420
ManagementService managementService;
421+
@Inject
422+
JsInterpreterHelper jsInterpreterHelper;
420423

421424
protected List<StoragePoolDiscoverer> _discoverers;
422425

@@ -962,6 +965,8 @@ protected void checkNFSMountOptionsForUpdate(Map<String, String> details, Storag
962965

963966
@Override
964967
public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws ResourceInUseException, IllegalArgumentException, UnknownHostException, ResourceUnavailableException {
968+
jsInterpreterHelper.ensureInterpreterEnabledIfParameterProvided(ApiConstants.IS_TAG_A_RULE, Boolean.TRUE.equals(cmd.isTagARule()));
969+
965970
String providerName = cmd.getStorageProviderName();
966971
Map<String,String> uriParams = extractUriParamsAsMap(cmd.getUrl());
967972
boolean isFileScheme = "file".equals(uriParams.get("scheme"));
@@ -1034,9 +1039,6 @@ public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws Resource
10341039
throw new PermissionDeniedException(String.format("Cannot perform this operation, Zone is currently disabled: %s", zone));
10351040
}
10361041

1037-
managementService.checkJsInterpretationAllowedIfNeededForParameterValue(ApiConstants.IS_TAG_A_RULE,
1038-
Boolean.TRUE.equals(cmd.isTagARule()));
1039-
10401042
Map<String, Object> params = new HashMap<>();
10411043
params.put("zoneId", zone.getId());
10421044
params.put("clusterId", clusterId);
@@ -1218,11 +1220,9 @@ public StoragePool enablePrimaryStoragePool(Long id) {
12181220
@ActionEvent(eventType = EventTypes.EVENT_UPDATE_PRIMARY_STORAGE, eventDescription = "update storage pool")
12191221
public PrimaryDataStoreInfo updateStoragePool(UpdateStoragePoolCmd cmd) throws IllegalArgumentException {
12201222
// Input validation
1221-
Long id = cmd.getId();
1222-
1223-
managementService.checkJsInterpretationAllowedIfNeededForParameterValue(ApiConstants.IS_TAG_A_RULE,
1224-
Boolean.TRUE.equals(cmd.isTagARule()));
1223+
jsInterpreterHelper.ensureInterpreterEnabledIfParameterProvided(ApiConstants.IS_TAG_A_RULE, Boolean.TRUE.equals(cmd.isTagARule()));
12251224

1225+
Long id = cmd.getId();
12261226
StoragePoolVO pool = _storagePoolDao.findById(id);
12271227
if (pool == null) {
12281228
throw new IllegalArgumentException("Unable to find storage pool with ID: " + id);
@@ -2751,6 +2751,7 @@ protected void validateHeuristicRule(String heuristicRule) {
27512751
if (StringUtils.isBlank(heuristicRule)) {
27522752
throw new IllegalArgumentException("Unable to create a new secondary storage selector as the given heuristic rule is blank.");
27532753
}
2754+
jsInterpreterHelper.ensureInterpreterEnabledIfParameterProvided(ApiConstants.HEURISTIC_RULE, true);
27542755
}
27552756

27562757
public void syncDatastoreClusterStoragePool(long datastoreClusterPoolId, List<ModifyStoragePoolAnswer> childDatastoreAnswerList, long hostId) {

server/src/main/java/org/apache/cloudstack/jsinterpreter/JsInterpreterHelper.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717

1818
package org.apache.cloudstack.jsinterpreter;
1919

20+
import com.cloud.exception.InvalidParameterValueException;
2021
import com.fasterxml.jackson.core.JsonProcessingException;
2122
import com.fasterxml.jackson.databind.JsonNode;
2223
import com.fasterxml.jackson.databind.ObjectMapper;
24+
import org.apache.cloudstack.framework.config.ConfigKey;
25+
import org.apache.cloudstack.framework.config.Configurable;
26+
import org.apache.cloudstack.utils.jsinterpreter.JsInterpreter;
2327
import org.apache.commons.lang3.StringUtils;
2428
import org.apache.logging.log4j.Logger;
2529
import org.apache.logging.log4j.LogManager;
@@ -36,9 +40,13 @@
3640
import java.util.regex.Matcher;
3741
import java.util.regex.Pattern;
3842

39-
public class JsInterpreterHelper {
43+
public class JsInterpreterHelper implements Configurable {
4044
private final Logger logger = LogManager.getLogger(getClass());
4145

46+
public static final ConfigKey<Boolean> JS_INTERPRETATION_ENABLED = new ConfigKey<>(ConfigKey.CATEGORY_SYSTEM, Boolean.class, "js.interpretation.enabled",
47+
"false", "Enable/disable all JavaScript interpretation related functionalities.",
48+
true, ConfigKey.Scope.Global);
49+
4250
private static final String NAME = "name";
4351
private static final String PROPERTY = "property";
4452
private static final String TYPE = "type";
@@ -237,4 +245,21 @@ public Set<String> getVariables() {
237245
public void setVariables(Set<String> variables) {
238246
this.variables = variables;
239247
}
248+
249+
public void ensureInterpreterEnabledIfParameterProvided(String paramName, boolean paramProvided) {
250+
if (paramProvided && !JS_INTERPRETATION_ENABLED.value()) {
251+
throw new InvalidParameterValueException(String.format(
252+
"'%s' cannot be set because JavaScript interpretation is disabled in setting '%s'.", paramName, JS_INTERPRETATION_ENABLED.key()));
253+
}
254+
}
255+
256+
@Override
257+
public String getConfigComponentName() {
258+
return JsInterpreter.class.getSimpleName();
259+
}
260+
261+
@Override
262+
public ConfigKey<?>[] getConfigKeys() {
263+
return new ConfigKey<?>[] { JS_INTERPRETATION_ENABLED };
264+
}
240265
}

0 commit comments

Comments
 (0)