3131import java .net .Socket ;
3232import java .net .URLDecoder ;
3333import java .net .URLEncoder ;
34+ import java .security .InvalidParameterException ;
3435import java .security .SecureRandom ;
3536import java .util .ArrayList ;
3637import java .util .Collections ;
8788import com .cloud .domain .DomainVO ;
8889import com .cloud .event .EventUtils ;
8990import com .cloud .exception .CloudAuthenticationException ;
91+ import com .cloud .exception .InvalidParameterValueException ;
9092import com .cloud .maid .StackMaid ;
9193import com .cloud .server .ManagementServer ;
9294import 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 ;
0 commit comments