Example usage for org.apache.hadoop.security.authentication.client KerberosAuthenticator KerberosAuthenticator

List of usage examples for org.apache.hadoop.security.authentication.client KerberosAuthenticator KerberosAuthenticator

Introduction

In this page you can find the example usage for org.apache.hadoop.security.authentication.client KerberosAuthenticator KerberosAuthenticator.

Prototype

KerberosAuthenticator

Source Link

Usage

From source file:org.apache.falcon.util.HadoopQueueUtil.java

License:Apache License

/**
 * Uses Resource Manager REST API to get the hadoop scheduler info.
 *
 * @param rmBaseUrlStr/*from  w w w .jav  a2  s. c o  m*/
 * @return JSON string representing hadoop Scheduler Info
 * @throws FalconException
 */

public static String getHadoopClusterSchedulerInfo(String rmBaseUrlStr) throws FalconException {
    KerberosAuthenticator kAUTHENTICATOR = new KerberosAuthenticator();
    AuthenticatedURL.Token authenticationToken = new AuthenticatedURL.Token();
    String rmSchedulerInfoURL = rmBaseUrlStr;
    if (!rmSchedulerInfoURL.endsWith("/")) {
        rmSchedulerInfoURL += "/";
    }
    rmSchedulerInfoURL += "ws/v1/cluster/scheduler";
    HttpURLConnection conn = null;
    BufferedReader reader = null;

    try {
        URL url = new URL(rmSchedulerInfoURL);
        conn = new AuthenticatedURL(kAUTHENTICATOR).openConnection(url, authenticationToken);
        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder jsonResponse = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonResponse.append(line);
        }
        return jsonResponse.toString();
    } catch (Exception ex) {
        throw new RuntimeException("Could not authenticate, " + ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(reader);
        if (conn != null) {
            conn.disconnect();
        }
    }

}

From source file:org.apache.sentry.api.service.thrift.TestSentryServiceMetrics.java

License:Apache License

@Test
public void testMetricsWeb() throws Exception {
    clientUgi.doAs(new PrivilegedExceptionAction<Void>() {
        @Override/*from www  .  j a va 2s .com*/
        public Void run() throws Exception {
            final URL url = new URL("http://" + SERVER_HOST + ":" + webServerPort + "/metrics");
            HttpURLConnection conn = new AuthenticatedURL(new KerberosAuthenticator()).openConnection(url,
                    new AuthenticatedURL.Token());
            //make sure we are able to access the metrics page
            Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
            String response = IOUtils.toString(conn.getInputStream());
            SentryWebMetricParser mp = new SentryWebMetricParser(response);
            Assert.assertEquals(Boolean.FALSE, mp.isHA());
            Assert.assertEquals(Boolean.TRUE, mp.isActive());
            return null;
        }
    });
}

From source file:org.apache.sentry.api.service.thrift.TestSentryWebServerWithKerberos.java

License:Apache License

@Test
public void testPing() throws Exception {
    SentryServiceIntegrationBase.clientUgi.doAs(new PrivilegedExceptionAction<Void>() {
        @Override/*www .j a  v a2s.  com*/
        public Void run() throws Exception {
            final URL url = new URL("http://" + SentryServiceIntegrationBase.SERVER_HOST + ":"
                    + SentryServiceIntegrationBase.webServerPort + "/ping");
            HttpURLConnection conn = new AuthenticatedURL(new KerberosAuthenticator()).openConnection(url,
                    new AuthenticatedURL.Token());
            Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
            String response = IOUtils.toString(conn.getInputStream());
            Assert.assertEquals("pong\n", response);
            return null;
        }
    });
}

From source file:org.apache.sentry.api.service.thrift.TestSentryWebServerWithKerberos.java

License:Apache License

@Test
public void testPingWithoutSubject() throws Exception {
    final URL url = new URL("http://" + SentryServiceIntegrationBase.SERVER_HOST + ":"
            + SentryServiceIntegrationBase.webServerPort + "/ping");
    try {/*w  w w  .  j  a  v a  2s  . c  om*/
        new AuthenticatedURL(new KerberosAuthenticator()).openConnection(url, new AuthenticatedURL.Token());
        fail("Here should fail.");
    } catch (Exception e) {
        boolean isExpectError = exceptionContainsMessage(e, "No valid credentials provided");
        Assert.assertTrue("Here should fail by 'No valid credentials provided'," + " but the exception is:" + e,
                isExpectError);
    }
}

