Search in sources :

Example 6 with GuidInfo

use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo in project GNS by MobilityFirst.

the class AppAdminServer method run.

/**
   * Start executing the thread.
   */
@Override
public void run() {
    int numRequest = 0;
    GNSConfig.getLogger().log(Level.INFO, "NS Node {0} starting Admin Request Server on port {1}", new Object[] { app.getNodeID(), serverSocket.getLocalPort() });
    while (true) {
        try {
            //Read the packet from the input stream
            try (Socket socket = serverSocket.accept()) {
                //Read the packet from the input stream
                JSONObject incomingJSON = Packet.getJSONObjectFrame(socket);
                switch(Packet.getPacketType(incomingJSON)) {
                    case DUMP_REQUEST:
                        DumpRequestPacket<String> dumpRequestPacket = new DumpRequestPacket<>(incomingJSON, gnsNodeConfig);
                        dumpRequestPacket.setPrimaryNameServer(app.getNodeID());
                        JSONArray jsonArray = new JSONArray();
                        // if there is an argument it is a TAGNAME we return all the records that have that tag
                        if (dumpRequestPacket.getArgument() != null) {
                            String tag = dumpRequestPacket.getArgument();
                            AbstractRecordCursor cursor = NameRecord.getAllRowsIterator(app.getDB());
                            while (cursor.hasNext()) {
                                NameRecord nameRecord = null;
                                JSONObject json = cursor.nextJSONObject();
                                try {
                                    nameRecord = new NameRecord(app.getDB(), json);
                                } catch (JSONException e) {
                                    GNSConfig.getLogger().log(Level.SEVERE, "Problem parsing json into NameRecord: {0} JSON is {1}", new Object[] { e, json.toString() });
                                }
                                if (nameRecord != null) {
                                    try {
                                        if (nameRecord.containsUserKey(AccountAccess.GUID_INFO)) {
                                            GuidInfo userInfo = new GuidInfo(nameRecord.getValuesMap().getJSONObject(AccountAccess.GUID_INFO));
                                            //GuidInfo userInfo = new GuidInfo(nameRecord.getUserKeyAsArray(AccountAccess.GUID_INFO).toResultValueString());
                                            if (userInfo.containsTag(tag)) {
                                                jsonArray.put(nameRecord.toJSONObject());
                                            }
                                        }
                                    } catch (FieldNotFoundException e) {
                                        GNSConfig.getLogger().log(Level.SEVERE, "FieldNotFoundException. Field Name =  {0}", e.getMessage());
                                        //To change body of catch statement use File | Settings | File Templates.
                                        e.printStackTrace();
                                    }
                                }
                            }
                        // OTHERWISE WE RETURN ALL THE RECORD
                        } else {
                            //for (NameRecord nameRecord : NameServer.getAllNameRecords()) {
                            AbstractRecordCursor cursor = NameRecord.getAllRowsIterator(app.getDB());
                            while (cursor.hasNext()) {
                                NameRecord nameRecord = null;
                                JSONObject json = cursor.nextJSONObject();
                                try {
                                    nameRecord = new NameRecord(app.getDB(), json);
                                } catch (JSONException e) {
                                    GNSConfig.getLogger().log(Level.SEVERE, "Problem parsing record cursor into NameRecord: {0} JSON is {1}", new Object[] { e, json.toString() });
                                }
                                if (nameRecord != null) {
                                    jsonArray.put(nameRecord.toJSONObject());
                                }
                            }
                        }
                        GNSConfig.getLogger().log(Level.FINER, "AppAdmin for {0} is {1}", new Object[] { app.getNodeID(), jsonArray.toString() });
                        dumpRequestPacket.setJsonArray(jsonArray);
                        Packet.sendTCPPacket(dumpRequestPacket.toJSONObject(), dumpRequestPacket.getReturnAddress());
                        GNSConfig.getLogger().log(Level.FINEST, "AppAdmin: Response to id:{0} --> {1}", new Object[] { dumpRequestPacket.getId(), dumpRequestPacket.toString() });
                        break;
                    case ADMIN_REQUEST:
                        AdminRequestPacket adminRequestPacket = new AdminRequestPacket(incomingJSON);
                        switch(adminRequestPacket.getOperation()) {
                            case CLEARCACHE:
                                GNSConfig.getLogger().log(Level.WARNING, "NSListenerAdmin ({0}) : Ignoring CLEARCACHE request", app.getNodeID());
                                break;
                            case DUMPCACHE:
                                GNSConfig.getLogger().log(Level.WARNING, "NSListenerAdmin ({0}) : Ignoring DUMPCACHE request", app.getNodeID());
                                break;
                        }
                        break;
                }
            }
        } catch (IOException | JSONException | FailedDBOperationException | ParseException | IllegalArgumentException | SecurityException e) {
            if (serverSocket.isClosed()) {
                GNSConfig.getLogger().warning("NS Admin shutting down.");
                // close this thread
                return;
            }
            e.printStackTrace();
        }
    }
}
Also used : JSONArray(org.json.JSONArray) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException) AdminRequestPacket(edu.umass.cs.gnsserver.gnsapp.packet.admin.AdminRequestPacket) JSONException(org.json.JSONException) GuidInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo) IOException(java.io.IOException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) DumpRequestPacket(edu.umass.cs.gnsserver.gnsapp.packet.admin.DumpRequestPacket) NameRecord(edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) AbstractRecordCursor(edu.umass.cs.gnsserver.database.AbstractRecordCursor) ParseException(java.text.ParseException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 7 with GuidInfo

use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo in project GNS by MobilityFirst.

the class AddGuid method execute.

@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, UnsupportedEncodingException {
    JSONObject json = commandPacket.getCommand();
    String name = json.getString(GNSProtocol.NAME.toString());
    String accountGuid = json.getString(GNSProtocol.GUID.toString());
    String publicKey = json.optString(GNSProtocol.PUBLIC_KEY.toString(), null);
    String signature = json.getString(GNSProtocol.SIGNATURE.toString());
    String message = json.getString(GNSProtocol.SIGNATUREFULLMESSAGE.toString());
    String newGuid;
    if (publicKey != null) {
        newGuid = SharedGuidUtils.createGuidStringFromBase64PublicKey(publicKey);
    } else {
        // add a fake public key
        publicKey = GuidInfo.KEYLESS_PREFIX + name;
        newGuid = SharedGuidUtils.createGuidStringFromPublicKey(publicKey.getBytes());
    }
    GuidInfo accountGuidInfo;
    if ((accountGuidInfo = AccountAccess.lookupGuidInfoAnywhere(header, accountGuid, handler)) == null) {
        return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + accountGuid);
    }
    if (NSAccessSupport.verifySignature(accountGuidInfo.getPublicKey(), signature, message)) {
        AccountInfo accountInfo = AccountAccess.lookupAccountInfoFromGuidAnywhere(header, accountGuid, handler);
        if (accountInfo == null) {
            return new CommandResponse(ResponseCode.BAD_ACCOUNT_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ACCOUNT.toString() + " " + accountGuid);
        }
        if (!accountInfo.isVerified()) {
            return new CommandResponse(ResponseCode.VERIFICATION_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.VERIFICATION_ERROR.toString() + " Account not verified");
        } else if (accountInfo.getGuids().size() > Config.getGlobalInt(GNSConfig.GNSC.ACCOUNT_GUID_MAX_SUBGUIDS)) {
            return new CommandResponse(ResponseCode.TOO_MANY_GUIDS_EXCEPTION, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.TOO_MANY_GUIDS.toString());
        } else {
            CommandResponse result = AccountAccess.addGuid(header, commandPacket, accountInfo, accountGuidInfo, name, newGuid, publicKey, handler);
            if (result.getExceptionOrErrorCode().isOKResult()) {
                // Everything is hunkey dorey so return the new guid
                return new CommandResponse(ResponseCode.NO_ERROR, newGuid);
            } else {
                // Otherwise return the error response
                return result;
            }
        }
    } else {
        // Signature verification failed
        return new CommandResponse(ResponseCode.SIGNATURE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_SIGNATURE.toString());
    }
//}
}
Also used : JSONObject(org.json.JSONObject) GuidInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) AccountInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.AccountInfo)

