Search in sources :

Example 26 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class TestRMWebappAuthentication method testSimpleAuth.

// ensure that in a non-secure cluster users can access
// the web pages as earlier and submit apps as anonymous
// user or by identifying themselves
@Test
public void testSimpleAuth() throws Exception {
    rm.start();
    // ensure users can access web pages
    // this should work for secure and non-secure clusters
    URL url = new URL("http://localhost:8088/cluster");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    try {
        conn.getInputStream();
        assertEquals(Status.OK.getStatusCode(), conn.getResponseCode());
    } catch (Exception e) {
        fail("Fetching url failed");
    }
    if (UserGroupInformation.isSecurityEnabled()) {
        testAnonymousKerberosUser();
    } else {
        testAnonymousSimpleUser();
    }
    rm.stop();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) IOException(java.io.IOException) Test(org.junit.Test)

Example 27 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class TestRMWebappAuthentication method testAnonymousKerberosUser.

private void testAnonymousKerberosUser() throws Exception {
    ApplicationSubmissionContextInfo app = new ApplicationSubmissionContextInfo();
    String appid = "application_123_0";
    app.setApplicationId(appid);
    String requestBody = TestRMWebServicesDelegationTokenAuthentication.getMarshalledAppInfo(app);
    URL url = new URL("http://localhost:8088/ws/v1/cluster/apps/new-application");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    TestRMWebServicesDelegationTokenAuthentication.setupConn(conn, "POST", "application/xml", requestBody);
    try {
        conn.getInputStream();
        fail("Anonymous users should not be allowed to get new application ids in secure mode.");
    } catch (IOException ie) {
        assertEquals(Status.FORBIDDEN.getStatusCode(), conn.getResponseCode());
    }
    url = new URL("http://localhost:8088/ws/v1/cluster/apps");
    conn = (HttpURLConnection) url.openConnection();
    TestRMWebServicesDelegationTokenAuthentication.setupConn(conn, "POST", "application/xml", requestBody);
    try {
        conn.getInputStream();
        fail("Anonymous users should not be allowed to submit apps in secure mode.");
    } catch (IOException ie) {
        assertEquals(Status.FORBIDDEN.getStatusCode(), conn.getResponseCode());
    }
    requestBody = "{ \"state\": \"KILLED\"}";
    url = new URL("http://localhost:8088/ws/v1/cluster/apps/application_123_0/state");
    conn = (HttpURLConnection) url.openConnection();
    TestRMWebServicesDelegationTokenAuthentication.setupConn(conn, "PUT", "application/json", requestBody);
    try {
        conn.getInputStream();
        fail("Anonymous users should not be allowed to kill apps in secure mode.");
    } catch (IOException ie) {
        assertEquals(Status.FORBIDDEN.getStatusCode(), conn.getResponseCode());
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) IOException(java.io.IOException) ApplicationSubmissionContextInfo(org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.ApplicationSubmissionContextInfo) URL(java.net.URL)

Example 28 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class TestWebAppProxyServlet method testWebAppProxyServlet.

@Test(timeout = 5000)
public void testWebAppProxyServlet() throws Exception {
    configuration.set(YarnConfiguration.PROXY_ADDRESS, "localhost:9090");
    // overriding num of web server threads, see HttpServer.HTTP_MAXTHREADS
    configuration.setInt("hadoop.http.max.threads", 10);
    WebAppProxyServerForTest proxy = new WebAppProxyServerForTest();
    proxy.init(configuration);
    proxy.start();
    int proxyPort = proxy.proxy.proxyServer.getConnectorAddress(0).getPort();
    AppReportFetcherForTest appReportFetcher = proxy.proxy.appReportFetcher;
    // wrong url
    try {
        // wrong url without app ID
        URL emptyUrl = new URL("http://localhost:" + proxyPort + "/proxy");
        HttpURLConnection emptyProxyConn = (HttpURLConnection) emptyUrl.openConnection();
        emptyProxyConn.connect();
        assertEquals(HttpURLConnection.HTTP_NOT_FOUND, emptyProxyConn.getResponseCode());
        // wrong url. Set wrong app ID
        URL wrongUrl = new URL("http://localhost:" + proxyPort + "/proxy/app");
        HttpURLConnection proxyConn = (HttpURLConnection) wrongUrl.openConnection();
        proxyConn.connect();
        assertEquals(HttpURLConnection.HTTP_INTERNAL_ERROR, proxyConn.getResponseCode());
        // set true Application ID in url
        URL url = new URL("http://localhost:" + proxyPort + "/proxy/application_00_0");
        proxyConn = (HttpURLConnection) url.openConnection();
        // set cookie
        proxyConn.setRequestProperty("Cookie", "checked_application_0_0000=true");
        proxyConn.connect();
        assertEquals(HttpURLConnection.HTTP_OK, proxyConn.getResponseCode());
        assertTrue(isResponseCookiePresent(proxyConn, "checked_application_0_0000", "true"));
        // test that redirection is squashed correctly
        URL redirectUrl = new URL("http://localhost:" + proxyPort + "/proxy/redirect/application_00_0");
        proxyConn = (HttpURLConnection) redirectUrl.openConnection();
        proxyConn.setInstanceFollowRedirects(false);
        proxyConn.connect();
        assertEquals("The proxy returned an unexpected status code rather than" + "redirecting the connection (302)", HttpURLConnection.HTTP_MOVED_TEMP, proxyConn.getResponseCode());
        String expected = WebAppUtils.getResolvedRMWebAppURLWithScheme(configuration) + "/cluster/failure/application_00_0";
        String redirect = proxyConn.getHeaderField(ProxyUtils.LOCATION);
        assertEquals("The proxy did not redirect the connection to the failure " + "page of the RM", expected, redirect);
        // cannot found application 1: null
        appReportFetcher.answer = 1;
        proxyConn = (HttpURLConnection) url.openConnection();
        proxyConn.setRequestProperty("Cookie", "checked_application_0_0000=true");
        proxyConn.connect();
        assertEquals(HttpURLConnection.HTTP_NOT_FOUND, proxyConn.getResponseCode());
        assertFalse(isResponseCookiePresent(proxyConn, "checked_application_0_0000", "true"));
        // cannot found application 2: ApplicationNotFoundException
        appReportFetcher.answer = 4;
        proxyConn = (HttpURLConnection) url.openConnection();
        proxyConn.setRequestProperty("Cookie", "checked_application_0_0000=true");
        proxyConn.connect();
        assertEquals(HttpURLConnection.HTTP_NOT_FOUND, proxyConn.getResponseCode());
        assertFalse(isResponseCookiePresent(proxyConn, "checked_application_0_0000", "true"));
        // wrong user
        appReportFetcher.answer = 2;
        proxyConn = (HttpURLConnection) url.openConnection();
        proxyConn.connect();
        assertEquals(HttpURLConnection.HTTP_OK, proxyConn.getResponseCode());
        String s = readInputStream(proxyConn.getInputStream());
        assertTrue(s.contains("to continue to an Application Master web interface owned by"));
        assertTrue(s.contains("WARNING: The following page may not be safe!"));
        //case if task has a not running status
        appReportFetcher.answer = 3;
        proxyConn = (HttpURLConnection) url.openConnection();
        proxyConn.setRequestProperty("Cookie", "checked_application_0_0000=true");
        proxyConn.connect();
        assertEquals(HttpURLConnection.HTTP_OK, proxyConn.getResponseCode());
        // test user-provided path and query parameter can be appended to the
        // original tracking url
        appReportFetcher.answer = 5;
        URL clientUrl = new URL("http://localhost:" + proxyPort + "/proxy/application_00_0/test/tez?x=y&h=p");
        proxyConn = (HttpURLConnection) clientUrl.openConnection();
        proxyConn.connect();
        LOG.info("" + proxyConn.getURL());
        LOG.info("ProxyConn.getHeaderField(): " + proxyConn.getHeaderField(ProxyUtils.LOCATION));
        assertEquals("http://localhost:" + originalPort + "/foo/bar/test/tez?a=b&x=y&h=p#main", proxyConn.getURL().toString());
    } finally {
        proxy.close();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) Test(org.junit.Test)

Example 29 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class TestWebAppProxyServlet method testWebAppProxyPassThroughHeaders.

@Test(timeout = 5000)
public void testWebAppProxyPassThroughHeaders() throws Exception {
    Configuration configuration = new Configuration();
    configuration.set(YarnConfiguration.PROXY_ADDRESS, "localhost:9091");
    configuration.setInt("hadoop.http.max.threads", 10);
    WebAppProxyServerForTest proxy = new WebAppProxyServerForTest();
    proxy.init(configuration);
    proxy.start();
    int proxyPort = proxy.proxy.proxyServer.getConnectorAddress(0).getPort();
    try {
        URL url = new URL("http://localhost:" + proxyPort + "/proxy/application_00_1");
        HttpURLConnection proxyConn = (HttpURLConnection) url.openConnection();
        // set headers
        proxyConn.addRequestProperty("Origin", "http://www.someurl.com");
        proxyConn.addRequestProperty("Access-Control-Request-Method", "GET");
        proxyConn.addRequestProperty("Access-Control-Request-Headers", "Authorization");
        proxyConn.addRequestProperty(UNKNOWN_HEADER, "unknown");
        // Verify if four headers mentioned above have been added
        assertEquals(proxyConn.getRequestProperties().size(), 4);
        proxyConn.connect();
        assertEquals(HttpURLConnection.HTTP_OK, proxyConn.getResponseCode());
        // Verify if number of headers received by end server is 8.
        // Eight headers include Accept, Host, Connection, User-Agent, Cookie,
        // Origin, Access-Control-Request-Method and
        // Access-Control-Request-Headers. Pls note that Unknown-Header is dropped
        // by proxy as it is not in the list of allowed headers.
        assertEquals(numberOfHeaders, 8);
        assertFalse(hasUnknownHeader);
    } finally {
        proxy.close();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) Configuration(org.apache.hadoop.conf.Configuration) URL(java.net.URL) Test(org.junit.Test)

Example 30 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class TestWebAppProxyServlet method testAppReportForEmptyTrackingUrl.

@Test(timeout = 5000)
public void testAppReportForEmptyTrackingUrl() throws Exception {
    configuration.set(YarnConfiguration.PROXY_ADDRESS, "localhost:9090");
    // overriding num of web server threads, see HttpServer.HTTP_MAXTHREADS
    configuration.setInt("hadoop.http.max.threads", 10);
    WebAppProxyServerForTest proxy = new WebAppProxyServerForTest();
    proxy.init(configuration);
    proxy.start();
    int proxyPort = proxy.proxy.proxyServer.getConnectorAddress(0).getPort();
    AppReportFetcherForTest appReportFetcher = proxy.proxy.appReportFetcher;
    try {
        //set AHS_ENBALED = false to simulate getting the app report from RM
        configuration.setBoolean(YarnConfiguration.APPLICATION_HISTORY_ENABLED, false);
        ApplicationId app = ApplicationId.newInstance(0, 0);
        appReportFetcher.answer = 6;
        URL url = new URL("http://localhost:" + proxyPort + "/proxy/" + app.toString());
        HttpURLConnection proxyConn = (HttpURLConnection) url.openConnection();
        proxyConn.connect();
        try {
            proxyConn.getResponseCode();
        } catch (ConnectException e) {
        // Connection Exception is expected as we have set
        // appReportFetcher.answer = 6, which does not set anything for
        // original tracking url field in the app report.
        }
        String appAddressInRm = WebAppUtils.getResolvedRMWebAppURLWithScheme(configuration) + "/cluster" + "/app/" + app.toString();
        assertTrue("Webapp proxy servlet should have redirected to RM", proxyConn.getURL().toString().equals(appAddressInRm));
        //set AHS_ENBALED = true to simulate getting the app report from AHS
        configuration.setBoolean(YarnConfiguration.APPLICATION_HISTORY_ENABLED, true);
        proxyConn = (HttpURLConnection) url.openConnection();
        proxyConn.connect();
        try {
            proxyConn.getResponseCode();
        } catch (ConnectException e) {
        // Connection Exception is expected as we have set
        // appReportFetcher.answer = 6, which does not set anything for
        // original tracking url field in the app report.
        }
        String appAddressInAhs = WebAppUtils.getHttpSchemePrefix(configuration) + WebAppUtils.getAHSWebAppURLWithoutScheme(configuration) + "/applicationhistory" + "/app/" + app.toString();
        assertTrue("Webapp proxy servlet should have redirected to AHS", proxyConn.getURL().toString().equals(appAddressInAhs));
    } finally {
        proxy.close();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) URL(java.net.URL) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)3831 URL (java.net.URL)2447 IOException (java.io.IOException)1634 InputStream (java.io.InputStream)1082 InputStreamReader (java.io.InputStreamReader)692 Test (org.junit.Test)650 BufferedReader (java.io.BufferedReader)573 OutputStream (java.io.OutputStream)466 MalformedURLException (java.net.MalformedURLException)372 URLConnection (java.net.URLConnection)248 HashMap (java.util.HashMap)216 OutputStreamWriter (java.io.OutputStreamWriter)208 Map (java.util.Map)199 Gson (com.google.gson.Gson)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)186 ArrayList (java.util.ArrayList)168 ExecutionException (java.util.concurrent.ExecutionException)161 File (java.io.File)159 AsyncTask (android.os.AsyncTask)158 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)157