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.ipc;
019
020import java.io.File;
021import java.security.PrivilegedExceptionAction;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.fs.CommonConfigurationKeys;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.TableNameTestRule;
027import org.apache.hadoop.hbase.security.HBaseKerberosUtils;
028import org.apache.hadoop.hbase.testclassification.MediumTests;
029import org.apache.hadoop.hbase.testclassification.RPCTests;
030import org.apache.hadoop.minikdc.MiniKdc;
031import org.apache.hadoop.security.UserGroupInformation;
032import org.junit.After;
033import org.junit.Before;
034import org.junit.ClassRule;
035import org.junit.Rule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039@Category({ RPCTests.class, MediumTests.class })
040public class TestSecureNettyRpcServer extends TestNettyRpcServer {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044    HBaseClassTestRule.forClass(TestSecureNettyRpcServer.class);
045
046  private static File KEYTAB_FILE;
047  private static MiniKdc KDC;
048  private static String HOST = "localhost";
049  private static String PRINCIPAL;
050  private static UserGroupInformation UGI;
051
052  @Rule
053  public TableNameTestRule name = new TableNameTestRule();
054
055  @Before
056  public void setup() throws Exception {
057    TEST_UTIL = new HBaseTestingUtil();
058    KEYTAB_FILE = new File(TEST_UTIL.getDataTestDir("keytab").toUri().getPath());
059    KDC = TEST_UTIL.setupMiniKdc(KEYTAB_FILE);
060    PRINCIPAL = "hbase/" + HOST;
061    KDC.createPrincipal(KEYTAB_FILE, PRINCIPAL);
062    String principalName = PRINCIPAL + "@" + KDC.getRealm();
063    HBaseKerberosUtils.setPrincipalForTesting(principalName);
064    Configuration conf = TEST_UTIL.getConfiguration();
065    HBaseKerberosUtils.setSecuredConfiguration(conf, principalName, principalName);
066    UGI = login(KEYTAB_FILE.toString(), principalName);
067    super.setup();
068  }
069
070  @After
071  public void tearDown() throws Exception {
072    if (KDC != null) {
073      KDC.stop();
074    }
075    KEYTAB_FILE.delete();
076    super.tearDown();
077    TEST_UTIL.cleanupTestDir();
078  }
079
080  @Override
081  @Test
082  public void testNettyRpcServer() throws Exception {
083    UGI.doAs(new PrivilegedExceptionAction<Void>() {
084      @Override
085      public Void run() throws Exception {
086        doTest(name.getTableName());
087        return null;
088      }
089    });
090  }
091
092  static UserGroupInformation login(String krbKeytab, String krbPrincipal) throws Exception {
093    Configuration conf = new Configuration();
094    conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
095    UserGroupInformation.setConfiguration(conf);
096    UserGroupInformation.loginUserFromKeytab(krbPrincipal, krbKeytab);
097    return UserGroupInformation.getLoginUser();
098  }
099
100}