Example 8 with GuidInfo

use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo in project GNS by MobilityFirst.

the class AddMultipleGuids method execute.

@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, UnsupportedEncodingException {
    JSONObject json = commandPacket.getCommand();
    String guid = json.getString(GNSProtocol.GUID.toString());
    String guidCntString = json.optString(GNSProtocol.GUIDCNT.toString());
    JSONArray names = json.optJSONArray(GNSProtocol.NAMES.toString());
    JSONArray publicKeys = json.optJSONArray(GNSProtocol.PUBLIC_KEYS.toString());
    String signature = json.getString(GNSProtocol.SIGNATURE.toString());
    String message = json.getString(GNSProtocol.SIGNATUREFULLMESSAGE.toString());
    GuidInfo accountGuidInfo;
    if ((accountGuidInfo = AccountAccess.lookupGuidInfoAnywhere(header, guid, handler)) == null) {
        return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + guid);
    }
    if (NSAccessSupport.verifySignature(accountGuidInfo.getPublicKey(), signature, message)) {
        AccountInfo accountInfo = AccountAccess.lookupAccountInfoFromGuidAnywhere(header, guid, handler);
        if (accountInfo == null) {
            return new CommandResponse(ResponseCode.BAD_ACCOUNT_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ACCOUNT.toString() + " " + guid);
        }
        if (!accountInfo.isVerified()) {
            return new CommandResponse(ResponseCode.VERIFICATION_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.VERIFICATION_ERROR.toString() + " Account not verified");
        } else if (accountInfo.getGuids().size() > Config.getGlobalInt(GNSConfig.GNSC.ACCOUNT_GUID_MAX_SUBGUIDS)) {
            return new CommandResponse(ResponseCode.TOO_MANY_GUIDS_EXCEPTION, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.TOO_MANY_GUIDS.toString());
        } else if (names != null && publicKeys != null) {
            GNSConfig.getLogger().log(Level.INFO, "ADD SLOW{0} / {1}", new Object[] { names, publicKeys });
            return AccountAccess.addMultipleGuids(header, commandPacket, JSONUtils.JSONArrayToArrayListString(names), JSONUtils.JSONArrayToArrayListString(publicKeys), accountInfo, accountGuidInfo, handler);
        } else if (names != null) {
            //GNS.getLogger().info("ADD FASTER" + names + " / " + publicKeys);
            return AccountAccess.addMultipleGuidsFaster(header, commandPacket, JSONUtils.JSONArrayToArrayListString(names), accountInfo, accountGuidInfo, handler);
        } else if (guidCntString != null) {
            //GNS.getLogger().info("ADD RANDOM" + names + " / " + publicKeys);
            int guidCnt = Integer.parseInt(guidCntString);
            return AccountAccess.addMultipleGuidsFasterAllRandom(header, commandPacket, guidCnt, accountInfo, accountGuidInfo, handler);
        } else {
            return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " bad arguments: need " + GNSProtocol.NAMES.toString() + " or " + GNSProtocol.NAMES.toString() + " and " + GNSProtocol.PUBLIC_KEYS.toString() + " or " + GNSProtocol.GUIDCNT.toString());
        }
    } else {
        return new CommandResponse(ResponseCode.SIGNATURE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_SIGNATURE.toString());
    }
