Search in sources :

Example 91 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class ServerIntegrationTest method test_210_GroupCreate.

/**
   * Create a group to test.
   *
   * @return a list of GuidEntry
   * @throws Exception
   */
public List<GuidEntry> test_210_GroupCreate() throws Exception {
    //CHECKED FOR VALIDITY
    String mygroupName = "mygroup" + RandomString.randomString(12);
    try {
        clientCommands.lookupGuid(mygroupName);
        failWithStackTrace(mygroupName + " entity should not exist");
    } catch (ClientException e) {
    //Expected
    }
    GuidEntry guidToDeleteEntry = clientCommands.guidCreate(masterGuid, "deleteMe" + RandomString.randomString(12));
    GuidEntry mygroupEntry = clientCommands.guidCreate(masterGuid, mygroupName);
    List<GuidEntry> entries = new ArrayList<>();
    entries.add(guidToDeleteEntry);
    entries.add(mygroupEntry);
    return entries;
}
Also used : ArrayList(java.util.ArrayList) RandomString(edu.umass.cs.gnscommon.utils.RandomString) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) BasicGuidEntry(edu.umass.cs.gnsclient.client.util.BasicGuidEntry) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 92 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class GNSQuickStart method main.

/**
   *
   * @param args
   * @throws IOException
   * @throws InvalidKeySpecException
   * @throws NoSuchAlgorithmException
   * @throws ClientException
   * @throws InvalidKeyException
   * @throws SignatureException
   * @throws Exception
   */
public static void main(String[] args) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, ClientException, InvalidKeyException, SignatureException, Exception {
    // Create a new client object
    GNSClientCommands client = new GNSClientCommands(null);
    System.out.println("Client connected to GNS");
    // Retrive the GUID using the account id
    String guid = client.lookupGuid(accountId);
    System.out.println("Retrieved GUID for " + accountId + ": " + guid);
    // Get the public key from the GNS
    PublicKey publicKey = client.publicKeyLookupFromGuid(guid);
    System.out.println("Retrieved public key: " + publicKey.toString());
    // Load the private key from a file
    PrivateKey privateKey = KeyPairUtils.getPrivateKeyFromPKCS8File(privateKeyFile);
    System.out.println("Retrieved private key: " + DatatypeConverter.printHexBinary(privateKey.getEncoded()));
    // Create a GuidEntry
    GuidEntry accountGuid = new GuidEntry(accountId, guid, publicKey, privateKey);
    System.out.println("Created GUID entry: " + accountGuid.toString());
    // Use the GuidEntry create a new record in the GNS
    client.fieldCreateOneElementList(accountGuid, "homestate", "Florida");
    System.out.println("Added location -> Florida record to the GNS for GUID " + accountGuid.getGuid());
    // Retrieve that record from the GNS
    String result = client.fieldReadArrayFirstElement(accountGuid.getGuid(), "homestate", accountGuid);
    System.out.println("Result of read location: " + result);
    // Update the value of the field
    client.fieldReplace(accountGuid, "homestate", "Massachusetts");
    System.out.println("Changed location -> Massachusetts in the GNS for GUID " + accountGuid.getGuid());
    // Retrieve that record from the GNS again
    result = client.fieldReadArrayFirstElement(accountGuid.getGuid(), "homestate", accountGuid);
    System.out.println("Result of read location: " + result);
    System.exit(0);
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 93 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class HttpClient method accountGuidCreate.

/**
   * Register a new account guid with the corresponding alias on the GNS server.
   * This generates a new guid and a public / private key pair. Returns a
   * GuidEntry for the new account which contains all of this information.
   *
   * @param alias - a human readable alias to the guid - usually an email
   * address
   * @param password
   * @return the GuidEntry for the new account
   * @throws java.io.IOException
   * @throws edu.umass.cs.gnscommon.exceptions.client.ClientException
   * @throws java.security.NoSuchAlgorithmException
   */
public GuidEntry accountGuidCreate(String alias, String password) throws IOException, ClientException, NoSuchAlgorithmException {
    GuidEntry entry = GuidUtils.lookupGuidEntryFromDatabase(this, alias);
    // incurring an GNSProtocol.ACTIVE_REPLICA_EXCEPTION.toString() for a new (non-existent) GNSProtocol.GUID.toString().
    if (entry == null) {
        KeyPair keyPair = KeyPairGenerator.getInstance(GNSProtocol.RSA_ALGORITHM.toString()).generateKeyPair();
        String guid = SharedGuidUtils.createGuidStringFromPublicKey(keyPair.getPublic().getEncoded());
        // Squirrel this away now just in case the call below times out.
        KeyPairUtils.saveKeyPair(getGNSProvider(), alias, guid, keyPair);
        entry = new GuidEntry(alias, guid, keyPair.getPublic(), keyPair.getPrivate());
    }
    assert (entry != null);
    String returnedGuid = accountGuidCreate(alias, entry, password);
    // Anything else we want to do here?
    if (!returnedGuid.equals(entry.guid)) {
        GNSClientConfig.getLogger().log(Level.WARNING, "Returned guid {0} doesn't match locally created guid {1}", new Object[] { returnedGuid, entry.guid });
    }
    assert returnedGuid.equals(entry.guid);
    return entry;
}
Also used : KeyPair(java.security.KeyPair) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 94 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class GNSClientCapacityTest method setupClientsAndGuids.

private static void setupClientsAndGuids() throws Exception {
    clients = new GNSClientCommands[numClients];
    executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(numClients);
    for (int i = 0; i < numClients; i++) clients[i] = new GNSClientCommands();
    String gnsInstance = clients[0].getGNSProvider();
    accountGuidEntries = new GuidEntry[numAccountGuids];
    int numPreExisting = 0;
    for (int i = 0; i < numAccountGuids; i++) {
        log.log(Level.FINE, "Creating account GUID {0}", new Object[] { accountGUIDPrefix + i });
        try {
            accountGuidEntries[i] = GuidUtils.lookupOrCreateAccountGuid(clients[0], accountGUIDPrefix + i, PASSWORD);
            log.log(Level.FINE, "Created account {0}", new Object[] { accountGuidEntries[i] });
            assert (accountGuidEntries[i].getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, accountGUIDPrefix + i).getGuid()));
        } catch (DuplicateNameException e) {
            numPreExisting++;
            accountGuidEntries[i] = KeyPairUtils.getGuidEntry(gnsInstance, accountGUIDPrefix + i);
            log.log(Level.INFO, "Found that account {0} already exists", new Object[] { accountGuidEntries[i] });
        }
    // any other exceptions should be thrown up
    }
    System.out.println("Created (" + (numAccountGuids - numPreExisting) + ") or found pre-existing (" + numPreExisting + ") a total of " + numAccountGuids + " account GUIDs: " + Arrays.asList(accountGuidEntries));
    if (accountGuidsOnly) {
        for (int i = 0; i < accountGuidEntries.length; i++) guidEntries[i] = accountGuidEntries[i];
        return;
    }
    guidEntries = new GuidEntry[numGuids];
    Set<String> subGuids = new HashSet<String>();
    for (int i = 0, j = 0; i < numGuids; i++) {
        subGuids.add(Config.getGlobalString(TC.TEST_GUID_PREFIX) + i);
        if (subGuids.size() == numGuidsPerAccount || i == numGuids - 1) {
            // because batch creation seems buggy
            if (subGuids.size() == 1) {
                String subGuid = subGuids.iterator().next();
                try {
                    GuidEntry created = GuidUtils.lookupOrCreateGuid(clients[0], accountGuidEntries[i / numGuidsPerAccount], subGuid);
                    assert (created.getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, subGuid).getGuid()));
                } catch (DuplicateNameException de) {
                // ignore, will retrieve it locally below
                }
            // any other exceptions should be thrown up
            } else
                try {
                    // batch create
                    clients[0].guidBatchCreate(accountGuidEntries[i / numGuidsPerAccount], subGuids);
                } catch (Exception e) {
                    for (String subGuid : subGuids) {
                        try {
                            clients[0].guidCreate(accountGuidEntries[i / numGuidsPerAccount], subGuid);
                        } catch (DuplicateNameException de) {
                        // ignore, will retrieve it locally below
                        }
                    // any other exception should be throw up
                    }
                }
            for (String subGuid : subGuids) {
                guidEntries[j++] = KeyPairUtils.getGuidEntry(gnsInstance, subGuid);
            }
            log.log(Level.FINE, "Created sub-guid(s) {0}", new Object[] { subGuids });
            subGuids.clear();
        }
    }
    for (GuidEntry guidEntry : accountGuidEntries) assert (guidEntry != null);
    for (GuidEntry guidEntry : guidEntries) assert (guidEntry != null);
    System.out.println("Created or found " + guidEntries.length + " pre-existing sub-guids " + Arrays.asList(guidEntries));
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) HashSet(java.util.HashSet)

