Search in sources :

Example 26 with ResponseCode

use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.

the class AccountAccess method addGuid.

/**
   * Adds a new GUID associated with an existing account.
   * <p>
   * These records will be created:<br>
   * GUID: "_GNS_PRIMARY_GUID" -- GUID (primary) for secondary guid<br>
   * GUID: "_GNS_GUID_INFO" -- {guid info}<br>
   * HRN: "_GNS_GUID" -- GUID<br>
   *
   * @param header
   * @param commandPacket
   *
   * @param accountInfo
   * - the accountInfo of the account to add the GUID to
   * @param accountGuidInfo
   * @param name
   * = the human readable name to associate with the GUID
   * @param guid
   * - the new GUID
   * @param publicKey
   * - the public key to use with the new account
   * @param handler
   * @return status result
   */
public static CommandResponse addGuid(InternalRequestHeader header, CommandPacket commandPacket, AccountInfo accountInfo, GuidInfo accountGuidInfo, String name, String guid, String publicKey, ClientRequestHandlerInterface handler) {
    /* arun: The commented out code below checking for duplicates is
		 * incorrect. What we need to do is to check for conflicts in HRN-GUID
		 * bindings. If an HRN being created already exists, but the
		 * corresponding GUID does not exist, we should create it. Otherwise,
		 * the caller will interpret the duplicate name exception incorrectly as
		 * a successful creation. 
		 * 
 		 * if ((AccountAccess.lookupGuidAnywhere(name, handler)) != null) {
		 * return new CommandResponse( ResponseCode.DUPLICATE_NAME_EXCEPTION,
		 * GNSProtocol.BAD_RESPONSE.toString() + " " +
		 * GNSProtocol.DUPLICATE_NAME.toString() + " " + name); }
		 * 
		 * if ((AccountAccess.lookupGuidInfoAnywhere(guid, handler)) != null) {
		 * return new CommandResponse( ResponseCode.DUPLICATE_GUID_EXCEPTION,
		 * GNSProtocol.BAD_RESPONSE.toString() + " " +
		 * GNSProtocol.DUPLICATE_GUID.toString() + " " + name); } */
    boolean createdName = false, createdGUID = false;
    try {
        JSONObject jsonHRN = new JSONObject();
        jsonHRN.put(HRN_GUID, guid);
        ResponseCode code;
        code = handler.getInternalClient().createOrExists(new CreateServiceName(name, jsonHRN.toString()));
        /* arun: Return the error if we could not createField the HRN
			 * (alias) record and the error indicates that it is not a duplicate
			 * ID exception because of a limbo create operation from a previous
			 * unsuccessful attempt. */
        String boundGUID = null;
        if (code.equals(ResponseCode.DUPLICATE_ID_EXCEPTION) && !(guid.equals(boundGUID = HRNMatchingGUIDExists(header, handler, code, name, guid)))) {
            return new CommandResponse(ResponseCode.CONFLICTING_GUID_EXCEPTION, GNSProtocol.BAD_RESPONSE.toString() + " " + ResponseCode.CONFLICTING_GUID_EXCEPTION.getProtocolCode() + " " + name + "(" + guid + ")" + " " + (code.getMessage() != null ? code.getMessage() + " " : "") + "; HRN " + name + " is already bound to GUID " + boundGUID + " != " + guid);
        }
        if (code.isExceptionOrError() && !code.equals(ResponseCode.DUPLICATE_ID_EXCEPTION)) {
            return new CommandResponse(code, GNSProtocol.BAD_RESPONSE.toString() + " " + code.getProtocolCode() + " " + name + "(" + guid + ")" + " " + code.getMessage());
        }
        assert (!code.isExceptionOrError() || guid.equals(boundGUID));
        createdName = true;
        // else name created
        GuidInfo guidInfo = new GuidInfo(name, guid, publicKey);
        JSONObject jsonGuid = new JSONObject();
        jsonGuid.put(GUID_INFO, guidInfo.toJSONObject());
        jsonGuid.put(PRIMARY_GUID, accountInfo.getGuid());
        // set up ACL to look like this
        // "_GNS_ACL": {
        // "READ_WHITELIST": {"+ALL+": {"MD": [<publickey>, "+ALL+"]}},
        // "WRITE_WHITELIST": {"+ALL+": {"MD": [<publickey>]}}
        JSONObject acl = createACL(GNSProtocol.ENTIRE_RECORD.toString(), Arrays.asList(GNSProtocol.EVERYONE.toString(), accountGuidInfo.getPublicKey()), GNSProtocol.ENTIRE_RECORD.toString(), Arrays.asList(accountGuidInfo.getPublicKey()));
        // prefix is the same for all acls so just pick one to use here
        jsonGuid.put(MetaDataTypeName.READ_WHITELIST.getPrefix(), acl);
        // The addGuid needs to be rolled back if the second step fails.
        ResponseCode guidCode;
        guidCode = handler.getInternalClient().createOrExists(new CreateServiceName(guid, jsonGuid.toString()));
        assert (guidCode != null);
        String boundHRN = null;
        if (guidCode.equals(ResponseCode.DUPLICATE_ID_EXCEPTION) && !name.equals(boundHRN = GUIDMatchingHRNExists(header, handler, guidCode, name, // rollback name creation
        guid))) {
            return rollback(handler, ResponseCode.CONFLICTING_GUID_EXCEPTION.setMessage(": Existing GUID " + guid + " is associated with " + boundHRN + " and can not be associated with the HRN " + name), name, guid);
        }
        // redundant to check with GNSClientInternal
        if (guidCode.isExceptionOrError() && !guidCode.equals(ResponseCode.DUPLICATE_ID_EXCEPTION)) {
            return new CommandResponse(guidCode, GNSProtocol.BAD_RESPONSE.toString() + " " + guidCode.getProtocolCode() + " " + guidCode.getMessage());
        }
        // else all good, continue
        assert (!guidCode.isExceptionOrError() || name.equals(boundHRN)) : "code=" + guidCode + "; boundHRN=" + boundHRN + "; name=" + name + "; for GUID=" + guid;
        createdGUID = true;
        // else both name and guid created successfully
        updateAccountInfoNoAuthentication(header, commandPacket, accountInfo.addGuid(guid).noteUpdate(), handler, true);
        return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString() + " " + " [created " + name + " and " + guid + " and updated account info successfully]");
    } catch (ClientException ce) {
        return new CommandResponse(ce.getCode(), GNSProtocol.BAD_RESPONSE.toString() + " " + ce.getCode() + " " + ce.getMessage() + (createdName ? "; created " + name + (createdGUID ? "; created " + guid + "; failed to update account info" : "") : "; created neither " + name + " nor " + guid));
    } catch (JSONException | ServerRuntimeException e) {
        return CommandResponse.toCommandResponse(e);
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) CreateServiceName(edu.umass.cs.reconfiguration.reconfigurationpackets.CreateServiceName) RandomString(edu.umass.cs.gnscommon.utils.RandomString) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) ServerRuntimeException(edu.umass.cs.gnscommon.exceptions.server.ServerRuntimeException)