//}
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) GuidInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) AccountInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.AccountInfo)

Example 9 with GuidInfo

use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo in project GNS by MobilityFirst.

the class RemoveGuid method execute.

@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, UnsupportedEncodingException, InternalRequestException {
    JSONObject json = commandPacket.getCommand();
    String guidToRemove = json.getString(GNSProtocol.GUID.toString());
    String accountGuid = json.optString(GNSProtocol.ACCOUNT_GUID.toString(), null);
    String signature = json.getString(GNSProtocol.SIGNATURE.toString());
    String message = json.getString(GNSProtocol.SIGNATUREFULLMESSAGE.toString());
    GuidInfo accountGuidInfo = null;
    GuidInfo guidInfoToRemove;
    if ((guidInfoToRemove = AccountAccess.lookupGuidInfoLocally(header, guidToRemove, handler)) == null) {
        // Removing a non-existant guid is no longer an error.
        return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
    //return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + guidToRemove);
    }
    if (accountGuid != null) {
        if ((accountGuidInfo = AccountAccess.lookupGuidInfoAnywhere(header, accountGuid, handler)) == null) {
            return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + accountGuid);
        }
    }
    if (NSAccessSupport.verifySignature(accountGuidInfo != null ? accountGuidInfo.getPublicKey() : guidInfoToRemove.getPublicKey(), signature, message)) {
        AccountInfo accountInfo = null;
        if (accountGuid != null) {
            accountInfo = AccountAccess.lookupAccountInfoFromGuidAnywhere(header, accountGuid, handler);
            if (accountInfo == null) {
                return new CommandResponse(ResponseCode.BAD_ACCOUNT_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ACCOUNT.toString() + " " + accountGuid);
            }
        }
        return AccountAccess.removeGuid(header, commandPacket, guidInfoToRemove, accountInfo, handler);
    } else {
        return new CommandResponse(ResponseCode.SIGNATURE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_SIGNATURE.toString());
    }
}
Also used : JSONObject(org.json.JSONObject) GuidInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) AccountInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.AccountInfo)