From source file:org.apache.sentry.api.service.thrift.TestSentryWebServerWithKerberos.java

License:Apache License

@Test
public void testPingWithUnauthorizedUser() throws Exception {
    // create an unauthorized User with Kerberos
    String userPrinciple = "user/" + SentryServiceIntegrationBase.SERVER_HOST;
    String userKerberosName = userPrinciple + "@" + SentryServiceIntegrationBase.REALM;
    Subject userSubject = new Subject(false, Sets.newHashSet(new KerberosPrincipal(userKerberosName)),
            new HashSet<Object>(), new HashSet<Object>());
    File userKeytab = new File(SentryServiceIntegrationBase.kdcWorkDir, "user.keytab");
    SentryServiceIntegrationBase.kdc.createPrincipal(userKeytab, userPrinciple);
    LoginContext userLoginContext = new LoginContext("", userSubject, null,
            KerberosConfiguration.createClientConfig(userKerberosName, userKeytab));
    userLoginContext.login();//from   ww  w.ja  v a2 s  .com
    Subject.doAs(userLoginContext.getSubject(), new PrivilegedExceptionAction<Void>() {
        @Override
        public Void run() throws Exception {
            final URL url = new URL("http://" + SentryServiceIntegrationBase.SERVER_HOST + ":"
                    + SentryServiceIntegrationBase.webServerPort + "/ping");
            try {
                new AuthenticatedURL(new KerberosAuthenticator()).openConnection(url,
                        new AuthenticatedURL.Token());
                fail("Here should fail.");
            } catch (AuthenticationException e) {
                String expectedError = "status code: 403";
                if (!exceptionContainsMessage(e, expectedError)) {
                    LOG.error("UnexpectedError: " + e.getMessage(), e);
                    fail("UnexpectedError: " + e.getMessage());
                }
            }
            return null;
        }
    });
}

From source file:org.apache.sentry.api.service.thrift.TestSentryWebServerWithKerberos.java

License:Apache License

@Test
public void testPingWithCaseSensitiveUser() throws Exception {
    // USER1 is present in the list of users who are allowed to connect to sentry web ui.
    String userPrinciple = "user1/" + SentryServiceIntegrationBase.SERVER_HOST;
    String userKerberosName = userPrinciple + "@" + SentryServiceIntegrationBase.REALM;
    Subject userSubject = new Subject(false, Sets.newHashSet(new KerberosPrincipal(userKerberosName)),
            new HashSet<Object>(), new HashSet<Object>());
    File userKeytab = new File(SentryServiceIntegrationBase.kdcWorkDir, "user1.keytab");
    SentryServiceIntegrationBase.kdc.createPrincipal(userKeytab, userPrinciple);
    LoginContext userLoginContext = new LoginContext("", userSubject, null,
            KerberosConfiguration.createClientConfig(userKerberosName, userKeytab));
    userLoginContext.login();//www  . j ava  2  s . c  om
    Subject.doAs(userLoginContext.getSubject(), new PrivilegedExceptionAction<Void>() {
        @Override
        public Void run() throws Exception {
            final URL url = new URL("http://" + SentryServiceIntegrationBase.SERVER_HOST + ":"
                    + SentryServiceIntegrationBase.webServerPort + "/ping");
            try {
                new AuthenticatedURL(new KerberosAuthenticator()).openConnection(url,
                        new AuthenticatedURL.Token());
                fail("Login with user1 should fail");
            } catch (AuthenticationException e) {
                String expectedError = "status code: 403";
                if (!exceptionContainsMessage(e, expectedError)) {
                    LOG.error("UnexpectedError: " + e.getMessage(), e);
                    fail("UnexpectedError: " + e.getMessage());
                }
            }
            return null;
        }
    });
}

From source file:org.apache.sentry.provider.db.service.thrift.TestSentryWebServerWithKerberos.java

License:Apache License

@Test
public void testPing() throws Exception {
    clientUgi.doAs(new PrivilegedExceptionAction<Void>() {
        @Override/*from  ww w  .ja v  a  2 s.c o  m*/
        public Void run() throws Exception {
            final URL url = new URL("http://" + SERVER_HOST + ":" + webServerPort + "/ping");
            HttpURLConnection conn = new AuthenticatedURL(new KerberosAuthenticator()).openConnection(url,
                    new AuthenticatedURL.Token());
            Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
            String response = IOUtils.toString(conn.getInputStream());
            Assert.assertEquals("pong\n", response);
            return null;
        }
    });
}

