Commit 0a81c250 by nixonrodrigues

ATLAS-3129 Fix SSL Truststore reloader leak from AtlasBaseClient

Change-Id: I5af8c26a41a7010de645ddaa6869c3ce15723f43
parent 4d6169f5
...@@ -42,7 +42,6 @@ import org.apache.hadoop.hbase.HBaseConfiguration; ...@@ -42,7 +42,6 @@ import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor; import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HBaseAdmin;
...@@ -123,6 +122,7 @@ public class HBaseBridge { ...@@ -123,6 +122,7 @@ public class HBaseBridge {
public static void main(String[] args) { public static void main(String[] args) {
int exitCode = EXIT_CODE_FAILED; int exitCode = EXIT_CODE_FAILED;
AtlasClientV2 atlasClientV2 =null;
try { try {
Options options = new Options(); Options options = new Options();
...@@ -142,7 +142,6 @@ public class HBaseBridge { ...@@ -142,7 +142,6 @@ public class HBaseBridge {
urls = new String[] { DEFAULT_ATLAS_URL }; urls = new String[] { DEFAULT_ATLAS_URL };
} }
final AtlasClientV2 atlasClientV2;
if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) { if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) {
String[] basicAuthUsernamePassword = AuthenticationUtil.getBasicAuthenticationInput(); String[] basicAuthUsernamePassword = AuthenticationUtil.getBasicAuthenticationInput();
...@@ -195,6 +194,10 @@ public class HBaseBridge { ...@@ -195,6 +194,10 @@ public class HBaseBridge {
System.out.println("ImportHBaseEntities failed. Please check the log file for the detailed error message"); System.out.println("ImportHBaseEntities failed. Please check the log file for the detailed error message");
LOG.error("ImportHBaseEntities failed", e); LOG.error("ImportHBaseEntities failed", e);
}finally {
if(atlasClientV2!=null) {
atlasClientV2.close();
}
} }
System.exit(exitCode); System.exit(exitCode);
......
...@@ -103,6 +103,7 @@ public class HiveMetaStoreBridge { ...@@ -103,6 +103,7 @@ public class HiveMetaStoreBridge {
public static void main(String[] args) { public static void main(String[] args) {
int exitCode = EXIT_CODE_FAILED; int exitCode = EXIT_CODE_FAILED;
AtlasClientV2 atlasClientV2 = null;
try { try {
Options options = new Options(); Options options = new Options();
...@@ -123,7 +124,6 @@ public class HiveMetaStoreBridge { ...@@ -123,7 +124,6 @@ public class HiveMetaStoreBridge {
atlasEndpoint = new String[] { DEFAULT_ATLAS_URL }; atlasEndpoint = new String[] { DEFAULT_ATLAS_URL };
} }
final AtlasClientV2 atlasClientV2;
if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) { if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) {
String[] basicAuthUsernamePassword = AuthenticationUtil.getBasicAuthenticationInput(); String[] basicAuthUsernamePassword = AuthenticationUtil.getBasicAuthenticationInput();
...@@ -174,6 +174,10 @@ public class HiveMetaStoreBridge { ...@@ -174,6 +174,10 @@ public class HiveMetaStoreBridge {
printUsage(); printUsage();
} catch(Exception e) { } catch(Exception e) {
LOG.error("Import failed", e); LOG.error("Import failed", e);
} finally {
if( atlasClientV2 !=null) {
atlasClientV2.close();
}
} }
System.exit(exitCode); System.exit(exitCode);
......
...@@ -88,6 +88,7 @@ public class KafkaBridge { ...@@ -88,6 +88,7 @@ public class KafkaBridge {
public static void main(String[] args) { public static void main(String[] args) {
int exitCode = EXIT_CODE_FAILED; int exitCode = EXIT_CODE_FAILED;
AtlasClientV2 atlasClientV2 = null;
try { try {
Options options = new Options(); Options options = new Options();
...@@ -105,7 +106,6 @@ public class KafkaBridge { ...@@ -105,7 +106,6 @@ public class KafkaBridge {
urls = new String[] { DEFAULT_ATLAS_URL }; urls = new String[] { DEFAULT_ATLAS_URL };
} }
final AtlasClientV2 atlasClientV2;
if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) { if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) {
String[] basicAuthUsernamePassword = AuthenticationUtil.getBasicAuthenticationInput(); String[] basicAuthUsernamePassword = AuthenticationUtil.getBasicAuthenticationInput();
...@@ -148,6 +148,10 @@ public class KafkaBridge { ...@@ -148,6 +148,10 @@ public class KafkaBridge {
System.out.println("ImportKafkaEntities failed. Please check the log file for the detailed error message"); System.out.println("ImportKafkaEntities failed. Please check the log file for the detailed error message");
e.printStackTrace(); e.printStackTrace();
LOG.error("ImportKafkaEntities failed", e); LOG.error("ImportKafkaEntities failed", e);
} finally {
if (atlasClientV2 != null) {
atlasClientV2.close();
}
} }
System.exit(exitCode); System.exit(exitCode);
......
...@@ -114,6 +114,8 @@ public abstract class AtlasBaseClient { ...@@ -114,6 +114,8 @@ public abstract class AtlasBaseClient {
private boolean retryEnabled = false; private boolean retryEnabled = false;
private Cookie cookie = null; private Cookie cookie = null;
private SecureClientUtils clientUtils;
protected AtlasBaseClient() { protected AtlasBaseClient() {
} }
...@@ -282,14 +284,15 @@ public abstract class AtlasBaseClient { ...@@ -282,14 +284,15 @@ public abstract class AtlasBaseClient {
} }
final URLConnectionClientHandler handler; final URLConnectionClientHandler handler;
clientUtils = new SecureClientUtils();
boolean isKerberosEnabled = AuthenticationUtil.isKerberosAuthenticationEnabled(ugi); boolean isKerberosEnabled = AuthenticationUtil.isKerberosAuthenticationEnabled(ugi);
if (isKerberosEnabled) { if (isKerberosEnabled) {
handler = SecureClientUtils.getClientConnectionHandler(config, configuration, doAsUser, ugi); handler = clientUtils.getClientConnectionHandler(config, configuration, doAsUser, ugi);
} else { } else {
if (configuration.getBoolean(TLS_ENABLED, false)) { if (configuration.getBoolean(TLS_ENABLED, false)) {
handler = SecureClientUtils.getUrlConnectionClientHandler(); handler = clientUtils.getUrlConnectionClientHandler();
} else { } else {
handler = new URLConnectionClientHandler(); handler = new URLConnectionClientHandler();
} }
...@@ -300,6 +303,12 @@ public abstract class AtlasBaseClient { ...@@ -300,6 +303,12 @@ public abstract class AtlasBaseClient {
return client; return client;
} }
public void close() {
if (clientUtils != null) {
clientUtils.destroyFactory();
}
}
@VisibleForTesting @VisibleForTesting
protected String determineActiveServiceURL(String[] baseUrls, Client client) { protected String determineActiveServiceURL(String[] baseUrls, Client client) {
if (baseUrls.length == 0) { if (baseUrls.length == 0) {
......
...@@ -57,9 +57,10 @@ public class SecureClientUtils { ...@@ -57,9 +57,10 @@ public class SecureClientUtils {
public final static int DEFAULT_SOCKET_TIMEOUT_IN_MSECS = 1 * 60 * 1000; // 1 minute public final static int DEFAULT_SOCKET_TIMEOUT_IN_MSECS = 1 * 60 * 1000; // 1 minute
private static final Logger LOG = LoggerFactory.getLogger(SecureClientUtils.class); private static final Logger LOG = LoggerFactory.getLogger(SecureClientUtils.class);
private SSLFactory factory = null;
public static URLConnectionClientHandler getClientConnectionHandler(DefaultClientConfig config, public URLConnectionClientHandler getClientConnectionHandler(DefaultClientConfig config,
org.apache.commons.configuration.Configuration clientConfig, String doAsUser, org.apache.commons.configuration.Configuration clientConfig, String doAsUser,
final UserGroupInformation ugi) { final UserGroupInformation ugi) {
config.getProperties().put(URLConnectionClientHandler.PROPERTY_HTTP_URL_CONNECTION_SET_METHOD_WORKAROUND, true); config.getProperties().put(URLConnectionClientHandler.PROPERTY_HTTP_URL_CONNECTION_SET_METHOD_WORKAROUND, true);
...@@ -125,7 +126,7 @@ public class SecureClientUtils { ...@@ -125,7 +126,7 @@ public class SecureClientUtils {
} }
}; };
private static ConnectionConfigurator newConnConfigurator(Configuration conf) { private ConnectionConfigurator newConnConfigurator(Configuration conf) {
try { try {
return newSslConnConfigurator(DEFAULT_SOCKET_TIMEOUT_IN_MSECS, conf); return newSslConnConfigurator(DEFAULT_SOCKET_TIMEOUT_IN_MSECS, conf);
} catch (Exception e) { } catch (Exception e) {
...@@ -134,14 +135,12 @@ public class SecureClientUtils { ...@@ -134,14 +135,12 @@ public class SecureClientUtils {
} }
} }
private static ConnectionConfigurator newSslConnConfigurator(final int timeout, Configuration conf) private ConnectionConfigurator newSslConnConfigurator(final int timeout, Configuration conf)
throws IOException, GeneralSecurityException { throws IOException, GeneralSecurityException {
final SSLFactory factory;
final SSLSocketFactory sf; final SSLSocketFactory sf;
final HostnameVerifier hv; final HostnameVerifier hv;
factory = new SSLFactory(SSLFactory.Mode.CLIENT, conf); factory = getSSLFactory(conf);
factory.init();
sf = factory.createSSLSocketFactory(); sf = factory.createSSLSocketFactory();
hv = factory.getHostnameVerifier(); hv = factory.getHostnameVerifier();
...@@ -159,6 +158,22 @@ public class SecureClientUtils { ...@@ -159,6 +158,22 @@ public class SecureClientUtils {
}; };
} }
public SSLFactory getSSLFactory(Configuration conf) throws IOException, GeneralSecurityException {
if (factory == null) {
factory = new SSLFactory(SSLFactory.Mode.CLIENT, conf);
factory.init();
}
return factory;
}
public void destroyFactory() {
if (factory != null) {
factory.destroy();
factory = null;
}
}
private static void setTimeouts(URLConnection connection, int socketTimeout) { private static void setTimeouts(URLConnection connection, int socketTimeout) {
connection.setConnectTimeout(socketTimeout); connection.setConnectTimeout(socketTimeout);
connection.setReadTimeout(socketTimeout); connection.setReadTimeout(socketTimeout);
...@@ -210,7 +225,7 @@ public class SecureClientUtils { ...@@ -210,7 +225,7 @@ public class SecureClientUtils {
} }
} }
public static URLConnectionClientHandler getUrlConnectionClientHandler() { public URLConnectionClientHandler getUrlConnectionClientHandler() {
return new URLConnectionClientHandler(new HttpURLConnectionFactory() { return new URLConnectionClientHandler(new HttpURLConnectionFactory() {
@Override @Override
public HttpURLConnection getHttpURLConnection(URL url) public HttpURLConnection getHttpURLConnection(URL url)
...@@ -230,8 +245,7 @@ public class SecureClientUtils { ...@@ -230,8 +245,7 @@ public class SecureClientUtils {
UserGroupInformation.setConfiguration(conf); UserGroupInformation.setConfiguration(conf);
HttpsURLConnection c = (HttpsURLConnection) connection; HttpsURLConnection c = (HttpsURLConnection) connection;
factory = new SSLFactory(SSLFactory.Mode.CLIENT, conf); factory = getSSLFactory(conf);
factory.init();
sf = factory.createSSLSocketFactory(); sf = factory.createSSLSocketFactory();
hv = factory.getHostnameVerifier(); hv = factory.getHostnameVerifier();
c.setSSLSocketFactory(sf); c.setSSLSocketFactory(sf);
......
...@@ -77,22 +77,28 @@ public class QuickStart { ...@@ -77,22 +77,28 @@ public class QuickStart {
@VisibleForTesting @VisibleForTesting
static void runQuickstart(String[] args, String[] basicAuthUsernamePassword) throws Exception { static void runQuickstart(String[] args, String[] basicAuthUsernamePassword) throws Exception {
String[] urls = getServerUrl(args); String[] urls = getServerUrl(args);
QuickStart quickStart; QuickStart quickStart = null;
if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) { try {
quickStart = new QuickStart(urls, basicAuthUsernamePassword); if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) {
} else { quickStart = new QuickStart(urls, basicAuthUsernamePassword);
quickStart = new QuickStart(urls); } else {
} quickStart = new QuickStart(urls);
}
// Shows how to create types in Atlas for your meta model // Shows how to create types in Atlas for your meta model
quickStart.createTypes(); quickStart.createTypes();
// Shows how to create entities (instances) for the added types in Atlas // Shows how to create entities (instances) for the added types in Atlas
quickStart.createEntities(); quickStart.createEntities();
// Shows some search queries using DSL based on types // Shows some search queries using DSL based on types
quickStart.search(); quickStart.search();
} finally {
if(quickStart!=null) {
quickStart.closeConnection();
}
}
} }
static String[] getServerUrl(String[] args) throws AtlasException { static String[] getServerUrl(String[] args) throws AtlasException {
...@@ -492,4 +498,10 @@ public class QuickStart { ...@@ -492,4 +498,10 @@ public class QuickStart {
throw new AtlasBaseException(AtlasErrorCode.QUICK_START, e, "one or more dsl queries failed"); throw new AtlasBaseException(AtlasErrorCode.QUICK_START, e, "one or more dsl queries failed");
} }
} }
private void closeConnection() {
if (metadataServiceClient != null) {
metadataServiceClient.close();
}
}
} }
...@@ -151,25 +151,30 @@ public class QuickStartV2 { ...@@ -151,25 +151,30 @@ public class QuickStartV2 {
static void runQuickstart(String[] args, String[] basicAuthUsernamePassword) throws Exception { static void runQuickstart(String[] args, String[] basicAuthUsernamePassword) throws Exception {
String[] urls = getServerUrl(args); String[] urls = getServerUrl(args);
QuickStartV2 quickStartV2; QuickStartV2 quickStartV2 = null;
try {
if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) { if (!AuthenticationUtil.isKerberosAuthenticationEnabled()) {
quickStartV2 = new QuickStartV2(urls, basicAuthUsernamePassword); quickStartV2 = new QuickStartV2(urls, basicAuthUsernamePassword);
} else { } else {
quickStartV2 = new QuickStartV2(urls); quickStartV2 = new QuickStartV2(urls);
} }
// Shows how to create v2 types in Atlas for your meta model // Shows how to create v2 types in Atlas for your meta model
quickStartV2.createTypes(); quickStartV2.createTypes();
// Shows how to create v2 entities (instances) for the added types in Atlas // Shows how to create v2 entities (instances) for the added types in Atlas
quickStartV2.createEntities(); quickStartV2.createEntities();
// Shows some search queries using DSL based on types // Shows some search queries using DSL based on types
quickStartV2.search(); quickStartV2.search();
// Shows some lineage information on entity // Shows some lineage information on entity
quickStartV2.lineage(); quickStartV2.lineage();
} finally {
if (quickStartV2!= null) {
quickStartV2.closeConnection();
}
}
} }
...@@ -647,4 +652,10 @@ public class QuickStartV2 { ...@@ -647,4 +652,10 @@ public class QuickStartV2 {
return tableEntity.getGuid(); return tableEntity.getGuid();
} }
private void closeConnection() {
if (atlasClientV2 != null) {
atlasClientV2.close();
}
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment