Skip to content

Commit 9ef6f9f

Browse files
committed
bug 9663: do account permission check against the volume when create snapshot
status 9663: resolved fixed Conflicts: api/src/com/cloud/storage/snapshot/SnapshotService.java
1 parent 62988ac commit 9ef6f9f

4 files changed

Lines changed: 105 additions & 114 deletions

File tree

‎api/src/com/cloud/api/commands/CreateSnapshotCmd.java‎

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,30 @@
3434
import com.cloud.storage.Volume;
3535
import com.cloud.user.Account;
3636

37-
@Implementation(description="Creates an instant snapshot of a volume.", responseObject=SnapshotResponse.class)
37+
@Implementation(description = "Creates an instant snapshot of a volume.", responseObject = SnapshotResponse.class)
3838
public class CreateSnapshotCmd extends BaseAsyncCreateCmd {
39-
public static final Logger s_logger = Logger.getLogger(CreateSnapshotCmd.class.getName());
40-
private static final String s_name = "createsnapshotresponse";
39+
public static final Logger s_logger = Logger.getLogger(CreateSnapshotCmd.class.getName());
40+
private static final String s_name = "createsnapshotresponse";
4141

42-
/////////////////////////////////////////////////////
43-
//////////////// API parameters /////////////////////
44-
/////////////////////////////////////////////////////
42+
// ///////////////////////////////////////////////////
43+
// ////////////// API parameters /////////////////////
44+
// ///////////////////////////////////////////////////
4545

46-
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="The account of the snapshot. The account parameter must be used with the domainId parameter.")
46+
@Parameter(name = ApiConstants.ACCOUNT, type = CommandType.STRING, description = "The account of the snapshot. The account parameter must be used with the domainId parameter.")
4747
private String accountName;
4848

49-
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="The domain ID of the snapshot. If used with the account parameter, specifies a domain for the account associated with the disk volume.")
49+
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.LONG, description = "The domain ID of the snapshot. If used with the account parameter, specifies a domain for the account associated with the disk volume.")
5050
private Long domainId;
5151

52-
@Parameter(name=ApiConstants.VOLUME_ID, type=CommandType.LONG, required=true, description="The ID of the disk volume")
52+
@Parameter(name = ApiConstants.VOLUME_ID, type = CommandType.LONG, required = true, description = "The ID of the disk volume")
5353
private Long volumeId;
5454

55-
@Parameter(name=ApiConstants.POLICY_ID, type=CommandType.LONG, description="policy id of the snapshot, if this is null, then use MANUAL_POLICY.")
55+
@Parameter(name = ApiConstants.POLICY_ID, type = CommandType.LONG, description = "policy id of the snapshot, if this is null, then use MANUAL_POLICY.")
5656
private Long policyId;
57-
58-
/////////////////////////////////////////////////////
59-
/////////////////// Accessors ///////////////////////
60-
/////////////////////////////////////////////////////
57+
58+
// ///////////////////////////////////////////////////
59+
// ///////////////// Accessors ///////////////////////
60+
// ///////////////////////////////////////////////////
6161