From source file:org.apache.sentry.provider.db.service.thrift.TestSentryWebServerWithKerberos.java

License:Apache License

@Test
public void testPingWithoutSubject() throws Exception {
    final URL url = new URL("http://" + SERVER_HOST + ":" + webServerPort + "/ping");
    try {//from www.j  a  va2s  . c om
        new AuthenticatedURL(new KerberosAuthenticator()).openConnection(url, new AuthenticatedURL.Token());
        fail("Here should fail.");
    } catch (Exception e) {
        boolean isExpectError = e.getMessage().contains("No valid credentials provided");
        Assert.assertTrue("Here should fail by 'No valid credentials provided'," + " but the exception is:" + e,
                isExpectError);
    }
}

From source file:org.apache.sentry.provider.db.service.thrift.TestSentryWebServerWithKerberos.java

License:Apache License

@Test
public void testPingWithUnauthorizedUser() throws Exception {
    // create an unauthorized User with Kerberos
    String userPrinciple = "user/" + SERVER_HOST;
    String userKerberosName = userPrinciple + "@" + REALM;
    Subject userSubject = new Subject(false, Sets.newHashSet(new KerberosPrincipal(userKerberosName)),
            new HashSet<Object>(), new HashSet<Object>());
    File userKeytab = new File(kdcWorkDir, "user.keytab");
    kdc.createPrincipal(userKeytab, userPrinciple);
    LoginContext userLoginContext = new LoginContext("", userSubject, null,
            KerberosConfiguration.createClientConfig(userKerberosName, userKeytab));
    userLoginContext.login();// www .  ja  v a 2  s .  c om
    Subject.doAs(userLoginContext.getSubject(), new PrivilegedExceptionAction<Void>() {
        @Override
        public Void run() throws Exception {
            final URL url = new URL("http://" + SERVER_HOST + ":" + webServerPort + "/ping");
            try {
                new AuthenticatedURL(new KerberosAuthenticator()).openConnection(url,
                        new AuthenticatedURL.Token());
                fail("Here should fail.");
            } catch (AuthenticationException e) {
                String expectedError = "status code: 403";
                if (!e.getMessage().contains(expectedError)) {
                    LOG.error("UnexpectedError: " + e.getMessage(), e);
                    fail("UnexpectedError: " + e.getMessage());
                }
            }
            return null;
        }
    });
}

From source file:org.apache.sentry.provider.db.service.thrift.TestSentryWebServerWithKerberos.java

License:Apache License

@Test
public void testPingWithCaseSensitiveUser() throws Exception {
    // USER1 is present in the list of users who are allowed to connect to sentry web ui.
    String userPrinciple = "user1/" + SERVER_HOST;
    String userKerberosName = userPrinciple + "@" + REALM;
    Subject userSubject = new Subject(false, Sets.newHashSet(new KerberosPrincipal(userKerberosName)),
            new HashSet<Object>(), new HashSet<Object>());
    File userKeytab = new File(kdcWorkDir, "user1.keytab");
    kdc.createPrincipal(userKeytab, userPrinciple);
    LoginContext userLoginContext = new LoginContext("", userSubject, null,
            KerberosConfiguration.createClientConfig(userKerberosName, userKeytab));
    userLoginContext.login();//from w w  w  . j av a2  s . c  o m
    Subject.doAs(userLoginContext.getSubject(), new PrivilegedExceptionAction<Void>() {
        @Override
        public Void run() throws Exception {
            final URL url = new URL("http://" + SERVER_HOST + ":" + webServerPort + "/ping");
            try {
                new AuthenticatedURL(new KerberosAuthenticator()).openConnection(url,
                        new AuthenticatedURL.Token());
                fail("Login with user1 should fail");
            } catch (AuthenticationException e) {
                String expectedError = "status code: 403";
                if (!e.getMessage().contains(expectedError)) {
                    LOG.error("UnexpectedError: " + e.getMessage(), e);
                    fail("UnexpectedError: " + e.getMessage());
                }
            }
            return null;
        }
    });
}