Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
server: charge snapshot copy primary storage to the snapshot owner ac…
…count

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.
  • Loading branch information
nagaboinaramgopal committed Sep 2, 2026
commit 076a2594c05339eca7fe9f53df13db005b830f05
Original file line number Diff line number Diff line change
Expand Up @@ -1768,7 +1768,7 @@ private void copyNewSnapshotToZonesOnPrimary(CreateSnapshotPayload payload, Snap
}
}

private boolean copySnapshotOnPool(SnapshotInfo snapshot, SnapshotStrategy snapshotStrategy, Long storagePoolId) {
protected boolean copySnapshotOnPool(SnapshotInfo snapshot, SnapshotStrategy snapshotStrategy, Long storagePoolId) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected boolean copySnapshotOnPool(SnapshotInfo snapshot, SnapshotStrategy snapshotStrategy, Long storagePoolId) {
boolean copySnapshotOnPool(SnapshotInfo snapshot, SnapshotStrategy snapshotStrategy, Long storagePoolId) {

we can make it package private as the test should be from the same package anyway

DataStore store = dataStoreMgr.getDataStore(storagePoolId, DataStoreRole.Primary);
SnapshotInfo snapshotOnStore = (SnapshotInfo) store.create(snapshot);

Expand All @@ -1780,8 +1780,8 @@ private boolean copySnapshotOnPool(SnapshotInfo snapshot, SnapshotStrategy snaps
return false;
}
snapshotZoneDao.addSnapshotToZone(snapshot.getId(), snapshotOnStore.getDataCenterId());
_resourceLimitMgr.incrementResourceCount(CallContext.current().getCallingUserId(), ResourceType.primary_storage, snapshot.getSize());
if (CallContext.current().getCallingUserId() != Account.ACCOUNT_ID_SYSTEM) {
_resourceLimitMgr.incrementResourceCount(snapshot.getAccountId(), ResourceType.primary_storage, snapshot.getSize());
if (CallContext.current().getCallingAccountId() != Account.ACCOUNT_ID_SYSTEM) {
SnapshotVO snapshotVO = _snapshotDao.findByIdIncludingRemoved(snapshot.getSnapshotId());
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_SNAPSHOT_COPY, CallContext.current().getCallingAccountId(), snapshotOnStore.getDataCenterId(), snapshotVO.getId(), null, null, null, snapshotVO.getSize(),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the usage record go to the snapshot owner too? right now if an admin makes the copy, the admin gets billed for it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thank you. You're right, that was inconsistent: the resource count went to the owner but the usage record used the caller. Fixed in f2dfb46 so the usage event is attributed to the snapshot owner (snapshot.getAccountId()), which also matches the snapshot create and delete events.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that covers it, thanks. f2dfb46 sends the usage record to the owner now

snapshotVO.getSize(), snapshotVO.getClass().getName(), snapshotVO.getUuid());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.storage.snapshot;

import static org.mockito.Mockito.when;

import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotService;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy;
import org.apache.cloudstack.framework.async.AsyncCallFuture;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import com.cloud.configuration.Resource.ResourceType;
import com.cloud.storage.DataStoreRole;
import com.cloud.storage.dao.SnapshotZoneDao;
import com.cloud.user.Account;
import com.cloud.user.ResourceLimitService;

@RunWith(MockitoJUnitRunner.Silent.class)
public class SnapshotManagerImplCopyResourceCountTest {

@Mock
DataStoreManager dataStoreMgr;
@Mock
SnapshotService snapshotSrv;
@Mock
SnapshotZoneDao snapshotZoneDao;
@Mock
ResourceLimitService _resourceLimitMgr;

@InjectMocks
SnapshotManagerImpl snapshotManager = new SnapshotManagerImpl();

@Test
@SuppressWarnings("unchecked")
public void copySnapshotOnPoolChargesPrimaryStorageToTheSnapshotOwnerAccount() throws Exception {
long ownerAccountId = 42L;
long snapshotSize = 1024L;
Long poolId = 5L;

SnapshotInfo snapshot = Mockito.mock(SnapshotInfo.class);
SnapshotInfo snapshotOnStore = Mockito.mock(SnapshotInfo.class);
SnapshotStrategy strategy = Mockito.mock(SnapshotStrategy.class);
DataStore store = Mockito.mock(DataStore.class);
AsyncCallFuture<SnapshotResult> future = Mockito.mock(AsyncCallFuture.class);
SnapshotResult result = Mockito.mock(SnapshotResult.class);

when(snapshot.getId()).thenReturn(100L);
when(snapshot.getSize()).thenReturn(snapshotSize);
when(snapshot.getAccountId()).thenReturn(ownerAccountId);
when(dataStoreMgr.getDataStore(poolId, DataStoreRole.Primary)).thenReturn(store);
when(store.create(snapshot)).thenReturn(snapshotOnStore);
when(snapshotOnStore.getDataCenterId()).thenReturn(2L);
when(snapshotSrv.copySnapshot(snapshot, snapshotOnStore, strategy)).thenReturn(future);
when(future.get()).thenReturn(result);
when(result.isFailed()).thenReturn(false);

try (MockedStatic<CallContext> ctxMock = Mockito.mockStatic(CallContext.class)) {
CallContext ctx = Mockito.mock(CallContext.class);
ctxMock.when(CallContext::current).thenReturn(ctx);
// Use SYSTEM ids so the usage-event branch is skipped; the point of the
// test is which account the primary_storage count is charged to.
when(ctx.getCallingUserId()).thenReturn(Account.ACCOUNT_ID_SYSTEM);
when(ctx.getCallingAccountId()).thenReturn(Account.ACCOUNT_ID_SYSTEM);

snapshotManager.copySnapshotOnPool(snapshot, strategy, poolId);
}

// Must charge the snapshot OWNER's account, not the calling user id.
// Before the fix this was called with CallContext.getCallingUserId().
Mockito.verify(_resourceLimitMgr).incrementResourceCount(
Mockito.eq(ownerAccountId), Mockito.eq(ResourceType.primary_storage), Mockito.eq(snapshotSize));
}
}