use of edu.umass.cs.gnscommon.exceptions.client.EncryptionException in project GNS by MobilityFirst.
the class ServerIntegrationTest method test_552_QuerySetupGuids.
/**
* Setup some guids for group testing.
*
* @param groupTestFieldName
* @param queryOne
* @param queryTwo
* @return List of groups
* @throws NoSuchAlgorithmException
* @throws EncryptionException
*/
public List<GuidEntry> test_552_QuerySetupGuids(String groupTestFieldName, String queryOne, String queryTwo) throws EncryptionException, NoSuchAlgorithmException {
if (!enable552) {
return new ArrayList<>(0);
}
try {
for (int cnt = 0; cnt < 5; cnt++) {
GuidEntry testEntry = clientCommands.guidCreate(masterGuid, "queryTest-" + RandomString.randomString(6));
JSONArray array = new JSONArray(Arrays.asList(Integer.parseInt(TEST_HIGH_VALUE)));
clientCommands.fieldReplaceOrCreateList(testEntry, groupTestFieldName, array);
}
for (int cnt = 0; cnt < 5; cnt++) {
GuidEntry testEntry = clientCommands.guidCreate(masterGuid, "queryTest-" + RandomString.randomString(6));
JSONArray array = new JSONArray(Arrays.asList(Integer.parseInt(TEST_LOW_VALUE)));
clientCommands.fieldReplaceOrCreateList(testEntry, groupTestFieldName, array);
}
} catch (Exception e) {
failWithStackTrace("Exception while trying to create the guids: ", e);
}
// the HRN is a hash of the query
String groupOneGuidName = Base64.encodeToString(SHA1HashFunction.getInstance().hash(queryOne), false);
GuidEntry groupOneGuid = GuidUtils.lookupOrCreateGuidEntry(groupOneGuidName, GNSClientCommands.getGNSProvider());
//groupGuid = client.guidCreate(masterGuid, groupGuidName + RandomString.randomString(6));
// the HRN is a hash of the query
String groupTwoGuidName = Base64.encodeToString(SHA1HashFunction.getInstance().hash(queryTwo), false);
GuidEntry groupTwoGuid = GuidUtils.lookupOrCreateGuidEntry(groupTwoGuidName, GNSClientCommands.getGNSProvider());
//groupTwoGuid = client.guidCreate(masterGuid, groupTwoGuidName + RandomString.randomString(6));
List<GuidEntry> list = new ArrayList<>(2);
list.add(groupOneGuid);
list.add(groupTwoGuid);
return list;
}
use of edu.umass.cs.gnscommon.exceptions.client.EncryptionException in project GNS by MobilityFirst.
the class GuidImport method parse.
@Override
public void parse(String commandText) throws Exception {
try {
StringTokenizer st = new StringTokenizer(commandText.trim());
if (st.countTokens() != 1) {
wrongArguments();
return;
}
String filename = st.nextToken();
File f = new File(filename);
GuidEntry guidEntry;
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
guidEntry = new GuidEntry(ois);
ois.close();
}
KeyPairUtils.saveKeyPair(module.getGnsInstance(), guidEntry.getEntityName(), guidEntry.getGuid(), new KeyPair(guidEntry.getPublicKey(), guidEntry.getPrivateKey()));
console.printString("Keys for " + guidEntry.getEntityName() + " read from " + filename + " and saved in local preferences.");
console.printNewline();
} catch (IOException | EncryptionException e) {
console.printString("Failed to load keys ( " + e + ")\n");
}
}
use of edu.umass.cs.gnscommon.exceptions.client.EncryptionException in project GNS by MobilityFirst.
the class ActiveCodeHelloWorldExample method main.
/**
* @param args
* @throws IOException
* @throws ClientException
* @throws JSONException
*/
public static void main(String[] args) throws IOException, ClientException, JSONException {
boolean update = true;
if (System.getProperty("update") != null) {
update = Boolean.parseBoolean(System.getProperty("update"));
}
boolean isRead = true;
if (System.getProperty("isRead") != null) {
isRead = Boolean.parseBoolean(System.getProperty("isRead"));
}
String name = "benign";
if (System.getProperty("name") != null) {
name = System.getProperty("name");
}
String codeFile = "scripts/activeCode/noop.js";
if (System.getProperty("codeFile") != null) {
codeFile = System.getProperty("codeFile");
}
if (args.length > 0) {
codeFile = args[0];
}
// create a client
final GNSClient client = new GNSClient();
final String ACCOUNT_GUID_PREFIX = "GNS_ACCOUNT_";
final String ACCOUNT_GUID = ACCOUNT_GUID_PREFIX + name;
final String PASSWORD = "";
edu.umass.cs.gnsclient.client.util.GuidEntry entry = null;
// create an account
try {
entry = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_GUID, PASSWORD);
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(new File("guid")));
entry.writeObject(output);
output.flush();
output.close();
} catch (Exception e) {
e.printStackTrace();
// The guid is already created, try to read from a local file
try {
ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("guid")));
entry = new GuidEntry(input);
input.close();
} catch (IOException | EncryptionException ie) {
ie.printStackTrace();
}
}
String field = "someField";
String value = "original value";
String depth_field = "depthField";
String depth_result = "Depth query succeeds";
// set up a field
client.execute(GNSCommand.fieldUpdate(entry, field, value));
client.execute(GNSCommand.fieldUpdate(entry, depth_field, depth_result));
// clear code for both read and write action
client.execute(GNSCommand.activeCodeClear(entry.getGuid(), ActiveCode.READ_ACTION, entry));
client.execute(GNSCommand.activeCodeClear(entry.getGuid(), ActiveCode.WRITE_ACTION, entry));
// get the value of the field
String response = client.execute(GNSCommand.fieldRead(entry, field)).getResultJSONObject().getString(field);
System.out.println("Before the code is deployed, the value of field(" + field + ") is " + response);
// read in the code as a string
final String code = new String(Files.readAllBytes(Paths.get(codeFile)));
// set up the code for on read operation
if (isRead) {
if (update) {
client.execute(GNSCommand.activeCodeSet(entry.getGuid(), ActiveCode.READ_ACTION, code, entry));
}
} else {
if (update) {
client.execute(GNSCommand.activeCodeSet(entry.getGuid(), ActiveCode.WRITE_ACTION, code, entry));
}
}
// get the value of the field again
response = client.execute(GNSCommand.fieldRead(entry, field)).getResultJSONObject().getString(field);
System.out.println("After the code is deployed, the value of field(" + field + ") is " + response);
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(new File("guid")));
entry.writeObject(output);
output.flush();
output.close();
System.exit(0);
}
use of edu.umass.cs.gnscommon.exceptions.client.EncryptionException in project GNS by MobilityFirst.
the class TestActiveCodeRemoteQueryClient method setupClientsAndGuids.
/**
* @throws IOException
* @throws ClientException
*/
@BeforeClass
public static void setupClientsAndGuids() throws IOException, ClientException {
client = new GNSClient();
entries = new GuidEntry[2];
// initialize two GUID
for (int i = 0; i < numGuid; i++) {
try {
entries[i] = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_GUID_PREFIX + i, PASSWORD);
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(new File("guid" + i)));
entries[i].writeObject(output);
output.flush();
output.close();
} catch (Exception e) {
// the GUID has already been created, try to fetch it from a file
try {
ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("guid" + i)));
entries[i] = new GuidEntry(input);
input.close();
} catch (IOException | EncryptionException ie) {
ie.printStackTrace();
}
}
}
// set the target guid to the second one and put it into the code
targetGuid = entries[numGuid - 1].getGuid();
noop_code = new String(Files.readAllBytes(Paths.get("scripts/activeCode/noop.js")));
String codeFile = System.getProperty("activeReadCode");
if (codeFile == null)
codeFile = "scripts/activeCode/remoteReadQuery.js";
String code = new String(Files.readAllBytes(Paths.get(codeFile)));
read_code = code.replace("//substitute this line with the targetGuid", "var targetGuid=\"" + targetGuid + "\";");
System.out.println("The read code is:\n" + read_code);
codeFile = System.getProperty("activeWriteCode");
if (codeFile == null)
codeFile = "scripts/activeCode/remoteWriteQuery.js";
code = new String(Files.readAllBytes(Paths.get(codeFile)));
write_code = code.replace("//substitute this line with the targetGuid", "var targetGuid=\"" + targetGuid + "\";");
System.out.println("The write code is:\n" + write_code);
// initialize the fields for each guid
client.execute(GNSCommand.fieldUpdate(entries[0], someField, someValue));
client.execute(GNSCommand.fieldUpdate(entries[1], depthField, depthResult));
System.out.println(">>>>>>>>>> Testing >>>>>>>>>>");
}
use of edu.umass.cs.gnscommon.exceptions.client.EncryptionException in project GNS by MobilityFirst.
the class BasicGuidEntry method generatePublicKey.
private static PublicKey generatePublicKey(String encodedPublic) throws EncryptionException {
byte[] encodedPublicKey = Base64.decode(encodedPublic);
try {
KeyFactory keyFactory = KeyFactory.getInstance(GNSProtocol.RSA_ALGORITHM.toString());
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
return keyFactory.generatePublic(publicKeySpec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new EncryptionException("Failed to generate keypair", e);
}
}
Aggregations