Allow to specify amqp passwords in etc/secure.config

Change-Id: Ic405afc3afea7acc4e5bde1cdadccbdfec47afdd
diff --git a/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/config/section/Gerrit.java b/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/config/section/Gerrit.java
index 5ad4a49..b400b76 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/config/section/Gerrit.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/config/section/Gerrit.java
@@ -24,6 +24,7 @@
 import org.eclipse.jgit.lib.Config;
 
 public class Gerrit implements Section {
+  private Config gerritConfig;
 
   @Default
   @MessageHeader("gerrit-name")
@@ -50,10 +51,15 @@
   @Default
   public String listenAs;
 
+  public String getAMQPUserPassword(String userName) {
+    return gerritConfig.getString("AMQP", userName, "password");
+  }
+
   @Inject
   public Gerrit(final @GerritServerConfig Config config) {
     this.canonicalWebUrl = config.getString("gerrit", null, "canonicalWebUrl");
     this.version = Version.getVersion();
+    this.gerritConfig = config;
   }
 
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/session/type/AMQPSession.java b/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/session/type/AMQPSession.java
index 1f9b7e3..6afec87 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/session/type/AMQPSession.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/rabbitmq/session/type/AMQPSession.java
@@ -17,6 +17,7 @@
 import com.googlesource.gerrit.plugins.rabbitmq.config.Properties;
 import com.googlesource.gerrit.plugins.rabbitmq.config.section.AMQP;
 import com.googlesource.gerrit.plugins.rabbitmq.config.section.Exchange;
+import com.googlesource.gerrit.plugins.rabbitmq.config.section.Gerrit;
 import com.googlesource.gerrit.plugins.rabbitmq.config.section.Message;
 import com.googlesource.gerrit.plugins.rabbitmq.config.section.Monitor;
 import com.googlesource.gerrit.plugins.rabbitmq.session.Session;
@@ -148,7 +149,11 @@
         if (StringUtils.isNotEmpty(amqp.username)) {
           factory.setUsername(amqp.username);
         }
-        if (StringUtils.isNotEmpty(amqp.password)) {
+        Gerrit gerrit = properties.getSection(Gerrit.class);
+        String securePassword = gerrit.getAMQPUserPassword(amqp.username);
+        if (StringUtils.isNotEmpty(securePassword)) {
+          factory.setPassword(securePassword);
+        } else if (StringUtils.isNotEmpty(amqp.password)) {
           factory.setPassword(amqp.password);
         }
         connection = factory.newConnection();
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index cda6fb5..f54918f 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -13,6 +13,18 @@
 If `rabbitmq.config` exists in `$site_path/data/rabbitmq`, it is loaded at first.
 It means that this is default for all config files.
 
+
+Secure.config
+---------------------
+
+If amqp.<username>.password is specified in `$site_path/etc/secure.config`. This
+password is used when trying to connect to amqp with username = <username>.
+
+```
+  [amqp "guest"]
+    password = guest
+```
+
 File format
 ---------------------