Search in sources :

Example 11 with GNSClientCommands

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

the class SetDefaultGuid method parse.

@Override
public void parse(String commandText) throws Exception {
    StringTokenizer st = new StringTokenizer(commandText.trim());
    if (st.countTokens() != 1) {
        wrongArguments();
        return;
    }
    String aliasName = st.nextToken();
    GNSClientCommands gnsClient = module.getGnsClient();
    try {
        try {
            gnsClient.lookupGuid(aliasName);
        } catch (ClientException e) {
            console.printString("Alias " + aliasName + " is not registered in the GNS\n");
            return;
        }
        if (!module.isSilent()) {
            console.printString("Looking up alias " + aliasName + " GUID and certificates...\n");
        }
        GuidEntry myGuid = KeyPairUtils.getGuidEntry(module.getGnsInstance(), aliasName);
        if (myGuid == null) {
            console.printString("You do not have the private key for alias " + aliasName);
            console.printNewline();
            return;
        }
        // Success, let's update the console prompt with the new alias name
        // module.setCurrentGuid(myGuid);
        module.setDefaultGuidAndCheckForVerified(myGuid);
        if (!module.isSilent()) {
            console.printString("Default GUID set to " + aliasName + "\n");
        }
    } catch (IOException e) {
        console.printString("Failed to access the GNS ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 12 with GNSClientCommands

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

the class HighConcurrencyReadsTest 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 = GNSClientCommands.getGNSProvider();
    accountGuidEntries = new GuidEntry[numAccountGuids];
    int numPreExisting = 0;
    for (int i = 0; i < numAccountGuids; i++) {
        LOGGER.log(Level.FINE, "Creating account GUID {0}", new Object[] { ACCOUNT_GUID_PREFIX + i });
        try {
            accountGuidEntries[i] = GuidUtils.lookupOrCreateAccountGuid(clients[0], ACCOUNT_GUID_PREFIX + i, PASSWORD);
            LOGGER.log(Level.FINE, "Created account {0}", new Object[] { accountGuidEntries[i] });
            assert (accountGuidEntries[i].getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i).getGuid()));
        } catch (DuplicateNameException e) {
            numPreExisting++;
            accountGuidEntries[i] = KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i);
            LOGGER.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 (ClientException | IOException 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);
            }
            LOGGER.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) RandomString(edu.umass.cs.gnscommon.utils.RandomString) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) HashSet(java.util.HashSet)

Example 13 with GNSClientCommands

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

the class AccountDelete method parse.

@Override
public void parse(String commandText) throws Exception {
    StringTokenizer st = new StringTokenizer(commandText.trim());
    if ((st.countTokens() != 1)) {
        wrongArguments();
        return;
    }
    String aliasName = st.nextToken();
    try {
        GNSClientCommands gnsClient = module.getGnsClient();
        try {
            gnsClient.lookupGuid(aliasName);
        } catch (IOException | ClientException expected) {
            printString("Alias " + aliasName + " doesn't exist.\n");
            return;
        }
        GuidEntry myGuid = KeyPairUtils.getGuidEntry(module.getGnsInstance(), aliasName);
        if (myGuid == null) {
            printString("Unable to retrieve GUID for alias " + aliasName + "\n");
            return;
        }
        gnsClient.accountGuidRemove(myGuid);
        KeyPairUtils.removeKeyPair(module.getGnsInstance(), aliasName);
        module.setCurrentGuidAndCheckForVerified(null);
        module.setPromptString(ConsoleModule.CONSOLE_PROMPT + ">");
        if (!module.isSilent()) {
            printString("Removed account GUID " + myGuid.getGuid() + "\n");
        }
    } catch (ClientException | IOException e) {
        printString("Failed to delete guid ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) IOException(java.io.IOException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 14 with GNSClientCommands

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

the class GuidLookup method parse.

@Override
public void parse(String commandText) throws Exception {
    try {
        StringTokenizer st = new StringTokenizer(commandText.trim());
        if (st.countTokens() != 1) {
            wrongArguments();
            return;
        }
        String alias = st.nextToken();
        GNSClientCommands gnsClient = module.getGnsClient();
        String value = gnsClient.lookupGuid(alias);
        console.printString(alias + " has GUID " + value);
        console.printNewline();
    } catch (IOException | ClientException e) {
        console.printString("Failed to access GNS ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) IOException(java.io.IOException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException)

Example 15 with GNSClientCommands

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

the class GroupMemberAdd method parse.

@Override
public void parse(String commandText) throws Exception {
    GNSClientCommands gnsClient = module.getGnsClient();
    try {
        StringTokenizer st = new StringTokenizer(commandText.trim());
        String groupGuid;
        switch(st.countTokens()) {
            case 1:
                groupGuid = module.getCurrentGuid().getGuid();
                break;
            case 2:
                groupGuid = st.nextToken();
                if (!StringUtil.isValidGuidString(groupGuid)) {
                    // We probably have an alias, lookup the GUID
                    groupGuid = gnsClient.lookupGuid(groupGuid);
                }
                break;
            default:
                wrongArguments();
                return;
        }
        String guidToAdd = st.nextToken();
        gnsClient.groupAddGuid(groupGuid, guidToAdd, module.getCurrentGuid());
        printString("GUID " + guidToAdd + " added to group " + groupGuid + "\n");
    } catch (IOException | ClientException e) {
        printString("Failed to access GNS ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) IOException(java.io.IOException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException)

Aggregations

GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)70 IOException (java.io.IOException)55 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)50 StringTokenizer (java.util.StringTokenizer)35 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)28 JSONArray (org.json.JSONArray)13 JSONException (org.json.JSONException)11 DuplicateNameException (edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)9 JSONObject (org.json.JSONObject)9 InvalidKeyException (java.security.InvalidKeyException)8 SignatureException (java.security.SignatureException)8 GNSClient (edu.umass.cs.gnsclient.client.GNSClient)6 PublicKey (java.security.PublicKey)6 HashSet (java.util.HashSet)6 BeforeClass (org.junit.BeforeClass)6 RandomString (edu.umass.cs.gnscommon.utils.RandomString)5 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5