Search in sources :

Example 56 with GNSClientCommands

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

the class ServerConnectTest method setUpBeforeClass.

/**
   *
   * @throws Exception
   */
@BeforeClass
public static void setUpBeforeClass() throws Exception {
    if (System.getProperty("startServer") != null && System.getProperty("startServer").equals("true")) {
        // clear explicitly if gigapaxos
        if (useGPScript()) {
            RunCommand.command("kill -s TERM `ps -ef | grep GNS.jar | grep -v grep | " + "grep -v ServerConnectTest  | grep -v \"context\" | awk '{print $2}'`", ".");
            System.out.println(System.getProperty(DefaultProps.SERVER_COMMAND.key) + " " + getGigaPaxosOptions() + " forceclear all");
            RunCommand.command(System.getProperty(DefaultProps.SERVER_COMMAND.key) + " " + getGigaPaxosOptions() + " forceclear all", ".");
            /* We need to do this to limit the number of files used by mongo.
				 * Otherwise failed runs quickly lead to more failed runs because
				 * index files created in previous runs are not removed.
         */
            dropAllDatabases();
            options = getGigaPaxosOptions() + " restart all";
        } else {
            options = SCRIPTS_OPTIONS;
        }
        System.out.println(System.getProperty(DefaultProps.SERVER_COMMAND.key) + " " + options);
        ArrayList<String> output = RunCommand.command(System.getProperty(DefaultProps.SERVER_COMMAND.key) + " " + options, ".");
        if (output != null) {
            for (String line : output) {
                System.out.println(line);
            }
        } else {
            failWithStackTrace("Server command failure: ; aborting all tests.");
        }
    }
    String gpConfFile = System.getProperty(DefaultProps.GIGAPAXOS_CONFIG.key);
    String logFile = System.getProperty(DefaultProps.LOGGING_PROPERTIES.key);
    ArrayList<String> output = RunCommand.command("cat " + logFile + " | grep \"java.util.logging.FileHandler.pattern\" | sed 's/java.util.logging.FileHandler.pattern = //g'", ".", false);
    String logFiles = output.get(0) + "*";
    System.out.println("Waiting for servers to be ready...");
    output = RunCommand.command("cat " + gpConfFile + " | grep \"reconfigurator\\.\" | wc -l ", ".", false);
    int numRC = Integer.parseInt(output.get(0));
    output = RunCommand.command("cat " + gpConfFile + " | grep \"active\\.\" | wc -l ", ".", false);
    int numAR = Integer.parseInt(output.get(0));
    int numServers = numRC + numAR;
    output = RunCommand.command("cat " + logFiles + " | grep \"server ready\" | wc -l ", ".", false);
    int numServersUp = Integer.parseInt(output.get(0));
    while (numServersUp < numServers) {
        Thread.sleep(5000);
        output = RunCommand.command("cat " + logFiles + " | grep \"server ready\" | wc -l ", ".", false);
        numServersUp = Integer.parseInt(output.get(0));
        System.out.println(Integer.toString(numServersUp) + " out of " + Integer.toString(numServers) + " servers are ready.");
    }
    System.out.println("Starting client");
    client = new GNSClientCommands();
    // Make all the reads be coordinated
    client.setForceCoordinatedReads(true);
    // arun: connectivity check embedded in GNSClient constructor
    boolean connected = client instanceof GNSClient;
    if (connected) {
        System.out.println("Client created and connected to server.");
    }
    //
    int tries = 5;
    boolean accountCreated = false;
    do {
        try {
            System.out.println("Creating account guid: " + (tries - 1) + " attempt remaining.");
            masterGuid = GuidUtils.lookupOrCreateAccountGuid(client, accountAlias, PASSWORD, true);
            accountCreated = true;
        } catch (Exception e) {
            e.printStackTrace();
            ThreadUtils.sleep((5 - tries) * 5000);
        }
    } while (!accountCreated && --tries > 0);
    if (accountCreated == false) {
        failWithStackTrace("Failure setting up account guid; aborting all tests.");
    }
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) GNSClient(edu.umass.cs.gnsclient.client.GNSClient) RandomString(edu.umass.cs.gnscommon.utils.RandomString) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 57 with GNSClientCommands

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

the class CreateDNSRecordExample method main.

/**
	 * @param args
	 * @throws Exception 
	 */