Example 10 with GuidInfo

use of edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo in project GNS by MobilityFirst.

the class RetrieveAliases method execute.

@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, UnsupportedEncodingException {
    JSONObject json = commandPacket.getCommand();
    String guid = json.getString(GNSProtocol.GUID.toString());
    String signature = json.getString(GNSProtocol.SIGNATURE.toString());
    String message = json.getString(GNSProtocol.SIGNATUREFULLMESSAGE.toString());
    GuidInfo guidInfo;
    if ((guidInfo = AccountAccess.lookupGuidInfoLocally(header, guid, handler)) == null) {
        return new CommandResponse(ResponseCode.BAD_GUID_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_GUID.toString() + " " + guid);
    }
    if (NSAccessSupport.verifySignature(guidInfo.getPublicKey(), signature, message)) {
        AccountInfo accountInfo = AccountAccess.lookupAccountInfoFromGuidLocally(header, guid, handler);
        if (accountInfo == null) {
            return new CommandResponse(ResponseCode.BAD_ACCOUNT_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_ACCOUNT.toString() + " " + guid);
        } else if (!accountInfo.isVerified()) {
            return new CommandResponse(ResponseCode.VERIFICATION_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.VERIFICATION_ERROR.toString() + " Account not verified");
        }
        List<String> aliases = accountInfo.getAliases();
        return new CommandResponse(ResponseCode.NO_ERROR, new JSONArray(aliases).toString());
    } else {
        return new CommandResponse(ResponseCode.SIGNATURE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_SIGNATURE.toString());
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) GuidInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) AccountInfo(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.AccountInfo)

Aggregations

GuidInfo (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo)15 JSONObject (org.json.JSONObject)13 CommandResponse (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse)11 AccountInfo (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.AccountInfo)6 ResponseCode (edu.umass.cs.gnscommon.ResponseCode)4 MetaDataTypeName (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.MetaDataTypeName)4 Date (java.util.Date)4 JSONArray (org.json.JSONArray)4 JSONException (org.json.JSONException)2 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)1 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.server.FieldNotFoundException)1 AbstractRecordCursor (edu.umass.cs.gnsserver.database.AbstractRecordCursor)1 AdminRequestPacket (edu.umass.cs.gnsserver.gnsapp.packet.admin.AdminRequestPacket)1 DumpRequestPacket (edu.umass.cs.gnsserver.gnsapp.packet.admin.DumpRequestPacket)1 NameRecord (edu.umass.cs.gnsserver.gnsapp.recordmap.NameRecord)1 IOException (java.io.IOException)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 UnknownHostException (java.net.UnknownHostException)1 ParseException (java.text.ParseException)1