Skip to content

Commit 076a259

Browse files
server: charge snapshot copy primary storage to the snapshot owner account
copySnapshotOnPool passed CallContext.getCallingUserId() to incrementResourceCount, whose first argument is an account id, not a user id. User ids and account ids are independent sequences, so the primary_storage count was charged to whichever account happened to share the caller's user id (and never to the real owner), corrupting quota accounting. The following guard also compared the calling user id against Account.ACCOUNT_ID_SYSTEM. Use the snapshot owner's account id for the count and the calling account id for the system check.
1 parent 3d70ce4 commit 076a259

2 files changed

Lines changed: 101 additions & 3 deletions

File tree

server/src/main/java/com/cloud/storage/snapshot/SnapshotManagerImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,7 +1768,7 @@ private void copyNewSnapshotToZonesOnPrimary(CreateSnapshotPayload payload, Snap
17681768
}
17691769
}
17701770

1771-
private boolean copySnapshotOnPool(SnapshotInfo snapshot, SnapshotStrategy snapshotStrategy, Long storagePoolId) {
1771+
protected boolean copySnapshotOnPool(SnapshotInfo snapshot, SnapshotStrategy snapshotStrategy, Long storagePoolId) {
17721772
DataStore store = dataStoreMgr.getDataStore(storagePoolId, DataStoreRole.Primary);
17731773
SnapshotInfo snapshotOnStore = (SnapshotInfo) store.create(snapshot);
17741774

@@ -1780,8 +1780,8 @@ private boolean copySnapshotOnPool(SnapshotInfo snapshot, SnapshotStrategy snaps
17801780
return false;
17811781
}
17821782
snapshotZoneDao.addSnapshotToZone(snapshot.getId(), snapshotOnStore.getDataCenterId());
1783-
_resourceLimitMgr.incrementResourceCount(CallContext.current().getCallingUserId(), ResourceType.primary_storage, snapshot.getSize());
1784-
if (CallContext.current().getCallingUserId() != Account.ACCOUNT_ID_SYSTEM) {
1783+
_resourceLimitMgr.incrementResourceCount(snapshot.getAccountId(), ResourceType.primary_storage, snapshot.getSize());
1784+
if (CallContext.current().getCallingAccountId() != Account.ACCOUNT_ID_SYSTEM) {
17851785
SnapshotVO snapshotVO = _snapshotDao.findByIdIncludingRemoved(snapshot.getSnapshotId());
17861786
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_SNAPSHOT_COPY, CallContext.current().getCallingAccountId(), snapshotOnStore.getDataCenterId(), snapshotVO.getId(), null, null, null, snapshotVO.getSize(),
17871787
snapshotVO.getSize(), snapshotVO.getClass().getName(), snapshotVO.getUuid());
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.storage.snapshot;
18+
19+
import static org.mockito.Mockito.when;
20+
21+
import org.apache.cloudstack.context.CallContext;
22+
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
23+
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
24+
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo;
25+
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult;
26+
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotService;
27+
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy;
28+
import org.apache.cloudstack.framework.async.AsyncCallFuture;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
import org.mockito.InjectMocks;
32+
import org.mockito.Mock;
33+
import org.mockito.MockedStatic;
34+
import org.mockito.Mockito;
35+
import org.mockito.junit.MockitoJUnitRunner;
36+
37+
import com.cloud.configuration.Resource.ResourceType;
38+
import com.cloud.storage.DataStoreRole;
39+
import com.cloud.storage.dao.SnapshotZoneDao;
40+
import com.cloud.user.Account;
41+
import com.cloud.user.ResourceLimitService;
42+
43+
@RunWith(MockitoJUnitRunner.Silent.class)
44+
public class SnapshotManagerImplCopyResourceCountTest {
45+
46+
@Mock
47+
DataStoreManager dataStoreMgr;
48+
@Mock
49+
SnapshotService snapshotSrv;
50+
@Mock
51+
SnapshotZoneDao snapshotZoneDao;
52+
@Mock
53+
ResourceLimitService _resourceLimitMgr;
54+
55+
@InjectMocks
56+
SnapshotManagerImpl snapshotManager = new SnapshotManagerImpl();
57+
58+
@Test
59+
@SuppressWarnings("unchecked")
60+
public void copySnapshotOnPoolChargesPrimaryStorageToTheSnapshotOwnerAccount() throws Exception {
61+
long ownerAccountId = 42L;
62+
long snapshotSize = 1024L;
63+
Long poolId = 5L;
64+
65+
SnapshotInfo snapshot = Mockito.mock(SnapshotInfo.class);
66+
SnapshotInfo snapshotOnStore = Mockito.mock(SnapshotInfo.class);
67+
SnapshotStrategy strategy = Mockito.mock(SnapshotStrategy.class);
68+
DataStore store = Mockito.mock(DataStore.class);
69+
AsyncCallFuture<SnapshotResult> future = Mockito.mock(AsyncCallFuture.class);
70+
SnapshotResult result = Mockito.mock(SnapshotResult.class);
71+
72+
when(snapshot.getId()).thenReturn(100L);
73+
when(snapshot.getSize()).thenReturn(snapshotSize);
74+
when(snapshot.getAccountId()).thenReturn(ownerAccountId);
75+
when(dataStoreMgr.getDataStore(poolId, DataStoreRole.Primary)).thenReturn(store);
76+
when(store.create(snapshot)).thenReturn(snapshotOnStore);
77+
when(snapshotOnStore.getDataCenterId()).thenReturn(2L);
78+
when(snapshotSrv.copySnapshot(snapshot, snapshotOnStore, strategy)).thenReturn(future);
79+
when(future.get()).thenReturn(result);
80+
when(result.isFailed()).thenReturn(false);
81+
82+
try (MockedStatic<CallContext> ctxMock = Mockito.mockStatic(CallContext.class)) {
83+
CallContext ctx = Mockito.mock(CallContext.class);
84+
ctxMock.when(CallContext::current).thenReturn(ctx);
85+
// Use SYSTEM ids so the usage-event branch is skipped; the point of the
86+
// test is which account the primary_storage count is charged to.
87+
when(ctx.getCallingUserId()).thenReturn(Account.ACCOUNT_ID_SYSTEM);
88+
when(ctx.getCallingAccountId()).thenReturn(Account.ACCOUNT_ID_SYSTEM);
89+
90+
snapshotManager.copySnapshotOnPool(snapshot, strategy, poolId);
91+
}
92+
93+
// Must charge the snapshot OWNER's account, not the calling user id.
94+
// Before the fix this was called with CallContext.getCallingUserId().
95+
Mockito.verify(_resourceLimitMgr).incrementResourceCount(
96+
Mockito.eq(ownerAccountId), Mockito.eq(ResourceType.primary_storage), Mockito.eq(snapshotSize));
97+
}
98+
}

0 commit comments

Comments
 (0)