Search in sources :

Example 1 with InvalidGuidException

use of edu.umass.cs.gnscommon.exceptions.client.InvalidGuidException in project GNS by MobilityFirst.

the class CommandUtils method checkResponse.

/**
   *
   * @param command
   *
   * @param responsePacket
   * @return Response as a string.
   * @throws ClientException
   */
public static ResponsePacket checkResponse(ResponsePacket responsePacket, CommandPacket command) throws ClientException {
    ResponseCode code = responsePacket.getErrorCode();
    String returnValue = responsePacket.getReturnValue();
    // wants to return a null value.
    if (code.isOKResult()) {
        return (returnValue.startsWith(GNSProtocol.NULL_RESPONSE.toString())) ? null : //returnValue;
        responsePacket;
    }
    // else error
    String errorSummary = code + ": " + returnValue + //+ ": " + responsePacket.getSummary()
    (command != null ? " for command " + command.getSummary() : "");
    switch(code) {
        case SIGNATURE_ERROR:
            throw new EncryptionException(code, errorSummary);
        case BAD_GUID_ERROR:
        case BAD_ACCESSOR_ERROR:
        case BAD_ACCOUNT_ERROR:
            throw new InvalidGuidException(code, errorSummary);
        case FIELD_NOT_FOUND_ERROR:
            throw new FieldNotFoundException(code, errorSummary);
        case ACCESS_ERROR:
            throw new AclException(code, errorSummary);
        case VERIFICATION_ERROR:
            throw new VerificationException(code, errorSummary);
        case ALREADY_VERIFIED_EXCEPTION:
            throw new VerificationException(code, errorSummary);
        case DUPLICATE_ID_EXCEPTION:
            //case DUPLICATE_NAME_EXCEPTION:
            throw new DuplicateNameException(code, errorSummary);
        case DUPLICATE_FIELD_EXCEPTION:
            throw new InvalidFieldException(code, errorSummary);
        case ACTIVE_REPLICA_EXCEPTION:
            throw new InvalidGuidException(code, errorSummary);
        case NONEXISTENT_NAME_EXCEPTION:
            throw new InvalidGuidException(code, errorSummary);
        case TIMEOUT:
        case RECONFIGURATION_EXCEPTION:
            throw new ClientException(code, errorSummary);
        default:
            throw new ClientException(code, "Error received with an unknown response code: " + errorSummary);
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) InvalidGuidException(edu.umass.cs.gnscommon.exceptions.client.InvalidGuidException) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException) VerificationException(edu.umass.cs.gnscommon.exceptions.client.VerificationException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) AclException(edu.umass.cs.gnscommon.exceptions.client.AclException) InvalidFieldException(edu.umass.cs.gnscommon.exceptions.client.InvalidFieldException)

Example 2 with InvalidGuidException

use of edu.umass.cs.gnscommon.exceptions.client.InvalidGuidException in project GNS by MobilityFirst.

the class GuidCreate method parse.

