Search in sources :

Example 6 with Principal

use of java.security.Principal in project hadoop by apache.

the class TestMDCFilter method mdc.

@Test
public void mdc() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Mockito.when(request.getUserPrincipal()).thenReturn(null);
    Mockito.when(request.getMethod()).thenReturn("METHOD");
    Mockito.when(request.getPathInfo()).thenReturn("/pathinfo");
    ServletResponse response = Mockito.mock(ServletResponse.class);
    final AtomicBoolean invoked = new AtomicBoolean();
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertEquals(MDC.get("hostname"), null);
            assertEquals(MDC.get("user"), null);
            assertEquals(MDC.get("method"), "METHOD");
            assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
        }
    };
    MDC.clear();
    Filter filter = new MDCFilter();
    filter.init(null);
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    assertNull(MDC.get("hostname"));
    assertNull(MDC.get("user"));
    assertNull(MDC.get("method"));
    assertNull(MDC.get("path"));
    Mockito.when(request.getUserPrincipal()).thenReturn(new Principal() {

        @Override
        public String getName() {
            return "name";
        }
    });
    invoked.set(false);
    chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertEquals(MDC.get("hostname"), null);
            assertEquals(MDC.get("user"), "name");
            assertEquals(MDC.get("method"), "METHOD");
            assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
        }
    };
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    HostnameFilter.HOSTNAME_TL.set("HOST");
    invoked.set(false);
    chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertEquals(MDC.get("hostname"), "HOST");
            assertEquals(MDC.get("user"), "name");
            assertEquals(MDC.get("method"), "METHOD");
            assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
        }
    };
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    HostnameFilter.HOSTNAME_TL.remove();
    filter.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletResponse(javax.servlet.ServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) IOException(java.io.IOException) Principal(java.security.Principal) Test(org.junit.Test)

Example 7 with Principal

use of java.security.Principal in project hadoop by apache.

the class MDCFilter method doFilter.

/**
   * Sets the slf4j <code>MDC</code> and delegates the request to the chain.
   *
   * @param request servlet request.
   * @param response servlet response.
   * @param chain filter chain.
   *
   * @throws IOException thrown if an IO error occurs.
   * @throws ServletException thrown if a servlet error occurs.
   */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    try {
        MDC.clear();
        String hostname = HostnameFilter.get();
        if (hostname != null) {
            MDC.put("hostname", HostnameFilter.get());
        }
        Principal principal = ((HttpServletRequest) request).getUserPrincipal();
        String user = (principal != null) ? principal.getName() : null;
        if (user != null) {
            MDC.put("user", user);
        }
        MDC.put("method", ((HttpServletRequest) request).getMethod());
        if (((HttpServletRequest) request).getPathInfo() != null) {
            MDC.put("path", ((HttpServletRequest) request).getPathInfo());
        }
        chain.doFilter(request, response);
    } finally {
        MDC.clear();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Principal(java.security.Principal)

Example 8 with Principal

use of java.security.Principal in project hadoop by apache.

the class TestWebDelegationToken method doAsKerberosUser.

public static <T> T doAsKerberosUser(String principal, String keytab, final Callable<T> callable) throws Exception {
    LoginContext loginContext = null;
    try {
        Set<Principal> principals = new HashSet<Principal>();
        principals.add(new KerberosPrincipal(principal));
        Subject subject = new Subject(false, principals, new HashSet<Object>(), new HashSet<Object>());
        loginContext = new LoginContext("", subject, null, new KerberosConfiguration(principal, keytab));
        loginContext.login();
        subject = loginContext.getSubject();
        return Subject.doAs(subject, new PrivilegedExceptionAction<T>() {

            @Override
            public T run() throws Exception {
                return callable.call();
            }
        });
    } catch (PrivilegedActionException ex) {
        throw ex.getException();
    } finally {
        if (loginContext != null) {
            loginContext.logout();
        }
    }
}
Also used : KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) PrivilegedActionException(java.security.PrivilegedActionException) Subject(javax.security.auth.Subject) AuthenticationException(org.apache.hadoop.security.authentication.client.AuthenticationException) ServletException(javax.servlet.ServletException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) LoginContext(javax.security.auth.login.LoginContext) KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) Principal(java.security.Principal) HashSet(java.util.HashSet)

Example 9 with Principal

use of java.security.Principal in project hadoop by apache.

the class RMWebServices method getCallerUserGroupInformation.

private UserGroupInformation getCallerUserGroupInformation(HttpServletRequest hsr, boolean usePrincipal) {
    String remoteUser = hsr.getRemoteUser();
    if (usePrincipal) {
        Principal princ = hsr.getUserPrincipal();
        remoteUser = princ == null ? null : princ.getName();
    }
    UserGroupInformation callerUGI = null;
    if (remoteUser != null) {
        callerUGI = UserGroupInformation.createRemoteUser(remoteUser);
    }
    return callerUGI;
}
Also used : Principal(java.security.Principal) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation)

Example 10 with Principal

use of java.security.Principal in project hadoop by apache.

the class TestSecureLogins method createLoginContextZookeeperLocalhost.

public LoginContext createLoginContextZookeeperLocalhost() throws LoginException {
    String principalAndRealm = getPrincipalAndRealm(ZOOKEEPER_LOCALHOST);
    Set<Principal> principals = new HashSet<Principal>();
    principals.add(new KerberosPrincipal(ZOOKEEPER_LOCALHOST));
    Subject subject = new Subject(false, principals, new HashSet<Object>(), new HashSet<Object>());
    return new LoginContext("", subject, null, KerberosConfiguration.createServerConfig(ZOOKEEPER_LOCALHOST, keytab_zk));
}
Also used : KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) LoginContext(javax.security.auth.login.LoginContext) KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) Principal(java.security.Principal) Subject(javax.security.auth.Subject) HashSet(java.util.HashSet)

Aggregations

Principal (java.security.Principal)931 Test (org.junit.Test)243 Subject (javax.security.auth.Subject)114 EveryonePrincipal (org.apache.jackrabbit.oak.spi.security.principal.EveryonePrincipal)114 HashSet (java.util.HashSet)89 User (org.apache.jackrabbit.api.security.user.User)75 Group (org.apache.jackrabbit.api.security.user.Group)74 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)58 Privilege (javax.jcr.security.Privilege)57 RepositoryException (javax.jcr.RepositoryException)51 IOException (java.io.IOException)50 ArrayList (java.util.ArrayList)48 HttpServletRequest (javax.servlet.http.HttpServletRequest)47 TestPrincipal (org.apache.jackrabbit.core.security.TestPrincipal)45 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)43 EveryonePrincipal (org.apache.jackrabbit.core.security.principal.EveryonePrincipal)42 PrincipalIterator (org.apache.jackrabbit.api.security.principal.PrincipalIterator)40 HashMap (java.util.HashMap)39 PrincipalImpl (org.apache.jackrabbit.oak.spi.security.principal.PrincipalImpl)39 X500Principal (javax.security.auth.x500.X500Principal)38