|
23 | 23 | import java.util.Collections; |
24 | 24 | import java.util.Date; |
25 | 25 | import java.util.List; |
| 26 | +import java.util.Map; |
26 | 27 | import java.util.stream.Collectors; |
27 | 28 |
|
28 | 29 | import javax.inject.Inject; |
| 30 | +import javax.naming.ConfigurationException; |
29 | 31 |
|
| 32 | +import org.apache.cloudstack.kms.KMSWrappedKeyVO; |
| 33 | +import org.apache.cloudstack.kms.dao.KMSWrappedKeyDao; |
30 | 34 | import org.apache.cloudstack.reservation.ReservationVO; |
31 | 35 | import org.apache.cloudstack.reservation.dao.ReservationDao; |
32 | | -import org.apache.cloudstack.kms.dao.KMSWrappedKeyDao; |
33 | 36 | import org.apache.commons.collections.CollectionUtils; |
34 | 37 | import org.springframework.stereotype.Component; |
35 | 38 |
|
|
50 | 53 | import com.cloud.utils.db.Filter; |
51 | 54 | import com.cloud.utils.db.GenericDaoBase; |
52 | 55 | import com.cloud.utils.db.GenericSearchBuilder; |
| 56 | +import com.cloud.utils.db.JoinBuilder; |
53 | 57 | import com.cloud.utils.db.QueryBuilder; |
54 | 58 | import com.cloud.utils.db.SearchBuilder; |
55 | 59 | import com.cloud.utils.db.SearchCriteria; |
@@ -82,6 +86,8 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol |
82 | 86 | private final SearchBuilder<VolumeVO> poolAndPathSearch; |
83 | 87 | final GenericSearchBuilder<VolumeVO, Integer> CountByOfferingId; |
84 | 88 | private final SearchBuilder<VolumeVO> kmsMigrationSearch; |
| 89 | + private SearchBuilder<VolumeVO> removedWithPassphraseSearch; |
| 90 | + private SearchBuilder<VolumeVO> removedWithLiveKmsWrappedKeySearch; |
85 | 91 |
|
86 | 92 | @Inject |
87 | 93 | ReservationDao reservationDao; |
@@ -543,6 +549,32 @@ public VolumeDaoImpl() { |
543 | 549 | kmsMigrationSearch.done(); |
544 | 550 | } |
545 | 551 |
|
| 552 | + @Override |
| 553 | + public boolean configure(String name, Map<String, Object> params) throws ConfigurationException { |
| 554 | + boolean result = super.configure(name, params); |
| 555 | + |
| 556 | + removedWithPassphraseSearch = createSearchBuilder(); |
| 557 | + removedWithPassphraseSearch.and("passphraseId", removedWithPassphraseSearch.entity().getPassphraseId(), Op.NNULL); |
| 558 | + removedWithPassphraseSearch.and().op("removed", removedWithPassphraseSearch.entity().getRemoved(), Op.NNULL); |
| 559 | + removedWithPassphraseSearch.or("state", removedWithPassphraseSearch.entity().getState(), Op.EQ); |
| 560 | + removedWithPassphraseSearch.cp(); |
| 561 | + removedWithPassphraseSearch.done(); |
| 562 | + |
| 563 | + // Joins with the table of another DAO, so it is built here rather than in the constructor. |
| 564 | + SearchBuilder<KMSWrappedKeyVO> liveWrappedKeySearch = kmsWrappedKeyDao.createSearchBuilder(); |
| 565 | + liveWrappedKeySearch.and("wrappedKeyRemoved", liveWrappedKeySearch.entity().getRemoved(), Op.NULL); |
| 566 | + |
| 567 | + removedWithLiveKmsWrappedKeySearch = createSearchBuilder(); |
| 568 | + removedWithLiveKmsWrappedKeySearch.and().op("removed", removedWithLiveKmsWrappedKeySearch.entity().getRemoved(), Op.NNULL); |
| 569 | + removedWithLiveKmsWrappedKeySearch.or("state", removedWithLiveKmsWrappedKeySearch.entity().getState(), Op.EQ); |
| 570 | + removedWithLiveKmsWrappedKeySearch.cp(); |
| 571 | + removedWithLiveKmsWrappedKeySearch.join("wrappedKey", liveWrappedKeySearch, liveWrappedKeySearch.entity().getId(), |
| 572 | + removedWithLiveKmsWrappedKeySearch.entity().getKmsWrappedKeyId(), JoinBuilder.JoinType.INNER); |
| 573 | + removedWithLiveKmsWrappedKeySearch.done(); |
| 574 | + |
| 575 | + return result; |
| 576 | + } |
| 577 | + |
546 | 578 | @Override |
547 | 579 | @DB() |
548 | 580 | public Pair<Long, Long> getCountAndTotalByPool(long poolId) { |
@@ -784,17 +816,6 @@ public boolean remove(Long id) { |
784 | 816 | logger.debug(String.format("Removing volume %s from DB", id)); |
785 | 817 | VolumeVO entry = findById(id); |
786 | 818 | if (entry != null) { |
787 | | - // Clean up KMS wrapped key if volume was encrypted with KMS |
788 | | - if (entry.getKmsWrappedKeyId() != null) { |
789 | | - try { |
790 | | - kmsWrappedKeyDao.remove(entry.getKmsWrappedKeyId()); |
791 | | - logger.debug("Removed KMS wrapped key [id={}] for volume [id={}, uuid={}]", |
792 | | - entry.getKmsWrappedKeyId(), id, entry.getUuid()); |
793 | | - } catch (Exception e) { |
794 | | - logger.warn("Failed to remove KMS wrapped key [id={}] for volume [id={}, uuid={}]: {}", |
795 | | - entry.getKmsWrappedKeyId(), id, entry.getUuid(), e.getMessage(), e); |
796 | | - } |
797 | | - } |
798 | 819 | tagsDao.removeByIdAndType(id, ResourceObjectType.Volume); |
799 | 820 | } |
800 | 821 | boolean result = super.remove(id); |
@@ -1011,4 +1032,32 @@ public List<VolumeVO> findByKmsWrappedKeyId(Long kmsWrappedKeyId) { |
1011 | 1032 | sc.setParameters("notDestroyed", Volume.State.Expunged); |
1012 | 1033 | return listBy(sc); |
1013 | 1034 | } |
| 1035 | + |
| 1036 | + @Override |
| 1037 | + public List<VolumeVO> listIncludingRemovedByKmsWrappedKeyId(long kmsWrappedKeyId) { |
| 1038 | + SearchCriteria<VolumeVO> sc = AllFieldsSearch.create(); |
| 1039 | + sc.setParameters("kmsWrappedKeyId", kmsWrappedKeyId); |
| 1040 | + return listIncludingRemovedBy(sc); |
| 1041 | + } |
| 1042 | + |
| 1043 | + @Override |
| 1044 | + public List<VolumeVO> listIncludingRemovedByPassphraseId(long passphraseId) { |
| 1045 | + SearchCriteria<VolumeVO> sc = AllFieldsSearch.create(); |
| 1046 | + sc.setParameters("passphraseId", passphraseId); |
| 1047 | + return listIncludingRemovedBy(sc); |
| 1048 | + } |
| 1049 | + |
| 1050 | + @Override |
| 1051 | + public List<VolumeVO> listRemovedOrExpungedWithKmsWrappedKey() { |
| 1052 | + SearchCriteria<VolumeVO> sc = removedWithLiveKmsWrappedKeySearch.create(); |
| 1053 | + sc.setParameters("state", Volume.State.Expunged); |
| 1054 | + return listIncludingRemovedBy(sc); |
| 1055 | + } |
| 1056 | + |
| 1057 | + @Override |
| 1058 | + public List<VolumeVO> listRemovedOrExpungedWithPassphrase() { |
| 1059 | + SearchCriteria<VolumeVO> sc = removedWithPassphraseSearch.create(); |
| 1060 | + sc.setParameters("state", Volume.State.Expunged); |
| 1061 | + return listIncludingRemovedBy(sc); |
| 1062 | + } |
1014 | 1063 | } |
0 commit comments