Skip to content

Commit fc461ab

Browse files
author
abhishek
committed
bug 6755: adding checks for commands before they hit the ms; to ensure they are valid. Also returning the right error msg if the command is not valid
status 6755: resolved fixed
1 parent 2537dc3 commit fc461ab

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.net.Socket;
3232
import java.net.URLDecoder;
3333
import java.net.URLEncoder;
34+
import java.security.InvalidParameterException;
3435
import java.security.SecureRandom;
3536
import java.util.ArrayList;
3637
import java.util.Collections;
@@ -87,6 +88,7 @@
8788
import com.cloud.domain.DomainVO;
8889
import com.cloud.event.EventUtils;
8990
import com.cloud.exception.CloudAuthenticationException;
91+
import com.cloud.exception.InvalidParameterValueException;
9092
import com.cloud.maid.StackMaid;
9193
import com.cloud.server.ManagementServer;
9294
import com.cloud.user.Account;
@@ -125,14 +127,16 @@ public class ApiServer implements HttpRequestHandler {
125127
private static List<String> s_resellerCommands = null; // AKA domain-admin
126128
private static List<String> s_adminCommands = null;
127129
private static List<String> s_readOnlyAdminCommands = null;
128-
130+
private static List<String> s_allCommands = null;
131+
129132
private static ExecutorService _executor = new ThreadPoolExecutor(10, 150, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("ApiServer"));
130133

131134
static {
132135
s_userCommands = new ArrayList<String>();
133136
s_resellerCommands = new ArrayList<String>();
134137
s_adminCommands = new ArrayList<String>();
135138
s_readOnlyAdminCommands = new ArrayList<String>();
139+
s_allCommands = new ArrayList<String>();
136140
}
137141

138142
private ApiServer() { }
@@ -182,6 +186,11 @@ public void init(String[] apiConfig) {
182186
}
183187
}
184188
}
189+
190+
s_allCommands.addAll(s_adminCommands);
191+
s_allCommands.addAll(s_readOnlyAdminCommands);
192+
s_allCommands.addAll(s_userCommands);
193+
s_allCommands.addAll(s_resellerCommands);
185194
}
186195
} catch (FileNotFoundException fnfex) {
187196
s_logger.error("Unable to find properites file", fnfex);
@@ -441,8 +450,13 @@ private void buildAuditTrail(StringBuffer auditTrailSb, String command, String r
441450
}
442451
*/
443452
}
453+
private static boolean isCommandAvailable(String commandName) {
454+
boolean isCommandAvailable = false;
455+
isCommandAvailable = s_allCommands.contains(commandName);
456+
return isCommandAvailable;
457+
}
444458

445-
public boolean verifyRequest(Map<String, Object[]> requestParameters, Long userId) {
459+
public boolean verifyRequest(Map<String, Object[]> requestParameters, Long userId) throws InvalidParameterException {
446460
try {
447461
String apiKey = null;
448462
String secretKey = null;
@@ -467,7 +481,15 @@ public boolean verifyRequest(Map<String, Object[]> requestParameters, Long userI
467481
return false;
468482
}
469483
return true;
484+
}else{
485+
//check against every available command to see if the command exists or not
486+
if(!isCommandAvailable(commandName)){
487+
s_logger.warn("The given command:"+commandName+" does not exist");
488+
throw new InvalidParameterException("The given command:"+commandName+" does not exist");
489+
}
490+
470491
}
492+
471493

472494
// - build a request string with sorted params, make sure it's all lowercase
473495
// - sign the request, verify the signature is the same
@@ -552,6 +574,9 @@ public boolean verifyRequest(Map<String, Object[]> requestParameters, Long userI
552574
}
553575
return equalSig;
554576
} catch (Exception ex) {
577+
if(ex instanceof InvalidParameterException){
578+
throw new InvalidParameterException(ex.getMessage());
579+
}
555580
s_logger.error("unable to verifty request signature", ex);
556581
}
557582
return false;

‎server/src/com/cloud/api/ApiServlet.java‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.IOException;
2222
import java.io.OutputStream;
23+
import java.security.InvalidParameterException;
2324
import java.util.Enumeration;
2425
import java.util.HashMap;
2526
import java.util.Map;
@@ -32,6 +33,7 @@
3233
import org.apache.log4j.Logger;
3334

3435
import com.cloud.exception.CloudAuthenticationException;
36+
import com.cloud.exception.InvalidParameterValueException;
3537
import com.cloud.maid.StackMaid;
3638
import com.cloud.user.Account;
3739
import com.cloud.user.UserContext;
@@ -266,7 +268,15 @@ private void processRequest(HttpServletRequest req, HttpServletResponse resp) {
266268
s_logger.trace("exception processing request: " + ioex);
267269
}
268270
auditTrailSb.append(" exception processing request" );
269-
} catch (Exception ex) {
271+
}catch (InvalidParameterException ipe){
272+
auditTrailSb.append(" " + HttpServletResponse.SC_NOT_FOUND + " " + ipe.getMessage());
273+
try {
274+
resp.sendError(HttpServletResponse.SC_NOT_FOUND, ipe.getMessage());
275+
} catch (IOException e) {
276+
s_logger.error("Unable to send back error response for invalid command");
277+
auditTrailSb.append(" " + HttpServletResponse.SC_NOT_FOUND + " " + "Unable to send back error response for "+ipe.getMessage());
278+
}
279+
}catch (Exception ex) {
270280
s_logger.error("unknown exception writing api response", ex);
271281
auditTrailSb.append(" unknown exception writing api response");
272282
} finally {

‎server/src/com/cloud/api/BaseCmd.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public enum CommandType {
104104
public static final int NET_LIST_ERROR = 570;
105105
public static final int CUSTOM_CERT_UPDATE_ERROR = 571;
106106
public static final int PREPARE_STORAGE_MAINTENANCE_ERROR = 572;
107-
public static final int CANCEL_STORAGE_MAINTENANCE_ERROR = 573;
107+
public static final int CANCEL_STORAGE_MAINTENANCE_ERROR = 573;
108108
public static final int STORAGE_RESOURCE_IN_USE = 580;
109109

110110
public static final DateFormat INPUT_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

0 commit comments

Comments
 (0)