Skip to content

Commit 007be77

Browse files
committed
Unhide setting 'js.interpretation.enabled'
1 parent 3d7d412 commit 007be77

File tree

9 files changed

+111
-66
lines changed

9 files changed

+111
-66
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
@@ -65,7 +65,6 @@
6565
import org.apache.cloudstack.api.command.user.vmgroup.UpdateVMGroupCmd;
6666
import org.apache.cloudstack.config.Configuration;
6767
import org.apache.cloudstack.config.ConfigurationGroup;
68-
import org.apache.cloudstack.framework.config.ConfigKey;
6968

7069
import com.cloud.alert.Alert;
7170
import com.cloud.capacity.Capacity;
@@ -102,14 +101,6 @@
102101
public interface ManagementService {
103102
static final String Name = "management-server";
104103

105-
ConfigKey<Boolean> JsInterpretationEnabled = new ConfigKey<>("Hidden"
106-
, Boolean.class
107-
, "js.interpretation.enabled"
108-
, "false"
109-
, "Enable/Disable all JavaScript interpretation related functionalities to create or update Javascript rules."
110-
, false
111-
, ConfigKey.Scope.Global);
112-
113104
/**
114105
* returns the a map of the names/values in the configuration table
115106
*
@@ -506,6 +497,4 @@ VirtualMachine upgradeSystemVM(ScaleSystemVMCmd cmd) throws ResourceUnavailableE
506497

507498
Pair<Boolean, String> patchSystemVM(PatchSystemVMCmd cmd);
508499

509-
void checkJsInterpretationAllowedIfNeededForParameterValue(String paramName, boolean paramValue);
510-
511500
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818

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

25+
import com.cloud.utils.crypt.DBEncryptionUtil;
2226
import com.cloud.utils.exception.CloudRuntimeException;
2327

2428
public class Upgrade42020to42030 extends DbUpgradeAbstractImpl implements DbUpgrade, DbUpgradeSystemVmTemplate {
@@ -51,6 +55,44 @@ public InputStream[] getPrepareScripts() {
5155

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

5698
@Override

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
import javax.inject.Inject;
4141

42+
import org.apache.cloudstack.api.ApiConstants;
4243
import org.apache.cloudstack.api.ApiErrorCode;
4344
import org.apache.cloudstack.api.ServerApiException;
4445
import org.apache.cloudstack.api.command.QuotaBalanceCmd;
@@ -52,6 +53,7 @@
5253
import org.apache.cloudstack.api.command.QuotaTariffUpdateCmd;
5354
import org.apache.cloudstack.context.CallContext;
5455
import org.apache.cloudstack.discovery.ApiDiscoveryService;
56+
import org.apache.cloudstack.jsinterpreter.JsInterpreterHelper;
5557
import org.apache.cloudstack.quota.QuotaManager;
5658
import org.apache.cloudstack.quota.QuotaManagerImpl;
5759
import org.apache.cloudstack.quota.QuotaService;
@@ -90,7 +92,6 @@
9092
import com.cloud.event.ActionEvent;
9193
import com.cloud.event.EventTypes;
9294
import com.cloud.exception.InvalidParameterValueException;
93-
import com.cloud.exception.PermissionDeniedException;
9495
import com.cloud.user.Account;
9596
import com.cloud.user.AccountManager;
9697
import com.cloud.user.AccountVO;
@@ -140,11 +141,8 @@ public class QuotaResponseBuilderImpl implements QuotaResponseBuilder {
140141
@Inject
141142
private ApiDiscoveryService apiDiscoveryService;
142143

143-
protected void checkActivationRulesAllowed(String activationRule) {
144-
if (!_quotaService.isJsInterpretationEnabled() && StringUtils.isNotEmpty(activationRule)) {
145-
throw new PermissionDeniedException("Quota Tariff Activation Rule cannot be set, as Javascript interpretation is disabled in the configuration.");
146-
}
147-
}
144+
@Inject
145+
private JsInterpreterHelper jsInterpreterHelper;
148146

149147
@Override
150148
public QuotaTariffResponse createQuotaTariffResponse(QuotaTariffVO tariff, boolean returnActivationRule) {
@@ -440,15 +438,14 @@ public QuotaTariffVO updateQuotaTariffPlan(QuotaTariffUpdateCmd cmd) {
440438
Integer position = cmd.getPosition();
441439

442440
warnQuotaTariffUpdateDeprecatedFields(cmd);
441+
jsInterpreterHelper.ensureInterpreterEnabledIfParameterProvided(ApiConstants.ACTIVATION_RULE, StringUtils.isNotBlank(activationRule));
443442

444443
QuotaTariffVO currentQuotaTariff = _quotaTariffDao.findByName(name);
445444

446445
if (currentQuotaTariff == null) {
447446
throw new InvalidParameterValueException(String.format("There is no quota tariffs with name [%s].", name));
448447
}
449448

450-
checkActivationRulesAllowed(activationRule);
451-
452449
Date currentQuotaTariffStartDate = currentQuotaTariff.getEffectiveOn();
453450

454451
currentQuotaTariff.setRemoved(now);
@@ -699,14 +696,14 @@ public QuotaTariffVO createQuotaTariff(QuotaTariffCreateCmd cmd) {
699696
String activationRule = cmd.getActivationRule();
700697
Integer position = ObjectUtils.defaultIfNull(cmd.getPosition(), 1);
701698

699+
jsInterpreterHelper.ensureInterpreterEnabledIfParameterProvided(ApiConstants.ACTIVATION_RULE, StringUtils.isNotBlank(activationRule));
700+
702701
QuotaTariffVO currentQuotaTariff = _quotaTariffDao.findByName(name);
703702

704703
if (currentQuotaTariff != null) {
705704
throw new InvalidParameterValueException(String.format("A quota tariff with name [%s] already exist.", name));
706705
}
707706

708-
checkActivationRulesAllowed(activationRule);
709-
710707
if (startDate.compareTo(now) < 0) {
711708
throw new InvalidParameterValueException(String.format("The value passed as Quota tariff's start date is in the past: [%s]. " +
712709
"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
@@ -60,7 +60,6 @@
6060
import com.cloud.domain.dao.DomainDao;
6161
import com.cloud.exception.InvalidParameterValueException;
6262
import com.cloud.exception.PermissionDeniedException;
63-
import com.cloud.server.ManagementService;
6463
import com.cloud.user.Account;
6564
import com.cloud.user.AccountVO;
6665
import com.cloud.user.dao.AccountDao;
@@ -87,8 +86,6 @@ public class QuotaServiceImpl extends ManagerBase implements QuotaService, Confi
8786

8887
private TimeZone _usageTimezone;
8988

90-
private boolean jsInterpretationEnabled = false;
91-
9289
public QuotaServiceImpl() {
9390
super();
9491
}
@@ -100,8 +97,6 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
10097
String timeZoneStr = ObjectUtils.defaultIfNull(_configDao.getValue(Config.UsageAggregationTimezone.toString()), "GMT");
10198
_usageTimezone = TimeZone.getTimeZone(timeZoneStr);
10299

103-
jsInterpretationEnabled = ManagementService.JsInterpretationEnabled.value();
104-
105100
return true;
106101
}
107102

@@ -288,9 +283,4 @@ public void setMinBalance(Long accountId, Double balance) {
288283
_quotaAcc.updateQuotaAccount(accountId, acc);
289284
}
290285
}
291-
292-
@Override
293-
public boolean isJsInterpretationEnabled() {
294-
return jsInterpretationEnabled;
295-
}
296286
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.apache.cloudstack.context.CallContext;
5757
import org.apache.cloudstack.framework.config.ConfigKey;
5858
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
59+
import org.apache.cloudstack.jsinterpreter.JsInterpreterHelper;
5960
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
6061
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
6162
import org.apache.cloudstack.utils.identity.ManagementServerNode;
@@ -154,7 +155,6 @@
154155
import com.cloud.org.Grouping;
155156
import com.cloud.org.Managed;
156157
import com.cloud.serializer.GsonHelper;
157-
import com.cloud.server.ManagementService;
158158
import com.cloud.service.ServiceOfferingVO;
159159
import com.cloud.service.dao.ServiceOfferingDao;
160160
import com.cloud.service.dao.ServiceOfferingDetailsDao;
@@ -273,7 +273,7 @@ public class ResourceManagerImpl extends ManagerBase implements ResourceManager,
273273
@Inject
274274
private UserVmManager userVmManager;
275275
@Inject
276-
ManagementService managementService;
276+
JsInterpreterHelper jsInterpreterHelper;
277277

278278
private List<? extends Discoverer> _discoverers;
279279

@@ -1939,15 +1939,14 @@ private void updateHostTags(HostVO host, Long hostId, List<String> hostTags, Boo
19391939

19401940
@Override
19411941
public Host updateHost(final UpdateHostCmd cmd) throws NoTransitionException {
1942-
managementService.checkJsInterpretationAllowedIfNeededForParameterValue(ApiConstants.IS_TAG_A_RULE,
1943-
Boolean.TRUE.equals(cmd.getIsTagARule()));
1944-
19451942
return updateHost(cmd.getId(), cmd.getName(), cmd.getOsCategoryId(),
19461943
cmd.getAllocationState(), cmd.getUrl(), cmd.getHostTags(), cmd.getIsTagARule(), cmd.getAnnotation(), false);
19471944
}
19481945

19491946
private Host updateHost(Long hostId, String name, Long guestOSCategoryId, String allocationState,
19501947
String url, List<String> hostTags, Boolean isTagARule, String annotation, boolean isUpdateFromHostHealthCheck) throws NoTransitionException {
1948+
jsInterpreterHelper.ensureInterpreterEnabledIfParameterProvided(ApiConstants.IS_TAG_A_RULE, Boolean.TRUE.equals(isTagARule));
1949+
19511950
// Verify that the host exists
19521951
final HostVO host = _hostDao.findById(hostId);
19531952
if (host == null) {

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,6 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
10411041

10421042
protected List<DeploymentPlanner> _planners;
10431043

1044-
private boolean jsInterpretationEnabled = false;
1045-
10461044
private final List<HypervisorType> supportedHypervisors = new ArrayList<>();
10471045

10481046
public List<DeploymentPlanner> getPlanners() {
@@ -1129,8 +1127,6 @@ public boolean configure(final String name, final Map<String, Object> params) th
11291127
supportedHypervisors.add(HypervisorType.KVM);
11301128
supportedHypervisors.add(HypervisorType.XenServer);
11311129

1132-
jsInterpretationEnabled = JsInterpretationEnabled.value();
1133-
11341130
return true;
11351131
}
11361132

@@ -4113,10 +4109,8 @@ public List<Class<?>> getCommands() {
41134109
cmdList.add(ListGuestVlansCmd.class);
41144110
cmdList.add(AssignVolumeCmd.class);
41154111
cmdList.add(ListSecondaryStorageSelectorsCmd.class);
4116-
if (jsInterpretationEnabled) {
4117-
cmdList.add(CreateSecondaryStorageSelectorCmd.class);
4118-
cmdList.add(UpdateSecondaryStorageSelectorCmd.class);
4119-
}
4112+
cmdList.add(CreateSecondaryStorageSelectorCmd.class);
4113+
cmdList.add(UpdateSecondaryStorageSelectorCmd.class);
41204114
cmdList.add(RemoveSecondaryStorageSelectorCmd.class);
41214115
cmdList.add(ListAffectedVmsForStorageScopeChangeCmd.class);
41224116

@@ -4159,8 +4153,7 @@ public String getConfigComponentName() {
41594153

41604154
@Override
41614155
public ConfigKey<?>[] getConfigKeys() {
4162-
return new ConfigKey<?>[] {vmPasswordLength, sshKeyLength, humanReadableSizes, customCsIdentifier,
4163-
JsInterpretationEnabled};
4156+
return new ConfigKey<?>[] {vmPasswordLength, sshKeyLength, humanReadableSizes, customCsIdentifier};
41644157
}
41654158

41664159
protected class EventPurgeTask extends ManagedContextRunnable {
@@ -5617,13 +5610,4 @@ public void setLockControllerListener(final LockControllerListener lockControlle
56175610
_lockControllerListener = lockControllerListener;
56185611
}
56195612

5620-
@Override
5621-
public void checkJsInterpretationAllowedIfNeededForParameterValue(String paramName, boolean paramValue) {
5622-
if (!paramValue || jsInterpretationEnabled) {
5623-
return;
5624-
}
5625-
throw new InvalidParameterValueException(String.format(
5626-
"The parameter %s cannot be set to true as JS interpretation is disabled",
5627-
paramName));
5628-
}
56295613
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
import org.apache.cloudstack.framework.config.ConfigKey;
109109
import org.apache.cloudstack.framework.config.Configurable;
110110
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
111+
import org.apache.cloudstack.jsinterpreter.JsInterpreterHelper;
111112
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
112113
import org.apache.cloudstack.management.ManagementServerHost;
113114
import org.apache.cloudstack.resourcedetail.dao.DiskOfferingDetailsDao;
@@ -213,7 +214,6 @@
213214
import com.cloud.resource.ResourceState;
214215
import com.cloud.server.ConfigurationServer;
215216
import com.cloud.server.ManagementServer;
216-
import com.cloud.server.ManagementService;
217217
import com.cloud.server.StatsCollector;
218218
import com.cloud.service.dao.ServiceOfferingDetailsDao;
219219
import com.cloud.storage.Storage.ImageFormat;
@@ -400,7 +400,7 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C
400400
@Inject
401401
private ImageStoreDetailsUtil imageStoreDetailsUtil;
402402
@Inject
403-
ManagementService managementService;
403+
JsInterpreterHelper jsInterpreterHelper;
404404

405405
protected List<StoragePoolDiscoverer> _discoverers;
406406

@@ -946,6 +946,8 @@ protected void checkNFSMountOptionsForUpdate(Map<String, String> details, Storag
946946

947947
@Override
948948
public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws ResourceInUseException, IllegalArgumentException, UnknownHostException, ResourceUnavailableException {
949+
jsInterpreterHelper.ensureInterpreterEnabledIfParameterProvided(ApiConstants.IS_TAG_A_RULE, Boolean.TRUE.equals(cmd.isTagARule()));
950+
949951
String providerName = cmd.getStorageProviderName();
950952
Map<String,String> uriParams = extractUriParamsAsMap(cmd.getUrl());
951953
boolean isFileScheme = "file".equals(uriParams.get("scheme"));
@@ -1018,9 +1020,6 @@ public PrimaryDataStoreInfo createPool(CreateStoragePoolCmd cmd) throws Resource
10181020
throw new PermissionDeniedException(String.format("Cannot perform this operation, Zone is currently disabled: %s", zone));
10191021
}
10201022

1021-
managementService.checkJsInterpretationAllowedIfNeededForParameterValue(ApiConstants.IS_TAG_A_RULE,
1022-
Boolean.TRUE.equals(cmd.isTagARule()));
1023-
10241023
Map<String, Object> params = new HashMap<>();
10251024
params.put("zoneId", zone.getId());
10261025
params.put("clusterId", clusterId);
@@ -1201,11 +1200,9 @@ public StoragePool enablePrimaryStoragePool(Long id) {
12011200
@ActionEvent(eventType = EventTypes.EVENT_UPDATE_PRIMARY_STORAGE, eventDescription = "update storage pool")
12021201
public PrimaryDataStoreInfo updateStoragePool(UpdateStoragePoolCmd cmd) throws IllegalArgumentException {
12031202
// Input validation
1204-
Long id = cmd.getId();
1205-
1206-
managementService.checkJsInterpretationAllowedIfNeededForParameterValue(ApiConstants.IS_TAG_A_RULE,
1207-
Boolean.TRUE.equals(cmd.isTagARule()));
1203+
jsInterpreterHelper.ensureInterpreterEnabledIfParameterProvided(ApiConstants.IS_TAG_A_RULE, Boolean.TRUE.equals(cmd.isTagARule()));
12081204

1205+
Long id = cmd.getId();
12091206
StoragePoolVO pool = _storagePoolDao.findById(id);
12101207
if (pool == null) {
12111208
throw new IllegalArgumentException("Unable to find storage pool with ID: " + id);
@@ -2508,6 +2505,7 @@ protected void validateHeuristicRule(String heuristicRule) {
25082505
if (StringUtils.isBlank(heuristicRule)) {
25092506
throw new IllegalArgumentException("Unable to create a new secondary storage selector as the given heuristic rule is blank.");
25102507
}
2508+
jsInterpreterHelper.ensureInterpreterEnabledIfParameterProvided(ApiConstants.HEURISTIC_RULE, true);
25112509
}
25122510

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

0 commit comments

Comments
 (0)