Commit f95f11a2 authored by chiaming2000's avatar chiaming2000
Browse files

Java simulator: Check max key size and throws InvalidRequestException if

key size exceeds 4096 bytes.
parent b919dc9a
Loading
Loading
Loading
Loading
+52 −0
Original line number Diff line number Diff line
/**
 * 
 * Copyright (C) 2014 Seagate Technology.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */
package com.seagate.kinetic.simulator.internal;

/**
 * Exception used when the simulator received an invalid request.  Such as key length too big.
 * 
 * @author chiaming
 *
 */
public class InvalidRequestException extends Exception {

    private static final long serialVersionUID = 9217464806941178257L;

    public InvalidRequestException() {
    }

    public InvalidRequestException(String message) {
        super(message);
    }

    public InvalidRequestException(Throwable cause) {
        super(cause);
    }

    public InvalidRequestException(String message, Throwable cause) {
        super(message, cause);
    }

    public InvalidRequestException(String message, Throwable cause,
            boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

}
+59 −12
Original line number Diff line number Diff line
@@ -37,12 +37,12 @@ import com.seagate.kinetic.proto.Kinetic.Message.Status;
import com.seagate.kinetic.proto.Kinetic.Message.Status.StatusCode;
import com.seagate.kinetic.proto.Kinetic.Message.Synchronization;
import com.seagate.kinetic.simulator.internal.Authorizer;
import com.seagate.kinetic.simulator.internal.InvalidRequestException;
import com.seagate.kinetic.simulator.internal.KVSecurityException;
import com.seagate.kinetic.simulator.internal.KVStoreException;
import com.seagate.kinetic.simulator.internal.KVStoreNotFound;
import com.seagate.kinetic.simulator.internal.KVStoreVersionMismatch;
import com.seagate.kinetic.simulator.lib.MyLogger;
//import kinetic.common.PersistOption;

class KvException extends Exception {
    private static final long serialVersionUID = -6541517825715118652L;
@@ -58,14 +58,25 @@ public class KVOp {

    private final static Logger LOG = MyLogger.get();

    private static long maxSize = SimulatorConfiguration
    //max value size
    private static long maxValueSize = SimulatorConfiguration
            .getMaxSupportedValueSize();

    //max key size
    private static int maxKeySize = SimulatorConfiguration.getMaxSupportedKeySize();
    
    //max key range size
    //private static final int MAX_KEY_RANGE_SIZE = 1024;
    
   //max version size
    //private static int MAX_VERSION_SIZE = 2048;
    
    static void oops(String s) throws KvException {
        oops(Status.StatusCode.INTERNAL_ERROR, s);
    }

    static void oops(Status.StatusCode status, String s) throws KvException {
        //LOG.warning("throwing exception., status code = " + status + ", msg = " +s);
        throw new KvException(status, s);
    }

@@ -115,6 +126,9 @@ public class KVOp {
                    // get entry from store
                    try {
                        
                        //check max key length
                        checkMaxKeyLenth (key.size());
                        
                        Authorizer.checkPermission(aclmap, request.getCommand()
                                .getHeader().getIdentity(), Permission.READ,
                                key);
@@ -145,10 +159,13 @@ public class KVOp {

                    try {
                        
                      //check max key length
                       checkMaxKeyLenth (key.size());

                        if (isSupportedValueSize(kmreq) == false) {
                            throw new KvException(StatusCode.INTERNAL_ERROR,
                            throw new KvException(StatusCode.INVALID_REQUEST,
                                    "value size exceeded max supported size. Supported size: "
                                            + maxSize + ", received size="
                                            + maxValueSize + ", received size="
                                            + kmreq.getValue().length
                                            + " (in bytes)");
                        }
@@ -191,6 +208,9 @@ public class KVOp {

                    try {
                        
                        //check max key length
                        checkMaxKeyLenth (key.size());
                        
                        Authorizer.checkPermission(aclmap, request.getCommand()
                                .getHeader().getIdentity(), Permission.DELETE,
                                key);
@@ -210,6 +230,10 @@ public class KVOp {
                    break;
                case GETVERSION:
                    try {
                        
                        //check max key length
                        checkMaxKeyLenth (key.size());
                        
                        Authorizer.checkPermission(aclmap, request.getCommand()
                                .getHeader().getIdentity(), Permission.READ,
                                key);
@@ -225,6 +249,10 @@ public class KVOp {
                    break;
                case GETNEXT:
                    try {
                        
                        //check max key length
                        checkMaxKeyLenth (key.size());
                        
                        storeEntry = store.getNext(key);
                        ByteString nextKey = storeEntry.getKeyOf();

@@ -252,6 +280,10 @@ public class KVOp {
                    break;
                case GETPREVIOUS:
                    try {
                        
                        //check max key length
                        checkMaxKeyLenth (key.size());
                        
                        storeEntry = store.getPrevious(key);
                        ByteString previousKey = storeEntry.getKeyOf();

@@ -283,11 +315,6 @@ public class KVOp {
                    oops("Unknown request");
                }

                // TODO check user authorizations.

                // TODO check multi-tenant key prefix

                // the information should be good at this point, we're done.
            } catch (KVStoreNotFound e) {
                oops(Status.StatusCode.NOT_FOUND);
            } catch (KVStoreVersionMismatch e) {
@@ -297,6 +324,8 @@ public class KVOp {
                        "Opps1: " + e.getMessage());
            } catch (KVSecurityException e) {
                oops(StatusCode.NOT_AUTHORIZED, e.getMessage());
            } catch (InvalidRequestException e) {
                oops(StatusCode.INVALID_REQUEST, e.getMessage());
            } catch (Exception e) {
                oops(Status.StatusCode.INTERNAL_ERROR, e.getMessage());
            }
@@ -329,7 +358,7 @@ public class KVOp {

        byte[] value = km.getValue();

        if (value == null || value.length <= maxSize) {
        if (value == null || value.length <= maxValueSize) {
            // value not set, this may happen if client library did not set
            // value as EMPTY for null value.
            supported = true;
@@ -371,4 +400,22 @@ public class KVOp {
        return option;
    }
    
    private static void checkMaxKeyLenth (int len) throws InvalidRequestException {
        if (len > maxKeySize) {
            throw new InvalidRequestException ("key length exceeds max size, size=" + maxKeySize);
        }
    }
    
//    private static void checkMaxKeyRange (int len) throws InvalidRequestException {
//        if (len > MAX_KEY_RANGE_SIZE) {
//            throw new InvalidRequestException ("max key range exceeds " + MAX_KEY_RANGE_SIZE);
//        }
//    }
//    
//    private static void checkMaxVersion (int len) throws InvalidRequestException {
//        if (len > MAX_VERSION_SIZE) {
//            throw new InvalidRequestException ("max version exceeds " + MAX_VERSION_SIZE);
//        }
//    }

}
+25 −0
Original line number Diff line number Diff line
@@ -143,6 +143,11 @@ public class SimulatorConfiguration extends Properties {
	 */
	private static long maxSupportedValueSize = 1024 * 1024;
	
	/**
	 * max supported key size in bytes.
	 */
    private static int maxSupportedKeySize = 4096;

	private HeartbeatProvider heartbeatProvider = null;

	/**
@@ -561,6 +566,26 @@ public class SimulatorConfiguration extends Properties {
		maxSupportedValueSize = size;
	}
	
	/**
     * Get max supported key size in bytes. Default is set to 4096 bytes.
     * 
     * @return max supported value size for the simulator.
     */
    public static int getMaxSupportedKeySize() {
        return maxSupportedKeySize;
    }

    /**
     * Set max supported key size, in bytes.
     * 
     * @param size
     *            set max supported key size.
     * @see #getMaxSupportedValueSize()
     */
    public static void setMaxSupportedKeySize(int size) {
        maxSupportedKeySize = size;
    }

	/**
	 * Set the heartbeat provider for the simulator.
	 *