public static void main(String[] args) throws Exception {
    // initialize parameters
    DOMAIN = "example.com.";
    if (System.getProperty("domain") != null) {
        DOMAIN = System.getProperty("domain");
    }
    // if domain does not end with a dot, add a dot to its end
    if (!DOMAIN.endsWith(".")) {
        DOMAIN = DOMAIN + ".";
    }
    // use domain name and a random number as account
    ACCOUNT = DOMAIN;
    /**
		 *  default record file is "conf/activeCode/records", it list the IP addresses in separate lines as:
		 *  1.1.1.1
		 *  2.2.2.2
		 *  ...
		 */
    RECORD_FILE = "conf/activeCode/records";
    if (System.getProperty("record_file") != null) {
        RECORD_FILE = System.getProperty("record_file");
    }
    // read in records
    List<String> records = new ArrayList<String>();
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(RECORD_FILE)));
    String line = reader.readLine();
    while (line != null) {
        records.add(line);
        line = reader.readLine();
    }
    reader.close();
    // initialize client and create record for the domain
    try {
        client = new GNSClientCommands();
    } catch (IOException e) {
        e.printStackTrace();
    }
    GuidEntry accountGuid = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT, "password", true);
    JSONObject recordObj = ManagedDNSServiceProxy.recordToCreate(records, TTL);
    client.execute(GNSCommand.fieldUpdate(accountGuid, "A", recordObj));
    System.out.println("A record for domain " + DOMAIN + " has been created.");
    System.exit(0);
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 58 with GNSClientCommands

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

the class AdminBadAuthTest method setupBeforeClass.

/**
   *
   * @throws IOException
   */
@BeforeClass
public static void setupBeforeClass() throws IOException {
    System.out.println("Starting client");
    clientCommands = new GNSClientCommands();
    // Make all the reads be coordinated
    clientCommands.setForceCoordinatedReads(true);
    // arun: connectivity check embedded in GNSClient constructor
    boolean connected = clientCommands instanceof GNSClient;
    if (connected) {
        System.out.println("Client created and connected to server.");
    }
    badClient = new BadClient(ReconfigurationConfig.getReconfiguratorAddresses());
    badClient.setForceCoordinatedReads(true);
    connected = badClient instanceof GNSClient;
    if (connected) {
        System.out.println("BadClient created and connected to server.");
    }
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) GNSClient(edu.umass.cs.gnsclient.client.GNSClient) BeforeClass(org.junit.BeforeClass)

Example 59 with GNSClientCommands

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

