Repository: ambari
Updated Branches:
  refs/heads/trunk 31bbefadf -> c74ec1d5c


AMBARI-5960.  Add support for auth proxy (ncole)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/c74ec1d5
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/c74ec1d5
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/c74ec1d5

Branch: refs/heads/trunk
Commit: c74ec1d5c9f0aabeae493b7125ec54a059249cc9
Parents: 31bbefa
Author: Nate Cole <[email protected]>
Authored: Wed Jun 4 10:57:13 2014 -0400
Committer: Nate Cole <[email protected]>
Committed: Wed Jun 4 11:40:31 2014 -0400

----------------------------------------------------------------------
 .../ambari/server/controller/AmbariServer.java  | 35 +++++++++++++++-
 .../server/controller/AmbariServerTest.java     | 44 ++++++++++++++++----
 2 files changed, 68 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/c74ec1d5/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
index 0527db1..c561ed2 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariServer.java
@@ -20,12 +20,13 @@ package org.apache.ambari.server.controller;
 
 
 import java.io.File;
+import java.net.Authenticator;
 import java.net.BindException;
+import java.net.PasswordAuthentication;
 import java.util.Map;
 
 import javax.crypto.BadPaddingException;
 
-import com.google.inject.name.Named;
 import org.apache.ambari.eventdb.webservice.WorkflowJsonService;
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.actionmanager.ActionManager;
@@ -97,6 +98,7 @@ import com.google.inject.Guice;
 import com.google.inject.Inject;
 import com.google.inject.Injector;
 import com.google.inject.Singleton;
+import com.google.inject.name.Named;
 import com.google.inject.persist.Transactional;
 import com.sun.jersey.spi.container.servlet.ServletContainer;
 
@@ -490,7 +492,7 @@ public class AmbariServer {
 
     LOG.info("DB store version is compatible");
   }
-
+  
   public void stop() throws Exception {
     try {
       server.stop();
@@ -523,6 +525,32 @@ public class AmbariServer {
     ClusterResourceProvider.init(injector.getInstance(BlueprintDAO.class), 
ambariMetaInfo);
     ViewRegistry.init(injector.getInstance(ViewDAO.class), 
injector.getInstance(ViewInstanceDAO.class));
   }
+  
+  /**
+   * Sets up proxy authentication.  This must be done before the server is
+   * initialized since <code>AmbariMetaInfo</code> requires potential URL
+   * lookups that may need the proxy.
+   */
+  static void setupProxyAuth() {
+    final String proxyUser = System.getProperty("http.proxyUser");
+    final String proxyPass = System.getProperty("http.proxyPassword");
+    
+    // to skip some hosts from proxy, pipe-separate names using, i.e.:
+    // -Dhttp.nonProxyHosts=*.domain.com|host.internal.net
+    
+    if (null != proxyUser && null != proxyPass) {
+      LOG.info("Proxy authentication enabled");
+      
+      Authenticator.setDefault(new Authenticator() {
+        @Override
+        protected PasswordAuthentication getPasswordAuthentication() {
+          return new PasswordAuthentication(proxyUser, 
proxyPass.toCharArray());
+        }
+      });
+    } else {
+      LOG.debug("Proxy authentication not specified");
+    }
+  }  
 
   public static void main(String[] args) throws Exception {
     Injector injector = Guice.createInjector(new ControllerModule());
@@ -530,6 +558,9 @@ public class AmbariServer {
     AmbariServer server = null;
     try {
       LOG.info("Getting the controller");
+
+      setupProxyAuth();
+      
       injector.getInstance(GuiceJpaInitializer.class);
       server = injector.getInstance(AmbariServer.class);
       CertificateManager certMan = 
injector.getInstance(CertificateManager.class);

http://git-wip-us.apache.org/repos/asf/ambari/blob/c74ec1d5/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariServerTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariServerTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariServerTest.java
index ea2062a..f5911f0 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariServerTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariServerTest.java
@@ -18,10 +18,15 @@
 
 package org.apache.ambari.server.controller;
 
-import com.google.inject.Guice;
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-import com.google.inject.persist.PersistService;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.junit.Assert.fail;
+
+import java.net.Authenticator;
+import java.net.InetAddress;
+import java.net.PasswordAuthentication;
+
 import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.api.services.AmbariMetaInfo;
 import org.apache.ambari.server.configuration.Configuration;
@@ -29,15 +34,16 @@ import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.dao.MetainfoDAO;
 import org.apache.ambari.server.orm.entities.MetainfoEntity;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import org.junit.After;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import static org.easymock.EasyMock.*;
-import static org.junit.Assert.fail;
+import com.google.inject.Guice;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
 
 public class AmbariServerTest {
 
@@ -94,6 +100,26 @@ public class AmbariServerTest {
       // Expected
     }
   }
+  
+  @Test
+  public void testProxyUser() throws Exception {
+    
+    PasswordAuthentication pa = Authenticator.requestPasswordAuthentication(
+        InetAddress.getLocalHost(), 80, null, null, null);
+    Assert.assertNull(pa);
+    
+    System.setProperty("http.proxyUser", "abc");
+    System.setProperty("http.proxyPassword", "def");
+    
+    AmbariServer.setupProxyAuth();
+
+    pa = Authenticator.requestPasswordAuthentication(
+        InetAddress.getLocalHost(), 80, null, null, null);
+    Assert.assertNotNull(pa);
+    Assert.assertEquals("abc", pa.getUserName());
+    Assert.assertArrayEquals("def".toCharArray(), pa.getPassword());
+
+  }
 
 
 }

Reply via email to