From 14f251fcd5f841a9ffcc2290e2357d7eb5c2ecc7 Mon Sep 17 00:00:00 2001 From: Junping Du Date: Thu, 30 Nov 2017 13:47:47 -0800 Subject: [PATCH] YARN-7558. yarn logs command fails to get logs for running containers if UI authentication is enabled. Contributed by Xuan Gong. (cherry picked from commit a409425986fc128bb54f656b05373201545f7213) --- .../hadoop/yarn/client/cli/LogsCLI.java | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/LogsCLI.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/LogsCLI.java index 0dc1e0467b5..11c3f04c268 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/LogsCLI.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/cli/LogsCLI.java @@ -18,13 +18,25 @@ package org.apache.hadoop.yarn.client.cli; +import com.google.common.annotations.VisibleForTesting; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientHandlerException; +import com.sun.jersey.api.client.ClientRequest; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.UniformInterfaceException; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.filter.ClientFilter; +import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory; +import com.sun.jersey.client.urlconnection.URLConnectionClientHandler; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.net.ConnectException; +import java.net.HttpURLConnection; import java.net.SocketException; import java.net.SocketTimeoutException; +import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -38,9 +50,7 @@ import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; import java.util.regex.Pattern; - import javax.ws.rs.core.MediaType; - import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.GnuParser; @@ -57,6 +67,8 @@ import org.apache.hadoop.classification.InterfaceStability.Evolving; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.security.UserGroupInformation; +import org.apache.hadoop.security.authentication.client.AuthenticatedURL; +import org.apache.hadoop.security.authentication.client.AuthenticationException; import org.apache.hadoop.util.Tool; import org.apache.hadoop.yarn.api.records.ApplicationAttemptReport; import org.apache.hadoop.yarn.api.records.ApplicationId; @@ -78,15 +90,6 @@ import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; -import com.google.common.annotations.VisibleForTesting; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientHandlerException; -import com.sun.jersey.api.client.ClientRequest; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.filter.ClientFilter; - @Public @Evolving public class LogsCLI extends Configured implements Tool { @@ -132,7 +135,21 @@ public class LogsCLI extends Configured implements Tool { public int run(String[] args) throws Exception { try { yarnClient = createYarnClient(); - webServiceClient = Client.create(); + webServiceClient = new Client(new URLConnectionClientHandler( + new HttpURLConnectionFactory() { + @Override + public HttpURLConnection getHttpURLConnection(URL url) + throws IOException { + AuthenticatedURL.Token token = new AuthenticatedURL.Token(); + HttpURLConnection conn = null; + try { + conn = new AuthenticatedURL().openConnection(url, token); + } catch (AuthenticationException e) { + throw new IOException(e); + } + return conn; + } + })); return runCommand(args); } finally { if (yarnClient != null) {