the class ClientACLExample 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 {
    client = new GNSClientCommands();
    System.out.println("[Client connected to GNS]\n");
    try {
        System.out.println("// account GUID creation\n" + "client.execute(" + ACCOUNT_NAME + ")");
        client.execute(GNSCommand.createAccount(ACCOUNT_NAME));
        GUID = GuidUtils.getGUIDKeys(ACCOUNT_NAME);
    } catch (Exception | Error e) {
        System.out.println("Exception during accountGuid creation: " + e);
        e.printStackTrace();
        System.exit(1);
    }
    // Create a JSON Object to initialize our guid record
    JSONObject json = new JSONObject("{\"name\":\"me\",\"location\":\"work\"}");
    // Write out the JSON Object
    client.execute(GNSCommand.update(GUID, json));
    System.out.println("\n// Update guid record\n" + "client.update(guid, record) // record=" + json);
    // Remove default read access from guid
    client.execute(GNSCommand.aclRemove(AclAccessType.READ_WHITELIST, GUID, GNSProtocol.ENTIRE_RECORD.toString(), GNSProtocol.ALL_GUIDS.toString()));
    System.out.println("\n// Remove default read access from guid\n" + "client.aclRemove(READ_WHITELIST, guid, ALL_FIELDS, ALL_GUIDS)");
    // Create phoneGuid
    // First we create an alias for the phoneGuid
    String phoneAlias = "phone" + RandomString.randomString(12);
    // Create a sub guid under our guid account
    client.execute(GNSCommand.guidCreate(GUID, phoneAlias));
    // Get the GuidEntry from the local database
    phoneGuid = GuidUtils.getGUIDKeys(phoneAlias);
    System.out.println("\n// Create phoneGuid\n" + "client.guidCreate(guid, phoneAlias) // phoneAlias=" + phoneAlias);
    // Give phoneGuid read access to fields in guid
    // If we had not removed the default read access from guid this step
    // would be unnecessary
    client.execute(GNSCommand.aclAdd(AclAccessType.READ_WHITELIST, GUID, GNSProtocol.ENTIRE_RECORD.toString(), phoneGuid.getGuid()));
    JSONObject result = client.execute(GNSCommand.read(GUID.getGuid(), phoneGuid)).getResultJSONObject();
    System.out.println("\n// Give phoneGuid read access to fields in guid and read guid entry as phoneGuid\n" + "client.aclAdd(READ_WHITELIST, guid, ALL_FIELDS, phoneGuid)\n" + "client.read(guid, phoneGuid) -> " + result);
    // Allow phoneGuid to write to the location field of guid
    client.execute(GNSCommand.aclAdd(AclAccessType.WRITE_WHITELIST, GUID, "location", phoneGuid.getGuid()));
    System.out.println("\n// Give phoneGuid write access to \"location\" field of guid\n" + "client.aclAdd(WRITE_WHITELIST, guid, \"location\", phoneGuid)");
    // As phoneGuid, update the location field on guid
    client.execute(GNSCommand.fieldUpdate(GUID.getGuid(), "location", "home", phoneGuid));
    String field_result = client.execute(GNSCommand.fieldRead(GUID.getGuid(), "location", phoneGuid)).getResultString();
    System.out.println("\n// Use phoneGuid to update \"location\" field of guid\n" + "client.fieldUpdate(guid, \"location\", \"home\", phoneGuid)\n" + "client.fieldRead(guid.getGuid(), \"location\", phoneGuid) -> " + field_result);
    // Remove phoneGuid from ACL
    client.execute(GNSCommand.aclRemove(AclAccessType.READ_WHITELIST, GUID, GNSProtocol.ENTIRE_RECORD.toString(), phoneGuid.getGuid()));
    client.execute(GNSCommand.aclRemove(AclAccessType.WRITE_WHITELIST, GUID, "location", phoneGuid.getGuid()));
    System.out.println("\n// Remove phoneGuid from guid's read and write whitelists \n" + "client.aclRemove(READ_WHITELIST, guid, ALL_FIELDS, phoneGuid))\n" + "client.aclRemove(WRITE_WHITELIST, guid, \"location\", phoneGuid);");
    // Verify phoneGuid can't read guid (exception expected)
    try {
        System.out.println("\n// Attempting to read from guid using phoneGuid (failure expected)\n" + "client.read(guid, phoneGuid)");
        result = client.execute(GNSCommand.read(GUID.getGuid(), phoneGuid)).getResultJSONObject();
        System.out.println("SOMETHING WENT WRONG. An exception should have been thrown. Terminating.");
        client.close();
        System.exit(1);
    } catch (AclException e) {
        System.out.println("// client.read failed as expected with the following AclException:\n" + e.getMessage());
    }
    // expected)
    try {
        System.out.println("\n// Attempting to update \"location\" field of guid using phoneGuid (failure expected)\n" + "client.fieldUpdate(guid.getGuid(), \"location\", \"vacation\", phoneGuid)");
        client.execute(GNSCommand.fieldUpdate(GUID.getGuid(), "location", "vacation", phoneGuid));
        System.out.println("\nSOMETHING WENT WRONG. An exception should have been thrown. Terminating.");
        client.close();
        System.exit(1);
    } catch (AclException e) {
        System.out.println("// client.fieldUpdate failed as expected with the following AclException:\n" + e.getMessage());
    }
    System.out.println("\n// Example complete, gracefully closing the client\n" + "client.close()");
    client.close();
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) JSONObject(org.json.JSONObject) RandomString(edu.umass.cs.gnscommon.utils.RandomString) AclException(edu.umass.cs.gnscommon.exceptions.client.AclException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) AclException(edu.umass.cs.gnscommon.exceptions.client.AclException)

Example 60 with GNSClientCommands

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

