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