From dfd1bcda5d4c1d26456e62c3f6bb94670dabff36 Mon Sep 17 00:00:00 2001 From: Josh Elser Date: Mon, 13 Jun 2016 18:44:40 -0400 Subject: [PATCH] HBASE-5291 SPNEGO authentication for web UIs Builds on the existing wiring with hadoop-auth's SPNEGO classes. Unit tests built with Apache Kerby and Apache HC. Documentation added to instruct how to set it up. --- hbase-server/pom.xml | 22 ++ .../org/apache/hadoop/hbase/util/InfoServer.java | 90 ++++++ .../hadoop/hbase/util/TestSpnegoHttpServer.java | 347 +++++++++++++++++++++ pom.xml | 17 + src/main/asciidoc/_chapters/security.adoc | 53 ++++ 5 files changed, 529 insertions(+) create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestSpnegoHttpServer.java diff --git a/hbase-server/pom.xml b/hbase-server/pom.xml index 7a4dc94..4689ee1 100644 --- a/hbase-server/pom.xml +++ b/hbase-server/pom.xml @@ -519,6 +519,28 @@ org.hamcrest hamcrest-core + + org.apache.kerby + kerb-client + test + + + org.apache.kerby + kerb-simplekdc + test + + + org.apache.httpcomponents + httpclient + + 4.5.2 + test + + + org.apache.httpcomponents + httpcore + test + diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/InfoServer.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/InfoServer.java index 24ef5c0..0ae6165 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/InfoServer.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/InfoServer.java @@ -22,11 +22,14 @@ package org.apache.hadoop.hbase.util; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URL; +import java.util.HashMap; import java.util.Map; import org.apache.hadoop.hbase.classification.InterfaceAudience; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.http.HttpServer; +import org.apache.hadoop.security.SecurityUtil; +import org.apache.hadoop.security.authentication.server.AuthenticationFilter; import org.mortbay.jetty.handler.ContextHandlerCollection; import org.mortbay.jetty.servlet.Context; import org.mortbay.jetty.servlet.DefaultServlet; @@ -41,6 +44,28 @@ import org.mortbay.jetty.servlet.DefaultServlet; */ @InterfaceAudience.Private public class InfoServer extends HttpServer { + private static final String EMPTY_STRING = ""; + + public static final String HTTP_UI_AUTHENTICATION = "hbase.security.authentication.ui"; + static final String HTTP_AUTHENTICATION_PREFIX = "hbase.security.authentication.spnego."; + static final String HTTP_SPNEGO_AUTHENTICATION_PREFIX = HTTP_AUTHENTICATION_PREFIX + + "spnego."; + static final String HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_SUFFIX = "kerberos.principal"; + public static final String HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_KEY = + HTTP_SPNEGO_AUTHENTICATION_PREFIX + HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_SUFFIX; + static final String HTTP_SPNEGO_AUTHENTICATION_KEYTAB_SUFFIX = "kerberos.keytab"; + public static final String HTTP_SPNEGO_AUTHENTICATION_KEYTAB_KEY = + HTTP_SPNEGO_AUTHENTICATION_PREFIX + HTTP_SPNEGO_AUTHENTICATION_KEYTAB_SUFFIX; + static final String HTTP_SPNEGO_AUTHENTICATION_KRB_NAME_SUFFIX = "kerberos.name.rules"; + public static final String HTTP_SPNEGO_AUTHENTICATION_KRB_NAME_KEY = + HTTP_SPNEGO_AUTHENTICATION_PREFIX + HTTP_SPNEGO_AUTHENTICATION_KRB_NAME_SUFFIX; + static final String HTTP_AUTHENTICATION_SIGNATURE_SECRET_FILE_SUFFIX = + "signature.secret.file"; + public static final String HTTP_AUTHENTICATION_SIGNATURE_SECRET_FILE_KEY = + HTTP_AUTHENTICATION_PREFIX + HTTP_AUTHENTICATION_SIGNATURE_SECRET_FILE_SUFFIX; + + public static final String SPNEGO_FILTER = "SpnegoFilter"; + private final Configuration config; /** @@ -59,6 +84,7 @@ public class InfoServer extends HttpServer { super(name, bindAddress, port, findPort, c); this.config = c; fixupLogsServletLocation(); + setupSpnego(c, bindAddress); } /** @@ -95,6 +121,70 @@ public class InfoServer extends HttpServer { } /** + * Enables the SPNEGO authentication filter if HTTP_UI_AUTHENTICATION + * is equal to "kerberos". + */ + private void setupSpnego(Configuration conf, String hostName) throws IOException { + // Check if SPNEGO/Kerberos auth should be added + if (!"kerberos".equalsIgnoreCase(conf.get(HTTP_UI_AUTHENTICATION, EMPTY_STRING))) { + return; + } + Map params = new HashMap(); + String principalInConf = getOrEmptyString(conf, HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_KEY); + if (!principalInConf.isEmpty()) { + params.put(HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_SUFFIX, SecurityUtil.getServerPrincipal( + principalInConf, hostName)); + } + String httpKeytab = getOrEmptyString(conf, HTTP_SPNEGO_AUTHENTICATION_KEYTAB_KEY); + if (!httpKeytab.isEmpty()) { + params.put(HTTP_SPNEGO_AUTHENTICATION_KEYTAB_SUFFIX, httpKeytab); + } + String kerberosNameRule = getOrEmptyString(conf, HTTP_SPNEGO_AUTHENTICATION_KRB_NAME_KEY); + if (!kerberosNameRule.isEmpty()) { + params.put(HTTP_SPNEGO_AUTHENTICATION_KRB_NAME_SUFFIX, kerberosNameRule); + } + String signatureSecretKeyFile = getOrEmptyString(conf, + HTTP_AUTHENTICATION_SIGNATURE_SECRET_FILE_KEY); + if (!signatureSecretKeyFile.isEmpty()) { + params.put(HTTP_AUTHENTICATION_SIGNATURE_SECRET_FILE_SUFFIX, + signatureSecretKeyFile); + } + params.put(AuthenticationFilter.AUTH_TYPE, "kerberos"); + + // Verify that the required options were provided + if (isMissing(params.get(HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_SUFFIX)) || + isMissing(params.get(HTTP_SPNEGO_AUTHENTICATION_KEYTAB_SUFFIX))) { + throw new IllegalArgumentException(HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_KEY + " and " + + HTTP_SPNEGO_AUTHENTICATION_KEYTAB_KEY + " are both required in the configuration " + + "to enable SPNEGO/Kerberos authentication for the Web UI"); + } + + addGlobalFilter(SPNEGO_FILTER, AuthenticationFilter.class.getName(), params); + } + + /** + * Returns true if the argument is non-null and not whitespace + */ + private boolean isMissing(String value) { + if (null == value) { + return true; + } + return value.trim().isEmpty(); + } + + /** + * Extracts the value for the given key from the configuration of returns a string of + * zero length. + */ + private String getOrEmptyString(Configuration conf, String key) { + if (null == key) { + return EMPTY_STRING; + } + final String value = conf.get(key.trim()); + return null == value ? EMPTY_STRING : value; + } + + /** * Get the pathname to the webapps files. * @param appName eg "secondary" or "datanode" * @return the pathname as a URL diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestSpnegoHttpServer.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestSpnegoHttpServer.java new file mode 100644 index 0000000..d270e67 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestSpnegoHttpServer.java @@ -0,0 +1,347 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ServerSocket; +import java.net.URL; +import java.security.Principal; +import java.security.PrivilegedExceptionAction; +import java.util.Enumeration; +import java.util.Set; +import java.util.TreeSet; + +import javax.security.auth.Subject; +import javax.security.auth.kerberos.KerberosTicket; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.testclassification.MiscTests; +import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.apache.hadoop.net.NetUtils; +import org.apache.hadoop.security.authentication.util.KerberosName; +import org.apache.http.HttpHost; +import org.apache.http.HttpResponse; +import org.apache.http.auth.AuthSchemeProvider; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.KerberosCredentials; +import org.apache.http.client.HttpClient; +import org.apache.http.client.config.AuthSchemes; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.config.Lookup; +import org.apache.http.config.RegistryBuilder; +import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.entity.ContentType; +import org.apache.http.impl.auth.SPNegoSchemeFactory; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.apache.kerby.kerberos.kerb.KrbException; +import org.apache.kerby.kerberos.kerb.client.JaasKrbUtil; +import org.apache.kerby.kerberos.kerb.server.SimpleKdcServer; +import org.ietf.jgss.GSSCredential; +import org.ietf.jgss.GSSManager; +import org.ietf.jgss.GSSName; +import org.ietf.jgss.Oid; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * Test class for SPNEGO authentication on the HttpServer. Uses Kerby's MiniKDC and Apache + * HttpComponents to verify that a simple Servlet is reachable via SPNEGO and unreachable w/o. + */ +@Category({MiscTests.class, SmallTests.class}) +@SuppressWarnings("deprecation") +public class TestSpnegoHttpServer { + private static final Log LOG = LogFactory.getLog(TestSpnegoHttpServer.class); + private static final String KDC_SERVER_HOST = "localhost"; + private static final String CLIENT_PRINCIPAL = "client"; + + private static InfoServer server; + private static URL baseUrl; + private static SimpleKdcServer kdc; + private static File infoServerKeytab; + private static File clientKeytab; + + @SuppressWarnings("serial") + public static class EchoServlet extends HttpServlet { + @SuppressWarnings("unchecked") + @Override + public void doGet(HttpServletRequest request, + HttpServletResponse response + ) throws ServletException, IOException { + PrintWriter out = response.getWriter(); + TreeSet sortedKeys = new TreeSet(); + Enumeration keys = request.getParameterNames(); + while(keys.hasMoreElements()) { + sortedKeys.add(keys.nextElement()); + } + for(String key: sortedKeys) { + out.print(key); + out.print(':'); + out.print(request.getParameter(key)); + out.print('\n'); + } + out.close(); + } + } + + @BeforeClass + public static void setupServer() throws Exception { + System.setProperty("sun.security.krb5.debug", "true"); + System.setProperty("sun.security.spnego.debug", "true"); + final String serverPrincipal = "HTTP/" + KDC_SERVER_HOST; + final File target = new File(System.getProperty("user.dir"), "target"); + assertTrue(target.exists()); + + kdc = buildMiniKdc(); + kdc.start(); + + File keytabDir = new File(target, TestSpnegoHttpServer.class.getSimpleName() + + "_keytabs"); + if (keytabDir.exists()) { + deleteRecursively(keytabDir); + } + keytabDir.mkdirs(); + + infoServerKeytab = new File(keytabDir, serverPrincipal.replace('/', '_') + ".keytab"); + clientKeytab = new File(keytabDir, CLIENT_PRINCIPAL + ".keytab"); + + setupUser(kdc, clientKeytab, CLIENT_PRINCIPAL); + setupUser(kdc, infoServerKeytab, serverPrincipal); + + Configuration conf = buildSpnegoConfiguration(serverPrincipal, infoServerKeytab); + + final String addr = "0.0.0.0"; + int port = 0; + // Reuse the RS's webapp for testing + server = new InfoServer("regionserver", addr, port, false, conf); + server.addServlet("echo", "/echo", EchoServlet.class); + server.start(); + baseUrl = getServerURL(server); + + LOG.info("HTTP server started: "+ baseUrl); + } + + @AfterClass + public static void stopServer() throws Exception { + try { + if (null != server) { + server.stop(); + } + } catch (Exception e) { + LOG.info("Failed to stop info server", e); + } + try { + if (null != kdc) { + kdc.stop(); + } + } catch (Exception e) { + LOG.info("Failed to stop mini KDC", e); + } + } + + private static void setupUser(SimpleKdcServer kdc, File keytab, String principal) + throws KrbException { + kdc.createPrincipal(principal); + kdc.exportPrincipal(principal, keytab); + } + + private static SimpleKdcServer buildMiniKdc() throws Exception { + SimpleKdcServer kdc = new SimpleKdcServer(); + + final File target = new File(System.getProperty("user.dir"), "target"); + File kdcDir = new File(target, TestSpnegoHttpServer.class.getSimpleName()); + if (kdcDir.exists()) { + deleteRecursively(kdcDir); + } + kdcDir.mkdirs(); + kdc.setWorkDir(kdcDir); + + kdc.setKdcHost(KDC_SERVER_HOST); + int kdcPort = getFreePort(); + kdc.setAllowTcp(true); + kdc.setAllowUdp(false); + kdc.setKdcTcpPort(kdcPort); + + LOG.info("Starting KDC server at " + KDC_SERVER_HOST + ":" + kdcPort); + + kdc.init(); + + return kdc; + } + + private static Configuration buildSpnegoConfiguration(String serverPrincipal, File + serverKeytab) { + Configuration conf = new Configuration(); + KerberosName.setRules("DEFAULT"); + + // Enable Kerberos (pre-req) + conf.set("hbase.security.authentication", "kerberos"); + conf.set(InfoServer.HTTP_UI_AUTHENTICATION, "kerberos"); + conf.set(InfoServer.HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_KEY, serverPrincipal); + conf.set(InfoServer.HTTP_SPNEGO_AUTHENTICATION_KEYTAB_KEY, serverKeytab.getAbsolutePath()); + + return conf; + } + + /** + * Pass in a server, return a URL bound to localhost and its port + * @param server server + * @return a URL bonded to the base of the server + * @throws MalformedURLException if the URL cannot be created. + */ + protected static URL getServerURL(InfoServer server) + throws MalformedURLException { + assertNotNull("No server", server); + String hostPort = NetUtils.getHostPortString(server.getListenerAddress()); + if (hostPort.startsWith("0.0.0.0:")) { + hostPort = hostPort.replace("0.0.0.0:", "localhost:"); + } + return new URL("http://" + + hostPort); + } + + /** + * Recursively deletes a {@link File}. + */ + protected static void deleteRecursively(File d) { + if (d.isDirectory()) { + for (String name : d.list()) { + File child = new File(d, name); + if (child.isFile()) { + child.delete(); + } else { + deleteRecursively(d); + } + } + } + d.delete(); + } + + /** + * Picks a free port on the host by binding a Socket to '0'. + */ + protected static int getFreePort() throws IOException { + ServerSocket s = new ServerSocket(0); + try { + s.setReuseAddress(true); + int port = s.getLocalPort(); + return port; + } finally { + if (null != s) { + s.close(); + } + } + } + + @Test + public void testUnauthorizedClientsDisallowed() throws IOException { + URL url = new URL(getServerURL(server), "/echo?a=b"); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, conn.getResponseCode()); + } + + @Test + public void testAllowedClient() throws Exception { + // Create the subject for the client + final Subject clientSubject = JaasKrbUtil.loginUsingKeytab(CLIENT_PRINCIPAL, clientKeytab); + final Set clientPrincipals = clientSubject.getPrincipals(); + // Make sure the subject has a principal + assertFalse(clientPrincipals.isEmpty()); + + // Get a TGT for the subject (might have many, different encryption types). The first should + // be the default encryption type. + Set privateCredentials = + clientSubject.getPrivateCredentials(KerberosTicket.class); + assertFalse(privateCredentials.isEmpty()); + KerberosTicket tgt = privateCredentials.iterator().next(); + assertNotNull(tgt); + + // The name of the principal + final String principalName = clientPrincipals.iterator().next().getName(); + + // Run this code, logged in as the subject (the client) + HttpResponse resp = Subject.doAs(clientSubject, + new PrivilegedExceptionAction() { + @Override + public HttpResponse run() throws Exception { + // Logs in with Kerberos via GSS + GSSManager gssManager = GSSManager.getInstance(); + // jGSS Kerberos login constant + Oid oid = new Oid("1.2.840.113554.1.2.2"); + GSSName gssClient = gssManager.createName(principalName, GSSName.NT_USER_NAME); + GSSCredential credential = gssManager.createCredential(gssClient, + GSSCredential.DEFAULT_LIFETIME, oid, GSSCredential.INITIATE_ONLY); + + HttpClientContext context = HttpClientContext.create(); + Lookup authRegistry = RegistryBuilder.create() + .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, true)) + .build(); + + HttpClient client = HttpClients.custom().setDefaultAuthSchemeRegistry(authRegistry).build(); + BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(AuthScope.ANY, new KerberosCredentials(credential)); + + URL url = new URL(getServerURL(server), "/echo?a=b"); + LOG.info("Connecting to " + url); + context.setTargetHost(new HttpHost(url.getHost(), url.getPort())); + context.setCredentialsProvider(credentialsProvider); + context.setAuthSchemeRegistry(authRegistry); + + HttpGet get = new HttpGet(url.toURI()); + return client.execute(get, context); + } + }); + + assertNotNull(resp); + assertEquals(HttpURLConnection.HTTP_OK, resp.getStatusLine().getStatusCode()); + assertEquals("a:b", EntityUtils.toString(resp.getEntity()).trim()); + } + + @Test(expected = IllegalArgumentException.class) + public void testMissingConfigurationThrowsException() throws Exception { + Configuration conf = new Configuration(); + // Enable Kerberos (pre-req) + conf.set("hbase.security.authentication", "kerberos"); + conf.set(InfoServer.HTTP_UI_AUTHENTICATION, "kerberos"); + // Intentionally skip keytab and principal + + InfoServer customServer = new InfoServer("regionserver", "0.0.0.0", 0, false, conf); + customServer.addServlet("echo", "/echo", EchoServlet.class); + customServer.start(); + } +} diff --git a/pom.xml b/pom.xml index 2b3bbdc..6668aa1 100644 --- a/pom.xml +++ b/pom.xml @@ -1087,6 +1087,7 @@ 3.2.2 3.1 + 4.4.4 2.2.0 12.0.1 1.8.8 @@ -1113,6 +1114,7 @@ 3.6.6.Final 2.1.2 1.0.8 + 1.0.0-RC2 2.4 1.6 @@ -1350,6 +1352,11 @@ ${httpclient.version} + org.apache.httpcomponents + httpcore + ${httpcore.version} + + commons-cli commons-cli ${commons-cli.version} @@ -1613,6 +1620,16 @@ htrace-core ${htrace.version} + + org.apache.kerby + kerb-client + ${kerby.version} + + + org.apache.kerby + kerb-simplekdc + ${kerby.version} + diff --git a/src/main/asciidoc/_chapters/security.adoc b/src/main/asciidoc/_chapters/security.adoc index 3d9082c..00bf6a8 100644 --- a/src/main/asciidoc/_chapters/security.adoc +++ b/src/main/asciidoc/_chapters/security.adoc @@ -69,6 +69,59 @@ See Nick Dimiduk's contribution on this link:http://stackoverflow.com/questions/ If you know how to fix this without opening a second port for HTTPS, patches are appreciated. ==== +[[hbase.secure.spnego.ui]] +== Using SPNEGO for Kerberos authentication with Web UIs + +Kerberos-authentication to HBase Web UIs can be enabled via configuring SPNEGO with the `hbase.security.authentication.ui` +property in _hbase-site.xml_. Enabling this authentication requires that HBase is also configured to use Kerberos authentication +for RPCs (e.g `hbase.security.authentication` = `kerberos`). + +[source,xml] +---- + + hbase.security.authentication.ui + kerberos + Controls what kind of authentication should be used for the HBase web UIs. + + + hbase.security.authentication + kerberos + The Kerberos keytab file to use for SPNEGO authentication by the web server. + +---- + +A number of properties exist to configure SPNEGO authentication for the web server: + +[source,xml] +---- + + hbase.security.authentication.spnego.kerberos.principal + HTTP/_HOST@EXAMPLE.COM + Required for SPNEGO, the Kerberos principal to use for SPNEGO authentication by the + web server. The _HOST keyword will be automatically substituted with the node's + hostname. + + + hbase.security.authentication.spnego.kerberos.keytab + /etc/security/keytabs/spnego.service.keytab + Required for SPNEGO, the Kerberos keytab file to use for SPNEGO authentication by the + web server. + + + hbase.security.authentication.spnego.kerberos.name.rules + + Optional, Hadoop-style `auth_to_local` rules which will be parsed and used in the + handling of Kerberos principals + + + hbase.security.authentication.signature.secret.file + + Optional, a file whose contents will be used as a secret to sign the HTTP cookies + as a part of the SPNEGO authentication handshake. If this is not provided, Java's `Random` library + will be used for the secret. + +---- + [[hbase.secure.configuration]] == Secure Client Access to Apache HBase -- 2.1.2