Search in sources :

Example 1 with URLConnectionFactory

use of org.apache.hadoop.hdfs.web.URLConnectionFactory in project hadoop by apache.

the class TestEditLogFileInputStream method testReadURL.

@Test
public void testReadURL() throws Exception {
    HttpURLConnection conn = mock(HttpURLConnection.class);
    doReturn(new ByteArrayInputStream(FAKE_LOG_DATA)).when(conn).getInputStream();
    doReturn(HttpURLConnection.HTTP_OK).when(conn).getResponseCode();
    doReturn(Integer.toString(FAKE_LOG_DATA.length)).when(conn).getHeaderField("Content-Length");
    URLConnectionFactory factory = mock(URLConnectionFactory.class);
    doReturn(conn).when(factory).openConnection(Mockito.<URL>any(), anyBoolean());
    URL url = new URL("http://localhost/fakeLog");
    EditLogInputStream elis = EditLogFileInputStream.fromUrl(factory, url, HdfsServerConstants.INVALID_TXID, HdfsServerConstants.INVALID_TXID, false);
    // Read the edit log and verify that we got all of the data.
    EnumMap<FSEditLogOpCodes, Holder<Integer>> counts = FSImageTestUtil.countEditLogOpTypes(elis);
    assertThat(counts.get(FSEditLogOpCodes.OP_ADD).held, is(1));
    assertThat(counts.get(FSEditLogOpCodes.OP_SET_GENSTAMP_V1).held, is(1));
    assertThat(counts.get(FSEditLogOpCodes.OP_CLOSE).held, is(1));
    // Check that length header was picked up.
    assertEquals(FAKE_LOG_DATA.length, elis.length());
    elis.close();
}
Also used : URLConnectionFactory(org.apache.hadoop.hdfs.web.URLConnectionFactory) HttpURLConnection(java.net.HttpURLConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) Holder(org.apache.hadoop.hdfs.util.Holder) URL(java.net.URL) Test(org.junit.Test)

Example 2 with URLConnectionFactory

use of org.apache.hadoop.hdfs.web.URLConnectionFactory in project incubator-atlas by apache.

the class AtlasAuthenticationKerberosFilterTest method testKerberosBasedLogin.

@Test(enabled = false)
public void testKerberosBasedLogin() throws Exception {
    String originalConf = System.getProperty("atlas.conf");
    setupKDCAndPrincipals();
    TestEmbeddedServer server = null;
    try {
        // setup the atlas-application.properties file
        String confDirectory = generateKerberosTestProperties();
        System.setProperty("atlas.conf", confDirectory);
        // need to create the web application programmatically in order to control the injection of the test
        // application properties
        server = new TestEmbeddedServer(23000, "webapp/target/apache-atlas");
        startEmbeddedServer(server.getServer());
        final URLConnectionFactory connectionFactory = URLConnectionFactory.DEFAULT_SYSTEM_CONNECTION_FACTORY;
        // attempt to hit server and get rejected
        URL url = new URL("http://localhost:23000/");
        HttpURLConnection connection = (HttpURLConnection) connectionFactory.openConnection(url, false);
        connection.setRequestMethod("GET");
        connection.connect();
        assertEquals(connection.getResponseCode(), 401);
        // need to populate the ticket cache with a local user, so logging in...
        Subject subject = loginTestUser();
        Subject.doAs(subject, new PrivilegedExceptionAction<Object>() {

            @Override
            public Object run() throws Exception {
                // attempt to hit server and get rejected
                URL url = new URL("http://localhost:23000/");
                HttpURLConnection connection = (HttpURLConnection) connectionFactory.openConnection(url, true);
                connection.setRequestMethod("GET");
                connection.connect();
                assertEquals(connection.getResponseCode(), 200);
                assertEquals(RequestContext.get().getUser(), TESTUSER);
                return null;
            }
        });
    } finally {
        server.getServer().stop();
        kdc.stop();
        if (originalConf != null) {
            System.setProperty("atlas.conf", originalConf);
        } else {
            System.clearProperty("atlas.conf");
        }
    }
}
Also used : URLConnectionFactory(org.apache.hadoop.hdfs.web.URLConnectionFactory) HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) Subject(javax.security.auth.Subject) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) BaseSecurityTest(org.apache.atlas.web.security.BaseSecurityTest) Test(org.testng.annotations.Test)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)2 URL (java.net.URL)2 URLConnectionFactory (org.apache.hadoop.hdfs.web.URLConnectionFactory)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Subject (javax.security.auth.Subject)1 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)1 LoginException (javax.security.auth.login.LoginException)1 BaseSecurityTest (org.apache.atlas.web.security.BaseSecurityTest)1 Holder (org.apache.hadoop.hdfs.util.Holder)1 Test (org.junit.Test)1 Test (org.testng.annotations.Test)1