Example 27 with ResponseCode

use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.

the class ActiveCode method setCode.

/**
   * Sets active code for the guid and action.
   *
   * @param header
   * @param guid
   * @param commandPacket
   * @param action
   * @param code
   * @param writer
   * @param signature
   * @param message
   * @param timestamp
   * @param handler
   * @return a {@link ResponseCode}
   * @throws org.json.JSONException
   */
public static ResponseCode setCode(InternalRequestHeader header, CommandPacket commandPacket, String guid, String action, String code, String writer, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) throws JSONException, IllegalArgumentException {
    JSONObject json;
    json = new JSONObject();
    // getCodeField can throw IllegalArgumentException
    json.put(getCodeField(action), code);
    ResponseCode response = FieldAccess.updateUserJSON(header, commandPacket, guid, json, writer, signature, message, timestamp, handler);
    return response;
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) JSONObject(org.json.JSONObject)

Example 28 with ResponseCode

use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.

the class ActiveCode method clearCode.

/**
   * Clears the active code for the guid and action.
   *
   * @param header
   * @param commandPacket
   * @param guid
   * @param action
   * @param writer
   * @param signature
   * @param message
   * @param timestamp
   * @param handler
   * @return a {@link ResponseCode}
   */
public static ResponseCode clearCode(InternalRequestHeader header, CommandPacket commandPacket, String guid, String action, String writer, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) throws IllegalArgumentException {
    // can throw IllegalArgumentException
    String field = getCodeField(action);
    ResponseCode response = FieldAccess.update(header, commandPacket, guid, field, "", null, -1, UpdateOperation.SINGLE_FIELD_REMOVE_FIELD, writer, signature, message, timestamp, handler);
    return response;
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode)

Example 29 with ResponseCode

use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.

the class ActiveCode method getCode.

/**
   * Gets the currently set active code for the guid and action.
   *
   * @param header
   * @param commandPacket
   * @param guid
   * @param action
   * @param reader
   * @param signature
   * @param message
   * @param timestamp
   * @param handler
   * @return a string
   * @throws edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException
   * @throws org.json.JSONException
   */
public static String getCode(InternalRequestHeader header, CommandPacket commandPacket, String guid, String action, String reader, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) throws IllegalArgumentException, FailedDBOperationException, JSONException {
    // can throw IllegalArgumentException
    String field = getCodeField(action);
    ResponseCode errorCode = FieldAccess.signatureAndACLCheckForRead(header, commandPacket, guid, field, // fields
    null, reader, signature, message, timestamp, handler.getApp());
    if (errorCode.isExceptionOrError()) {
        return GNSProtocol.NULL_RESPONSE.toString();
    }
    ValuesMap result = NSFieldAccess.lookupJSONFieldLocalNoAuth(null, guid, field, handler.getApp(), // the false disables active code handling which we obviously don't want here
    false);
    return result.getString(field);
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap)

Example 30 with ResponseCode

