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.assertArrayEquals;
21  
22  import java.util.Arrays;
23  import java.util.Collection;
24  
25  import org.apache.hadoop.hbase.HColumnDescriptor;
26  import org.apache.hadoop.hbase.HTableDescriptor;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.client.Admin;
29  import org.apache.hadoop.hbase.client.Connection;
30  import org.apache.hadoop.hbase.client.ConnectionFactory;
31  import org.apache.hadoop.hbase.client.Get;
32  import org.apache.hadoop.hbase.client.Put;
33  import org.apache.hadoop.hbase.client.Result;
34  import org.apache.hadoop.hbase.client.Table;
35  import org.apache.hadoop.hbase.ipc.BlockingRpcClient;
36  import org.apache.hadoop.hbase.ipc.NettyRpcClient;
37  import org.apache.hadoop.hbase.ipc.RpcClientFactory;
38  import org.apache.hadoop.hbase.testclassification.MediumTests;
39  import org.apache.hadoop.hbase.testclassification.SecurityTests;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.apache.hadoop.security.UserGroupInformation;
42  import org.apache.hadoop.security.token.Token;
43  import org.apache.hadoop.security.token.TokenIdentifier;
44  import org.junit.Before;
45  import org.junit.BeforeClass;
46  import org.junit.Rule;
47  import org.junit.Test;
48  import org.junit.experimental.categories.Category;
49  import org.junit.rules.TestName;
50  import org.junit.runner.RunWith;
51  import org.junit.runners.Parameterized;
52  import org.junit.runners.Parameterized.Parameter;
53  import org.junit.runners.Parameterized.Parameters;
54  
55  @RunWith(Parameterized.class)
56  @Category({ SecurityTests.class, MediumTests.class })
57  public class TestDelegationTokenWithEncryption extends SecureTestCluster {
58  
59    @BeforeClass
60    public static void setUp() throws Exception {
61      // enable rpc encryption
62      TEST_UTIL.getConfiguration().set("hbase.rpc.protection", "privacy");
63      SecureTestCluster.setUp();
64      try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) {
65        Token<? extends TokenIdentifier> token = TokenUtil.obtainToken(conn);
66        UserGroupInformation.getCurrentUser().addToken(token);
67      }
68    }
69  
70    @Parameters(name = "{index}: rpcClientImpl={0}")
71    public static Collection<Object[]> parameters() {
72      return Arrays.asList(new Object[] { BlockingRpcClient.class.getName() },
73        new Object[] { NettyRpcClient.class.getName() });
74    }
75  
76    @Parameter
77    public String rpcClientImpl;
78  
79    @Rule
80    public TestName testName = new TestName();
81  
82    @Before
83    public void setUpBeforeMethod() {
84      TEST_UTIL.getConfiguration().set(RpcClientFactory.CUSTOM_RPC_CLIENT_IMPL_CONF_KEY,
85        rpcClientImpl);
86    }
87  
88    private TableName getTestTableName() {
89      return TableName.valueOf(testName.getMethodName().replaceAll("[^0-9A-Za-z]", "_"));
90    }
91  
92    @Test
93    public void testPutGetWithDelegationToken() throws Exception {
94      TableName tableName = getTestTableName();
95      byte[] family = Bytes.toBytes("f");
96      byte[] qualifier = Bytes.toBytes("q");
97      byte[] row = Bytes.toBytes("row");
98      byte[] value = Bytes.toBytes("data");
99      try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) {
100       Admin admin = conn.getAdmin();
101       HTableDescriptor tableDescriptor = new HTableDescriptor(new HTableDescriptor(tableName));
102       tableDescriptor.addFamily(new HColumnDescriptor(family));
103       admin.createTable(tableDescriptor);
104       try (Table table = conn.getTable(tableName)) {
105         table.put(new Put(row).addColumn(family, qualifier, value));
106         Result result = table.get(new Get(row));
107         assertArrayEquals(value, result.getValue(family, qualifier));
108       }
109     }
110   }
111 }