Search in sources :

Example 1 with EncryptionException

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;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) RandomString(edu.umass.cs.gnscommon.utils.RandomString) BasicGuidEntry(edu.umass.cs.gnsclient.client.util.BasicGuidEntry) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

Example 2 with EncryptionException

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");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) KeyPair(java.security.KeyPair) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) IOException(java.io.IOException) File(java.io.File) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 3 with EncryptionException

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);
}
Also used : GNSClient(edu.umass.cs.gnsclient.client.GNSClient) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ObjectOutputStream(java.io.ObjectOutputStream) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) IOException(java.io.IOException) JSONException(org.json.JSONException) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ObjectInputStream(java.io.ObjectInputStream)

Example 4 with EncryptionException

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 >>>>>>>>>>");
}
Also used : GNSClient(edu.umass.cs.gnsclient.client.GNSClient) FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) ObjectInputStream(java.io.ObjectInputStream) BeforeClass(org.junit.BeforeClass)

Example 5 with EncryptionException

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);
    }
}
Also used : EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Aggregations

EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 KeyFactory (java.security.KeyFactory)7 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)7 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)7 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)6 IOException (java.io.IOException)6 File (java.io.File)5 JSONException (org.json.JSONException)5 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)4 PrivateKey (java.security.PrivateKey)4 PublicKey (java.security.PublicKey)4 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)4 FileInputStream (java.io.FileInputStream)3 ObjectInputStream (java.io.ObjectInputStream)3 GNSClient (edu.umass.cs.gnsclient.client.GNSClient)2 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException)2 BufferedReader (java.io.BufferedReader)2 FileOutputStream (java.io.FileOutputStream)2 FileReader (java.io.FileReader)2