Skip to content

Commit d5acabd

Browse files
server: Avoid Null pointer at DomainChecker and enhance AssignVMCmd (#4279)
When executing request assignVirtualMachine with null domainID and a valid projectID then a NullPointerException happens at DomainChecker.java. Command example: assign virtualmachine virtualmachineid=vmID projectid=projectID account=admin The NullPointerException that is thrown at DomainChecker is handled at AssignVMCmd.java#L142, resulting in the following log message: Failed to move vm null.
1 parent 4746c8c commit d5acabd

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

api/src/main/java/org/apache/cloudstack/api/command/admin/vm/AssignVMCmd.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,14 @@ public void execute() {
138138
e.printStackTrace();
139139
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
140140
} catch (Exception e) {
141-
e.printStackTrace();
142-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to move vm " + e.getMessage());
141+
s_logger.error("Failed to move vm due to: " + e.getStackTrace());
142+
if (e.getMessage() != null) {
143+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to move vm due to " + e.getMessage());
144+
} else if (e.getCause() != null) {
145+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to move vm due to " + e.getCause());
146+
} else {
147+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to move vm");
148+
}
143149
}
144150

145151
}

server/src/main/java/com/cloud/acl/DomainChecker.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ public boolean checkAccess(Account caller, Domain domain) throws PermissionDenie
106106
if (caller.getState() != Account.State.enabled) {
107107
throw new PermissionDeniedException(caller + " is disabled.");
108108
}
109+
110+
if (domain == null) {
111+
throw new PermissionDeniedException(String.format("Provided domain is NULL, cannot check access for account [uuid=%s, name=%s]", caller.getUuid(), caller.getAccountName()));
112+
}
113+
109114
long domainId = domain.getId();
110115

111116
if (_accountService.isNormalUser(caller.getId())) {

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6133,6 +6133,10 @@ public UserVm moveVMToUser(final AssignVMCmd cmd) throws ResourceAllocationExcep
61336133
throw new InvalidParameterValueException("The new account owner " + cmd.getAccountName() + " is disabled.");
61346134
}
61356135

6136+
if (cmd.getProjectId() != null && cmd.getDomainId() == null) {
6137+
throw new InvalidParameterValueException("Please provide a valid domain ID; cannot assign VM to a project if domain ID is NULL.");
6138+
}
6139+
61366140
//check caller has access to both the old and new account
61376141
_accountMgr.checkAccess(caller, null, true, oldAccount);
61386142
_accountMgr.checkAccess(caller, null, true, newAccount);

0 commit comments

Comments
 (0)