# HG changeset patch # User Severin Gehwolf # Date 1442582345 -7200 # Node ID f57cc3785d2528f52c0fe76a9347958b0c82b226 # Parent 4281c1d18ea0c8f31bead2e071edfa6bd0339883 Wrap create-user.js expressions in try/catch. Reviewed-by: omajid, neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-September/016226.html PR2638 diff -r 4281c1d18ea0 -r f57cc3785d25 distribution/libs/create-user.js --- a/distribution/libs/create-user.js Fri Sep 18 10:18:08 2015 -0400 +++ b/distribution/libs/create-user.js Fri Sep 18 15:19:05 2015 +0200 @@ -3,20 +3,28 @@ var majorVersion = v.substring(0, v.indexOf('.')) var minorMicro = v.substr(v.indexOf('.') + 1) var minorVersion = minorMicro.substr(0, minorMicro.indexOf('.')) -if ( majorVersion < 2 || ( majorVersion == 2 && minorVersion <= 2 ) ) { - // mongodb version 2.2 and below don't have the third argument. - // this should create the user as read + write. - db.addUser("$USERNAME","$PASSWORD") -} else if ( majorVersion == 2 && minorVersion <= 4 ) { - db.addUser({ user: "$USERNAME", pwd: "$PASSWORD", roles: [ "readWrite" ] }) -} else if ( majorVersion == 2 ) { - db.createUser({ user: "$USERNAME", pwd: "$PASSWORD", roles: [ "readWrite" ] }) -} else if ( majorVersion == 3 ) { - db = db.getSiblingDB("admin") - db.createUser({ user: "thermostat-admin", pwd: "$PASSWORD", roles: [ "dbOwner", "userAdminAnyDatabase" ] }) - db.auth("thermostat-admin", "$PASSWORD") - db = db.getSiblingDB("thermostat") - db.createUser({ user: "$USERNAME", pwd: "$PASSWORD", roles: [ "readWrite" ] }) -} else { - throw "Unknown mongo version: " + v +try { + if ( majorVersion < 2 || ( majorVersion == 2 && minorVersion <= 2 ) ) { + // mongodb version 2.2 and below don't have the third argument. + // this should create the user as read + write. + db.addUser("$USERNAME","$PASSWORD") + } else if ( majorVersion == 2 && minorVersion <= 4 ) { + db.addUser({ user: "$USERNAME", pwd: "$PASSWORD", roles: [ "readWrite" ] }) + } else if ( majorVersion == 2 ) { + db.createUser({ user: "$USERNAME", pwd: "$PASSWORD", roles: [ "readWrite" ] }) + } else if ( majorVersion == 3 ) { + db = db.getSiblingDB("admin") + db.createUser({ user: "thermostat-admin", pwd: "$PASSWORD", roles: [ "dbOwner", "userAdminAnyDatabase" ] }) + db.auth("thermostat-admin", "$PASSWORD") + db = db.getSiblingDB("thermostat") + db.createUser({ user: "$USERNAME", pwd: "$PASSWORD", roles: [ "readWrite" ] }) + } else { + throw "Unknown mongo version: " + v + } + // Exit with a success return code. + quit(0) +} catch (e) { + print(e) + // Be sure to leave the script with a non-zero code + quit(1) } diff -r 4281c1d18ea0 -r f57cc3785d25 setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/MongodbUserSetup.java --- a/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/MongodbUserSetup.java Fri Sep 18 10:18:08 2015 -0400 +++ b/setup/command/src/main/java/com/redhat/thermostat/setup/command/internal/model/MongodbUserSetup.java Fri Sep 18 15:19:05 2015 +0200 @@ -40,6 +40,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.lang.ProcessBuilder.Redirect; @@ -51,11 +52,14 @@ import java.util.Scanner; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.logging.Logger; import com.redhat.thermostat.common.ActionEvent; import com.redhat.thermostat.common.ActionListener; import com.redhat.thermostat.common.cli.AbstractStateNotifyingCommand; import com.redhat.thermostat.common.tools.ApplicationState; +import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.common.utils.StreamUtils; import com.redhat.thermostat.launcher.Launcher; import com.redhat.thermostat.shared.config.CommonPaths; @@ -64,6 +68,7 @@ static final String[] STORAGE_START_ARGS = {"storage", "--start", "--permitLocalhostException"}; static final String[] STORAGE_STOP_ARGS = {"storage", "--stop"}; private static final String WEB_AUTH_FILE = "web.auth"; + private static final Logger logger = LoggingUtils.getLogger(MongodbUserSetup.class); private final UserCredsValidator validator; private final Launcher launcher; private final CredentialFinder finder; @@ -184,9 +189,11 @@ //package-private for testing int runMongo() throws IOException, InterruptedException { + logger.fine("running 'mongo 127.0.0.1:27518/thermostat' with piped input"); ProcessBuilder mongoProcessBuilder = new ProcessBuilder("mongo", "127.0.0.1:27518/thermostat"); mongoProcessBuilder.redirectInput(Redirect.PIPE); Process process = mongoProcessBuilder.start(); + ProcOutErrReader reader = new ProcOutErrReader(process); File createUserTemplate = new File(paths.getSystemLibRoot(), "create-user.js"); // Write to the forked processes stdIn replacing username/password // on the fly. @@ -202,7 +209,14 @@ outWriter.write(line); } } - return process.waitFor(); + reader.start(); + int exitStatus = process.waitFor(); + reader.join(); + String errOutput = reader.getErrOutput(); + String stdOutput = reader.getOutput(); + logger.fine("mongo stdout >>> " + stdOutput); + logger.fine("mongo stderr >>> " + errOutput); + return exitStatus; } private boolean startStorage() throws MongodbUserSetupException { @@ -291,5 +305,83 @@ return failed; } } + + private static class ProcOutErrReader { + + private final Thread errReaderThread; + private final Thread outReaderThread; + private final ProcStreamReader outReader; + private final ProcStreamReader errReader; + + private ProcOutErrReader(Process process) { + outReader = new ProcStreamReader(process.getInputStream()); + errReader = new ProcStreamReader(process.getErrorStream()); + Runnable outRunnable = new Runnable() { + + @Override + public void run() { + outReader.readAll(); + } + }; + Runnable errRunnable = new Runnable() { + + @Override + public void run() { + errReader.readAll(); + } + + }; + errReaderThread = new Thread(errRunnable); + outReaderThread = new Thread(outRunnable); + } + + public void start() { + errReaderThread.start(); + outReaderThread.start(); + } + + public void join() { + try { + errReaderThread.join(); + } catch (InterruptedException e) { + // ignore + } + try { + outReaderThread.join(); + } catch (InterruptedException e) { + // ignore + } + } + + public String getOutput() { + return outReader.getOutput(); + } + + public String getErrOutput() { + return errReader.getOutput(); + } + } + + private static class ProcStreamReader { + + private final InputStream in; + private byte[] contents; + + private ProcStreamReader(InputStream in) { + this.in = in; + } + + public void readAll() { + try { + contents = StreamUtils.readAll(in); + } catch (IOException e) { + // ignore + } + } + + public String getOutput() { + return new String(contents); + } + } }