6262
public String getAccountName() {
6363
return accountName;
@@ -70,26 +70,26 @@ public Long getDomainId() {
7070
public Long getVolumeId() {
7171
return volumeId;
7272
}
73-
73+
7474
public Long getPolicyId() {
75-
if( policyId != null) {
76-
return policyId;
77-
} else {
78-
return Snapshot.MANUAL_POLICY_ID;
79-
}
75+
if (policyId != null) {
76+
return policyId;
77+
} else {
78+
return Snapshot.MANUAL_POLICY_ID;
79+
}
8080
}
8181

82-
/////////////////////////////////////////////////////
83-
/////////////// API Implementation///////////////////
84-
/////////////////////////////////////////////////////
82+
// ///////////////////////////////////////////////////
83+
// ///////////// API Implementation///////////////////
84+
// ///////////////////////////////////////////////////
8585

8686
@Override
8787
public String getCommandName() {
8888
return s_name;
8989
}
90-
90+
9191
public static String getResultObjectName() {
92-
return "snapshot";
92+
return "snapshot";
9393
}
9494

9595
@Override
@@ -110,27 +110,27 @@ public String getEventType() {
110110

111111
@Override
112112
public String getEventDescription() {
113-
return "creating snapshot for volume: " + getVolumeId();
113+
return "creating snapshot for volume: " + getVolumeId();
114114
}
115-
115+
116116
@Override
117117
public AsyncJob.Type getInstanceType() {
118-
return AsyncJob.Type.Snapshot;
118+
return AsyncJob.Type.Snapshot;
119119
}
120-
120+
121121
@Override
122-
public void create() throws ResourceAllocationException{
123-
Snapshot snapshot = _snapshotService.allocSnapshot(this);
122+
public void create() throws ResourceAllocationException {
123+
Snapshot snapshot = _snapshotService.allocSnapshot(getVolumeId(), getPolicyId());
124124
if (snapshot != null) {
125125
this.setEntityId(snapshot.getId());
126126
} else {
127127
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create snapshot");
128128
}
129129
}
130-
130+
131131
@Override
132-
public void execute() {
133-
Snapshot snapshot = _snapshotService.createSnapshot(this);
132+
public void execute() {
133+
Snapshot snapshot = _snapshotService.createSnapshot(getVolumeId(), getPolicyId(), getEntityId());
134134
if (snapshot != null) {
135135
SnapshotResponse response = _responseGenerator.createSnapshotResponse(snapshot);
136136
response.setResponseName(getCommandName());

‎api/src/com/cloud/storage/snapshot/SnapshotService.java‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.util.List;
2121

22-
import com.cloud.api.commands.CreateSnapshotCmd;
2322
import com.cloud.api.commands.CreateSnapshotPolicyCmd;
2423
import com.cloud.api.commands.DeleteSnapshotCmd;
2524
import com.cloud.api.commands.DeleteSnapshotPoliciesCmd;
@@ -31,14 +30,6 @@
3130
import com.cloud.storage.Snapshot;
3231

3332
public interface SnapshotService {
34-
/**
35-
* Create a snapshot of a volume
36-
*
37-
* @param cmd
38-
* the API command wrapping the parameters for creating the snapshot (mainly volumeId)
39-
* @return the Snapshot that was created
40-
*/
41-
Snapshot createSnapshot(CreateSnapshotCmd cmd);
4233

4334
/**
4435
* List all snapshots of a disk volume. Optionally lists snapshots created by specified interval
@@ -87,6 +78,14 @@ public interface SnapshotService {
8778

8879
boolean deleteSnapshotPolicies(DeleteSnapshotPoliciesCmd cmd);
8980

90-
Snapshot allocSnapshot(CreateSnapshotCmd cmd) throws ResourceAllocationException;
81+
Snapshot allocSnapshot(Long volumeId, Long policyId) throws ResourceAllocationException;
9182

83+
/**
84+
* Create a snapshot of a volume
85+
*
86+
* @param cmd
87+
* the API command wrapping the parameters for creating the snapshot (mainly volumeId)
88+
* @return the Snapshot that was created
89+
*/
90+
Snapshot createSnapshot(Long volumeId, Long policyId, Long snapshotId);
9291
}

‎server/src/com/cloud/storage/snapshot/SnapshotManager.java‎

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,52 +22,49 @@
2222
import com.cloud.exception.ResourceAllocationException;
2323
import com.cloud.storage.SnapshotPolicyVO;
2424
import com.cloud.storage.SnapshotVO;
25-
import com.cloud.storage.Storage.ImageFormat;
2625
import com.cloud.storage.VolumeVO;
2726
import com.cloud.utils.db.Filter;
2827

2928
/**
30-
*
31-
* SnapshotManager contains all of the code to work with volume snapshots.
32-
*
33-
*/
29+
*
30+
* SnapshotManager contains all of the code to work with volume snapshots.
31+
*
32+
*/
3433
public interface SnapshotManager {
35-
36-
public static final int HOURLYMAX = 8;
37-
public static final int DAILYMAX = 8;
38-
public static final int WEEKLYMAX = 8;
39-
public static final int MONTHLYMAX = 12;
40-
public static final int DELTAMAX = 16;
41-
42-
/**
43-
* Create a snapshot of a volume
44-
* @param cmd the API command wrapping the parameters for creating the snapshot (mainly volumeId)
45-
* @return the Snapshot that was created
46-
*/
47-
SnapshotVO createSnapshotImpl(Long volumeId, Long policyId, Long snapshotId);
34+
35+
public static final int HOURLYMAX = 8;
36+
public static final int DAILYMAX = 8;
37+
public static final int WEEKLYMAX = 8;
38+
public static final int MONTHLYMAX = 12;
39+
public static final int DELTAMAX = 16;
4840

4941
/**
50-
* After successfully creating a snapshot of a volume, copy the snapshot to the secondary storage for
51-
* 1) reliability
52-
* 2) So that storage space on Primary is conserved.
53-
* @param snapshot Info about the created snapshot on primary storage.
54-
* @param startEventId event id of the scheduled event for this snapshot
55-
* @return True if the snapshot was successfully backed up.
42+
* After successfully creating a snapshot of a volume, copy the snapshot to the secondary storage for 1) reliability 2) So
43+
* that storage space on Primary is conserved.
44+
*
45+
* @param snapshot
46+
* Info about the created snapshot on primary storage.
47+
* @param startEventId
48+
* event id of the scheduled event for this snapshot
49+
* @return True if the snapshot was successfully backed up.
5650
*/
5751
public boolean backupSnapshotToSecondaryStorage(SnapshotVO snapshot);
58-
52+
5953
/**
60-
* Once a snapshot has completed,
61-
* 1) If success, update the database entries
62-
* 2) If success and there are excess snapshots for any of the policies given, delete the oldest one.
63-
* 3) Schedule the next recurring snapshot.
64-
* @param volumeId The volume for which the snapshot is being taken
65-
* @param snapshotId The snapshot which has just completed
66-
* @param policyIds The list of policyIds to which this snapshot belongs to
67-
* @param backedUp If true, the snapshot has been successfully created.
54+
* Once a snapshot has completed, 1) If success, update the database entries 2) If success and there are excess snapshots
55+
* for any of the policies given, delete the oldest one. 3) Schedule the next recurring snapshot.
56+
*
57+
* @param volumeId
58+
* The volume for which the snapshot is being taken
59+
* @param snapshotId
60+
* The snapshot which has just completed
61+
* @param policyIds
62+
* The list of policyIds to which this snapshot belongs to
63+
* @param backedUp
64+
* If true, the snapshot has been successfully created.
6865
*/
6966
void postCreateSnapshot(Long volumeId, Long snapshotId, Long policyId, boolean backedUp);
70-
67+
7168
/**
7269
* Destroys the specified snapshot from secondary storage
7370
*/
@@ -77,41 +74,41 @@ public interface SnapshotManager {
7774
* Deletes snapshot scheduling policy. Delete will fail if this policy is assigned to one or more volumes
7875
*/
7976
boolean deletePolicy(long userId, Long policyId);
80-
77+
8178
/**
8279
* Lists all snapshots for the volume which are created using schedule of the specified policy
8380
*/
8481
/*
85-
List<SnapshotVO> listSnapsforPolicy(long policyId, Filter filter);
86-
*/
82+
* List<SnapshotVO> listSnapsforPolicy(long policyId, Filter filter);
83+
*/
8784
/**
8885
* List all policies which are assigned to the specified volume
8986
*/
9087
List<SnapshotPolicyVO> listPoliciesforVolume(long volumeId);
9188

9289
/**
93-
* List all policies to which a specified snapshot belongs. For ex: A snapshot
94-
* may belong to a hourly snapshot and a daily snapshot run at the same time
90+
* List all policies to which a specified snapshot belongs. For ex: A snapshot may belong to a hourly snapshot and a daily
91+
* snapshot run at the same time
9592
*/
9693
/*
97-
List<SnapshotPolicyVO> listPoliciesforSnapshot(long snapshotId);
98-
*/
94+
* List<SnapshotPolicyVO> listPoliciesforSnapshot(long snapshotId);
95+
*/
9996
/**
100-
* List all snapshots for a specified volume irrespective of the policy which
101-
* created the snapshot
97+
* List all snapshots for a specified volume irrespective of the policy which created the snapshot
10298
*/
10399
List<SnapshotVO> listSnapsforVolume(long volumeId);
104100

105-
void deletePoliciesForVolume(Long volumeId);
101+
void deletePoliciesForVolume(Long volumeId);
106102

107103
/**
108-
* For each of the volumes in the account,
109-
* (which can span across multiple zones and multiple secondary storages),
110-
* delete the dir on the secondary storage which contains the backed up snapshots for that volume.
111-
* This is called during deleteAccount.
112-
* @param accountId The account which is to be deleted.
104+
* For each of the volumes in the account, (which can span across multiple zones and multiple secondary storages), delete
105+
* the dir on the secondary storage which contains the backed up snapshots for that volume. This is called during
106+
* deleteAccount.
107+
*
108+
* @param accountId
109+
* The account which is to be deleted.
113110
*/
114-
boolean deleteSnapshotDirsForAccount(long accountId);
111+
boolean deleteSnapshotDirsForAccount(long accountId);
115112

116113
void validateSnapshot(Long userId, SnapshotVO snapshot);
117114

@@ -121,10 +118,12 @@ public interface SnapshotManager {
121118

122119
/**
123120
* Create a snapshot of a volume
124-
* @param cmd the API command wrapping the parameters for creating the snapshot (mainly volumeId)
121+
*
122+
* @param cmd
123+
* the API command wrapping the parameters for creating the snapshot (mainly volumeId)
125124
* @return the Snapshot that was created
126125
*/
127-
SnapshotVO createSnapshotOnPrimary(VolumeVO volume, Long polocyId, Long snapshotId) throws ResourceAllocationException;
126+
SnapshotVO createSnapshotOnPrimary(VolumeVO volume, Long polocyId, Long snapshotId) throws ResourceAllocationException;
128127

129128
List<SnapshotPolicyVO> listPoliciesforSnapshot(long snapshotId);
130129

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

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import com.cloud.agent.api.DeleteSnapshotsDirCommand;
3939
import com.cloud.agent.api.ManageSnapshotAnswer;
4040
import com.cloud.agent.api.ManageSnapshotCommand;
41-
import com.cloud.api.commands.CreateSnapshotCmd;
4241
import com.cloud.api.commands.CreateSnapshotPolicyCmd;
4342
import com.cloud.api.commands.DeleteSnapshotCmd;
4443
import com.cloud.api.commands.DeleteSnapshotPoliciesCmd;
@@ -229,10 +228,10 @@ protected Answer sendToPool(Volume vol, Command cmd) {
229228
Thread.sleep(_pauseInterval * 1000);
230229
} catch (InterruptedException e) {
231230
}
232-
231+
233232
s_logger.debug("Retrying...");
234233
}
235-
234+
236235
s_logger.warn("After " + _totalRetries + " retries, the command " + cmd.getClass().getName() + " did not succeed.");
237236

238237
return null;
@@ -327,7 +326,8 @@ public SnapshotVO createSnapshotImpl(long volumeId, long policyId) throws Resour
327326

328327
@Override
329328
@DB
330-
public SnapshotVO createSnapshotImpl(Long volumeId, Long policyId, Long snapshotId) {
329+
@ActionEvent(eventType = EventTypes.EVENT_SNAPSHOT_CREATE, eventDescription = "creating snapshot", async = true)
330+
public SnapshotVO createSnapshot(Long volumeId, Long policyId, Long snapshotId) {
331331
VolumeVO v = _volsDao.findById(volumeId);
332332
Account owner = _accountMgr.getAccount(v.getAccountId());
333333
SnapshotVO snapshot = null;
@@ -419,15 +419,6 @@ public SnapshotVO createSnapshotImpl(Long volumeId, Long policyId, Long snapshot
419419
return snapshot;
420420
}
421421

422-
@Override
423-
@ActionEvent(eventType = EventTypes.EVENT_SNAPSHOT_CREATE, eventDescription = "creating snapshot", async = true)
424-
public SnapshotVO createSnapshot(CreateSnapshotCmd cmd) {
425-
Long volumeId = cmd.getVolumeId();
426-
Long policyId = cmd.getPolicyId();
427-
Long snapshotId = cmd.getEntityId();
428-
return createSnapshotImpl(volumeId, policyId, snapshotId);
429-
}
430-
431422
private SnapshotVO updateDBOnCreate(Long id, String snapshotPath, long preSnapshotId) {
432423
SnapshotVO createdSnapshot = _snapshotDao.findByIdIncludingRemoved(id);
433424
createdSnapshot.setPath(snapshotPath);
@@ -1169,21 +1160,23 @@ public Type getSnapshotType(IntervalType intvType) {
11691160
}
11701161

11711162
@Override
1172-
public SnapshotVO allocSnapshot(CreateSnapshotCmd cmd) throws ResourceAllocationException {
1173-
Long volumeId = cmd.getVolumeId();
1174-
Long policyId = cmd.getPolicyId();
1163+
public SnapshotVO allocSnapshot(Long volumeId, Long policyId) throws ResourceAllocationException {
1164+
Account caller = UserContext.current().getCaller();
11751165
VolumeVO volume = _volsDao.findById(volumeId);
11761166
if (volume == null) {
1177-
throw new CloudRuntimeException("Creating snapshot failed due to volume:" + volumeId + " doesn't exist");
1167+
throw new InvalidParameterValueException("Creating snapshot failed due to volume:" + volumeId + " doesn't exist");
11781168
}
11791169
if (volume.getState() != Volume.State.Ready) {
1180-
throw new InvalidParameterValueException("VolumeId: " + volumeId + " is not in Created state but " + volume.getState() + ". Cannot take snapshot.");
1170+
throw new InvalidParameterValueException("VolumeId: " + volumeId + " is not in " + Volume.State.Ready + " state but " + volume.getState() + ". Cannot take snapshot.");
11811171
}
11821172
StoragePoolVO storagePoolVO = _storagePoolDao.findById(volume.getPoolId());
11831173
if (storagePoolVO == null) {
11841174
throw new InvalidParameterValueException("VolumeId: " + volumeId + " please attach this volume to a VM before create snapshot for it");
11851175
}
11861176

1177+
// Verify permissions
1178+
_accountMgr.checkAccess(caller, volume);
1179+
11871180
Account owner = _accountMgr.getAccount(volume.getAccountId());
11881181
if (_accountMgr.resourceLimitExceeded(owner, ResourceType.snapshot)) {
11891182
ResourceAllocationException rae = new ResourceAllocationException("Maximum number of snapshots for account: " + owner.getAccountName() + " has been exceeded.");

0 commit comments

Comments
 (0)