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