Parcourir la source

AMBARI-12225. Unable to initialize Slider View on secure cluster with Wire Encryption (Gour Saha via srimanth)

Srimanth Gunturi il y a 10 ans
Parent
commit
5dd1dbd453

+ 6 - 1
contrib/views/slider/pom.xml

@@ -126,6 +126,11 @@
       <artifactId>commons-io</artifactId>
       <artifactId>commons-io</artifactId>
       <version>${commons-io.version}</version>
       <version>${commons-io.version}</version>
     </dependency>
     </dependency>
+    <dependency>
+      <groupId>org.apache.ambari.contrib.views</groupId>
+      <artifactId>ambari-views-utils</artifactId>
+      <version>0.0.1-SNAPSHOT</version>
+    </dependency>
   </dependencies>
   </dependencies>
 
 
   <properties>
   <properties>
@@ -137,7 +142,7 @@
     <commons-lang.version>2.6</commons-lang.version>
     <commons-lang.version>2.6</commons-lang.version>
     <commons-compress.version>1.9</commons-compress.version>
     <commons-compress.version>1.9</commons-compress.version>
     <httpclient.version>4.2.5</httpclient.version>
     <httpclient.version>4.2.5</httpclient.version>
-    <slider.version>0.72.0-incubating-SNAPSHOT</slider.version>
+    <slider.version>0.81.0-incubating-SNAPSHOT</slider.version>
     <guice.version>3.0</guice.version>
     <guice.version>3.0</guice.version>
     <jackson.version>1.9.13</jackson.version>
     <jackson.version>1.9.13</jackson.version>
     <jersey.version>1.9</jersey.version>
     <jersey.version>1.9</jersey.version>

+ 3 - 3
contrib/views/slider/src/main/java/org/apache/ambari/view/slider/SliderAppsViewControllerImpl.java

