|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with this |
| 4 | + * work for additional information regarding copyright ownership. The ASF |
| 5 | + * licenses this file to you under the Apache License, Version 2.0 (the |
| 6 | + * "License"); you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations under |
| 15 | + * the License. |
| 16 | + */ |
| 17 | +package org.apache.hadoop.security; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.net.InetAddress; |
| 21 | +import java.net.ServerSocket; |
| 22 | +import java.security.Principal; |
| 23 | +import java.util.Random; |
| 24 | + |
| 25 | +import javax.net.ssl.SSLContext; |
| 26 | +import javax.net.ssl.SSLServerSocket; |
| 27 | +import javax.net.ssl.SSLServerSocketFactory; |
| 28 | +import javax.net.ssl.SSLSocket; |
| 29 | +import javax.security.auth.kerberos.KerberosPrincipal; |
| 30 | +import javax.servlet.Filter; |
| 31 | +import javax.servlet.FilterChain; |
| 32 | +import javax.servlet.FilterConfig; |
| 33 | +import javax.servlet.ServletException; |
| 34 | +import javax.servlet.ServletRequest; |
| 35 | +import javax.servlet.ServletResponse; |
| 36 | +import javax.servlet.http.HttpServletRequest; |
| 37 | +import javax.servlet.http.HttpServletRequestWrapper; |
| 38 | +import javax.servlet.http.HttpServletResponse; |
| 39 | + |
| 40 | +import org.apache.commons.logging.Log; |
| 41 | +import org.apache.commons.logging.LogFactory; |
| 42 | +import org.mortbay.io.EndPoint; |
| 43 | +import org.mortbay.jetty.HttpSchemes; |
| 44 | +import org.mortbay.jetty.Request; |
| 45 | +import org.mortbay.jetty.security.ServletSSL; |
| 46 | +import org.mortbay.jetty.security.SslSocketConnector; |
| 47 | + |
| 48 | +/** |
| 49 | + * Extend Jetty's {@link SslSocketConnector} to optionally also provide |
| 50 | + * Kerberos5ized SSL sockets. The only change in behavior from superclass |
| 51 | + * is that we no longer honor requests to turn off NeedAuthentication when |
| 52 | + * running with Kerberos support. |
| 53 | + */ |
| 54 | +public class Krb5AndCertsSslSocketConnector extends SslSocketConnector { |
| 55 | + public static final String[] KRB5_CIPHER_SUITES = |
| 56 | + new String [] {"TLS_KRB5_WITH_3DES_EDE_CBC_SHA"}; |
| 57 | + static { |
| 58 | + System.setProperty("https.cipherSuites", KRB5_CIPHER_SUITES[0]); |
| 59 | + } |
| 60 | + |
| 61 | + private static final Log LOG = LogFactory |
| 62 | + .getLog(Krb5AndCertsSslSocketConnector.class); |
| 63 | + |
| 64 | + private static final String REMOTE_PRINCIPAL = "remote_principal"; |
| 65 | + |
| 66 | + public enum MODE {KRB, CERTS, BOTH} // Support Kerberos, certificates or both? |
| 67 | + |
| 68 | + private final boolean useKrb; |
| 69 | + private final boolean useCerts; |
| 70 | + |
| 71 | + public Krb5AndCertsSslSocketConnector() { |
| 72 | + super(); |
| 73 | + useKrb = true; |
| 74 | + useCerts = false; |
| 75 | + |
| 76 | + setPasswords(); |
| 77 | + } |
| 78 | + |
| 79 | + public Krb5AndCertsSslSocketConnector(MODE mode) { |
| 80 | + super(); |
| 81 | + useKrb = mode == MODE.KRB || mode == MODE.BOTH; |
| 82 | + useCerts = mode == MODE.CERTS || mode == MODE.BOTH; |
| 83 | + setPasswords(); |
| 84 | + logIfDebug("useKerb = " + useKrb + ", useCerts = " + useCerts); |
| 85 | + } |
| 86 | + |
| 87 | + // If not using Certs, set passwords to random gibberish or else |
| 88 | + // Jetty will actually prompt the user for some. |
| 89 | + private void setPasswords() { |
| 90 | + if(!useCerts) { |
| 91 | + Random r = new Random(); |
| 92 | + System.setProperty("jetty.ssl.password", String.valueOf(r.nextLong())); |
| 93 | + System.setProperty("jetty.ssl.keypassword", String.valueOf(r.nextLong())); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + protected SSLServerSocketFactory createFactory() throws Exception { |
| 99 | + if(useCerts) |
| 100 | + return super.createFactory(); |
| 101 | + |
| 102 | + SSLContext context = super.getProvider()==null |
| 103 | + ? SSLContext.getInstance(super.getProtocol()) |
| 104 | + :SSLContext.getInstance(super.getProtocol(), super.getProvider()); |
| 105 | + context.init(null, null, null); |
| 106 | + |
| 107 | + return context.getServerSocketFactory(); |
| 108 | + } |
| 109 | + |
| 110 | + /* (non-Javadoc) |
| 111 | + * @see org.mortbay.jetty.security.SslSocketConnector#newServerSocket(java.lang.String, int, int) |
| 112 | + */ |
| 113 | + @Override |
| 114 | + protected ServerSocket newServerSocket(String host, int port, int backlog) |
| 115 | + throws IOException { |
| 116 | + logIfDebug("Creating new KrbServerSocket for: " + host); |
| 117 | + SSLServerSocket ss = null; |
| 118 | + |
| 119 | + if(useCerts) // Get the server socket from the SSL super impl |
| 120 | + ss = (SSLServerSocket)super.newServerSocket(host, port, backlog); |
| 121 | + else { // Create a default server socket |
| 122 | + try { |
| 123 | + ss = (SSLServerSocket)(host == null |
| 124 | + ? createFactory().createServerSocket(port, backlog) : |
| 125 | + createFactory().createServerSocket(port, backlog, InetAddress.getByName(host))); |
| 126 | + } catch (Exception e) |
| 127 | + { |
| 128 | + LOG.warn("Could not create KRB5 Listener", e); |
| 129 | + throw new IOException("Could not create KRB5 Listener: " + e.toString()); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Add Kerberos ciphers to this socket server if needed. |
| 134 | + if(useKrb) { |
| 135 | + ss.setNeedClientAuth(true); |
| 136 | + String [] combined; |
| 137 | + if(useCerts) { // combine the cipher suites |
| 138 | + String[] certs = ss.getEnabledCipherSuites(); |
| 139 | + combined = new String[certs.length + KRB5_CIPHER_SUITES.length]; |
| 140 | + System.arraycopy(certs, 0, combined, 0, certs.length); |
| 141 | + System.arraycopy(KRB5_CIPHER_SUITES, 0, combined, certs.length, KRB5_CIPHER_SUITES.length); |
| 142 | + } else { // Just enable Kerberos auth |
| 143 | + combined = KRB5_CIPHER_SUITES; |
| 144 | + } |
| 145 | + |
| 146 | + ss.setEnabledCipherSuites(combined); |
| 147 | + } |
| 148 | + |
| 149 | + return ss; |
| 150 | + }; |
| 151 | + |
| 152 | + @Override |
| 153 | + public void customize(EndPoint endpoint, Request request) throws IOException { |
| 154 | + if(useKrb) { // Add Kerberos-specific info |
| 155 | + SSLSocket sslSocket = (SSLSocket)endpoint.getTransport(); |
| 156 | + Principal remotePrincipal = sslSocket.getSession().getPeerPrincipal(); |
| 157 | + logIfDebug("Remote principal = " + remotePrincipal); |
| 158 | + request.setScheme(HttpSchemes.HTTPS); |
| 159 | + request.setAttribute(REMOTE_PRINCIPAL, remotePrincipal); |
| 160 | + |
| 161 | + if(!useCerts) { // Add extra info that would have been added by super |
| 162 | + String cipherSuite = sslSocket.getSession().getCipherSuite(); |
| 163 | + Integer keySize = Integer.valueOf(ServletSSL.deduceKeyLength(cipherSuite));; |
| 164 | + |
| 165 | + request.setAttribute("javax.servlet.request.cipher_suite", cipherSuite); |
| 166 | + request.setAttribute("javax.servlet.request.key_size", keySize); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if(useCerts) super.customize(endpoint, request); |
| 171 | + } |
| 172 | + |
| 173 | + private void logIfDebug(String s) { |
| 174 | + if(LOG.isDebugEnabled()) |
| 175 | + LOG.debug(s); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Filter that takes the Kerberos principal identified in the |
| 180 | + * {@link Krb5AndCertsSslSocketConnector} and provides it the to the servlet |
| 181 | + * at runtime, setting the principal and short name. |
| 182 | + */ |
| 183 | + public static class Krb5SslFilter implements Filter { |
| 184 | + @Override |
| 185 | + public void doFilter(ServletRequest req, ServletResponse resp, |
| 186 | + FilterChain chain) throws IOException, ServletException { |
| 187 | + final Principal princ = |
| 188 | + (Principal)req.getAttribute(Krb5AndCertsSslSocketConnector.REMOTE_PRINCIPAL); |
| 189 | + |
| 190 | + if(princ == null || !(princ instanceof KerberosPrincipal)) { |
| 191 | + // Should never actually get here, since should be rejected at socket |
| 192 | + // level. |
| 193 | + LOG.warn("User not authenticated via kerberos from " + req.getRemoteAddr()); |
| 194 | + ((HttpServletResponse)resp).sendError(HttpServletResponse.SC_FORBIDDEN, |
| 195 | + "User not authenticated via Kerberos"); |
| 196 | + return; |
| 197 | + } |
| 198 | + |
| 199 | + // Provide principal information for servlet at runtime |
| 200 | + ServletRequest wrapper = |
| 201 | + new HttpServletRequestWrapper((HttpServletRequest) req) { |
| 202 | + @Override |
| 203 | + public Principal getUserPrincipal() { |
| 204 | + return princ; |
| 205 | + } |
| 206 | + |
| 207 | + /* |
| 208 | + * Return the full name of this remote user. |
| 209 | + * @see javax.servlet.http.HttpServletRequestWrapper#getRemoteUser() |
| 210 | + */ |
| 211 | + @Override |
| 212 | + public String getRemoteUser() { |
| 213 | + return princ.getName(); |
| 214 | + } |
| 215 | + }; |
| 216 | + |
| 217 | + chain.doFilter(wrapper, resp); |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public void init(FilterConfig arg0) throws ServletException { |
| 222 | + /* Nothing to do here */ |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public void destroy() { /* Nothing to do here */ } |
| 227 | + } |
| 228 | +} |
0 commit comments