You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by tb...@apache.org on 2015/06/19 02:14:35 UTC
ambari git commit: AMBARI-12006 - Provide meaningful error message
for URLStreamProvider (tbeerbower)
Repository: ambari
Updated Branches:
refs/heads/trunk f0559128d -> cf842a05a
AMBARI-12006 - Provide meaningful error message for URLStreamProvider (tbeerbower)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/cf842a05
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/cf842a05
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/cf842a05
Branch: refs/heads/trunk
Commit: cf842a05a7de4e6755e2aaef03eb36052495a824
Parents: f055912
Author: tbeerbower <tb...@hortonworks.com>
Authored: Thu Jun 18 20:13:53 2015 -0400
Committer: tbeerbower <tb...@hortonworks.com>
Committed: Thu Jun 18 20:14:24 2015 -0400
----------------------------------------------------------------------
.../controller/internal/URLStreamProvider.java | 50 ++++++++++++--------
.../AmbariManagementControllerTest.java | 7 ++-
.../internal/URLStreamProviderTest.java | 27 +++++++++++
3 files changed, 60 insertions(+), 24 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/cf842a05/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/URLStreamProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/URLStreamProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/URLStreamProvider.java
index 1a8b085..94940e6 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/URLStreamProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/URLStreamProvider.java
@@ -54,9 +54,9 @@ public class URLStreamProvider implements StreamProvider {
private final int connTimeout;
private final int readTimeout;
- private final String path;
- private final String password;
- private final String type;
+ private final String trustStorePath;
+ private final String trustStorePassword;
+ private final String trustStoreType;
private volatile SSLSocketFactory sslSocketFactory = null;
private AppCookieManager appCookieManager = null;
@@ -83,19 +83,20 @@ public class URLStreamProvider implements StreamProvider {
/**
* Provide the connection timeout for the underlying connection.
*
- * @param connectionTimeout
- * time, in milliseconds, to attempt a connection
- * @param readTimeout
- * the read timeout in milliseconds
+ * @param connectionTimeout time, in milliseconds, to attempt a connection
+ * @param readTimeout the read timeout in milliseconds
+ * @param trustStorePath the path to the truststore required for secure connections
+ * @param trustStorePassword the truststore password
+ * @param trustStoreType the truststore type (e.g. "JKS")
*/
- public URLStreamProvider(int connectionTimeout, int readTimeout, String path,
- String password, String type) {
-
- this.connTimeout = connectionTimeout;
- this.readTimeout = readTimeout;
- this.path = path; // truststroe path
- this.password = password; // truststore password
- this.type = type; // truststroe type
+ public URLStreamProvider(int connectionTimeout, int readTimeout, String trustStorePath,
+ String trustStorePassword, String trustStoreType) {
+
+ this.connTimeout = connectionTimeout;
+ this.readTimeout = readTimeout;
+ this.trustStorePath = trustStorePath;
+ this.trustStorePassword = trustStorePassword;
+ this.trustStoreType = trustStoreType;
}
@@ -271,17 +272,26 @@ public class URLStreamProvider implements StreamProvider {
}
// Get an ssl connection
- protected HttpsURLConnection getSSLConnection(String spec) throws IOException {
+ protected HttpsURLConnection getSSLConnection(String spec) throws IOException, IllegalStateException {
if (sslSocketFactory == null) {
synchronized (this) {
if (sslSocketFactory == null) {
+
+ if (trustStorePath == null || trustStorePassword == null) {
+ String msg =
+ String.format("Can't get secure connection to %s. Truststore path or password is not set.", spec);
+
+ LOG.error(msg);
+ throw new IllegalStateException(msg);
+ }
+
try {
- FileInputStream in = new FileInputStream(new File(path));
- KeyStore store = KeyStore.getInstance(type == null ? KeyStore
- .getDefaultType() : type);
+ FileInputStream in = new FileInputStream(new File(trustStorePath));
+ KeyStore store = KeyStore.getInstance(trustStoreType == null ?
+ KeyStore.getDefaultType() : trustStoreType);
- store.load(in, password.toCharArray());
+ store.load(in, trustStorePassword.toCharArray());
in.close();
TrustManagerFactory tmf = TrustManagerFactory
http://git-wip-us.apache.org/repos/asf/ambari/blob/cf842a05/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
index 4e18c7f..5ee2016 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerTest.java
@@ -8366,10 +8366,9 @@ public class AmbariManagementControllerTest {
// test bad url
try {
controller.updateRepositories(requests);
- } catch (Exception e) {
- System.err.println(e.getMessage());
- assertTrue(e.getMessage().contains(IOException.class.getName())
- || e.getMessage().contains("Could not access base url"));
+ fail("Expected IllegalStateException");
+ } catch (IllegalStateException e) {
+ //expected
}
requests.clear();
http://git-wip-us.apache.org/repos/asf/ambari/blob/cf842a05/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/URLStreamProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/URLStreamProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/URLStreamProviderTest.java
index 7268ff2..4c04438 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/URLStreamProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/URLStreamProviderTest.java
@@ -21,6 +21,7 @@ package org.apache.ambari.server.controller.internal;
import org.junit.Assert;
import org.junit.Test;
+import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Collections;
import java.util.HashMap;
@@ -74,6 +75,32 @@ public class URLStreamProviderTest {
}
@Test
+ public void testProcessURL_securityNotSetup() throws Exception {
+
+ URLStreamProvider urlStreamProvider = createMockBuilder(URLStreamProvider.class).
+ withConstructor(Integer.TYPE, Integer.TYPE, String.class, String.class, String.class).
+ withArgs(1000, 1000, null, null, null).
+ addMockedMethod("getAppCookieManager").
+ addMockedMethod("getConnection", String.class).
+ createMock();
+
+ Map<String, List<String>> headerMap = new HashMap<String, List<String>>();
+ headerMap.put("Header1", Collections.singletonList("value"));
+ headerMap.put("Cookie", Collections.singletonList("FOO=bar"));
+
+ replay(urlStreamProvider);
+
+ try {
+ urlStreamProvider.processURL("https://spec", "GET", (String) null, headerMap);
+ Assert.fail("Expected IllegalStateException");
+ } catch (IllegalStateException e) {
+ // expected
+ }
+
+ verify(urlStreamProvider);
+ }
+
+ @Test
public void testAppendCookie() throws Exception {
Assert.assertEquals("newCookie", URLStreamProvider.appendCookie(null, "newCookie"));
Assert.assertEquals("newCookie", URLStreamProvider.appendCookie("", "newCookie"));