@@ -136,7 +136,7 @@ public class SliderAppsViewControllerImpl implements SliderAppsViewController {
           clusterName = clusterName.substring(0, clusterName.length() - 1);
           clusterName = clusterName.substring(0, clusterName.length() - 1);
         }
         }
         AmbariHttpClient ambariClient = new AmbariHttpClient(ambariUrl,
         AmbariHttpClient ambariClient = new AmbariHttpClient(ambariUrl,
-            ambariUsername, ambariPassword);
+            ambariUsername, ambariPassword, viewContext);
         try {
         try {
           AmbariClusterInfo clusterInfo = ambariClient.getClusterInfo();
           AmbariClusterInfo clusterInfo = ambariClient.getClusterInfo();
           if (clusterInfo!=null && clusterName.equals(clusterInfo.getName())) {
           if (clusterInfo!=null && clusterName.equals(clusterInfo.getName())) {
@@ -292,7 +292,7 @@ public class SliderAppsViewControllerImpl implements SliderAppsViewController {
                     + clusterName + "] was not found on Ambari server"));
                     + clusterName + "] was not found on Ambari server"));
           }
           }
         } catch (Throwable e) {
         } catch (Throwable e) {
-          logger.warn("Exception determining view status", e);
+          logger.error("Exception determining view status", e);
           String message = e.getClass().getName() + ": " + e.getMessage();
           String message = e.getClass().getName() + ": " + e.getMessage();
           if (e instanceof RuntimeException && e.getCause() != null) {
           if (e instanceof RuntimeException && e.getCause() != null) {
             message = e.getCause().getClass().getName() + ": " + e.getMessage();
             message = e.getCause().getClass().getName() + ": " + e.getMessage();
@@ -659,7 +659,7 @@ public class SliderAppsViewControllerImpl implements SliderAppsViewController {
       }
       }
 
 
       SliderAppMasterClient sliderAppClient = yarnApp.getTrackingUrl() == null ? null
       SliderAppMasterClient sliderAppClient = yarnApp.getTrackingUrl() == null ? null
-          : new SliderAppMasterClient(yarnApp.getTrackingUrl());
+          : new SliderAppMasterClient(yarnApp.getTrackingUrl(), viewContext);
       SliderAppMasterData appMasterData = null;
       SliderAppMasterData appMasterData = null;
       Map<String, String> quickLinks = new HashMap<String, String>();
       Map<String, String> quickLinks = new HashMap<String, String>();
       Set<String> metrics = new HashSet<String>();
       Set<String> metrics = new HashSet<String>();

+ 4 - 2
contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/AmbariHttpClient.java

@@ -26,6 +26,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Map.Entry;
 
 
+import org.apache.ambari.view.ViewContext;
 import org.apache.ambari.view.slider.clients.AmbariClient;
 import org.apache.ambari.view.slider.clients.AmbariClient;
 import org.apache.ambari.view.slider.clients.AmbariCluster;
 import org.apache.ambari.view.slider.clients.AmbariCluster;
 import org.apache.ambari.view.slider.clients.AmbariClusterInfo;
 import org.apache.ambari.view.slider.clients.AmbariClusterInfo;
@@ -47,8 +48,9 @@ public class AmbariHttpClient extends BaseHttpClient implements AmbariClient {
 
 
 	private static final Logger logger = Logger.getLogger(AmbariHttpClient.class);
 	private static final Logger logger = Logger.getLogger(AmbariHttpClient.class);
 
 
-	public AmbariHttpClient(String url, String userId, String password) {
-		super(url, userId, password);
+	public AmbariHttpClient(String url, String userId, String password,
+			ViewContext viewContext) {
+		super(url, userId, password, viewContext);
 	}
 	}
 	
 	
     @SuppressWarnings("deprecation")
     @SuppressWarnings("deprecation")

+ 74 - 51
contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/BaseHttpClient.java

@@ -19,37 +19,64 @@
 package org.apache.ambari.view.slider.rest.client;
 package org.apache.ambari.view.slider.rest.client;
 
 
 import java.io.IOException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.InputStreamReader;
+import java.util.HashMap;
+import java.util.Map;
 
 
-import org.apache.commons.httpclient.HttpClient;
+import org.apache.ambari.view.URLStreamProvider;
+import org.apache.ambari.view.ViewContext;
+import org.apache.ambari.view.utils.ambari.AmbariApi;
+import org.apache.ambari.view.utils.ambari.URLStreamProviderBasicAuth;
 import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.HttpException;
-import org.apache.commons.httpclient.HttpStatus;
-import org.apache.commons.httpclient.UsernamePasswordCredentials;
-import org.apache.commons.httpclient.auth.AuthScope;
-import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.io.IOUtils;
+import org.apache.log4j.Logger;
 
 
 import com.google.gson.JsonElement;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonParser;
 import com.google.gson.JsonParser;
 import com.google.gson.stream.JsonReader;
 import com.google.gson.stream.JsonReader;
 
 
 public class BaseHttpClient {
 public class BaseHttpClient {
+	private static final Logger logger = Logger.getLogger(BaseHttpClient.class);
 
 
-	private HttpClient httpClient;
 	private String url;
 	private String url;
 	private boolean needsAuthentication;
 	private boolean needsAuthentication;
 	private String userId;
 	private String userId;
 	private String password;
 	private String password;
+	protected ViewContext viewContext;
+	protected AmbariApi ambariApi;
 
 
-	public BaseHttpClient(String url) {
+	public BaseHttpClient(String url, ViewContext viewContext) {
 		setUrl(url);
 		setUrl(url);
 		setNeedsAuthentication(false);
 		setNeedsAuthentication(false);
+		setViewContext(viewContext);
+		if (viewContext != null) {
+			ambariApi = new AmbariApi(viewContext);
+		}
 	}
 	}
 
 
-	public BaseHttpClient(String url, String userId, String password) {
+	public BaseHttpClient(String url, String userId, String password,
+			ViewContext viewContext) {
 		setUrl(url);
 		setUrl(url);
 		setNeedsAuthentication(true);
 		setNeedsAuthentication(true);
 		setUserId(userId);
 		setUserId(userId);
 		setPassword(password);
 		setPassword(password);
+		setViewContext(viewContext);
+		if (viewContext != null) {
+			ambariApi = new AmbariApi(viewContext);
+		}
+	}
+
+	public void setViewContext(ViewContext viewContext) {
+		this.viewContext = viewContext;
+	}
+
+	public URLStreamProvider getUrlStreamProvider() {
+		return viewContext.getURLStreamProvider();
+	}
+
+	public URLStreamProviderBasicAuth getUrlStreamProviderBasicAuth() {
+		return ambariApi.getUrlStreamProviderBasicAuth();
 	}
 	}
 
 
 	public String getUrl() {
 	public String getUrl() {
@@ -88,53 +115,49 @@ public class BaseHttpClient {
 		return doGetJson(getUrl(), path);
 		return doGetJson(getUrl(), path);
 	}
 	}
 
 
-	@SuppressWarnings("deprecation")
-    public JsonElement doGetJson(String url, String path) throws HttpException,
-	    IOException {
-		GetMethod get = new GetMethod(url + path);
-		if (isNeedsAuthentication()) {
-			get.setDoAuthentication(true);
+	public JsonElement doGetJson(String url, String path) throws HttpException,
+			IOException {
+		InputStream inputStream = null;
+		try {
+			Map<String, String> headers = new HashMap<String, String>();
+			if (isNeedsAuthentication()) {
+				inputStream = getUrlStreamProviderBasicAuth().readFrom(
+						url + path, "GET", (String) null, headers);
+			} else {
+				inputStream = getUrlStreamProviderBasicAuth().readAsCurrent(
+						url + path, "GET", (String) null, headers);
+			}
+		} catch (IOException e) {
+			logger.error("Error while reading from url " + url + path, e);
+			HttpException httpException = new HttpException(
+					e.getLocalizedMessage());
+			throw httpException;
 		}
 		}
-		int executeMethod = getHttpClient().executeMethod(get);
-        switch (executeMethod) {
-        case HttpStatus.SC_OK:
-          JsonElement jsonElement = new JsonParser().parse(new JsonReader(new InputStreamReader(get.getResponseBodyAsStream())));
-          return jsonElement;
-        case HttpStatus.SC_NOT_FOUND:
-          return null;
-        default:
-          HttpException httpException = new HttpException(get.getResponseBodyAsString());
-          httpException.setReason(HttpStatus.getStatusText(executeMethod));
-          httpException.setReasonCode(executeMethod);
-          throw httpException;
-        }
+		JsonElement jsonElement = new JsonParser().parse(new JsonReader(
+				new InputStreamReader(inputStream)));
+		return jsonElement;
 	}
 	}
 
 
 	public String doGet(String path) throws HttpException, IOException {
 	public String doGet(String path) throws HttpException, IOException {
-		GetMethod get = new GetMethod(url + path);
-		if (isNeedsAuthentication()) {
-			get.setDoAuthentication(true);
-		}
-		int executeMethod = getHttpClient().executeMethod(get);
-		switch (executeMethod) {
-		case HttpStatus.SC_OK:
-			return get.getResponseBodyAsString();
-		default:
-			break;
-		}
-		return null;
-	}
-
-	private HttpClient getHttpClient() {
-		if (httpClient == null) {
-			httpClient = new HttpClient();
-		}
-		if (isNeedsAuthentication()) {
-			httpClient.getState().setCredentials(
-			    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
-			    new UsernamePasswordCredentials(getUserId(), getPassword()));
-			httpClient.getParams().setAuthenticationPreemptive(true);
+		String response = null;
+		try {
+			InputStream inputStream = null;
+			if (isNeedsAuthentication()) {
+				inputStream = getUrlStreamProviderBasicAuth().readFrom(
+						getUrl() + path, "GET", (String) null,
+						new HashMap<String, String>());
+			} else {
+				inputStream = getUrlStreamProviderBasicAuth().readAsCurrent(
+						getUrl() + path, "GET", (String) null,
+						new HashMap<String, String>());
+			}
+			response = IOUtils.toString(inputStream);
+		} catch (IOException e) {
+			logger.error("Error while reading from url " + getUrl() + path, e);
+			HttpException httpException = new HttpException(
+					e.getLocalizedMessage());
+			throw httpException;
 		}
 		}
-		return httpClient;
+		return response;
 	}
 	}
 }
 }

+ 2 - 2
contrib/views/slider/src/main/java/org/apache/ambari/view/slider/rest/client/SliderAppMasterClient.java

@@ -46,8 +46,8 @@ public class SliderAppMasterClient extends BaseHttpClient {
   private static final Logger logger = Logger
   private static final Logger logger = Logger
       .getLogger(SliderAppMasterClient.class);
       .getLogger(SliderAppMasterClient.class);
 
 
-  public SliderAppMasterClient(String url) {
-    super(url);
+  public SliderAppMasterClient(String url, ViewContext viewContext) {
+    super(url, viewContext);
   }
   }
 
 
   public SliderAppMasterData getAppMasterData() {
   public SliderAppMasterData getAppMasterData() {

+ 1 - 1
contrib/views/slider/src/test/java/org/apache/ambari/view/slider/rest/client/SliderAppMasterClientTest.java

@@ -31,7 +31,7 @@ public class SliderAppMasterClientTest {
 
 
   @Test
   @Test
   public void testSliderClientClassAvailability() {
   public void testSliderClientClassAvailability() {
-    SliderAppMasterClient client = new SliderAppMasterClient("http://tmpurl.org");
+    SliderAppMasterClient client = new SliderAppMasterClient("http://tmpurl.org", null);
     Map<String, Metric> metrics = new HashMap<String, Metric>();
     Map<String, Metric> metrics = new HashMap<String, Metric>();
     Map<String, String> jmxProperties = new HashMap<String, String>();
     Map<String, String> jmxProperties = new HashMap<String, String>();
     Map<String, Map<String, Object>> categories = new HashMap<String, Map<String, Object>>();
     Map<String, Map<String, Object>> categories = new HashMap<String, Map<String, Object>>();