Example 95 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class HttpClient method guidCreate.

/**
   * Creates an new GNSProtocol.GUID.toString() associated with an account on the GNS server.
   *
   * @param accountGuid
   * @param alias the alias
   * @return the newly created GNSProtocol.GUID.toString() entry
   * @throws java.io.IOException
   * @throws edu.umass.cs.gnscommon.exceptions.client.ClientException
   * @throws java.security.NoSuchAlgorithmException
   */
public GuidEntry guidCreate(GuidEntry accountGuid, String alias) throws IOException, ClientException, NoSuchAlgorithmException {
    KeyPair keyPair = KeyPairGenerator.getInstance(GNSProtocol.RSA_ALGORITHM.toString()).generateKeyPair();
    String newGuid = guidCreate(accountGuid, alias, keyPair.getPublic());
    KeyPairUtils.saveKeyPair(host + ":" + port, alias, newGuid, keyPair);
    GuidEntry entry = new GuidEntry(alias, newGuid, keyPair.getPublic(), keyPair.getPrivate());
    return entry;
}
Also used : KeyPair(java.security.KeyPair) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Aggregations

GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)134 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)87 IOException (java.io.IOException)85 Test (org.junit.Test)69 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)61 RandomString (edu.umass.cs.gnscommon.utils.RandomString)54 BasicGuidEntry (edu.umass.cs.gnsclient.client.util.BasicGuidEntry)46 JSONException (org.json.JSONException)45 JSONArray (org.json.JSONArray)36 GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)28 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)18 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)15 DuplicateNameException (edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException)14 StringTokenizer (java.util.StringTokenizer)13 JSONObject (org.json.JSONObject)12 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException)11 FileNotFoundException (java.io.FileNotFoundException)11 HashSet (java.util.HashSet)10 FileInputStream (java.io.FileInputStream)8 Repeat (edu.umass.cs.utils.Repeat)7