Skip to content

Commit

Permalink
Merge pull request #749 from MissionCriticalCloud/fix/userdata
Browse files Browse the repository at this point in the history
Fix incorrect userdata decoding
  • Loading branch information
neubauerf authored Jul 20, 2018
2 parents 6cd3e90 + 9143e83 commit 6c28cc9
Showing 1 changed file with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.cloud.api.command.user.vmgroup.DeleteVMGroupCmd;
import com.cloud.capacity.Capacity;
import com.cloud.capacity.CapacityManager;
import com.cloud.common.managed.context.ManagedContextRunnable;
import com.cloud.configuration.Config;
import com.cloud.configuration.ConfigurationManager;
import com.cloud.context.CallContext;
Expand Down Expand Up @@ -121,15 +122,13 @@
import com.cloud.legacymodel.to.DiskTO;
import com.cloud.legacymodel.to.NicTO;
import com.cloud.legacymodel.to.VirtualMachineTO;
import com.cloud.legacymodel.to.VolumeTO;
import com.cloud.legacymodel.user.Account;
import com.cloud.legacymodel.user.SSHKeyPair;
import com.cloud.legacymodel.user.User;
import com.cloud.legacymodel.utils.Pair;
import com.cloud.legacymodel.vm.VirtualMachine;
import com.cloud.legacymodel.vm.VirtualMachine.State;
import com.cloud.legacymodel.vm.VmStatsEntry;
import com.cloud.common.managed.context.ManagedContextRunnable;
import com.cloud.model.enumeration.AllocationState;
import com.cloud.model.enumeration.DataStoreRole;
import com.cloud.model.enumeration.DiskControllerType;
Expand Down Expand Up @@ -237,9 +236,13 @@
import com.cloud.vm.snapshot.VMSnapshotManager;
import com.cloud.vm.snapshot.VMSnapshotVO;
import com.cloud.vm.snapshot.dao.VMSnapshotDao;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.naming.ConfigurationException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
Expand All @@ -257,10 +260,6 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserVmManagerImpl extends ManagerBase implements UserVmManager, VirtualMachineGuru, UserVmService, Configurable {
private static final ConfigKey<Integer> VmIpFetchWaitInterval = new ConfigKey<>("Advanced", Integer.class, "externaldhcp.vmip.retrieval.interval", "180",
"Wait Interval (in seconds) for shared network vm dhcp ip addr fetch for next iteration ", true);
Expand Down Expand Up @@ -1051,6 +1050,11 @@ public UserVm updateVirtualMachine(final long id, String displayName, final Stri
if (userData != null) {
// check and replace newlines
userData = userData.replace("\\n", "");
try {
userData = java.net.URLDecoder.decode(userData, "UTF-8");
} catch (final UnsupportedEncodingException e) {
throw new InvalidParameterValueException("Unsupported encoding");
}
validateUserData(userData, httpMethod);
// update userData on domain router.
updateUserdata = true;
Expand Down Expand Up @@ -3161,8 +3165,8 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
final List<? extends Network> virtualNetworks = _networkModel.listNetworksForAccount(newAccount.getId(), zone.getId(), GuestType.Isolated);
if (virtualNetworks.isEmpty()) {
final long physicalNetworkId = _networkModel.findPhysicalNetworkId(zone.getId(), requiredOfferings.get(0).getTags(), requiredOfferings.get(0)
.getTrafficType
());
.getTrafficType
());
// Validate physical network
final PhysicalNetwork physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
if (physicalNetwork == null) {
Expand Down Expand Up @@ -4031,8 +4035,17 @@ private UserVm createVirtualMachine(final Zone zone, final ServiceOffering servi
// Check templates permissions
_accountMgr.checkAccess(owner, AccessType.UseEntry, false, template);

// Decode userData
String decodedUserData = null;
try {
if (userData != null) {
decodedUserData = java.net.URLDecoder.decode(userData, "UTF-8");
}
} catch (final UnsupportedEncodingException e) {
throw new InvalidParameterValueException("Unsupported encoding");
}
// check if the user data is correct
validateUserData(userData, httpmethod);
validateUserData(decodedUserData, httpmethod);

// Find an SSH public key corresponding to the key pair name, if one is
// given
Expand Down Expand Up @@ -4176,7 +4189,7 @@ private UserVm createVirtualMachine(final Zone zone, final ServiceOffering servi
}
}

final UserVmVO vm = commitUserVm(zone, template, hostName, displayName, owner, diskOfferingId, diskSize, userData, caller, isDisplayVm, keyboard, accountId, userId,
final UserVmVO vm = commitUserVm(zone, template, hostName, displayName, owner, diskOfferingId, diskSize, decodedUserData, caller, isDisplayVm, keyboard, accountId, userId,
offering, isIso, sshPublicKey, networkNicMap, id, instanceName, uuidName, hypervisorType, customParameters, diskControllerType, manufacturerString, optimiseFor,
macLarning, cpuFlags, maintenancePolicy, bootMenuTimeout);

Expand Down

0 comments on commit 6c28cc9

Please sign in to comment.