Skip to content

Commit 869ffad

Browse files
committed
server,engine-schema: add check for account userdata cleanup
Fixes #9477 Signed-off-by: Abhishek Kumar <[email protected]>
1 parent d26122b commit 869ffad

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

engine/schema/src/main/java/com/cloud/storage/dao/VMTemplateDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,6 @@ public interface VMTemplateDao extends GenericDao<VMTemplateVO, Long>, StateDao<
9999
List<VMTemplateVO> listByIds(List<Long> ids);
100100

101101
List<Long> listIdsByTemplateTag(String tag);
102+
103+
List<Long> listByUserdataIdsNotAccount(List<Long> userdataIds, long accountId);
102104
}

engine/schema/src/main/java/com/cloud/storage/dao/VMTemplateDaoImpl.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,4 +863,22 @@ public boolean updateState(
863863
}
864864
return rows > 0;
865865
}
866+
867+
@Override
868+
public List<Long> listByUserdataIdsNotAccount(List<Long> userdataIds, long accountId) {
869+
if (CollectionUtils.isEmpty(userdataIds)) {
870+
return Collections.emptyList();
871+
}
872+
GenericSearchBuilder<VMTemplateVO, Long> sb = createSearchBuilder(Long.class);
873+
sb.selectFields(userDataSearch.entity().getId());
874+
sb.and("userDataId", sb.entity().getUserDataId(), SearchCriteria.Op.EQ);
875+
sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
876+
sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.NEQ);
877+
sb.done();
878+
SearchCriteria<Long> sc = sb.create();
879+
sc.setParameters("userDataId", userdataIds.toArray());
880+
sc.setParameters("state", VirtualMachineTemplate.State.Active.toString());
881+
sc.setParameters("accountId", accountId);
882+
return customSearch(sc, null);
883+
}
866884
}

engine/schema/src/main/java/com/cloud/user/dao/UserDataDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// under the License.
1717
package com.cloud.user.dao;
1818

19+
import java.util.List;
20+
1921
import com.cloud.user.UserDataVO;
2022
import com.cloud.utils.db.GenericDao;
2123

@@ -25,6 +27,7 @@ public interface UserDataDao extends GenericDao<UserDataVO, Long> {
2527

2628
public UserDataVO findByName(long accountId, long domainId, String name);
2729

30+
List<UserDataVO> listByAccountId(long accountId);
2831
int removeByAccountId(long accountId);
2932

3033
}

server/src/main/java/com/cloud/user/AccountManagerImpl.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,20 @@ public void setQuerySelectors(List<QuerySelector> querySelectors) {
475475
_querySelectors = querySelectors;
476476
}
477477

478+
protected void deleteUserDataForAccount(long accountId) {
479+
List<UserDataVO> userdataList = userDataDao.listByAccountId(accountId);
480+
if (CollectionUtils.isNotEmpty(userdataList)) {
481+
List<Long> conflictingTemplateIds = _templateDao.listByUserdataIdsNotAccount(userdataList
482+
.stream()
483+
.map(UserDataVO::getId)
484+
.collect(Collectors.toList()), accountId);
485+
if (CollectionUtils.isNotEmpty(conflictingTemplateIds)) {
486+
throw new CloudRuntimeException("User data owned by account linked to templates not owned by the account");
487+
}
488+
}
489+
userDataDao.removeByAccountId(accountId);
490+
}
491+
478492
protected void deleteWebhooksForAccount(long accountId) {
479493
try {
480494
WebhookHelper webhookService = ComponentContext.getDelegateComponentOfType(WebhookHelper.class);
@@ -1200,7 +1214,7 @@ public int compare(NetworkVO network1, NetworkVO network2) {
12001214
}
12011215

12021216
// Delete registered UserData
1203-
userDataDao.removeByAccountId(accountId);
1217+
deleteUserDataForAccount(accountId);
12041218

12051219
// Delete Webhooks
12061220
deleteWebhooksForAccount(accountId);

0 commit comments

Comments
 (0)