Skip to content

Commit

Permalink
Fix compilation. Add auth parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
adyatlov committed Mar 29, 2017
1 parent 551ee1b commit 05258d6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
-r,--restore-znode <arg> the znode into which read data should be
restored

-u, --user <arg> user name, optional
-p, --password <arg> password, optional

-v,--verbose enable debug output
25 changes: 21 additions & 4 deletions src/main/java/com/d2fn/guano/DumpJob.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.d2fn.guano;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
Expand All @@ -17,11 +20,15 @@ public class DumpJob implements Job, Watcher {
private String zkServer;
private String outputDir;
private String znode;
private String user;
private String pass;

public DumpJob(String zkServer, String outputDir, String znode) {
public DumpJob(String zkServer, String outputDir, String znode, String user, String pass) {
this.zkServer = zkServer;
this.outputDir = outputDir;
this.znode = znode;
this.user = user;
this.pass = pass;
}

public void go() {
Expand All @@ -30,13 +37,23 @@ public void go() {
System.out.println("reading from zookeeper path: " + znode);
System.out.println("dumping to local directory: " + outputDir);

ZooKeeper zk;
try {
ZooKeeper zk = new ZooKeeper(zkServer + znode, 10000, this);
go(zk);
zk = new ZooKeeper(zkServer + znode, 10000, this);
} catch (IOException e) {
System.err.println("error connecting to " + zkServer);
System.exit(1);
return;
}
if (user != null && pass != null) {
try {
zk.addAuthInfo("digest", (user + ":" + pass).getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
System.err.println("error processing credentials: " + e.getMessage());
System.exit(1);
}
}
go(zk);
}

private void go(ZooKeeper zk) {
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/d2fn/guano/Guano.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,25 @@ public void buildJob() {
}

String server = cmd.getOptionValue("s");
String user = null;
String pass = null;

if (cmd.hasOption("u") && cmd.hasOption("p")) {
user = cmd.getOptionValue("u");
pass = cmd.getOptionValue("p");
}

// dump?
if(cmd.hasOption("d") && cmd.hasOption("o")) {
String znode = cmd.getOptionValue("d");
String outputDir = cmd.getOptionValue("o");
job = new DumpJob(server, outputDir, znode);
job = new DumpJob(server, outputDir, znode, user, pass);
}
// restore
else if(cmd.hasOption("r") && cmd.hasOption("i")) {
String znode = cmd.getOptionValue("r");
String inputDir = cmd.getOptionValue("i");
job = new RestoreJob(server, znode, inputDir);
job = new RestoreJob(server, znode, inputDir, user, pass);
}
else {
usage(options);
Expand Down Expand Up @@ -84,6 +91,8 @@ public static Options initOptions() {
options.addOption("o", "output-dir", true, "the output directory to which znode information should be written (must be a normal, empty directory)");
options.addOption("i", "input-dir", true, "the input directory from which znode information should be read");
options.addOption("v", "verbose", false, "enable debug output");
options.addOption("u", "user", true, "user name, optional");
options.addOption("p", "password", true, "password, optional");
return options;
}
}
9 changes: 8 additions & 1 deletion src/main/java/com/d2fn/guano/RestoreJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ public class RestoreJob implements Job, Watcher, FSVisitor {
private String zkServer;
private String znode;
private String inputDir;
private String user;
private String pass;

private ZooKeeper zk;

public RestoreJob(String zkServer, String znode, String inputDir) {
public RestoreJob(String zkServer, String znode, String inputDir, String user, String pass) {
this.zkServer = zkServer;
this.znode = znode;
this.inputDir = inputDir;
this.user = user;
this.pass = pass;
}

public void go() {
Expand All @@ -33,6 +37,9 @@ public void go() {

try {
zk = new ZooKeeper(zkServer + znode, 10000, this);
if (user != null && pass != null) {
zk.addAuthInfo("digest", (user + ":" + pass).getBytes("UTF-8"));
}

This comment has been minimized.

Copy link
@aquamatthias

aquamatthias Mar 29, 2017

You handle an UnsupportedEncodingException in DumpJob.

while(!zk.getState().isConnected()) {
System.out.println("connecting to " + zkServer + " with chroot " + znode);
Thread.sleep(1000L);
Expand Down

1 comment on commit 05258d6

@aquamatthias
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple addition - cool! Have you tried accessing a secured zk?
The restore operation could be improved by taking ACLs into account.

Please sign in to comment.