@Override
public void parse(String commandText) throws Exception {
    GuidEntry accountGuid = module.getCurrentGuid();
    StringTokenizer st = new StringTokenizer(commandText.trim());
    if ((st.countTokens() != 1)) {
        wrongArguments();
        return;
    }
    String aliasName = st.nextToken();
    try {
        GNSClientCommands gnsClient = module.getGnsClient();
        try {
            gnsClient.lookupGuid(aliasName);
            printString("Alias " + aliasName + " already exists.\n");
            return;
        } catch (ClientException expected) {
        // The alias does not exists, that's good, let's create it
        }
        if (!module.isSilent()) {
            printString("Looking for alias " + aliasName + " GUID and certificates...\n");
        }
        GuidEntry myGuid = KeyPairUtils.getGuidEntry(module.getGnsInstance(), aliasName);
        if (myGuid != null) {
            try {
                PublicKey pk = gnsClient.publicKeyLookupFromGuid(myGuid.getGuid());
                if (myGuid.getPublicKey().equals(pk)) {
                    // We already have the key but the alias is missing in the GNS,
                    // re-add the alias
                    printString("Alias info found locally but missing in GNS, re-adding alias to the GNS\n");
                    gnsClient.guidCreate(myGuid, aliasName);
                } else {
                    printString("Old certificates found locally and not matching key in GNS, deleting local keys\n");
                    KeyPairUtils.removeKeyPair(module.getGnsInstance(), aliasName);
                }
            } catch (InvalidGuidException e) {
                KeyPairUtils.removeKeyPair(module.getGnsInstance(), aliasName);
            }
        }
        if (!module.isSilent()) {
            printString("Generating new GUID and keys for account " + accountGuid.getEntityName() + " \n");
        }
        myGuid = gnsClient.guidCreate(accountGuid, aliasName);
        if (!module.isSilent()) {
            printString("Created GUID " + myGuid.getGuid() + "\n");
        }
        if (module.getCurrentGuid() == null) {
            module.setCurrentGuidAndCheckForVerified(myGuid);
            module.setPromptString(ConsoleModule.CONSOLE_PROMPT + aliasName + ">");
        }
    } catch (IOException | ClientException e) {
        printString("Failed to create new guid ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) PublicKey(java.security.PublicKey) InvalidGuidException(edu.umass.cs.gnscommon.exceptions.client.InvalidGuidException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 3 with InvalidGuidException

use of edu.umass.cs.gnscommon.exceptions.client.InvalidGuidException in project GNS by MobilityFirst.

the class AccountCreate method parse.

@Override
public void parse(String commandText) throws Exception {
    StringTokenizer st = new StringTokenizer(commandText.trim());
    if ((st.countTokens() != 2)) {
        wrongArguments();
        return;
    }
    String aliasName = st.nextToken();
    String password = st.nextToken();
    try {
        GNSClientCommands gnsClient = module.getGnsClient();
        try {
            gnsClient.lookupGuid(aliasName);
            if (!module.isSilent()) {
                printString("Alias " + aliasName + " already exists.\n");
            }
            return;
        } catch (Exception expected) {
        // The alias does not exists, that's good, let's create it
        }
        GuidEntry myGuid = KeyPairUtils.getGuidEntry(module.getGnsInstance(), aliasName);
        if (myGuid != null) {
            try {
                PublicKey pk = gnsClient.publicKeyLookupFromGuid(myGuid.getGuid());
                if (myGuid.getPublicKey().equals(pk)) {
                    // We already have the key but the alias is missing in the GNS,
                    // re-add the alias
                    printString("Alias info found locally but missing in GNS, re-adding alias to the GNS\n");
                    gnsClient.guidCreate(myGuid, aliasName);
                } else {
                    printString("Old certificates found locally and not matching key in GNS, deleting local keys\n");
                    KeyPairUtils.removeKeyPair(module.getGnsInstance(), aliasName);
                }
            } catch (InvalidGuidException e) {
                KeyPairUtils.removeKeyPair(module.getGnsInstance(), aliasName);
            }
        }
        myGuid = gnsClient.accountGuidCreate(aliasName, password);
        if (!module.isSilent()) {
            printString("Created an account with GUID " + myGuid.getGuid() + ".\n");
        }
        if (module.getCurrentGuid() == null) {
            module.setCurrentGuidAndCheckForVerified(myGuid);
            if (KeyPairUtils.getDefaultGuidEntry(module.getGnsInstance()) == null) {
                KeyPairUtils.setDefaultGuidEntry(module.getGnsInstance(), aliasName);
                module.printString(aliasName + " saved as default GUID \n");
            }
            module.setPromptString(ConsoleModule.CONSOLE_PROMPT + aliasName + ">");
        }
    } catch (Exception e) {
        e.printStackTrace();
        printString("Failed to create new guid ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) PublicKey(java.security.PublicKey) InvalidGuidException(edu.umass.cs.gnscommon.exceptions.client.InvalidGuidException) InvalidGuidException(edu.umass.cs.gnscommon.exceptions.client.InvalidGuidException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Aggregations

InvalidGuidException (edu.umass.cs.gnscommon.exceptions.client.InvalidGuidException)3 GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)2 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)2 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)2 PublicKey (java.security.PublicKey)2 StringTokenizer (java.util.StringTokenizer)2 ResponseCode (edu.umass.cs.gnscommon.ResponseCode)1 AclException (edu.umass.cs.gnscommon.exceptions.client.AclException)1 DuplicateNameException (edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException)1 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)1 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException)1 InvalidFieldException (edu.umass.cs.gnscommon.exceptions.client.InvalidFieldException)1 VerificationException (edu.umass.cs.gnscommon.exceptions.client.VerificationException)1 IOException (java.io.IOException)1