001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.security.token;
019
020import static org.junit.Assert.assertArrayEquals;
021import java.util.Arrays;
022import java.util.Collection;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HColumnDescriptor;
025import org.apache.hadoop.hbase.HTableDescriptor;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.Admin;
028import org.apache.hadoop.hbase.client.Connection;
029import org.apache.hadoop.hbase.client.ConnectionFactory;
030import org.apache.hadoop.hbase.client.Get;
031import org.apache.hadoop.hbase.client.Put;
032import org.apache.hadoop.hbase.client.Result;
033import org.apache.hadoop.hbase.client.Table;
034import org.apache.hadoop.hbase.ipc.NettyRpcClient;
035import org.apache.hadoop.hbase.ipc.RpcClientFactory;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.testclassification.SecurityTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.apache.hadoop.security.UserGroupInformation;
040import org.apache.hadoop.security.token.Token;
041import org.apache.hadoop.security.token.TokenIdentifier;
042import org.junit.Before;
043import org.junit.BeforeClass;
044import org.junit.ClassRule;
045import org.junit.Rule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048import org.junit.rules.TestName;
049import org.junit.runner.RunWith;
050import org.junit.runners.Parameterized;
051import org.junit.runners.Parameterized.Parameter;
052import org.junit.runners.Parameterized.Parameters;
053
054@RunWith(Parameterized.class)
055@Category({ SecurityTests.class, MediumTests.class })
056public class TestDelegationTokenWithEncryption extends SecureTestCluster {
057
058  @ClassRule
059  public static final HBaseClassTestRule CLASS_RULE =
060      HBaseClassTestRule.forClass(TestDelegationTokenWithEncryption.class);
061
062  @BeforeClass
063  public static void setUp() throws Exception {
064    // enable rpc encryption
065    TEST_UTIL.getConfiguration().set("hbase.rpc.protection", "privacy");
066    SecureTestCluster.setUp();
067    try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) {
068      Token<? extends TokenIdentifier> token = ClientTokenUtil.obtainToken(conn);
069      UserGroupInformation.getCurrentUser().addToken(token);
070    }
071  }
072
073  @Parameters(name = "{index}: rpcClientImpl={0}")
074  public static Collection<Object> parameters() {
075    // Client connection supports only non-blocking RPCs (due to master registry restriction), hence
076    // we only test NettyRpcClient.
077    return Arrays.asList(
078      new Object[] { NettyRpcClient.class.getName() });
079  }
080
081  @Parameter
082  public String rpcClientImpl;
083
084  @Rule
085  public TestName testName = new TestName();
086
087  @Before
088  public void setUpBeforeMethod() {
089    TEST_UTIL.getConfiguration().set(RpcClientFactory.CUSTOM_RPC_CLIENT_IMPL_CONF_KEY,
090      rpcClientImpl);
091  }
092
093  private TableName getTestTableName() {
094    return TableName.valueOf(testName.getMethodName().replaceAll("[^0-9A-Za-z]", "_"));
095  }
096
097  @Test
098  public void testPutGetWithDelegationToken() throws Exception {
099    TableName tableName = getTestTableName();
100    byte[] family = Bytes.toBytes("f");
101    byte[] qualifier = Bytes.toBytes("q");
102    byte[] row = Bytes.toBytes("row");
103    byte[] value = Bytes.toBytes("data");
104    try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) {
105      Admin admin = conn.getAdmin();
106      HTableDescriptor tableDescriptor = new HTableDescriptor(new HTableDescriptor(tableName));
107      tableDescriptor.addFamily(new HColumnDescriptor(family));
108      admin.createTable(tableDescriptor);
109      try (Table table = conn.getTable(tableName)) {
110        table.put(new Put(row).addColumn(family, qualifier, value));
111        Result result = table.get(new Get(row));
112        assertArrayEquals(value, result.getValue(family, qualifier));
113      }
114    }
115  }
116}