Search in sources :

Example 6 with AuthenticationToken

use of org.apache.hadoop.security.authentication.server.AuthenticationToken in project hadoop by apache.

the class TestHttpServerWithSpengo method getEncryptedAuthToken.

private AuthenticatedURL.Token getEncryptedAuthToken(Signer signer, String user) throws Exception {
    AuthenticationToken token = new AuthenticationToken(user, user, "kerberos");
    token.setExpires(System.currentTimeMillis() + TIMEOUT);
    return new AuthenticatedURL.Token(signer.sign(token.toString()));
}
Also used : AuthenticationToken(org.apache.hadoop.security.authentication.server.AuthenticationToken) AuthenticationToken(org.apache.hadoop.security.authentication.server.AuthenticationToken)

Example 7 with AuthenticationToken

use of org.apache.hadoop.security.authentication.server.AuthenticationToken in project hadoop by apache.

the class TestDelegationTokenAuthenticationHandlerWithMocks method testWriterNotClosed.

@Test
public void testWriterNotClosed() throws Exception {
    Properties conf = new Properties();
    conf.put(KerberosDelegationTokenAuthenticationHandler.TOKEN_KIND, "foo");
    conf.put(DelegationTokenAuthenticationHandler.JSON_MAPPER_PREFIX + "AUTO_CLOSE_TARGET", "false");
    DelegationTokenAuthenticationHandler noAuthCloseHandler = new MockDelegationTokenAuthenticationHandler();
    try {
        noAuthCloseHandler.initTokenManager(conf);
        noAuthCloseHandler.initJsonFactory(conf);
        DelegationTokenAuthenticator.DelegationTokenOperation op = GETDELEGATIONTOKEN;
        HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
        HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
        Mockito.when(request.getQueryString()).thenReturn(DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString());
        Mockito.when(request.getMethod()).thenReturn(op.getHttpMethod());
        AuthenticationToken token = Mockito.mock(AuthenticationToken.class);
        Mockito.when(token.getUserName()).thenReturn("user");
        final MutableBoolean closed = new MutableBoolean();
        PrintWriter printWriterCloseCount = new PrintWriter(new StringWriter()) {

            @Override
            public void close() {
                closed.setValue(true);
                super.close();
            }

            @Override
            public void write(String str) {
                if (closed.booleanValue()) {
                    throw new RuntimeException("already closed!");
                }
                super.write(str);
            }
        };
        Mockito.when(response.getWriter()).thenReturn(printWriterCloseCount);
        Assert.assertFalse(noAuthCloseHandler.managementOperation(token, request, response));
    } finally {
        noAuthCloseHandler.destroy();
    }
}
Also used : AuthenticationToken(org.apache.hadoop.security.authentication.server.AuthenticationToken) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean) HttpServletResponse(javax.servlet.http.HttpServletResponse) Properties(java.util.Properties) HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) DelegationTokenOperation(org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticator.DelegationTokenOperation) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 8 with AuthenticationToken

use of org.apache.hadoop.security.authentication.server.AuthenticationToken in project hadoop by apache.

the class TestDelegationTokenAuthenticationHandlerWithMocks method getToken.

private String getToken() throws Exception {
    DelegationTokenAuthenticator.DelegationTokenOperation op = DelegationTokenAuthenticator.DelegationTokenOperation.GETDELEGATIONTOKEN;
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    Mockito.when(request.getQueryString()).thenReturn(DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString());
    Mockito.when(request.getMethod()).thenReturn(op.getHttpMethod());
    AuthenticationToken token = Mockito.mock(AuthenticationToken.class);
    Mockito.when(token.getUserName()).thenReturn("user");
    Mockito.when(response.getWriter()).thenReturn(new PrintWriter(new StringWriter()));
    Assert.assertFalse(handler.managementOperation(token, request, response));
    Mockito.when(request.getQueryString()).thenReturn(DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString() + "&" + DelegationTokenAuthenticator.RENEWER_PARAM + "=" + null);
    Mockito.reset(response);
    Mockito.reset(token);
    Mockito.when(token.getUserName()).thenReturn("user");
    StringWriter writer = new StringWriter();
    PrintWriter pwriter = new PrintWriter(writer);
    Mockito.when(response.getWriter()).thenReturn(pwriter);
    Assert.assertFalse(handler.managementOperation(token, request, response));
    Mockito.verify(token).getUserName();
    Mockito.verify(response).setStatus(HttpServletResponse.SC_OK);
    Mockito.verify(response).setContentType(MediaType.APPLICATION_JSON);
    pwriter.close();
    String responseOutput = writer.toString();
    String tokenLabel = DelegationTokenAuthenticator.DELEGATION_TOKEN_JSON;
    Assert.assertTrue(responseOutput.contains(tokenLabel));
    Assert.assertTrue(responseOutput.contains(DelegationTokenAuthenticator.DELEGATION_TOKEN_URL_STRING_JSON));
    ObjectMapper jsonMapper = new ObjectMapper();
    Map json = jsonMapper.readValue(responseOutput, Map.class);
    json = (Map) json.get(tokenLabel);
    String tokenStr;
    tokenStr = (String) json.get(DelegationTokenAuthenticator.DELEGATION_TOKEN_URL_STRING_JSON);
    Token<DelegationTokenIdentifier> dt = new Token<DelegationTokenIdentifier>();
    dt.decodeFromUrlString(tokenStr);
    handler.getTokenManager().verifyToken(dt);
    return tokenStr;
}
Also used : AuthenticationToken(org.apache.hadoop.security.authentication.server.AuthenticationToken) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthenticationToken(org.apache.hadoop.security.authentication.server.AuthenticationToken) Token(org.apache.hadoop.security.token.Token) HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) DelegationTokenOperation(org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticator.DelegationTokenOperation) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PrintWriter(java.io.PrintWriter)

