use of edu.umass.cs.reconfiguration.reconfigurationpackets.CreateServiceName in project GNS by MobilityFirst.
the class AccountAccess method addMultipleGuids.
/**
* Add multiple guids to an account.
*
* @param header
* @param commandPacket
*
* @param names
* - the list of names
* @param publicKeys
* - the list of public keys associated with the names
* @param accountInfo
* @param accountGuidInfo
* @param handler
* @return a command response
*/
public static CommandResponse addMultipleGuids(InternalRequestHeader header, CommandPacket commandPacket, List<String> names, List<String> publicKeys, AccountInfo accountInfo, GuidInfo accountGuidInfo, ClientRequestHandlerInterface handler) {
try {
long startTime = System.currentTimeMillis();
//Set<String> guids = new HashSet<>();
Map<String, JSONObject> hrnMap = new HashMap<>();
Map<String, JSONObject> guidInfoMap = new HashMap<>();
for (int i = 0; i < names.size(); i++) {
String name = names.get(i);
String publicKey = publicKeys.get(i);
String guid = SharedGuidUtils.createGuidStringFromBase64PublicKey(publicKey);
accountInfo.addGuid(guid);
//guids.add(guid);
// HRN records
JSONObject jsonHRN = new JSONObject();
jsonHRN.put(HRN_GUID, guid);
hrnMap.put(name, jsonHRN);
// guid info record
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);
guidInfoMap.put(guid, jsonGuid);
}
DelayProfiler.updateDelay("addMultipleGuidsSetup", startTime);
accountInfo.noteUpdate();
// First we create the HRN records as a batch
ResponseCode returnCode;
// First try to create the HRNS to insure that that name does not already exist
Map<String, String> nameStates = new HashMap<>();
for (String key : hrnMap.keySet()) {
nameStates.put(key, hrnMap.get(key).toString());
}
if (!(returnCode = handler.getInternalClient().createOrExists(new CreateServiceName(nameStates))).isExceptionOrError()) {
// now we update the account info
if (updateAccountInfoNoAuthentication(header, commandPacket, accountInfo, handler, true).isOKResult()) {
HashMap<String, String> guidInfoNameStates = new HashMap<>();
for (String key : guidInfoMap.keySet()) {
guidInfoNameStates.put(key, guidInfoMap.get(key).toString());
}
handler.getInternalClient().createOrExists(new CreateServiceName(guidInfoNameStates));
GNSConfig.getLogger().info(DelayProfiler.getStats());
return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
}
}
return new CommandResponse(returnCode, GNSProtocol.BAD_RESPONSE.toString() + " " + returnCode.getProtocolCode() + " " + names);
} catch (JSONException e) {
return new CommandResponse(ResponseCode.JSON_PARSE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.JSON_PARSE_ERROR.toString() + " " + e.getMessage());
} catch (ServerRuntimeException | ClientException e) {
return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " " + e.getMessage());
}
}
use of edu.umass.cs.reconfiguration.reconfigurationpackets.CreateServiceName in project GNS by MobilityFirst.
the class AccountAccess method addAlias.
/**
* Add a new human readable name (alias) to an account.
* <p>
* These records will be added:<br>
* HRN: "_GNS_GUID" -- GUID<br>
*
* @param header
* @param commandPacket
*
* @param accountInfo
* @param alias
* @param writer
* @param signature
* @param message
* @param timestamp
* @param handler
* @return status result
*/
public static CommandResponse addAlias(InternalRequestHeader header, CommandPacket commandPacket, AccountInfo accountInfo, String alias, String writer, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) {
// insure that that name does not already exist
try {
ResponseCode returnCode;
JSONObject jsonHRN = new JSONObject();
jsonHRN.put(HRN_GUID, accountInfo.getGuid());
if ((returnCode = handler.getInternalClient().createOrExists(new CreateServiceName(alias, jsonHRN.toString()))).isExceptionOrError()) {
// roll this back
accountInfo.removeAlias(alias);
return new CommandResponse(returnCode, GNSProtocol.BAD_RESPONSE.toString() + " " + returnCode.getProtocolCode() + " " + alias + " " + returnCode.getMessage());
}
accountInfo.addAlias(alias);
accountInfo.noteUpdate();
if (updateAccountInfo(header, commandPacket, accountInfo.getGuid(), accountInfo, writer, signature, message, timestamp, handler, true).isExceptionOrError()) {
// back out if we got an error
handler.getInternalClient().deleteOrNotExists(alias, true);
return new CommandResponse(ResponseCode.UPDATE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UPDATE_ERROR.toString());
} else {
return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
}
} catch (JSONException e) {
return new CommandResponse(ResponseCode.JSON_PARSE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.JSON_PARSE_ERROR.toString() + " " + e.getMessage());
} catch (ClientException ce) {
return new CommandResponse(ce.getCode(), GNSProtocol.BAD_RESPONSE.toString() + " " + ce.getCode() + " " + alias + " " + ce.getMessage());
}
}
use of edu.umass.cs.reconfiguration.reconfigurationpackets.CreateServiceName in project GNS by MobilityFirst.
the class AccountAccess method addAccountInternal.
/**
* Create a new GNS user account.
*
* THIS CAN BYPASS THE EMAIL VERIFICATION if you set emailVerify to false;
*
* <p>
* This adds three records to the GNS for the account:<br>
* GNSProtocol.NAME.toString(): "_GNS_GUID" -- guid<br>
* GUID: "_GNS_ACCOUNT_INFO" -- {account record - an AccountInfo object
* stored as JSON}<br>
* GUID: "_GNS_GUID_INFO" -- {guid record - a GuidInfo object stored as
* JSON}<br>
*
* @param header
*
* @param name
* @param guid
* @param publicKey
* @param password
* @param emailVerify
* @param verifyCode
* @param handler
* @return status result
* @throws IOException
*/
public static CommandResponse addAccountInternal(InternalRequestHeader header, String name, String guid, String publicKey, String password, boolean emailVerify, String verifyCode, ClientRequestHandlerInterface handler) throws IOException {
try {
ResponseCode returnCode;
// First try to createField the HRN record to make sure this name
// isn't already registered
JSONObject jsonHRN = new JSONObject();
jsonHRN.put(HRN_GUID, guid);
returnCode = handler.getInternalClient().createOrExists(new CreateServiceName(name, jsonHRN.toString()));
String boundGUID = null;
if (!returnCode.isExceptionOrError() || (guid.equals(boundGUID = HRNMatchingGUIDExists(header, handler, returnCode, name, guid)))) {
// if that's cool then add the entry that links the guid to the
// username and public key
// this one could fail if someone uses the same public key to
// register another one... that's a nono
// Note that password here is base64 encoded
AccountInfo accountInfo = new AccountInfo(name, guid, password);
accountInfo.noteUpdate();
// if email verifications are off we just set it to verified
if (!emailVerify) {
accountInfo.setVerified(true);
} else {
accountInfo.setVerificationCode(verifyCode);
}
JSONObject json = new JSONObject();
json.put(ACCOUNT_INFO, accountInfo.toJSONObject());
GuidInfo guidInfo = new GuidInfo(name, guid, publicKey);
json.put(GUID_INFO, guidInfo.toJSONObject());
// set up ACL to look like this
// "_GNS_ACL": {
// "READ_WHITELIST": {"+ALL+": {"MD": "+ALL+"]}}}
JSONObject acl = createACL(GNSProtocol.ENTIRE_RECORD.toString(), Arrays.asList(GNSProtocol.EVERYONE.toString()), null, null);
// prefix is the same for all acls so just pick one to use here
json.put(MetaDataTypeName.READ_WHITELIST.getPrefix(), acl);
// set up the default read access
returnCode = handler.getInternalClient().createOrExists(new CreateServiceName(guid, json.toString()));
String boundHRN = null;
assert (returnCode != null);
if (!returnCode.isExceptionOrError() || name.equals(boundHRN = GUIDMatchingHRNExists(header, handler, returnCode, name, // all good if here
guid))) {
return CommandResponse.noError();
}
if (// try to delete the record we added above
returnCode.equals(ResponseCode.DUPLICATE_ID_EXCEPTION)) {
return rollback(handler, ResponseCode.CONFLICTING_GUID_EXCEPTION.setMessage(" Existing GUID " + guid + " has HRN " + boundHRN + " and can not be associated with the HRN " + name), name, guid);
}
} else if (returnCode.equals(ResponseCode.DUPLICATE_FIELD_EXCEPTION) && !guid.equals(boundGUID)) {
return new CommandResponse(ResponseCode.CONFLICTING_GUID_EXCEPTION, GNSProtocol.BAD_RESPONSE.toString() + " " + ResponseCode.CONFLICTING_GUID_EXCEPTION.getProtocolCode() + " " + name + "(" + guid + ")" + " " + (returnCode.getMessage() != null ? returnCode.getMessage() + " " : "") + "; HRN " + name + " is already bound to GUID " + boundGUID + " != " + guid);
}
// else the first HRN creation likely failed
return new CommandResponse(returnCode, GNSProtocol.BAD_RESPONSE.toString() + " " + returnCode.getProtocolCode() + " " + name + "(" + guid + ") " + returnCode.getMessage());
} catch (JSONException e) {
return CommandResponse.toCommandResponse(e);
} catch (ClientException ce) {
return new CommandResponse(ce.getCode(), GNSProtocol.BAD_RESPONSE.toString() + " " + ce.getCode() + " " + name + " " + ce.getMessage() + " (" + name + " may have gotten created despite this exception)");
}
}
use of edu.umass.cs.reconfiguration.reconfigurationpackets.CreateServiceName 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);
}
}
Aggregations