Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -41,6 +42,7 @@
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientRequestContext;
Expand All @@ -64,6 +66,7 @@
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.security.ssl.SSLFactory;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
import org.apache.hadoop.yarn.api.records.ApplicationAttemptReport;
Expand Down Expand Up @@ -122,6 +125,7 @@ public class LogsCLI extends Configured implements Tool {
private PrintStream outStream = System.out;
private YarnClient yarnClient = null;
private Client webServiceClient = null;
private static SSLFactory sslFactory = null;

private static final int DEFAULT_MAX_RETRIES = 30;
private static final long DEFAULT_RETRY_INTERVAL = 1000;
Expand All @@ -147,6 +151,9 @@ public int run(String[] args) throws Exception {
if (webServiceClient != null) {
webServiceClient.close();
}
if (sslFactory != null) {
sslFactory.destroy();
}
}
}

Expand Down Expand Up @@ -428,6 +435,7 @@ public static void main(String[] args) throws Exception {
LogsCLI logDumper = new LogsCLI();
logDumper.setConf(conf);
WebServiceClient.initialize(conf);
sslFactory = WebServiceClient.getSSLFactory();
int exitCode = logDumper.run(args);
WebServiceClient.destroy();
System.exit(exitCode);
Expand Down Expand Up @@ -1551,18 +1559,29 @@ public void filter(ClientRequestContext requestContext) throws IOException {
}
}

private void checkUrlConnectivity(URI uri) throws IOException {
private void checkUrlConnectivity(URI uri) throws IOException, GeneralSecurityException {
URL url = uri.toURL();

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("HEAD");
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);

// The purpose of getting the `responseCode` here is to check if the service is online.
int responseCode = connection.getResponseCode();
if (responseCode >= 400) {
throw new IOException("URL connectivity check failed with HTTP code " + responseCode);
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) url.openConnection();
if (sslFactory != null) {
HttpsURLConnection httpsConn = (HttpsURLConnection) connection;
httpsConn.setSSLSocketFactory(sslFactory.createSSLSocketFactory());
httpsConn.setHostnameVerifier(sslFactory.getHostnameVerifier());
}
connection.setRequestMethod("HEAD");
connection.setConnectTimeout(TIME_OUT);
connection.setReadTimeout(TIME_OUT);

// The purpose of getting the `responseCode` here is to check if the service is online.
int responseCode = connection.getResponseCode();
if (responseCode >= 400) {
throw new IOException("URL connectivity check failed with HTTP code " + responseCode);
}
} finally {
if (connection != null) {
connection.disconnect();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public static WebServiceClient getWebServiceClient() {
return instance;
}

@VisibleForTesting
SSLFactory getSSLFactory() {
public static SSLFactory getSSLFactory() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to modify this part?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @slfan1989!
As I could see we can not reach the SSL factory from the Logs CLI without this

return sslFactory;
}

Expand Down
Loading