Example 9 with AuthenticationToken

use of org.apache.hadoop.security.authentication.server.AuthenticationToken in project hadoop by apache.

the class TestDelegationTokenAuthenticationHandlerWithMocks method testValidDelegationTokenQueryString.

@SuppressWarnings("unchecked")
private void testValidDelegationTokenQueryString() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    Token<DelegationTokenIdentifier> dToken = (Token<DelegationTokenIdentifier>) handler.getTokenManager().createToken(UserGroupInformation.getCurrentUser(), "user");
    Mockito.when(request.getQueryString()).thenReturn(DelegationTokenAuthenticator.DELEGATION_PARAM + "=" + dToken.encodeToUrlString());
    AuthenticationToken token = handler.authenticate(request, response);
    Assert.assertEquals(UserGroupInformation.getCurrentUser().getShortUserName(), token.getUserName());
    Assert.assertEquals(0, token.getExpires());
    Assert.assertEquals(handler.getType(), token.getType());
    Assert.assertTrue(token.isExpired());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AuthenticationToken(org.apache.hadoop.security.authentication.server.AuthenticationToken) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthenticationToken(org.apache.hadoop.security.authentication.server.AuthenticationToken) Token(org.apache.hadoop.security.token.Token)

Example 10 with AuthenticationToken

use of org.apache.hadoop.security.authentication.server.AuthenticationToken in project hadoop by apache.

the class TestDelegationTokenAuthenticationHandlerWithMocks method testGetToken.

private void testGetToken(String renewer, Text expectedTokenKind) throws Exception {
    DelegationTokenAuthenticator.DelegationTokenOperation op = DelegationTokenAuthenticator.DelegationTokenOperation.GETDELEGATIONTOKEN;
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    Mockito.when(request.getQueryString()).thenReturn(DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString());
    Mockito.when(request.getMethod()).thenReturn(op.getHttpMethod());
    AuthenticationToken token = Mockito.mock(AuthenticationToken.class);
    Mockito.when(token.getUserName()).thenReturn("user");
    Mockito.when(response.getWriter()).thenReturn(new PrintWriter(new StringWriter()));
    Assert.assertFalse(handler.managementOperation(token, request, response));
    Mockito.when(request.getQueryString()).thenReturn(DelegationTokenAuthenticator.OP_PARAM + "=" + op.toString() + "&" + DelegationTokenAuthenticator.RENEWER_PARAM + "=" + renewer);
    Mockito.reset(response);
    Mockito.reset(token);
    Mockito.when(token.getUserName()).thenReturn("user");
    StringWriter writer = new StringWriter();
    PrintWriter pwriter = new PrintWriter(writer);
    Mockito.when(response.getWriter()).thenReturn(pwriter);
    Assert.assertFalse(handler.managementOperation(token, request, response));
    if (renewer == null) {
        Mockito.verify(token).getUserName();
    } else {
        Mockito.verify(token).getUserName();
    }
    Mockito.verify(response).setStatus(HttpServletResponse.SC_OK);
    Mockito.verify(response).setContentType(MediaType.APPLICATION_JSON);
    pwriter.close();
    String responseOutput = writer.toString();
    String tokenLabel = DelegationTokenAuthenticator.DELEGATION_TOKEN_JSON;
    Assert.assertTrue(responseOutput.contains(tokenLabel));
    Assert.assertTrue(responseOutput.contains(DelegationTokenAuthenticator.DELEGATION_TOKEN_URL_STRING_JSON));
    ObjectMapper jsonMapper = new ObjectMapper();
    Map json = jsonMapper.readValue(responseOutput, Map.class);
    json = (Map) json.get(tokenLabel);
    String tokenStr;
    tokenStr = (String) json.get(DelegationTokenAuthenticator.DELEGATION_TOKEN_URL_STRING_JSON);
    Token<DelegationTokenIdentifier> dt = new Token<DelegationTokenIdentifier>();
    dt.decodeFromUrlString(tokenStr);
    handler.getTokenManager().verifyToken(dt);
    Assert.assertEquals(expectedTokenKind, dt.getKind());
}
Also used : AuthenticationToken(org.apache.hadoop.security.authentication.server.AuthenticationToken) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthenticationToken(org.apache.hadoop.security.authentication.server.AuthenticationToken) Token(org.apache.hadoop.security.token.Token) HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) DelegationTokenOperation(org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticator.DelegationTokenOperation) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PrintWriter(java.io.PrintWriter)

Aggregations

AuthenticationToken (org.apache.hadoop.security.authentication.server.AuthenticationToken)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 Token (org.apache.hadoop.security.token.Token)7 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4 AuthenticationException (org.apache.hadoop.security.authentication.client.AuthenticationException)4 DelegationTokenOperation (org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticator.DelegationTokenOperation)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Map (java.util.Map)3 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)3 Principal (java.security.Principal)2 Properties (java.util.Properties)2 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)2 AuthenticationHandler (org.apache.hadoop.security.authentication.server.AuthenticationHandler)2 KerberosAuthenticationHandler (org.apache.hadoop.security.authentication.server.KerberosAuthenticationHandler)2 AuthorizationException (org.apache.hadoop.security.authorize.AuthorizationException)2 AbstractDelegationTokenIdentifier (org.apache.hadoop.security.token.delegation.AbstractDelegationTokenIdentifier)2 Test (org.junit.Test)2 AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)2