-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HBASE-27924 Remove duplicate code for NettyHBaseSaslRpcServerHandler … (
#5285) Co-authored-by: comnetwork <comnetwork@163.com> Signed-off-by: Duo Zhang <zhangduo@apache.org>
- Loading branch information
1 parent
663bc64
commit 0703d36
Showing
2 changed files
with
157 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestSecurityRpcSentBytesMetrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.hadoop.hbase.ipc; | ||
|
||
import static org.apache.hadoop.hbase.ipc.TestProtobufRpcServiceImpl.SERVICE; | ||
import static org.apache.hadoop.hbase.ipc.TestProtobufRpcServiceImpl.newBlockingStub; | ||
import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.getKeytabFileForTesting; | ||
import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.getPrincipalForTesting; | ||
import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.loginKerberosPrincipal; | ||
import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.setSecuredConfiguration; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.File; | ||
import java.net.InetSocketAddress; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.hbase.HBaseClassTestRule; | ||
import org.apache.hadoop.hbase.HBaseTestingUtil; | ||
import org.apache.hadoop.hbase.HConstants; | ||
import org.apache.hadoop.hbase.security.HBaseKerberosUtils; | ||
import org.apache.hadoop.hbase.security.SecurityInfo; | ||
import org.apache.hadoop.hbase.security.User; | ||
import org.apache.hadoop.hbase.testclassification.MediumTests; | ||
import org.apache.hadoop.hbase.testclassification.SecurityTests; | ||
import org.apache.hadoop.minikdc.MiniKdc; | ||
import org.apache.hadoop.security.UserGroupInformation; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.mockito.Mockito; | ||
|
||
import org.apache.hbase.thirdparty.com.google.common.collect.Lists; | ||
|
||
import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestProtos; | ||
import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestRpcServiceProtos.TestProtobufRpcProto.BlockingInterface; | ||
|
||
@Category({ SecurityTests.class, MediumTests.class }) | ||
public class TestSecurityRpcSentBytesMetrics { | ||
|
||
@ClassRule | ||
public static final HBaseClassTestRule CLASS_RULE = | ||
HBaseClassTestRule.forClass(TestSecurityRpcSentBytesMetrics.class); | ||
|
||
protected static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); | ||
|
||
protected static final File KEYTAB_FILE = | ||
new File(TEST_UTIL.getDataTestDir("keytab").toUri().getPath()); | ||
|
||
protected static MiniKdc KDC; | ||
protected static String HOST = "localhost"; | ||
protected static String PRINCIPAL; | ||
|
||
protected String krbKeytab; | ||
protected String krbPrincipal; | ||
protected UserGroupInformation ugi; | ||
protected Configuration clientConf; | ||
protected Configuration serverConf; | ||
|
||
protected static void initKDCAndConf() throws Exception { | ||
KDC = TEST_UTIL.setupMiniKdc(KEYTAB_FILE); | ||
PRINCIPAL = "hbase/" + HOST; | ||
KDC.createPrincipal(KEYTAB_FILE, PRINCIPAL); | ||
HBaseKerberosUtils.setPrincipalForTesting(PRINCIPAL + "@" + KDC.getRealm()); | ||
// set a smaller timeout and retry to speed up tests | ||
TEST_UTIL.getConfiguration().setInt(RpcClient.SOCKET_TIMEOUT_READ, 2000000000); | ||
TEST_UTIL.getConfiguration().setInt("hbase.security.relogin.maxretries", 1); | ||
} | ||
|
||
protected static void stopKDC() throws InterruptedException { | ||
if (KDC != null) { | ||
KDC.stop(); | ||
} | ||
} | ||
|
||
protected final void setUpPrincipalAndConf() throws Exception { | ||
krbKeytab = getKeytabFileForTesting(); | ||
krbPrincipal = getPrincipalForTesting(); | ||
ugi = loginKerberosPrincipal(krbKeytab, krbPrincipal); | ||
clientConf = new Configuration(TEST_UTIL.getConfiguration()); | ||
setSecuredConfiguration(clientConf); | ||
serverConf = new Configuration(TEST_UTIL.getConfiguration()); | ||
setSecuredConfiguration(serverConf); | ||
} | ||
|
||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
initKDCAndConf(); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDown() throws Exception { | ||
stopKDC(); | ||
TEST_UTIL.cleanupTestDir(); | ||
} | ||
|
||
@Before | ||
public void setUpTest() throws Exception { | ||
setUpPrincipalAndConf(); | ||
} | ||
|
||
/** | ||
* This test is for HBASE-27924, before this JIRA, bytes sent by | ||
* {@link NettyHBaseSaslRpcServerHandler} is ignored by {@link MetricsHBaseServer#sentBytes}. | ||
*/ | ||
@Test | ||
public void test() throws Exception { | ||
SecurityInfo securityInfoMock = Mockito.mock(SecurityInfo.class); | ||
Mockito.when(securityInfoMock.getServerPrincipal()) | ||
.thenReturn(HBaseKerberosUtils.KRB_PRINCIPAL); | ||
SecurityInfo.addInfo("TestProtobufRpcProto", securityInfoMock); | ||
|
||
NettyRpcServer rpcServer = new NettyRpcServer(null, getClass().getSimpleName(), | ||
Lists.newArrayList(new RpcServer.BlockingServiceAndInterface(SERVICE, null)), | ||
new InetSocketAddress(HOST, 0), serverConf, new FifoRpcScheduler(serverConf, 1), true) { | ||
|
||
@Override | ||
public void start() { | ||
metrics = Mockito.spy(metrics); | ||
super.start(); | ||
} | ||
}; | ||
|
||
rpcServer.start(); | ||
try (NettyRpcClient rpcClient = | ||
new NettyRpcClient(clientConf, HConstants.DEFAULT_CLUSTER_ID.toString(), null, null)) { | ||
BlockingInterface stub = newBlockingStub(rpcClient, rpcServer.getListenerAddress(), | ||
User.create(UserGroupInformation.getCurrentUser())); | ||
|
||
String response = | ||
stub.echo(null, TestProtos.EchoRequestProto.newBuilder().setMessage("test").build()) | ||
.getMessage(); | ||
assertTrue("test".equals(response)); | ||
} finally { | ||
rpcServer.stop(); | ||
} | ||
Mockito.verify(rpcServer.metrics, Mockito.atLeast(2)).sentBytes(Mockito.anyLong()); | ||
} | ||
} |