Skip to content

Commit 95c2f76

Browse files
Improve certificate name validation and some code/log improvements
1 parent de562cf commit 95c2f76

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixGetVmIpAddressCommandWrapper.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,18 @@ public Answer execute(final GetVmIpAddressCommand command, final CitrixResourceB
6363
}
6464

6565
if (vmIp != null) {
66-
s_logger.debug("VM " +vmName + " ip address got retrieved "+vmIp);
66+
s_logger.debug("VM " + vmName + " IP address got retrieved " + vmIp);
6767
result = true;
6868
return new Answer(command, result, vmIp);
6969
}
70-
71-
}catch (Types.XenAPIException e) {
72-
s_logger.debug("Got exception in GetVmIpAddressCommand "+ e.getMessage());
73-
errorMsg = "Failed to retrived vm ip addr, exception: "+e.getMessage();
74-
}catch (XmlRpcException e) {
75-
s_logger.debug("Got exception in GetVmIpAddressCommand "+ e.getMessage());
76-
errorMsg = "Failed to retrived vm ip addr, exception: "+e.getMessage();
70+
} catch (Types.XenAPIException e) {
71+
s_logger.debug("Got exception in GetVmIpAddressCommand " + e.getMessage());
72+
errorMsg = "Failed to retrieve vm ip addr, exception: " + e.getMessage();
73+
} catch (XmlRpcException e) {
74+
s_logger.debug("Got exception in GetVmIpAddressCommand " + e.getMessage());
75+
errorMsg = "Failed to retrieve vm ip addr, exception: " + e.getMessage();
7776
}
7877

7978
return new Answer(command, result, errorMsg);
80-
8179
}
8280
}

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,6 @@ public void decrementCount() {
751751
}
752752

753753
private class VmIpAddrFetchThread extends ManagedContextRunnable {
754-
755-
756754
long nicId;
757755
long vmId;
758756
String vmName;
@@ -775,7 +773,7 @@ protected void runInContext() {
775773
boolean decrementCount = true;
776774

777775
try {
778-
s_logger.debug("Trying for vm "+ vmId +" nic Id "+nicId +" ip retrieval ...");
776+
s_logger.debug(String.format("Trying IP retrieval for VM %s (%d), nic Id %d", vmName, vmId, nicId));
779777
Answer answer = _agentMgr.send(hostId, cmd);
780778
NicVO nic = _nicDao.findById(nicId);
781779
if (answer.getResult()) {
@@ -786,12 +784,12 @@ protected void runInContext() {
786784
if (nic != null) {
787785
nic.setIPv4Address(vmIp);
788786
_nicDao.update(nicId, nic);
789-
s_logger.debug("Vm "+ vmId +" IP "+vmIp +" got retrieved successfully");
787+
s_logger.debug(String.format("VM %s (%d) - IP %s retrieved successfully", vmName, vmId, vmIp));
790788
vmIdCountMap.remove(nicId);
791789
decrementCount = false;
792790
ActionEventUtils.onActionEvent(User.UID_SYSTEM, Account.ACCOUNT_ID_SYSTEM,
793791
Domain.ROOT_DOMAIN, EventTypes.EVENT_NETWORK_EXTERNAL_DHCP_VM_IPFETCH,
794-
"VM " + vmId + " nic id " + nicId + " ip address " + vmIp + " got fetched successfully", vmId, ApiCommandResourceType.VirtualMachine.toString());
792+
"VM " + vmId + ", nic id " + nicId + ", IP address " + vmIp + " fetched successfully", vmId, ApiCommandResourceType.VirtualMachine.toString());
795793
}
796794
}
797795
} else {
@@ -802,7 +800,7 @@ protected void runInContext() {
802800
_nicDao.update(nicId, nic);
803801
}
804802
if (answer.getDetails() != null) {
805-
s_logger.debug("Failed to get vm ip for Vm "+ vmId + answer.getDetails());
803+
s_logger.debug(String.format("Failed to get IP for VM %s (%d), details: %s", vmName, vmId, answer.getDetails()));
806804
}
807805
}
808806
} catch (OperationTimedoutException e) {
@@ -813,7 +811,7 @@ protected void runInContext() {
813811
if (decrementCount) {
814812
VmAndCountDetails vmAndCount = vmIdCountMap.get(nicId);
815813
vmAndCount.decrementCount();
816-
s_logger.debug("Ip is not retrieved for VM " + vmId +" nic "+nicId + " ... decremented count to "+vmAndCount.getRetrievalCount());
814+
s_logger.debug(String.format("IP is not retrieved for VM %s (%d), nic %d ... decremented count to %d", vmName, vmId, nicId, vmAndCount.getRetrievalCount()));
817815
vmIdCountMap.put(nicId, vmAndCount);
818816
}
819817
}

server/src/main/java/org/apache/cloudstack/direct/download/DirectDownloadManagerImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
import com.cloud.utils.component.ManagerBase;
104104
import com.cloud.utils.concurrency.NamedThreadFactory;
105105
import com.cloud.utils.exception.CloudRuntimeException;
106+
import com.cloud.utils.net.NetUtils;
106107
import com.cloud.utils.security.CertificateHelper;
107108

108109
import sun.security.x509.X509CertImpl;
@@ -471,10 +472,18 @@ protected void certificateSanity(String certificatePem) {
471472
@Override
472473
public Pair<DirectDownloadCertificate, List<HostCertificateStatus>> uploadCertificateToHosts(
473474
String certificateCer, String alias, String hypervisor, Long zoneId, Long hostId) {
474-
if (alias != null && (alias.equalsIgnoreCase("cloud") || alias.startsWith("cloudca"))) {
475+
if (StringUtils.isBlank(alias)) {
476+
throw new CloudRuntimeException("Certificate name not provided, please provide a valid name");
477+
}
478+
479+
if (alias.equalsIgnoreCase("cloud") || alias.startsWith("cloudca")) {
475480
throw new CloudRuntimeException("Please provide a different alias name for the certificate");
476481
}
477482

483+
if (!NetUtils.verifyDomainNameLabel(alias, false)) {
484+
throw new CloudRuntimeException("The provided certificate name is invalid, please provide a valid name");
485+
}
486+
478487
List<HostVO> hosts;
479488
DirectDownloadCertificateVO certificateVO;
480489
HypervisorType hypervisorType = HypervisorType.getType(hypervisor);

utils/src/main/java/com/cloud/utils/net/NetUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ public static boolean verifyDomainNameLabel(final String hostName, final boolean
10691069
s_logger.warn("Domain name label may contain only the ASCII letters 'a' through 'z' (in a case-insensitive manner)");
10701070
return false;
10711071
} else if (hostName.startsWith("-") || hostName.endsWith("-")) {
1072-
s_logger.warn("Domain name label can not start with a hyphen and digit, and must not end with a hyphen");
1072+
s_logger.warn("Domain name label can not start or end with a hyphen");
10731073
return false;
10741074
} else if (isHostName && START_HOSTNAME_PATTERN.matcher(hostName).matches()) {
10751075
s_logger.warn("Host name can't start with digit");

0 commit comments

Comments
 (0)