Commit 456ee14f authored by chiaming2000's avatar chiaming2000
Browse files

Simulator:

Clear db, Security (ACL and Pins), and Setup data (cluster version) for
ERASE_PINOP/SECURE_ERASE_PINOP operations.

The simulator reset all its state to Default (out of box installation)
after ERASE_PINOP/SECURE_ERASE_PINOP successfully returned.  
parent 51f48d36
Loading
Loading
Loading
Loading
+21 −8
Original line number Diff line number Diff line
@@ -88,6 +88,9 @@ public class MessageHandler implements ClientMessageService, Runnable {
	
	private boolean isStatusMessageReceived = false;
	
	// is this a TLS transport handler
	private boolean isSecuredChannel = false;

	/**
	 * Constructor.
	 *
@@ -105,14 +108,24 @@ public class MessageHandler implements ClientMessageService, Runnable {

		this.requestTimeout = this.client.getConfiguration()
				.getRequestTimeoutMillis();
	}
	
		// this.myThread = new Thread(this);
	/**
	 * Set to true if this is a secured channel.
	 * 
	 * @param flag
	 */
	public void setSecuredChannel (boolean flag) {
	    this.isSecuredChannel = flag;
	}
	
		// this.myThread.setName("ClientMessageHandler-"
		// + client.getConfiguration().getHost() + "-"
		// + client.getConfiguration().getPort());
		//
		// this.myThread.start();
	/**
	 * Get if this is a secured channel handler.
	 * 
	 * @return true if this is a secured channel handler.
	 */
	public boolean getSecuredChannel () {
	    return this.isSecuredChannel;
	}

	/**
+25 −21
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@ import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import kinetic.client.KineticException;

import com.google.protobuf.ByteString;
import com.seagate.kinetic.common.lib.KineticMessage;
import com.seagate.kinetic.proto.Kinetic.Command;
@@ -44,15 +46,11 @@ public abstract class PinOperationHandler {
    private final static Logger logger = Logger.getLogger(PinOperationHandler.class
            .getName());

    @SuppressWarnings("rawtypes")
    public static boolean handleOperation (KineticMessage request,
            KineticMessage respond, SecurityPin securityPin, Store store, String kineticHome) throws KVStoreException {
    public static void handleOperation (KineticMessage request,
            KineticMessage respond, SimulatorEngine engine) throws KVStoreException, KineticException {
        
        boolean hasPermission = false;
        
        // remove set up in if erase db
        boolean removeSetup = false;
        
        Message.Builder messageBuilder = (Message.Builder) respond.getMessage();
        // set pin auth
        messageBuilder.setAuthType(AuthType.PINAUTH);
@@ -76,13 +74,13 @@ public abstract class PinOperationHandler {
        switch (pinOpType) {
        case LOCK_PINOP:
            // check if has permission
            hasPermission = comparePin (requestPin, securityPin.getLockPin());
            hasPermission = comparePin (requestPin, engine.getSecurityPin().getLockPin());
            if (hasPermission) {
                logger.info("Device locked ...");
            }
            break;
        case UNLOCK_PINOP:
            hasPermission = comparePin (requestPin, securityPin.getLockPin());
            hasPermission = comparePin (requestPin, engine.getSecurityPin().getLockPin());
            if (hasPermission) {
                logger.info("Device unlocked ...");
            }
@@ -95,22 +93,28 @@ public abstract class PinOperationHandler {
            // or not. The implication is that it may be faster
            // than the secure operation.
           
            hasPermission = comparePin (requestPin, securityPin.getErasePin());
            hasPermission = comparePin (requestPin, engine.getSecurityPin().getErasePin());
            if (hasPermission) {
                store.reset();
                resetSetup (kineticHome);
                removeSetup = true;
                
                // reset store
                engine.getStore().reset();
                
                //reset setup
                resetSetup (engine);
                
                //reset security
                SecurityHandler.resetSecurity(engine);
            }
            break;
        case SECURE_ERASE_PINOP:
            // Erase the device in a way that will
            // physical access and disassembly of the device
            // will not
            hasPermission = comparePin (requestPin, securityPin.getErasePin());
            hasPermission = comparePin (requestPin, engine.getSecurityPin().getErasePin());
            if (hasPermission) {
                store.reset();
                resetSetup (kineticHome);
                removeSetup = true;
                engine.getStore().reset();
                resetSetup (engine);  
                SecurityHandler.resetSecurity(engine);
            }
            break;
        case INVALID_PINOP:
@@ -128,14 +132,14 @@ public abstract class PinOperationHandler {
            logger.warning("unauthorized pin opeartion request, pin=" + requestPin);
        }
      
        return removeSetup;
    }
    
    private static void resetSetup(String kineticHome) {
    private static void resetSetup(SimulatorEngine engine) {
        Setup.Builder sb = Setup.newBuilder();
        sb.setNewClusterVersion(0);
        try {
            SetupHandler.persistSetup(sb.build().toByteArray(), kineticHome);
            SetupHandler.persistSetup(sb.build().toByteArray(), engine.getKineticHome());
            engine.setClusterVersion(0);
        } catch (IOException e) {
            logger.log(Level.WARNING, e.getMessage(), e);
        }
+71 −7
Original line number Diff line number Diff line
@@ -23,9 +23,13 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Key;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import kinetic.client.KineticException;

import com.google.protobuf.ByteString;
import com.seagate.kinetic.common.lib.HMACAlgorithmUtil;
@@ -38,6 +42,7 @@ import com.seagate.kinetic.proto.Kinetic.Command.Security;
import com.seagate.kinetic.proto.Kinetic.Command.Security.ACL;
import com.seagate.kinetic.proto.Kinetic.Command.Security.ACL.Permission;
import com.seagate.kinetic.proto.Kinetic.Command.Status.StatusCode;
import com.seagate.kinetic.simulator.lib.HmacStore;

/**
 * Security handler prototype.
@@ -47,6 +52,9 @@ import com.seagate.kinetic.proto.Kinetic.Command.Status.StatusCode;
 */
public abstract class SecurityHandler {
    
    private final static Logger logger = Logger.getLogger(SecurityHandler.class
            .getName());
    
    public static boolean checkPermission(KineticMessage request,
            KineticMessage respond, Map<Long, ACL> currentMap) {
        
@@ -219,11 +227,14 @@ public abstract class SecurityHandler {
        out.close();
    }

    public static Map<Long, ACL> loadACL(String kineticHome, SecurityPin securityPin) throws IOException {
        String aclPersistFilePath = kineticHome + File.separator + ".acl";
    public static void loadACL(SimulatorEngine engine) throws IOException, KineticException {
        
        String aclPersistFilePath = engine.getKineticHome() + File.separator + ".acl";

        File aclFile = new File(aclPersistFilePath);
        Map<Long, ACL> aclMap = new HashMap<Long, ACL>();
        
        Map<Long, ACL> aclmap = new HashMap<Long, ACL>();
        
        if (aclFile.exists()) {
            Long fileLength = aclFile.length();
            if (fileLength != 0) {
@@ -235,18 +246,71 @@ public abstract class SecurityHandler {
                List<ACL> aclList = security.getAclList();

                for (ACL acl : aclList) {
                    aclMap.put(acl.getIdentity(), acl);
                    aclmap.put(acl.getIdentity(), acl);
                }
                
                // set erase pin in cache
                securityPin.setErasePin(security.getNewErasePIN());
                engine.getSecurityPin().setErasePin(security.getNewErasePIN());
                
                // set lock pin in cache
                securityPin.setLockPin(security.getNewLockPIN());
                engine.getSecurityPin().setLockPin(security.getNewLockPIN());
            }
        } 
        
        if (aclmap.size() == 0) {        
            // get default acl map
            aclmap = HmacStore.getAclMap();       
        } 
        
        // set to engine
        engine.setAclMap(aclmap);
        
        // set default hmac key map
        engine.setHmacKeyMap(HmacStore.getHmacKeyMap(aclmap));
        
    }
    
    /**
     * Reset security ACL and pins to default.
     * 
     * @param kineticHome
     * @param securityPin
     * @param aclmap
     * @param hmacKeyMap
     * @throws KineticException
     */
    public static void resetSecurity (SimulatorEngine engine) throws KineticException {
        
        String aclPersistFilePath = engine.getKineticHome() + File.separator + ".acl";

        File aclFile = new File(aclPersistFilePath);
        
        // delete security file
        boolean deleted = aclFile.delete();
        if (deleted) {
            logger.info("removed security data ....");
        }
        
        // clear erase pin
        engine.getSecurityPin().setErasePin(null);
        
        // clear lock pin
        engine.getSecurityPin().setLockPin(null);
        
        // clear acl map
        engine.getAclMap().clear();
        
        // clear key map
        engine.getHmacKeyMap().clear();
        
        Map <Long, ACL> aclmap = HmacStore.getAclMap();
        
        // set default ack map
        engine.setAclMap(aclmap);

        // set default key map
        engine.setHmacKeyMap(HmacStore.getHmacKeyMap(aclmap));
        
        return aclMap;
        logger.info("reset security data to its factory defaults ...");
    }
}
+15 −20
Original line number Diff line number Diff line
@@ -138,14 +138,13 @@ public abstract class SetupHandler {
        // modify clusterVersion
        if (request.getCommand().getBody().getSetup()
                .hasNewClusterVersion()) {
            Long newClusterVersion = request.getCommand()
            long newClusterVersion = request.getCommand()
                    .getBody().getSetup()
                    .getNewClusterVersion();
            if (null != newClusterVersion) {
            //if (null != newClusterVersion) {
                setupInfo.setClusterVersion(newClusterVersion);
                logger.info("the cluster version is set: "
                        + Long.valueOf(newClusterVersion));
            }
                logger.info("the cluster version is set to: " + newClusterVersion);
            //}
        }

        /**
@@ -226,13 +225,16 @@ public abstract class SetupHandler {
        out.close();
    }

    public static SetupInfo loadSetup(String kineticHome) throws IOException {
        String setupPersistFilePath = kineticHome + File.separator + ".setup";
    public static void loadSetup(SimulatorEngine engine) throws IOException {
        
        String setupPersistFilePath = engine.getKineticHome() + File.separator + ".setup";

        File setupFile = new File(setupPersistFilePath);
        SetupInfo setupInfo = new SetupInfo();
        
        if (setupFile.exists()) {
            
            Long fileLength = setupFile.length();
            
            if (fileLength != 0) {
                // read info from file
                byte[] fileContent = new byte[fileLength.intValue()];
@@ -240,17 +242,10 @@ public abstract class SetupHandler {
                in.read(fileContent);
                in.close();
                Setup setup = Setup.parseFrom(fileContent);
                setupInfo.setClusterVersion(setup.getNewClusterVersion());
                
                //if (!setup.getSetPin().isEmpty()) {
                //    setupInfo.setPin(setup.getSetPin().toByteArray());
                //} else {
                    // setupInfo.setPin(setup.getPin().toByteArray());
                //    setupInfo.setPin("".getBytes());
                //}
                // set cluster version
                engine.setClusterVersion(setup.getNewClusterVersion());
            }
        }

        return setupInfo;
    }
}
+29 −20
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import java.io.File;
import java.net.UnknownHostException;
import java.security.Key;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
@@ -113,7 +114,7 @@ public class SimulatorEngine implements MessageService {

    private Map<Long, Key> hmacKeyMap = null;

    private Long clusterVersion = null;
    private long clusterVersion = 0;

    private final boolean isHttp = Boolean.getBoolean("kinetic.io.http");

@@ -202,20 +203,11 @@ public class SimulatorEngine implements MessageService {
            // calculate my home
            kineticHome = kineticHome(config);
            
            Map<Long, ACL> loadedAclMap = SecurityHandler.loadACL(kineticHome, securityPin);
            if (loadedAclMap.size() > 0) {
                this.aclmap = loadedAclMap;
                this.hmacKeyMap = HmacStore.getHmacKeyMap(loadedAclMap);
            } else {
                // get ack map
                this.aclmap = HmacStore.getAclMap();
            SecurityHandler.loadACL(this);
            
                // get hmac key map
                this.hmacKeyMap = HmacStore.getHmacKeyMap(aclmap);
            }
            SetupInfo setupInfo = SetupHandler.loadSetup(kineticHome);
            clusterVersion = setupInfo.getClusterVersion();
            //pin = setupInfo.getPin();
            SetupHandler.loadSetup(this);
            
            //clusterVersion = setupInfo.getClusterVersion();

            // initialize db store
            this.initStore();
@@ -321,6 +313,14 @@ public class SimulatorEngine implements MessageService {
        return this.aclmap;
    }
    
    public void setAclMap ( Map<Long, ACL> aclmap) {
        this.aclmap = aclmap;
    }
    
    public void setHmacKeyMap(Map<Long, Key> hmacKeyMap) {
        this.hmacKeyMap = hmacKeyMap;
    }
        
    public Map<Long, Key> getHmacKeyMap() {
        return this.hmacKeyMap;
    }
@@ -330,6 +330,18 @@ public class SimulatorEngine implements MessageService {
        return this.store;
    }
    
    public void setClusterVersion (long cversion) {
        this.clusterVersion = cversion;
    }
    
    public SecurityPin getSecurityPin() {
        return this.securityPin;
    }
    
    public String getKineticHome() {
        return this.kineticHome;
    }

    /**
     * start new instance of store.
     */
@@ -445,10 +457,7 @@ public class SimulatorEngine implements MessageService {
            HeaderOp.checkHeader(kmreq, kmresp, key, clusterVersion);
            
            if (kmreq.getCommand().getHeader().getMessageType() == MessageType.PINOP) {
                boolean removeSetup = PinOperationHandler.handleOperation(kmreq, kmresp, securityPin, store, kineticHome);
                if (removeSetup) {
                    clusterVersion = 0L;
                }
                PinOperationHandler.handleOperation(kmreq, kmresp, this); 
            } else if (kmreq.getCommand().getHeader().getMessageType() == MessageType.FLUSHALLDATA) {
                commandBuilder.getHeaderBuilder()
                .setMessageType(MessageType.FLUSHALLDATA_RESPONSE);
Loading