use of edu.umass.cs.gnscommon.ResponseCode in project GNS by MobilityFirst.

the class AccountAccess method removeAccount.

/**
   * Removes a GNS user account.
   *
   * @param header
   * @param commandPacket
   * @param accountInfo
   * @param handler
   * @return status result
   */
public static CommandResponse removeAccount(InternalRequestHeader header, CommandPacket commandPacket, AccountInfo accountInfo, ClientRequestHandlerInterface handler) {
    // Step 1 - remove any group links
    ResponseCode removedGroupLinksResponseCode;
    try {
        removedGroupLinksResponseCode = GroupAccess.removeGuidFromGroups(header, commandPacket, accountInfo.getGuid(), handler);
    } catch (ClientException e) {
        removedGroupLinksResponseCode = e.getCode();
    } catch (IOException | InternalRequestException | JSONException e) {
        removedGroupLinksResponseCode = ResponseCode.UPDATE_ERROR;
    }
    // Step 2 - delete all the aliases records for this account
    ResponseCode deleteAliasesResponseCode = ResponseCode.NO_ERROR;
    for (String alias : accountInfo.getAliases()) {
        ResponseCode responseCode;
        try {
            responseCode = handler.getInternalClient().deleteOrNotExists(alias, true);
        } catch (ClientException e) {
            responseCode = e.getCode();
        }
        if (responseCode.isExceptionOrError()) {
            deleteAliasesResponseCode = ResponseCode.UPDATE_ERROR;
        }
    }
    // Step 3 - delete all the subGuids
    ResponseCode deleteSubGuidsResponseCode = ResponseCode.NO_ERROR;
    for (String subguid : accountInfo.getGuids()) {
        GuidInfo subGuidInfo = lookupGuidInfoAnywhere(header, subguid, handler);
        if (subGuidInfo != null && removeGuidInternal(header, commandPacket, subGuidInfo, accountInfo, true, handler).getExceptionOrErrorCode().isExceptionOrError()) {
            deleteSubGuidsResponseCode = ResponseCode.UPDATE_ERROR;
        }
    }
    // Step 4 - delete the HRN record
    ResponseCode deleteNameResponseCode;
    try {
        deleteNameResponseCode = handler.getInternalClient().deleteOrNotExists(accountInfo.getName(), true);
    } catch (ClientException e) {
        deleteNameResponseCode = e.getCode();
    }
    if ((removedGroupLinksResponseCode.isExceptionOrError() || deleteAliasesResponseCode.isExceptionOrError()) || deleteSubGuidsResponseCode.isExceptionOrError() || deleteNameResponseCode.isExceptionOrError()) {
        // Don't really care who caused the error, other than for debugging.
        return new CommandResponse(ResponseCode.UPDATE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + (removedGroupLinksResponseCode.isOKResult() ? "" : "; failed to remove links") + (deleteAliasesResponseCode.isOKResult() ? "" : "; failed to remove aliases") + (deleteSubGuidsResponseCode.isOKResult() ? "" : "; failed to remove subguids") + (deleteNameResponseCode.isOKResult() ? "" : "failed to delete " + accountInfo.getName()));
    } else {
        // Step 4.5 - delete the cache guid info cache entry
        GUID_INFO_CACHE.invalidate(accountInfo.getGuid());
        // Step 5 - If all the above stuff worked we delete the account guid record
        ResponseCode deleteGuidResponseCode;
        try {
            deleteGuidResponseCode = handler.getInternalClient().deleteOrNotExists(accountInfo.getGuid(), true);
        } catch (ClientException e) {
            return new CommandResponse(e.getCode(), GNSProtocol.BAD_RESPONSE.toString() + " Failed to delete " + accountInfo.getGuid());
        }
        if (deleteGuidResponseCode.isOKResult()) {
            return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
        } else {
            return new CommandResponse(deleteGuidResponseCode, GNSProtocol.BAD_RESPONSE.toString() + " Failed to delete " + accountInfo.getGuid());
        }
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) InternalRequestException(edu.umass.cs.gnscommon.exceptions.server.InternalRequestException) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) RandomString(edu.umass.cs.gnscommon.utils.RandomString)

Aggregations

ResponseCode (edu.umass.cs.gnscommon.ResponseCode)40 JSONObject (org.json.JSONObject)28 Date (java.util.Date)18 CommandResponse (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse)16 JSONException (org.json.JSONException)14 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)13 ResultValue (edu.umass.cs.gnsserver.utils.ResultValue)8 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)7 InternalRequestException (edu.umass.cs.gnscommon.exceptions.server.InternalRequestException)7 IOException (java.io.IOException)7 MetaDataTypeName (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.MetaDataTypeName)6 RandomString (edu.umass.cs.gnscommon.utils.RandomString)5 ValuesMap (edu.umass.cs.gnsserver.utils.ValuesMap)5 GuidInfo (edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.GuidInfo)4 CreateServiceName (edu.umass.cs.reconfiguration.reconfigurationpackets.CreateServiceName)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 SignatureException (java.security.SignatureException)3 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)3