ViewURLStreamProvider implements org.apache.ambari.view.URLStreamProvider, URLConnectionProvider { protected boolean isProxyCallAllowed(String spec) { if (StringUtils.isNotBlank(spec) && getHostPortRestrictionHandler().proxyCallRestricted()) { try { URL url = new URL(spec); return getHostPortRestrictionHandler().allowProxy(url.getHost(), Integer.toString(url.getPort() == -1 ? url.getDefaultPort() : url.getPort())); } catch (MalformedURLException ignored) { } } return true; } protected  ViewURLStreamProvider(ViewContext viewContext, URLStreamProvider streamProvider); @Override InputStream readFrom(String spec, String requestMethod, String body, Map<String, String> headers); @Override InputStream readFrom(String spec, String requestMethod, InputStream body, Map<String, String> headers); @Override InputStream readAs(String spec, String requestMethod, String body, Map<String, String> headers,
                            String userName); @Override InputStream readAs(String spec, String requestMethod, InputStream body, Map<String, String> headers,
                            String userName); @Override InputStream readAsCurrent(String spec, String requestMethod, String body, Map<String, String> headers); @Override InputStream readAsCurrent(String spec, String requestMethod, InputStream body, Map<String, String> headers); @Override HttpURLConnection getConnection(String spec,
                                         String requestMethod,
                                         String body,
                                         Map<String, String> headers); @Override HttpURLConnection getConnection(String spec,
                                         String requestMethod,
                                         InputStream body,
                                         Map<String, String> headers); @Override HttpURLConnection getConnectionAs(String spec,
                                           String requestMethod,
                                           String body,
                                           Map<String, String> headers,
                                           String userName); @Override HttpURLConnection getConnectionAs(String spec,
                                           String requestMethod,
                                           InputStream body,
                                           Map<String, String> headers,
                                           String userName); @Override HttpURLConnection getConnectionAsCurrent(String spec,
                                                  String requestMethod,
                                                  String body,
                                                  Map<String, String> headers); @Override HttpURLConnection getConnectionAsCurrent(String spec,
                                                  String requestMethod,
                                                  InputStream body,
                                                  Map<String, String> headers);  }
@Test public void testProxyRestriction() throws Exception { ViewURLStreamProvider viewURLStreamProvider = new ViewURLStreamProvider(null, null); Properties ambariProperties = new Properties(); Configuration configuration = new Configuration(ambariProperties); Assert.assertEquals( Configuration.PROXY_ALLOWED_HOST_PORTS.getDefaultValue(), configuration.getProxyHostAndPorts()); ViewURLStreamProvider.HostPortRestrictionHandler hprh = viewURLStreamProvider.new HostPortRestrictionHandler( configuration.getProxyHostAndPorts()); Assert.assertFalse(hprh.proxyCallRestricted()); Assert.assertTrue(hprh.allowProxy("host1.com", null)); Assert.assertTrue(hprh.allowProxy(null, null)); Assert.assertTrue(hprh.allowProxy("host1.com", " ")); Assert.assertTrue(hprh.allowProxy("host1.com ", " ")); Assert.assertTrue(hprh.allowProxy(" host1.com ", "8080")); ambariProperties = new Properties(); ambariProperties.setProperty(Configuration.PROXY_ALLOWED_HOST_PORTS.getKey(), ""); configuration = new Configuration(ambariProperties); hprh = viewURLStreamProvider.new HostPortRestrictionHandler( configuration.getProxyHostAndPorts()); Assert.assertFalse(hprh.proxyCallRestricted()); Assert.assertTrue(hprh.allowProxy("host1.com", null)); Assert.assertTrue(hprh.allowProxy(null, null)); Assert.assertTrue(hprh.allowProxy("host1.com", " ")); Assert.assertTrue(hprh.allowProxy("host1.com ", " ")); Assert.assertTrue(hprh.allowProxy(" host1.com ", "8080")); ambariProperties = new Properties(); ambariProperties.setProperty(Configuration.PROXY_ALLOWED_HOST_PORTS.getKey(), "host1.com:*"); configuration = new Configuration(ambariProperties); hprh = viewURLStreamProvider.new HostPortRestrictionHandler( configuration.getProxyHostAndPorts()); Assert.assertTrue(hprh.proxyCallRestricted()); Assert.assertTrue(hprh.allowProxy("host1.com", null)); Assert.assertTrue(hprh.allowProxy(null, null)); Assert.assertTrue(hprh.allowProxy("host1.com", "20")); Assert.assertFalse(hprh.allowProxy("host2.com ", " ")); Assert.assertFalse(hprh.allowProxy(" host2.com ", "8080")); ambariProperties = new Properties(); ambariProperties.setProperty(Configuration.PROXY_ALLOWED_HOST_PORTS.getKey(), " host1.com:80 ,host2.org:443, host2.org:22"); configuration = new Configuration(ambariProperties); hprh = viewURLStreamProvider.new HostPortRestrictionHandler( configuration.getProxyHostAndPorts()); Assert.assertTrue(hprh.proxyCallRestricted()); Assert.assertTrue(hprh.allowProxy("host1.com", "80")); Assert.assertFalse(hprh.allowProxy("host1.com", "20")); Assert.assertFalse(hprh.allowProxy("host2.org", "404")); Assert.assertFalse(hprh.allowProxy("host2.com", "22")); ViewContext viewContext = createNiceMock(ViewContext.class); expect(viewContext.getAmbariProperty(anyObject(String.class))).andReturn( " host1.com:80 ,host2.org:443, host2.org:22"); replay(viewContext); viewURLStreamProvider = new ViewURLStreamProvider(viewContext, null); Assert.assertTrue(viewURLStreamProvider.isProxyCallAllowed("http: Assert.assertTrue(viewURLStreamProvider.isProxyCallAllowed("https: Assert.assertFalse(viewURLStreamProvider.isProxyCallAllowed("https: viewContext = createNiceMock(ViewContext.class); expect(viewContext.getAmbariProperty(anyObject(String.class))).andReturn("c6401.ambari.apache.org:8088"); replay(viewContext); viewURLStreamProvider = new ViewURLStreamProvider(viewContext, null); Assert.assertTrue(viewURLStreamProvider.isProxyCallAllowed( "http: viewContext = createNiceMock(ViewContext.class); expect(viewContext.getAmbariProperty(anyObject(String.class))).andReturn("*:8088"); replay(viewContext); viewURLStreamProvider = new ViewURLStreamProvider(viewContext, null); Assert.assertFalse(viewURLStreamProvider.isProxyCallAllowed( "http: viewContext = createNiceMock(ViewContext.class); expect(viewContext.getAmbariProperty(anyObject(String.class))).andReturn("c6401.ambari.apache.org:*"); replay(viewContext); viewURLStreamProvider = new ViewURLStreamProvider(viewContext, null); Assert.assertTrue(viewURLStreamProvider.isProxyCallAllowed( "http: viewContext = createNiceMock(ViewContext.class); expect(viewContext.getAmbariProperty(anyObject(String.class))).andReturn( "c6401.ambari.apache.org:80,c6401.ambari.apache.org:443"); replay(viewContext); viewURLStreamProvider = new ViewURLStreamProvider(viewContext, null); Assert.assertTrue(viewURLStreamProvider.isProxyCallAllowed( "http: Assert.assertTrue(viewURLStreamProvider.isProxyCallAllowed( "https: }