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  
19  package org.apache.hadoop.hbase.security.token;
20  
21  import com.google.protobuf.ServiceException;
22  import org.apache.hadoop.hbase.HColumnDescriptor;
23  import org.apache.hadoop.hbase.HTableDescriptor;
24  import org.apache.hadoop.hbase.TableName;
25  import org.apache.hadoop.hbase.client.Admin;
26  import org.apache.hadoop.hbase.client.Connection;
27  import org.apache.hadoop.hbase.client.ConnectionFactory;
28  import org.apache.hadoop.hbase.client.Get;
29  import org.apache.hadoop.hbase.client.Put;
30  import org.apache.hadoop.hbase.client.Result;
31  import org.apache.hadoop.hbase.client.Table;
32  import org.apache.hadoop.hbase.ipc.AsyncRpcClient;
33  import org.apache.hadoop.hbase.ipc.RpcClient;
34  import org.apache.hadoop.hbase.ipc.RpcClientFactory;
35  import org.apache.hadoop.hbase.ipc.RpcClientImpl;
36  import org.apache.hadoop.hbase.testclassification.MediumTests;
37  import org.apache.hadoop.hbase.testclassification.SecurityTests;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.apache.hadoop.security.UserGroupInformation;
40  import org.apache.hadoop.security.token.Token;
41  import org.apache.hadoop.security.token.TokenIdentifier;
42  import org.junit.Assert;
43  import org.junit.BeforeClass;
44  import org.junit.Test;
45  import org.junit.experimental.categories.Category;
46  import java.io.IOException;
47  
48  @Category({ SecurityTests.class, MediumTests.class })
49  public class TestDelegationTokenWithEncryption extends SecureTestCluster {
50  
51    @BeforeClass
52    public static void setUp() throws Exception {
53      // enable rpc encryption
54      TEST_UTIL.getConfiguration().set("hbase.rpc.protection", "privacy");
55      SecureTestCluster.setUp();
56    }
57  
58    private void testPutGetWithDelegationToken(Class<? extends RpcClient> rpcImplClass)
59        throws IOException, ServiceException {
60      TEST_UTIL.getConfiguration().set(RpcClientFactory.CUSTOM_RPC_CLIENT_IMPL_CONF_KEY,
61          rpcImplClass.getName());
62      try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration());
63           Table table = conn.getTable(TableName.valueOf("testtable"));) {
64        Put p = new Put(Bytes.toBytes("row"));
65        p.addColumn(Bytes.toBytes("family"),
66            Bytes.toBytes("data"), Bytes.toBytes("testdata"));
67        table.put(p);
68        Get g = new Get(Bytes.toBytes("row"));
69        Result result = table.get(g);
70        Assert.assertArrayEquals(Bytes.toBytes("testdata"),
71            result.getValue(Bytes.toBytes("family"), Bytes.toBytes("data")));
72      }
73    }
74  
75    @Test
76    public void testPutGetWithDelegationToken()  throws Exception {
77      try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) {
78        Token<? extends TokenIdentifier> token = TokenUtil.obtainToken(conn);
79        UserGroupInformation.getCurrentUser().addToken(token);
80        // create the table for test
81        Admin admin = conn.getAdmin();
82        HTableDescriptor tableDescriptor = new
83            HTableDescriptor(new HTableDescriptor(TableName.valueOf("testtable")));
84        tableDescriptor.addFamily(new HColumnDescriptor("family"));
85        admin.createTable(tableDescriptor);
86  
87        testPutGetWithDelegationToken(RpcClientImpl.class);
88        testPutGetWithDelegationToken(AsyncRpcClient.class);
89      }
90    }
91  }