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