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.thrift; 019 020import static org.apache.hadoop.hbase.thrift.Constants.THRIFT_SUPPORT_PROXYUSER_KEY; 021import static org.junit.jupiter.api.Assertions.assertEquals; 022import static org.junit.jupiter.api.Assertions.assertFalse; 023import static org.junit.jupiter.api.Assertions.assertNotNull; 024import static org.junit.jupiter.api.Assertions.assertThrows; 025import static org.junit.jupiter.api.Assertions.assertTrue; 026 027import java.io.File; 028import java.net.InetAddress; 029import java.nio.ByteBuffer; 030import java.nio.file.Paths; 031import java.security.Principal; 032import java.security.PrivilegedExceptionAction; 033import java.util.ArrayList; 034import java.util.Collections; 035import java.util.List; 036import java.util.Set; 037import java.util.function.Supplier; 038import java.util.stream.Collectors; 039import javax.security.auth.Subject; 040import javax.security.auth.kerberos.KerberosTicket; 041import org.apache.hadoop.conf.Configuration; 042import org.apache.hadoop.hbase.HBaseTestingUtil; 043import org.apache.hadoop.hbase.security.HBaseKerberosUtils; 044import org.apache.hadoop.hbase.testclassification.ClientTests; 045import org.apache.hadoop.hbase.testclassification.LargeTests; 046import org.apache.hadoop.hbase.thrift.generated.Hbase; 047import org.apache.hadoop.hbase.thrift.generated.IOError; 048import org.apache.hadoop.hbase.thrift.generated.Mutation; 049import org.apache.hadoop.hbase.util.Bytes; 050import org.apache.hadoop.hbase.util.SimpleKdcServerUtil; 051import org.apache.hadoop.security.authentication.util.KerberosName; 052import org.apache.http.HttpHeaders; 053import org.apache.http.auth.AuthSchemeProvider; 054import org.apache.http.auth.AuthScope; 055import org.apache.http.auth.KerberosCredentials; 056import org.apache.http.client.config.AuthSchemes; 057import org.apache.http.config.Lookup; 058import org.apache.http.config.RegistryBuilder; 059import org.apache.http.impl.auth.SPNegoSchemeFactory; 060import org.apache.http.impl.client.BasicCredentialsProvider; 061import org.apache.http.impl.client.CloseableHttpClient; 062import org.apache.http.impl.client.HttpClients; 063import org.apache.kerby.kerberos.kerb.client.JaasKrbUtil; 064import org.apache.kerby.kerberos.kerb.server.SimpleKdcServer; 065import org.apache.thrift.protocol.TBinaryProtocol; 066import org.apache.thrift.protocol.TProtocol; 067import org.apache.thrift.transport.THttpClient; 068import org.ietf.jgss.GSSCredential; 069import org.ietf.jgss.GSSManager; 070import org.ietf.jgss.GSSName; 071import org.ietf.jgss.Oid; 072import org.junit.jupiter.api.AfterAll; 073import org.junit.jupiter.api.BeforeAll; 074import org.junit.jupiter.api.Disabled; 075import org.junit.jupiter.api.Tag; 076import org.junit.jupiter.api.Test; 077import org.slf4j.Logger; 078import org.slf4j.LoggerFactory; 079 080/** 081 * Start the HBase Thrift HTTP server on a random port through the command-line interface and talk 082 * to it from client side with SPNEGO security enabled. 083 */ 084@Tag(ClientTests.TAG) 085@Tag(LargeTests.TAG) 086public class TestThriftSpnegoHttpServer extends TestThriftHttpServerBase { 087 088 private static final Logger LOG = LoggerFactory.getLogger(TestThriftSpnegoHttpServer.class); 089 090 private static SimpleKdcServer kdc; 091 private static File serverKeytab; 092 private static File spnegoServerKeytab; 093 private static File clientKeytab; 094 095 private static String clientPrincipal; 096 private static String clientPrincipal2; 097 private static String serverPrincipal; 098 private static String spnegoServerPrincipal; 099 100 private static void addSecurityConfigurations(Configuration conf) { 101 KerberosName.setRules("DEFAULT"); 102 103 HBaseKerberosUtils.setKeytabFileForTesting(serverKeytab.getAbsolutePath()); 104 105 conf.setBoolean(THRIFT_SUPPORT_PROXYUSER_KEY, true); 106 conf.setBoolean(Constants.USE_HTTP_CONF_KEY, true); 107 108 conf.set(Constants.THRIFT_KERBEROS_PRINCIPAL_KEY, serverPrincipal); 109 conf.set(Constants.THRIFT_KEYTAB_FILE_KEY, serverKeytab.getAbsolutePath()); 110 111 HBaseKerberosUtils.setSecuredConfiguration(conf, serverPrincipal, spnegoServerPrincipal); 112 conf.set("hadoop.proxyuser.hbase.hosts", "*"); 113 conf.set("hadoop.proxyuser.hbase.groups", "*"); 114 conf.set(Constants.THRIFT_SPNEGO_PRINCIPAL_KEY, spnegoServerPrincipal); 115 conf.set(Constants.THRIFT_SPNEGO_KEYTAB_FILE_KEY, spnegoServerKeytab.getAbsolutePath()); 116 } 117 118 @BeforeAll 119 public static void beforeAll() throws Exception { 120 kdc = SimpleKdcServerUtil.getRunningSimpleKdcServer( 121 new File(TEST_UTIL.getDataTestDir().toString()), HBaseTestingUtil::randomFreePort); 122 File keytabDir = Paths.get(TEST_UTIL.getRandomDir().toString()).toAbsolutePath().toFile(); 123 assertTrue(keytabDir.mkdirs()); 124 125 clientPrincipal = "client@" + kdc.getKdcConfig().getKdcRealm(); 126 clientPrincipal2 = "client2@" + kdc.getKdcConfig().getKdcRealm(); 127 clientKeytab = new File(keytabDir, clientPrincipal + ".keytab"); 128 kdc.createAndExportPrincipals(clientKeytab, clientPrincipal, clientPrincipal2); 129 130 String hostname = InetAddress.getLoopbackAddress().getHostName(); 131 serverPrincipal = "hbase/" + hostname + "@" + kdc.getKdcConfig().getKdcRealm(); 132 serverKeytab = new File(keytabDir, serverPrincipal.replace('/', '_') + ".keytab"); 133 134 // Setup separate SPNEGO keytab 135 spnegoServerPrincipal = "HTTP/" + hostname + "@" + kdc.getKdcConfig().getKdcRealm(); 136 spnegoServerKeytab = new File(keytabDir, spnegoServerPrincipal.replace('/', '_') + ".keytab"); 137 kdc.createAndExportPrincipals(spnegoServerKeytab, spnegoServerPrincipal); 138 kdc.createAndExportPrincipals(serverKeytab, serverPrincipal); 139 140 TEST_UTIL.getConfiguration().setBoolean(Constants.USE_HTTP_CONF_KEY, true); 141 addSecurityConfigurations(TEST_UTIL.getConfiguration()); 142 143 TestThriftHttpServerBase.setUpBeforeClass(); 144 } 145 146 @Override 147 protected Supplier<ThriftServer> getThriftServerSupplier() { 148 return () -> new ThriftServer(TEST_UTIL.getConfiguration()); 149 } 150 151 @AfterAll 152 public static void afterAll() throws Exception { 153 TestThriftHttpServerBase.tearDownAfterClass(); 154 155 try { 156 if (null != kdc) { 157 kdc.stop(); 158 kdc = null; 159 } 160 } catch (Exception e) { 161 LOG.info("Failed to stop mini KDC", e); 162 } 163 } 164 165 /** 166 * Block call through to this method. It is a messy test that fails because of bad config and then 167 * succeeds only the first attempt adds a table which the second attempt doesn't want to be in 168 * place to succeed. Let the super impl of this test be responsible for verifying we fail if bad 169 * header size. 170 */ 171 @Disabled 172 @Test 173 @Override 174 public void testRunThriftServerWithHeaderBufferLength() throws Exception { 175 super.testRunThriftServerWithHeaderBufferLength(); 176 } 177 178 private void testScanWithDifferentClients(Hbase.Client client, Hbase.Client client2) 179 throws Exception { 180 List<Mutation> mutations = new ArrayList<>(1); 181 mutations 182 .add(new Mutation(false, TestThriftServer.columnAAname, TestThriftServer.valueAname, true)); 183 client.mutateRow(TestThriftServer.tableAname, TestThriftServer.rowAname, mutations, 184 Collections.emptyMap()); 185 186 int id = client.scannerOpen(TestThriftServer.tableAname, ByteBuffer.allocate(0), 187 Collections.emptyList(), Collections.emptyMap()); 188 189 assertThrows(IOError.class, () -> client2.scannerGet(id)).printStackTrace(); 190 assertThrows(IOError.class, () -> client2.scannerClose(id)).printStackTrace(); 191 192 assertEquals(1, client.scannerGet(id).size()); 193 assertEquals(0, client.scannerGet(id).size()); 194 client.scannerClose(id); 195 } 196 197 @Override 198 protected void talkToThriftServer(String url, int customHeaderSize) throws Exception { 199 // Close httpClient and THttpClient automatically on any failures 200 try (CloseableHttpClient httpClient = createHttpClient(clientPrincipal); 201 THttpClient tHttpClient = new THttpClient(url, httpClient); 202 CloseableHttpClient httpClient2 = createHttpClient(clientPrincipal2); 203 THttpClient tHttpClient2 = new THttpClient(url, httpClient2)) { 204 tHttpClient.open(); 205 if (customHeaderSize > 0) { 206 StringBuilder sb = new StringBuilder(); 207 for (int i = 0; i < customHeaderSize; i++) { 208 sb.append("a"); 209 } 210 tHttpClient.setCustomHeader(HttpHeaders.USER_AGENT, sb.toString()); 211 } 212 213 TProtocol prot = new TBinaryProtocol(tHttpClient); 214 Hbase.Client client = new Hbase.Client(prot); 215 List<ByteBuffer> bbs = client.getTableNames(); 216 LOG.info("PRE-EXISTING {}", 217 bbs.stream().map(b -> Bytes.toString(b.array())).collect(Collectors.joining(","))); 218 if (!bbs.isEmpty()) { 219 for (ByteBuffer bb : bbs) { 220 client.disableTable(bb); 221 client.deleteTable(bb); 222 } 223 } 224 TestThriftServer.createTestTables(client); 225 TestThriftServer.checkTableList(client); 226 227 TProtocol prop2 = new TBinaryProtocol(tHttpClient2); 228 Hbase.Client client2 = new Hbase.Client(prop2); 229 testScanWithDifferentClients(client, client2); 230 231 TestThriftServer.dropTestTables(client); 232 } 233 } 234 235 private CloseableHttpClient createHttpClient(String clientPrincipal) throws Exception { 236 final Subject clientSubject = JaasKrbUtil.loginUsingKeytab(clientPrincipal, clientKeytab); 237 final Set<Principal> clientPrincipals = clientSubject.getPrincipals(); 238 // Make sure the subject has a principal 239 assertFalse(clientPrincipals.isEmpty(), "Found no client principals in the clientSubject."); 240 241 // Get a TGT for the subject (might have many, different encryption types). The first should 242 // be the default encryption type. 243 Set<KerberosTicket> privateCredentials = 244 clientSubject.getPrivateCredentials(KerberosTicket.class); 245 assertFalse(privateCredentials.isEmpty(), "Found no private credentials in the clientSubject."); 246 KerberosTicket tgt = privateCredentials.iterator().next(); 247 assertNotNull(tgt, "No kerberos ticket found."); 248 249 // The name of the principal 250 final String clientPrincipalName = clientPrincipals.iterator().next().getName(); 251 252 return Subject.doAs(clientSubject, (PrivilegedExceptionAction<CloseableHttpClient>) () -> { 253 // Logs in with Kerberos via GSS 254 GSSManager gssManager = GSSManager.getInstance(); 255 // jGSS Kerberos login constant 256 Oid oid = new Oid("1.2.840.113554.1.2.2"); 257 GSSName gssClient = gssManager.createName(clientPrincipalName, GSSName.NT_USER_NAME); 258 GSSCredential credential = gssManager.createCredential(gssClient, 259 GSSCredential.DEFAULT_LIFETIME, oid, GSSCredential.INITIATE_ONLY); 260 261 Lookup<AuthSchemeProvider> authRegistry = RegistryBuilder.<AuthSchemeProvider> create() 262 .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, true)).build(); 263 264 BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); 265 credentialsProvider.setCredentials(AuthScope.ANY, new KerberosCredentials(credential)); 266 267 return HttpClients.custom().setDefaultAuthSchemeRegistry(authRegistry) 268 .setDefaultCredentialsProvider(credentialsProvider).build(); 269 }); 270 } 271}