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.http; 019 020import java.io.File; 021import java.io.IOException; 022import java.net.HttpURLConnection; 023import java.net.URL; 024import java.security.Principal; 025import java.security.PrivilegedExceptionAction; 026import java.util.Set; 027import javax.security.auth.Subject; 028import javax.security.auth.kerberos.KerberosTicket; 029import org.apache.hadoop.conf.Configuration; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseCommonTestingUtility; 032import org.apache.hadoop.hbase.http.TestHttpServer.EchoServlet; 033import org.apache.hadoop.hbase.http.resource.JerseyResource; 034import org.apache.hadoop.hbase.testclassification.MiscTests; 035import org.apache.hadoop.hbase.testclassification.SmallTests; 036import org.apache.hadoop.hbase.util.SimpleKdcServerUtil; 037import org.apache.hadoop.security.authentication.util.KerberosName; 038import org.apache.http.HttpHost; 039import org.apache.http.HttpResponse; 040import org.apache.http.auth.AuthSchemeProvider; 041import org.apache.http.auth.AuthScope; 042import org.apache.http.auth.KerberosCredentials; 043import org.apache.http.client.HttpClient; 044import org.apache.http.client.config.AuthSchemes; 045import org.apache.http.client.methods.HttpGet; 046import org.apache.http.client.protocol.HttpClientContext; 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.HttpClients; 052import org.apache.http.util.EntityUtils; 053import org.apache.kerby.kerberos.kerb.KrbException; 054import org.apache.kerby.kerberos.kerb.client.JaasKrbUtil; 055import org.apache.kerby.kerberos.kerb.server.SimpleKdcServer; 056import org.ietf.jgss.GSSCredential; 057import org.ietf.jgss.GSSManager; 058import org.ietf.jgss.GSSName; 059import org.ietf.jgss.Oid; 060import org.junit.AfterClass; 061import org.junit.BeforeClass; 062import org.junit.ClassRule; 063import org.junit.Test; 064import org.junit.experimental.categories.Category; 065import org.slf4j.Logger; 066import org.slf4j.LoggerFactory; 067 068/** 069 * Test class for SPNEGO authentication on the HttpServer. Uses Kerby's MiniKDC and Apache 070 * HttpComponents to verify that a simple Servlet is reachable via SPNEGO and unreachable w/o. 071 */ 072@Category({MiscTests.class, SmallTests.class}) 073public class TestSpnegoHttpServer extends HttpServerFunctionalTest { 074 @ClassRule 075 public static final HBaseClassTestRule CLASS_RULE = 076 HBaseClassTestRule.forClass(TestSpnegoHttpServer.class); 077 078 private static final Logger LOG = LoggerFactory.getLogger(TestSpnegoHttpServer.class); 079 private static final String KDC_SERVER_HOST = "localhost"; 080 private static final String CLIENT_PRINCIPAL = "client"; 081 082 private static HttpServer server; 083 private static URL baseUrl; 084 private static SimpleKdcServer kdc; 085 private static File infoServerKeytab; 086 private static File clientKeytab; 087 088 @BeforeClass 089 public static void setupServer() throws Exception { 090 Configuration conf = new Configuration(); 091 HBaseCommonTestingUtility htu = new HBaseCommonTestingUtility(conf); 092 093 final String serverPrincipal = "HTTP/" + KDC_SERVER_HOST; 094 095 kdc = SimpleKdcServerUtil.getRunningSimpleKdcServer(new File(htu.getDataTestDir().toString()), 096 HBaseCommonTestingUtility::randomFreePort); 097 File keytabDir = new File(htu.getDataTestDir("keytabs").toString()); 098 if (keytabDir.exists()) { 099 deleteRecursively(keytabDir); 100 } 101 keytabDir.mkdirs(); 102 103 infoServerKeytab = new File(keytabDir, serverPrincipal.replace('/', '_') + ".keytab"); 104 clientKeytab = new File(keytabDir, CLIENT_PRINCIPAL + ".keytab"); 105 106 setupUser(kdc, clientKeytab, CLIENT_PRINCIPAL); 107 setupUser(kdc, infoServerKeytab, serverPrincipal); 108 109 buildSpnegoConfiguration(conf, serverPrincipal, infoServerKeytab); 110 111 server = createTestServerWithSecurity(conf); 112 server.addUnprivilegedServlet("echo", "/echo", EchoServlet.class); 113 server.addJerseyResourcePackage(JerseyResource.class.getPackage().getName(), "/jersey/*"); 114 server.start(); 115 baseUrl = getServerURL(server); 116 117 LOG.info("HTTP server started: "+ baseUrl); 118 } 119 120 @AfterClass 121 public static void stopServer() throws Exception { 122 try { 123 if (null != server) { 124 server.stop(); 125 } 126 } catch (Exception e) { 127 LOG.info("Failed to stop info server", e); 128 } 129 try { 130 if (null != kdc) { 131 kdc.stop(); 132 } 133 } catch (Exception e) { 134 LOG.info("Failed to stop mini KDC", e); 135 } 136 } 137 138 private static void setupUser(SimpleKdcServer kdc, File keytab, String principal) 139 throws KrbException { 140 kdc.createPrincipal(principal); 141 kdc.exportPrincipal(principal, keytab); 142 } 143 144 private static Configuration buildSpnegoConfiguration(Configuration conf, String serverPrincipal, 145 File serverKeytab) { 146 KerberosName.setRules("DEFAULT"); 147 148 conf.setInt(HttpServer.HTTP_MAX_THREADS, TestHttpServer.MAX_THREADS); 149 150 // Enable Kerberos (pre-req) 151 conf.set("hbase.security.authentication", "kerberos"); 152 conf.set(HttpServer.HTTP_UI_AUTHENTICATION, "kerberos"); 153 conf.set(HttpServer.HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_KEY, serverPrincipal); 154 conf.set(HttpServer.HTTP_SPNEGO_AUTHENTICATION_KEYTAB_KEY, serverKeytab.getAbsolutePath()); 155 156 return conf; 157 } 158 159 @Test 160 public void testUnauthorizedClientsDisallowed() throws IOException { 161 URL url = new URL(getServerURL(server), "/echo?a=b"); 162 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 163 assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, conn.getResponseCode()); 164 } 165 166 @Test 167 public void testAllowedClient() throws Exception { 168 // Create the subject for the client 169 final Subject clientSubject = JaasKrbUtil.loginUsingKeytab(CLIENT_PRINCIPAL, clientKeytab); 170 final Set<Principal> clientPrincipals = clientSubject.getPrincipals(); 171 // Make sure the subject has a principal 172 assertFalse(clientPrincipals.isEmpty()); 173 174 // Get a TGT for the subject (might have many, different encryption types). The first should 175 // be the default encryption type. 176 Set<KerberosTicket> privateCredentials = 177 clientSubject.getPrivateCredentials(KerberosTicket.class); 178 assertFalse(privateCredentials.isEmpty()); 179 KerberosTicket tgt = privateCredentials.iterator().next(); 180 assertNotNull(tgt); 181 182 // The name of the principal 183 final String principalName = clientPrincipals.iterator().next().getName(); 184 185 // Run this code, logged in as the subject (the client) 186 HttpResponse resp = Subject.doAs(clientSubject, new PrivilegedExceptionAction<HttpResponse>() { 187 @Override 188 public HttpResponse run() throws Exception { 189 // Logs in with Kerberos via GSS 190 GSSManager gssManager = GSSManager.getInstance(); 191 // jGSS Kerberos login constant 192 Oid oid = new Oid("1.2.840.113554.1.2.2"); 193 GSSName gssClient = gssManager.createName(principalName, GSSName.NT_USER_NAME); 194 GSSCredential credential = gssManager.createCredential(gssClient, 195 GSSCredential.DEFAULT_LIFETIME, oid, GSSCredential.INITIATE_ONLY); 196 197 HttpClientContext context = HttpClientContext.create(); 198 Lookup<AuthSchemeProvider> authRegistry = RegistryBuilder.<AuthSchemeProvider>create() 199 .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, true)) 200 .build(); 201 202 HttpClient client = HttpClients.custom().setDefaultAuthSchemeRegistry(authRegistry) 203 .build(); 204 BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); 205 credentialsProvider.setCredentials(AuthScope.ANY, new KerberosCredentials(credential)); 206 207 URL url = new URL(getServerURL(server), "/echo?a=b"); 208 context.setTargetHost(new HttpHost(url.getHost(), url.getPort())); 209 context.setCredentialsProvider(credentialsProvider); 210 context.setAuthSchemeRegistry(authRegistry); 211 212 HttpGet get = new HttpGet(url.toURI()); 213 return client.execute(get, context); 214 } 215 }); 216 217 assertNotNull(resp); 218 assertEquals(HttpURLConnection.HTTP_OK, resp.getStatusLine().getStatusCode()); 219 assertEquals("a:b", EntityUtils.toString(resp.getEntity()).trim()); 220 } 221 222 @Test(expected = IllegalArgumentException.class) 223 public void testMissingConfigurationThrowsException() throws Exception { 224 Configuration conf = new Configuration(); 225 conf.setInt(HttpServer.HTTP_MAX_THREADS, TestHttpServer.MAX_THREADS); 226 // Enable Kerberos (pre-req) 227 conf.set("hbase.security.authentication", "kerberos"); 228 // Intentionally skip keytab and principal 229 230 HttpServer customServer = createTestServerWithSecurity(conf); 231 customServer.addUnprivilegedServlet("echo", "/echo", EchoServlet.class); 232 customServer.addJerseyResourcePackage(JerseyResource.class.getPackage().getName(), "/jersey/*"); 233 customServer.start(); 234 } 235}