1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.thrift;
20
21 import java.io.IOException;
22 import java.security.PrivilegedExceptionAction;
23
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.hbase.classification.InterfaceAudience;
32 import org.apache.hadoop.hbase.security.SecurityUtil;
33 import org.apache.hadoop.hbase.util.Base64;
34 import org.apache.hadoop.security.UserGroupInformation;
35 import org.apache.hadoop.security.authorize.AuthorizationException;
36 import org.apache.hadoop.security.authorize.ProxyUsers;
37 import org.apache.thrift.TProcessor;
38 import org.apache.thrift.protocol.TProtocolFactory;
39 import org.apache.thrift.server.TServlet;
40 import org.ietf.jgss.GSSContext;
41 import org.ietf.jgss.GSSCredential;
42 import org.ietf.jgss.GSSException;
43 import org.ietf.jgss.GSSManager;
44 import org.ietf.jgss.GSSName;
45 import org.ietf.jgss.Oid;
46
47
48
49
50
51 @InterfaceAudience.Private
52 public class ThriftHttpServlet extends TServlet {
53 private static final long serialVersionUID = 1L;
54 public static final Log LOG = LogFactory.getLog(ThriftHttpServlet.class.getName());
55 private transient final UserGroupInformation realUser;
56 private transient final Configuration conf;
57 private final boolean securityEnabled;
58 private final boolean doAsEnabled;
59 private transient ThriftServerRunner.HBaseHandler hbaseHandler;
60 private String outToken;
61
62
63 public static final String WWW_AUTHENTICATE = "WWW-Authenticate";
64 public static final String AUTHORIZATION = "Authorization";
65 public static final String NEGOTIATE = "Negotiate";
66
67 public ThriftHttpServlet(TProcessor processor, TProtocolFactory protocolFactory,
68 UserGroupInformation realUser, Configuration conf, ThriftServerRunner.HBaseHandler
69 hbaseHandler, boolean securityEnabled, boolean doAsEnabled) {
70 super(processor, protocolFactory);
71 this.realUser = realUser;
72 this.conf = conf;
73 this.hbaseHandler = hbaseHandler;
74 this.securityEnabled = securityEnabled;
75 this.doAsEnabled = doAsEnabled;
76 }
77
78 @Override
79 protected void doPost(HttpServletRequest request, HttpServletResponse response)
80 throws ServletException, IOException {
81 String effectiveUser = request.getRemoteUser();
82 if (securityEnabled) {
83 try {
84
85
86 effectiveUser = doKerberosAuth(request);
87
88
89 response.addHeader(WWW_AUTHENTICATE, NEGOTIATE + " " + outToken);
90 } catch (HttpAuthenticationException e) {
91 LOG.error("Kerberos Authentication failed", e);
92
93 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
94 response.addHeader(WWW_AUTHENTICATE, NEGOTIATE);
95 response.getWriter().println("Authentication Error: " + e.getMessage());
96 return;
97 }
98 }
99 String doAsUserFromQuery = request.getHeader("doAs");
100 if(effectiveUser == null) {
101 effectiveUser = realUser.getShortUserName();
102 }
103 if (doAsUserFromQuery != null) {
104 if (!doAsEnabled) {
105 throw new ServletException("Support for proxyuser is not configured");
106 }
107
108 UserGroupInformation remoteUser = UserGroupInformation.createRemoteUser(effectiveUser);
109
110
111 UserGroupInformation ugi = UserGroupInformation.createProxyUser(doAsUserFromQuery,
112 remoteUser);
113
114 try {
115 ProxyUsers.authorize(ugi, request.getRemoteAddr(), conf);
116 } catch (AuthorizationException e) {
117 throw new ServletException(e.getMessage());
118 }
119 effectiveUser = doAsUserFromQuery;
120 }
121 hbaseHandler.setEffectiveUser(effectiveUser);
122 super.doPost(request, response);
123 }
124
125
126
127
128
129
130 private String doKerberosAuth(HttpServletRequest request)
131 throws HttpAuthenticationException {
132 HttpKerberosServerAction action = new HttpKerberosServerAction(request, realUser);
133 try {
134 String principal = realUser.doAs(action);
135 outToken = action.outToken;
136 return principal;
137 } catch (Exception e) {
138 LOG.error("Failed to perform authentication");
139 throw new HttpAuthenticationException(e);
140 }
141 }
142
143
144 private static class HttpKerberosServerAction implements PrivilegedExceptionAction<String> {
145 HttpServletRequest request;
146 UserGroupInformation serviceUGI;
147 String outToken = null;
148 HttpKerberosServerAction(HttpServletRequest request, UserGroupInformation serviceUGI) {
149 this.request = request;
150 this.serviceUGI = serviceUGI;
151 }
152
153 @Override
154 public String run() throws HttpAuthenticationException {
155
156 GSSManager manager = GSSManager.getInstance();
157 GSSContext gssContext = null;
158 String serverPrincipal = SecurityUtil.getPrincipalWithoutRealm(serviceUGI.getUserName());
159 try {
160
161 Oid kerberosMechOid = new Oid("1.2.840.113554.1.2.2");
162
163 Oid spnegoMechOid = new Oid("1.3.6.1.5.5.2");
164
165 Oid krb5PrincipalOid = new Oid("1.2.840.113554.1.2.2.1");
166
167 GSSName serverName = manager.createName(serverPrincipal, krb5PrincipalOid);
168
169 GSSCredential serverCreds = manager.createCredential(serverName,
170 GSSCredential.DEFAULT_LIFETIME,
171 new Oid[]{kerberosMechOid, spnegoMechOid},
172 GSSCredential.ACCEPT_ONLY);
173
174 gssContext = manager.createContext(serverCreds);
175
176 String serviceTicketBase64 = getAuthHeader(request);
177 byte[] inToken = Base64.decode(serviceTicketBase64);
178 byte[] res = gssContext.acceptSecContext(inToken, 0, inToken.length);
179 if(res != null) {
180 outToken = Base64.encodeBytes(res).replace("\n", "");
181 }
182
183 if (!gssContext.isEstablished()) {
184 throw new HttpAuthenticationException("Kerberos authentication failed: " +
185 "unable to establish context with the service ticket " +
186 "provided by the client.");
187 }
188 return SecurityUtil.getUserFromPrincipal(gssContext.getSrcName().toString());
189 } catch (GSSException e) {
190 throw new HttpAuthenticationException("Kerberos authentication failed: ", e);
191 } finally {
192 if (gssContext != null) {
193 try {
194 gssContext.dispose();
195 } catch (GSSException e) {
196 LOG.warn("Error while disposing GSS Context", e);
197 }
198 }
199 }
200 }
201
202
203
204
205
206
207 private String getAuthHeader(HttpServletRequest request)
208 throws HttpAuthenticationException {
209 String authHeader = request.getHeader(AUTHORIZATION);
210
211 if (authHeader == null || authHeader.isEmpty()) {
212 throw new HttpAuthenticationException("Authorization header received " +
213 "from the client is empty.");
214 }
215 String authHeaderBase64String;
216 int beginIndex = (NEGOTIATE + " ").length();
217 authHeaderBase64String = authHeader.substring(beginIndex);
218
219 if (authHeaderBase64String == null || authHeaderBase64String.isEmpty()) {
220 throw new HttpAuthenticationException("Authorization header received " +
221 "from the client does not contain any data.");
222 }
223 return authHeaderBase64String;
224 }
225 }
226 }