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