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.HBaseTestingUtil;
025import org.apache.hadoop.hbase.security.HBaseKerberosUtils;
026import org.apache.hadoop.hbase.testclassification.MediumTests;
027import org.apache.hadoop.hbase.testclassification.RPCTests;
028import org.apache.hadoop.minikdc.MiniKdc;
029import org.apache.hadoop.security.UserGroupInformation;
030import org.junit.jupiter.api.AfterAll;
031import org.junit.jupiter.api.BeforeAll;
032import org.junit.jupiter.api.Tag;
033import org.junit.jupiter.api.Test;
034
035@Tag(RPCTests.TAG)
036@Tag(MediumTests.TAG)
037public class TestSecureSimpleRpcServer extends AbstractTestRpcServer {
038
039  private static File KEYTAB_FILE;
040  private static MiniKdc KDC;
041  private static String HOST = "localhost";
042  private static String PRINCIPAL;
043  private static UserGroupInformation UGI;
044
045  @BeforeAll
046  public static void setupClass() throws Exception {
047    TEST_UTIL = new HBaseTestingUtil();
048    KEYTAB_FILE = new File(TEST_UTIL.getDataTestDir("keytab").toUri().getPath());
049    KDC = TEST_UTIL.setupMiniKdc(KEYTAB_FILE);
050    PRINCIPAL = "hbase/" + HOST;
051    KDC.createPrincipal(KEYTAB_FILE, PRINCIPAL);
052    String principalName = PRINCIPAL + "@" + KDC.getRealm();
053    HBaseKerberosUtils.setPrincipalForTesting(principalName);
054    Configuration conf = TEST_UTIL.getConfiguration();
055    HBaseKerberosUtils.setSecuredConfiguration(conf, principalName, principalName);
056    UGI = login(KEYTAB_FILE.toString(), principalName);
057    TEST_UTIL.getConfiguration().set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY,
058      SimpleRpcServer.class.getName());
059    TEST_UTIL.startMiniCluster();
060  }
061
062  @AfterAll
063  public static void tearDownClass() throws Exception {
064    if (KDC != null) {
065      KDC.stop();
066    }
067    if (KEYTAB_FILE != null) {
068      KEYTAB_FILE.delete();
069    }
070    if (TEST_UTIL != null) {
071      TEST_UTIL.shutdownMiniCluster();
072      TEST_UTIL.cleanupTestDir();
073    }
074  }
075
076  @Test
077  public void testSimpleRpcServer() throws Exception {
078    UGI.doAs(new PrivilegedExceptionAction<Void>() {
079      @Override
080      public Void run() throws Exception {
081        doTest(tableName);
082        return null;
083      }
084    });
085  }
086
087  static UserGroupInformation login(String krbKeytab, String krbPrincipal) throws Exception {
088    Configuration conf = new Configuration();
089    conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
090    UserGroupInformation.setConfiguration(conf);
091    UserGroupInformation.loginUserFromKeytab(krbPrincipal, krbKeytab);
092    return UserGroupInformation.getLoginUser();
093  }
094
095}