Skip to content

Commit 62750da

Browse files
sureshanapartiLocharla, Sandeep
authored andcommitted
test_accounts.py failure fix - keep the camelCase parameter "domainId" (apache#12689)
1 parent 1704576 commit 62750da

4 files changed

Lines changed: 47 additions & 8 deletions

File tree

plugins/user-authenticators/oauth2/src/main/java/org/apache/cloudstack/oauth2/api/command/OauthLoginAPIAuthenticatorCmd.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,14 @@ private String doOauthAuthentication(HttpSession session, Long domainId, String
183183
}
184184

185185
protected Long getDomainIdFromParams(Map<String, Object[]> params, StringBuilder auditTrailSb, String responseType) {
186-
String[] domainIdArr = (String[])params.get(ApiConstants.DOMAIN_ID);
186+
String domainIdStr = _apiServer.getDomainId(params);
187187
Long domainId = null;
188-
if (domainIdArr != null && domainIdArr.length > 0) {
188+
if (StringUtils.isNotEmpty(domainIdStr)) {
189189
try {
190190
//check if UUID is passed in for domain
191-
domainId = _apiServer.fetchDomainId(domainIdArr[0]);
191+
domainId = _apiServer.fetchDomainId(domainIdStr);
192192
if (domainId == null) {
193-
domainId = Long.parseLong(domainIdArr[0]);
193+
domainId = Long.parseLong(domainIdStr);
194194
}
195195
auditTrailSb.append(" domainid=" + domainId);// building the params for POST call
196196
} catch (final NumberFormatException e) {

plugins/user-authenticators/oauth2/src/test/java/org/apache/cloudstack/oauth2/api/command/OauthLoginAPIAuthenticatorCmdTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,29 @@ public void testGetDomainIdFromParams() {
8585
ApiServer apiServer = mock(ApiServer.class);
8686
cmd._apiServer = apiServer;
8787
when(apiServer.fetchDomainId("1234")).thenReturn(5678L);
88+
when(apiServer.getDomainId(params)).thenCallRealMethod();
8889

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

9192
assertEquals(Long.valueOf(5678), domainId);
9293
assertEquals(" domainid=5678", auditTrailSb.toString());
9394
}
95+
96+
@Test
97+
public void testGetDomainIdFromCamelCaseParam() {
98+
StringBuilder auditTrailSb = new StringBuilder();
99+
String responseType = "json";
100+
Map<String, Object[]> params = new HashMap<>();
101+
params.put(ApiConstants.DOMAIN_ID, null);
102+
params.put(ApiConstants.DOMAIN__ID, new String[]{"5678"});
103+
ApiServer apiServer = mock(ApiServer.class);
104+
cmd._apiServer = apiServer;
105+
when(apiServer.fetchDomainId("5678")).thenReturn(1234L);
106+
when(apiServer.getDomainId(params)).thenCallRealMethod();
107+
108+
Long domainId = cmd.getDomainIdFromParams(params, auditTrailSb, responseType);
109+
110+
assertEquals(Long.valueOf(1234), domainId);
111+
assertEquals(" domainid=1234", auditTrailSb.toString());
112+
}
94113
}

server/src/main/java/com/cloud/api/ApiServer.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,25 @@ public boolean resetPassword(UserAccount userAccount, String token, String passw
14131413
return userPasswordResetManager.validateAndResetPassword(userAccount, token, password);
14141414
}
14151415

1416+
@Override
1417+
public String getDomainId(Map<String, Object[]> params) {
1418+
if (MapUtils.isEmpty(params)) {
1419+
return null;
1420+
}
1421+
1422+
String[] domainIdArr = (String[])params.get(ApiConstants.DOMAIN_ID);
1423+
if (domainIdArr == null) {
1424+
// Fallback to support clients using the camelCase parameter name "domainId"
1425+
domainIdArr = (String[])params.get(ApiConstants.DOMAIN__ID);
1426+
}
1427+
1428+
if (domainIdArr == null || domainIdArr.length == 0) {
1429+
return null;
1430+
}
1431+
1432+
return domainIdArr[0];
1433+
}
1434+
14161435
private void checkCommandAvailable(final User user, final String commandName, final InetAddress remoteAddress) throws PermissionDeniedException {
14171436
if (user == null) {
14181437
throw new PermissionDeniedException("User is null for role based API access check for command" + commandName);

server/src/main/java/com/cloud/api/auth/DefaultLoginAPIAuthenticatorCmd.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.cloud.domain.Domain;
2121
import com.cloud.user.User;
2222
import com.cloud.user.UserAccount;
23+
import com.cloud.utils.StringUtils;
2324
import org.apache.cloudstack.api.ApiServerService;
2425
import com.cloud.api.response.ApiResponseSerializer;
2526
import com.cloud.exception.CloudAuthenticationException;
@@ -115,14 +116,14 @@ public String authenticate(String command, Map<String, Object[]> params, HttpSes
115116
// FIXME: ported from ApiServlet, refactor and cleanup
116117
final String[] username = (String[])params.get(ApiConstants.USERNAME);
117118
final String[] password = (String[])params.get(ApiConstants.PASSWORD);
118-
final String[] domainIdArr = (String[])params.get(ApiConstants.DOMAIN_ID);
119+
String domainIdStr = _apiServer.getDomainId(params);
119120
Long domainId = null;
120-
if (domainIdArr != null && domainIdArr.length > 0) {
121+
if (StringUtils.isNotEmpty(domainIdStr)) {
121122
try {
122123
//check if UUID is passed in for domain
123-
domainId = _apiServer.fetchDomainId(domainIdArr[0]);
124+
domainId = _apiServer.fetchDomainId(domainIdStr);
124125
if (domainId == null) {
125-
domainId = Long.parseLong(domainIdArr[0]);
126+
domainId = Long.parseLong(domainIdStr);
126127
}
127128
auditTrailSb.append(" domainid=" + domainId);// building the params for POST call
128129
} catch (final NumberFormatException e) {

0 commit comments

Comments
 (0)