Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ public ResponseObject loginUser(HttpSession session, String username, String pas
boolean forgotPassword(UserAccount userAccount, Domain domain);

boolean resetPassword(UserAccount userAccount, String token, String password);

String getDomainId(Map<String, Object[]> params);
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,14 @@ private String doOauthAuthentication(HttpSession session, Long domainId, String
}

protected Long getDomainIdFromParams(Map<String, Object[]> params, StringBuilder auditTrailSb, String responseType) {
String[] domainIdArr = (String[])params.get(ApiConstants.DOMAIN_ID);
String domainIdStr = _apiServer.getDomainId(params);
Long domainId = null;
if (domainIdArr != null && domainIdArr.length > 0) {
if (StringUtils.isNotEmpty(domainIdStr)) {
try {
//check if UUID is passed in for domain
domainId = _apiServer.fetchDomainId(domainIdArr[0]);
domainId = _apiServer.fetchDomainId(domainIdStr);
if (domainId == null) {
domainId = Long.parseLong(domainIdArr[0]);
domainId = Long.parseLong(domainIdStr);
}
auditTrailSb.append(" domainid=" + domainId);// building the params for POST call
} catch (final NumberFormatException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,29 @@ public void testGetDomainIdFromParams() {
ApiServer apiServer = mock(ApiServer.class);
cmd._apiServer = apiServer;
when(apiServer.fetchDomainId("1234")).thenReturn(5678L);
when(apiServer.getDomainId(params)).thenCallRealMethod();

Long domainId = cmd.getDomainIdFromParams(params, auditTrailSb, responseType);

assertEquals(Long.valueOf(5678), domainId);
assertEquals(" domainid=5678", auditTrailSb.toString());
}

@Test
public void testGetDomainIdFromCamelCaseParam() {
StringBuilder auditTrailSb = new StringBuilder();
String responseType = "json";
Map<String, Object[]> params = new HashMap<>();
params.put(ApiConstants.DOMAIN_ID, null);
params.put(ApiConstants.DOMAIN__ID, new String[]{"5678"});
ApiServer apiServer = mock(ApiServer.class);
cmd._apiServer = apiServer;
when(apiServer.fetchDomainId("5678")).thenReturn(1234L);
when(apiServer.getDomainId(params)).thenCallRealMethod();

Long domainId = cmd.getDomainIdFromParams(params, auditTrailSb, responseType);

assertEquals(Long.valueOf(1234), domainId);
assertEquals(" domainid=1234", auditTrailSb.toString());
}
}
20 changes: 20 additions & 0 deletions server/src/main/java/com/cloud/api/ApiServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
import org.apache.cloudstack.user.UserPasswordResetManager;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.EnumUtils;
import org.apache.http.ConnectionClosedException;
import org.apache.http.HttpException;
Expand Down Expand Up @@ -1354,6 +1355,25 @@ public boolean resetPassword(UserAccount userAccount, String token, String passw
return userPasswordResetManager.validateAndResetPassword(userAccount, token, password);
}

@Override
public String getDomainId(Map<String, Object[]> params) {
if (MapUtils.isEmpty(params)) {
return null;
}

String[] domainIdArr = (String[])params.get(ApiConstants.DOMAIN_ID);
if (domainIdArr == null) {
// Fallback to support clients using the camelCase parameter name "domainId"
domainIdArr = (String[])params.get(ApiConstants.DOMAIN__ID);
}

if (domainIdArr == null || domainIdArr.length == 0) {
return null;
}

return domainIdArr[0];
}

private void checkCommandAvailable(final User user, final String commandName, final InetAddress remoteAddress) throws PermissionDeniedException {
if (user == null) {
throw new PermissionDeniedException("User is null for role based API access check for command" + commandName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.cloud.domain.Domain;
import com.cloud.user.User;
import com.cloud.user.UserAccount;
import com.cloud.utils.StringUtils;
import org.apache.cloudstack.api.ApiServerService;
import com.cloud.api.response.ApiResponseSerializer;
import com.cloud.exception.CloudAuthenticationException;
Expand Down Expand Up @@ -110,14 +111,14 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
// FIXME: ported from ApiServlet, refactor and cleanup
final String[] username = (String[])params.get(ApiConstants.USERNAME);
final String[] password = (String[])params.get(ApiConstants.PASSWORD);
final String[] domainIdArr = (String[])params.get(ApiConstants.DOMAIN_ID);
String domainIdStr = _apiServer.getDomainId(params);
Long domainId = null;
if (domainIdArr != null && domainIdArr.length > 0) {
if (StringUtils.isNotEmpty(domainIdStr)) {
try {
//check if UUID is passed in for domain
domainId = _apiServer.fetchDomainId(domainIdArr[0]);
domainId = _apiServer.fetchDomainId(domainIdStr);
if (domainId == null) {
domainId = Long.parseLong(domainIdArr[0]);
domainId = Long.parseLong(domainIdStr);
}
auditTrailSb.append(" domainid=" + domainId);// building the params for POST call
} catch (final NumberFormatException e) {
Expand Down
Loading