View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.security.token;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.IOException;
24  
25  import org.apache.hadoop.hbase.HConstants;
26  import org.apache.hadoop.hbase.TableName;
27  import org.apache.hadoop.hbase.client.Connection;
28  import org.apache.hadoop.hbase.client.ConnectionFactory;
29  import org.apache.hadoop.hbase.client.Table;
30  import org.apache.hadoop.hbase.ipc.AsyncRpcClient;
31  import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel;
32  import org.apache.hadoop.hbase.ipc.RpcClient;
33  import org.apache.hadoop.hbase.ipc.RpcClientFactory;
34  import org.apache.hadoop.hbase.ipc.RpcClientImpl;
35  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
36  import org.apache.hadoop.hbase.protobuf.generated.AuthenticationProtos;
37  import org.apache.hadoop.hbase.protobuf.generated.AuthenticationProtos.GetAuthenticationTokenRequest;
38  import org.apache.hadoop.hbase.protobuf.generated.AuthenticationProtos.WhoAmIRequest;
39  import org.apache.hadoop.hbase.protobuf.generated.AuthenticationProtos.WhoAmIResponse;
40  import org.apache.hadoop.hbase.security.AccessDeniedException;
41  import org.apache.hadoop.hbase.testclassification.MediumTests;
42  import org.apache.hadoop.hbase.testclassification.SecurityTests;
43  import org.apache.hadoop.security.UserGroupInformation;
44  import org.apache.hadoop.security.UserGroupInformation.AuthenticationMethod;
45  import org.apache.hadoop.security.token.Token;
46  import org.apache.hadoop.security.token.TokenIdentifier;
47  import org.junit.Test;
48  import org.junit.experimental.categories.Category;
49  
50  import com.google.protobuf.ServiceException;
51  
52  @Category({ SecurityTests.class, MediumTests.class })
53  public class TestGenerateDelegationToken extends SecureTestCluster {
54  
55    private void testTokenAuth(Class<? extends RpcClient> rpcImplClass) throws IOException,
56        ServiceException {
57      TEST_UTIL.getConfiguration().set(RpcClientFactory.CUSTOM_RPC_CLIENT_IMPL_CONF_KEY,
58        rpcImplClass.getName());
59      try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration());
60          Table table = conn.getTable(TableName.META_TABLE_NAME)) {
61        CoprocessorRpcChannel rpcChannel = table.coprocessorService(HConstants.EMPTY_START_ROW);
62        AuthenticationProtos.AuthenticationService.BlockingInterface service =
63            AuthenticationProtos.AuthenticationService.newBlockingStub(rpcChannel);
64        WhoAmIResponse response = service.whoAmI(null, WhoAmIRequest.getDefaultInstance());
65        assertEquals(USERNAME, response.getUsername());
66        assertEquals(AuthenticationMethod.TOKEN.name(), response.getAuthMethod());
67        try {
68          service.getAuthenticationToken(null, GetAuthenticationTokenRequest.getDefaultInstance());
69        } catch (ServiceException e) {
70          AccessDeniedException exc = (AccessDeniedException) ProtobufUtil.getRemoteException(e);
71          assertTrue(exc.getMessage().contains(
72            "Token generation only allowed for Kerberos authenticated clients"));
73        }
74      }
75    }
76  
77    @Test
78    public void test() throws Exception {
79      try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) {
80        Token<? extends TokenIdentifier> token = TokenUtil.obtainToken(conn);
81        UserGroupInformation.getCurrentUser().addToken(token);
82        testTokenAuth(RpcClientImpl.class);
83        testTokenAuth(AsyncRpcClient.class);
84      }
85    }
86  }