the class ClientWebACLExample 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, JSONException {
    client = new GNSClientCommands();
    System.out.println("[Client connected to GNS]\n");
    try {
        // deleting the entries, any if created earlier
        System.out.println("// User GUID creation\n" + "client.execute(" + ACCOUNT_NAME_USER + ")");
        try {
            client.execute(GNSCommand.createAccount(ACCOUNT_NAME_USER));
        } catch (Exception e) {
            System.out.println("Exception during accountGuid creation: " + e.getMessage());
        }
        GUID_USER = GuidUtils.getGUIDKeys(ACCOUNT_NAME_USER);
        // Create reader
        // First we create an alias for the reader
        // Create a sub guid under our guid account
        GuidEntry readerGuidEntry = null;
        try {
            readerGuidEntry = GuidUtils.getGUIDKeys(READER_ALIAS);
            if (readerGuidEntry == null) {
                client.execute(GNSCommand.createGUID(GUID_USER, READER_ALIAS));
            }
        } catch (Exception e) {
            System.out.println("Exception during reader creation: " + e.getMessage());
        }
        // Get the GuidEntry from the local database
        reader = GuidUtils.getGUIDKeys(READER_ALIAS);
        System.out.println("\n// Create reader\n" + "client.createGuid(guid, readerAlias) // readerAlias=" + READER_ALIAS);
    } catch (Exception | Error e) {
        System.out.println("Exception during accountGuid creation: " + e);
        e.printStackTrace();
        System.exit(1);
    }
    // Create a JSON Object to initialize our guid record
    JSONObject json = new JSONObject("{\"name\":\"John Doe\",\"location\":\"Amherst\",\"type\":\"USER\"," + "\"status\":\"EMPLOYEE\"," + "\"contact\":\"0123456789\"," + "\"id\":\"ES_JD_123\"}");
    // Write out the JSON Object
    client.execute(GNSCommand.update(GUID_USER, json));
    System.out.println("\n// Update guid record\n" + "client.update(guid, record) // record=" + json);
    // Remove default read access from guid
    /*client.execute(GNSCommand.aclRemove(AclAccessType.READ_WHITELIST, GUID_USER,
                GNSProtocol.ENTIRE_RECORD.toString(),
                GNSProtocol.ALL_GUIDS.toString()));
        System.out
                .println("\n// Remove default read access from guid\n"
                        + "client.aclRemove(READ_WHITELIST, guid, ALL_FIELDS, ALL_GUIDS)");*/
    // Give reader read access to fields in guid
    // If we had not removed the default read access from guid this step
    // would be unnecessary
    client.execute(GNSCommand.aclAdd(AclAccessType.READ_WHITELIST, GUID_USER, "name", reader.getGuid()));
    client.execute(GNSCommand.aclAdd(AclAccessType.READ_WHITELIST, GUID_USER, "ID", reader.getGuid()));
    client.execute(GNSCommand.aclAdd(AclAccessType.READ_WHITELIST, GUID_USER, "location", reader.getGuid()));
    client.execute(GNSCommand.aclAdd(AclAccessType.READ_WHITELIST, GUID_USER, "type", reader.getGuid()));
    client.execute(GNSCommand.aclAdd(AclAccessType.READ_WHITELIST, GUID_USER, "status", reader.getGuid()));
    client.execute(GNSCommand.aclAdd(AclAccessType.READ_WHITELIST, GUID_USER, "contact", reader.getGuid()));
    JSONObject result = client.execute(GNSCommand.read(GUID_USER.getGuid(), reader)).getResultJSONObject();
    System.out.println("\n// Give reader read access to the fields : 'name' and 'ID' in guid " + "and read guid entry as reader\n" + "client.aclAdd(READ_WHITELIST, guid, ALL_FIELDS, reader)\n" + "client.read(guid, reader) -> " + result);
    // Remove reader from ACL
    client.execute(GNSCommand.aclRemove(AclAccessType.READ_WHITELIST, GUID_USER, "location", reader.getGuid()));
    client.execute(GNSCommand.aclRemove(AclAccessType.READ_WHITELIST, GUID_USER, "type", reader.getGuid()));
    client.execute(GNSCommand.aclRemove(AclAccessType.READ_WHITELIST, GUID_USER, "status", reader.getGuid()));
    client.execute(GNSCommand.aclRemove(AclAccessType.READ_WHITELIST, GUID_USER, "contact", reader.getGuid()));
    System.out.println("\n// Remove reader from guid's read whitelists for the fields: " + "location, type, status, contact \n" + "client.aclRemove(READ_WHITELIST, guid, ALL_FIELDS, reader))\n" + "client.aclRemove(WRITE_WHITELIST, guid, \"location\", reader);");
    System.out.println("ACCOUNT GUID:" + GUID_USER.getGuid());
    System.out.println("ACCOUNT GUID:" + GUID_USER.getPrivateKey());
    System.out.println("ACCOUNT GUID(str):" + GUID_USER.getPrivateKey().toString());
    System.out.println("READER GUID:" + reader.getGuid());
    System.out.println("\n// Example complete, gracefully closing the client\n" + "client.close()");
    client.close();
    writeToConfigFile();
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) JSONObject(org.json.JSONObject) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SignatureException(java.security.SignatureException) InvalidGuidException(edu.umass.cs.gnscommon.exceptions.client.InvalidGuidException) IOException(java.io.IOException) JSONException(org.json.JSONException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

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