Skip to content

Commit 126ed86

Browse files
committed
Fix marvin test and revoke certificate
1 parent de3923e commit 126ed86

6 files changed

Lines changed: 34 additions & 18 deletions

File tree

engine/schema/src/main/java/org/apache/cloudstack/direct/download/DirectDownloadCertificateDao.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.List;
2323

2424
public interface DirectDownloadCertificateDao extends GenericDao<DirectDownloadCertificateVO, Long> {
25-
DirectDownloadCertificateVO findByAlias(String alias);
26-
List<DirectDownloadCertificateVO> listByHypervisorType(Hypervisor.HypervisorType hypervisorType);
25+
DirectDownloadCertificateVO findByAlias(String alias, Hypervisor.HypervisorType hypervisorType, long zoneId);
2726
List<DirectDownloadCertificateVO> listByZone(long zoneId);
2827
}

engine/schema/src/main/java/org/apache/cloudstack/direct/download/DirectDownloadCertificateDaoImpl.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,12 @@ public DirectDownloadCertificateDaoImpl() {
3636
}
3737

3838
@Override
39-
public DirectDownloadCertificateVO findByAlias(String alias) {
39+
public DirectDownloadCertificateVO findByAlias(String alias, Hypervisor.HypervisorType hypervisorType, long zoneId) {
4040
SearchCriteria<DirectDownloadCertificateVO> sc = certificateSearchBuilder.create();
4141
sc.setParameters("alias", alias);
42-
return findOneBy(sc);
43-
}
44-
45-
@Override
46-
public List<DirectDownloadCertificateVO> listByHypervisorType(Hypervisor.HypervisorType hypervisorType) {
47-
SearchCriteria<DirectDownloadCertificateVO> sc = certificateSearchBuilder.create();
4842
sc.setParameters("hypervisor_type", hypervisorType);
49-
return listBy(sc);
43+
sc.setParameters("zone_id", zoneId);
44+
return findOneBy(sc);
5045
}
5146

5247
@Override

engine/schema/src/main/java/org/apache/cloudstack/direct/download/DirectDownloadCertificateHostMapDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import com.cloud.utils.db.GenericDao;
2020

21+
import java.util.List;
22+
2123
public interface DirectDownloadCertificateHostMapDao extends GenericDao<DirectDownloadCertificateHostMapVO, Long> {
2224
DirectDownloadCertificateHostMapVO findByCertificateAndHost(long certificateId, long hostId);
25+
List<DirectDownloadCertificateHostMapVO> listByCertificateId(long certificateId);
2326
}

engine/schema/src/main/java/org/apache/cloudstack/direct/download/DirectDownloadCertificateHostMapDaoImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.cloud.utils.db.SearchBuilder;
2121
import com.cloud.utils.db.SearchCriteria;
2222

23+
import java.util.List;
24+
2325
public class DirectDownloadCertificateHostMapDaoImpl extends GenericDaoBase<DirectDownloadCertificateHostMapVO, Long> implements DirectDownloadCertificateHostMapDao {
2426
private final SearchBuilder<DirectDownloadCertificateHostMapVO> mapSearchBuilder;
2527

@@ -36,4 +38,11 @@ public DirectDownloadCertificateHostMapVO findByCertificateAndHost(long certific
3638
sc.setParameters("host_id", hostId);
3739
return findOneBy(sc);
3840
}
41+
42+
@Override
43+
public List<DirectDownloadCertificateHostMapVO> listByCertificateId(long certificateId) {
44+
SearchCriteria<DirectDownloadCertificateHostMapVO> sc = mapSearchBuilder.create();
45+
sc.setParameters("certificate_id", certificateId);
46+
return listBy(sc);
47+
}
3948
}

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ public boolean uploadCertificateToHosts(String certificateCer, String alias, Str
391391
String certificatePem = getPretifiedCertificate(certificateCer);
392392
certificateSanity(certificatePem);
393393

394-
DirectDownloadCertificateVO certificateVO = directDownloadCertificateDao.findByAlias(alias);
394+
DirectDownloadCertificateVO certificateVO = directDownloadCertificateDao.findByAlias(alias, hypervisorType, zoneId);
395395
if (certificateVO != null) {
396396
throw new CloudRuntimeException("Certificate alias " + alias + " has been already created");
397397
}
@@ -453,15 +453,22 @@ public boolean uploadCertificate(long certificateId, long hostId) {
453453
@Override
454454
public boolean revokeCertificateAlias(String certificateAlias, String hypervisor, Long zoneId) {
455455
HypervisorType hypervisorType = HypervisorType.getType(hypervisor);
456-
List<HostVO> hosts = getRunningHostsToUploadCertificate(zoneId, hypervisorType);
457-
s_logger.info("Attempting to revoke certificate alias: " + certificateAlias + " from " + hosts.size() + " hosts");
458-
if (CollectionUtils.isNotEmpty(hosts)) {
459-
for (HostVO host : hosts) {
460-
if (!revokeCertificateAliasFromHost(certificateAlias, host.getId())) {
461-
String msg = "Could not revoke certificate from host: " + host.getName() + " (" + host.getUuid() + ")";
456+
DirectDownloadCertificateVO certificateVO = directDownloadCertificateDao.findByAlias(certificateAlias, hypervisorType, zoneId);
457+
if (certificateVO == null) {
458+
throw new CloudRuntimeException("Certificate alias " + certificateAlias + " does not exist");
459+
}
460+
461+
List<DirectDownloadCertificateHostMapVO> maps = directDownloadCertificateHostMapDao.listByCertificateId(certificateVO.getId());
462+
s_logger.info("Attempting to revoke certificate alias: " + certificateAlias + " from " + maps.size() + " hosts");
463+
if (CollectionUtils.isNotEmpty(maps)) {
464+
for (DirectDownloadCertificateHostMapVO map : maps) {
465+
Long hostId = map.getHostId();
466+
if (!revokeCertificateAliasFromHost(certificateAlias, hostId)) {
467+
String msg = "Could not revoke certificate from host: " + hostId;
462468
s_logger.error(msg);
463469
throw new CloudRuntimeException(msg);
464470
}
471+
directDownloadCertificateHostMapDao.remove(map.getId());
465472
}
466473
}
467474
return true;

test/integration/smoke/test_direct_download.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def test_01_sanity_check_on_certificates(self):
9292
cmd.hypervisor = self.hypervisor
9393
cmd.name = "marvin-test-verify-certs"
9494
cmd.certificate = self.certificates["invalid"]
95+
cmd.zoneid = self.zone.id
9596

9697
invalid_cert_uploadFails = False
9798
expired_cert_upload_fails = False
@@ -126,6 +127,7 @@ def test_02_upload_direct_download_certificates(self):
126127
cmd.hypervisor = self.hypervisor
127128
cmd.name = "marvin-test-verify-certs"
128129
cmd.certificate = self.certificates["valid"]
130+
cmd.zoneid = self.zone.id
129131

130132
try:
131133
self.apiclient.uploadTemplateDirectDownloadCertificate(cmd)
@@ -135,9 +137,10 @@ def test_02_upload_direct_download_certificates(self):
135137
revokecmd = revokeTemplateDirectDownloadCertificate.revokeTemplateDirectDownloadCertificateCmd()
136138
revokecmd.hypervisor = self.hypervisor
137139
revokecmd.name = cmd.name
140+
revokecmd.zoneid = self.zone.id
138141

139142
try:
140-
self.apiclient.revokeTemplateDirectDownloadCertificate(cmd)
143+
self.apiclient.revokeTemplateDirectDownloadCertificate(revokecmd)
141144
except Exception as e:
142145
self.fail("Uploaded certificates should be revoked when needed")
143146

0 commit comments

Comments
 (0)