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;
021
022import java.util.Arrays;
023import java.util.Collection;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HColumnDescriptor;
026import org.apache.hadoop.hbase.HTableDescriptor;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.Admin;
029import org.apache.hadoop.hbase.client.Connection;
030import org.apache.hadoop.hbase.client.ConnectionFactory;
031import org.apache.hadoop.hbase.client.Get;
032import org.apache.hadoop.hbase.client.Put;
033import org.apache.hadoop.hbase.client.Result;
034import org.apache.hadoop.hbase.client.Table;
035import org.apache.hadoop.hbase.ipc.NettyRpcClient;
036import org.apache.hadoop.hbase.ipc.RpcClientFactory;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.testclassification.SecurityTests;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.apache.hadoop.security.UserGroupInformation;
041import org.apache.hadoop.security.token.Token;
042import org.apache.hadoop.security.token.TokenIdentifier;
043import org.junit.Before;
044import org.junit.BeforeClass;
045import org.junit.ClassRule;
046import org.junit.Rule;
047import org.junit.Test;
048import org.junit.experimental.categories.Category;
049import org.junit.rules.TestName;
050import org.junit.runner.RunWith;
051import org.junit.runners.Parameterized;
052import org.junit.runners.Parameterized.Parameter;
053import org.junit.runners.Parameterized.Parameters;
054
055@RunWith(Parameterized.class)
056@Category({ SecurityTests.class, MediumTests.class })
057public class TestDelegationTokenWithEncryption extends SecureTestCluster {
058
059  @ClassRule
060  public static final HBaseClassTestRule CLASS_RULE =
061    HBaseClassTestRule.forClass(TestDelegationTokenWithEncryption.class);
062
063  @BeforeClass
064  public static void setUp() throws Exception {
065    // enable rpc encryption
066    TEST_UTIL.getConfiguration().set("hbase.rpc.protection", "privacy");
067    SecureTestCluster.setUp();
068    try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())) {
069      Token<? extends TokenIdentifier> token = ClientTokenUtil.obtainToken(conn);
070      UserGroupInformation.getCurrentUser().addToken(token);
071    }
072  }
073
074  @Parameters(name = "{index}: rpcClientImpl={0}")
075  public static Collection<Object> parameters() {
076    // Client connection supports only non-blocking RPCs (due to master registry restriction), hence
077    // we only test NettyRpcClient.
078    return Arrays.asList(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}