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.thrift;
019
020import static org.apache.hadoop.hbase.thrift.Constants.THRIFT_SUPPORT_PROXYUSER_KEY;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertNotNull;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025import java.io.File;
026import java.nio.file.Paths;
027import java.security.Principal;
028import java.security.PrivilegedExceptionAction;
029import java.util.Set;
030import java.util.function.Supplier;
031import javax.security.auth.Subject;
032import javax.security.auth.kerberos.KerberosTicket;
033import org.apache.hadoop.conf.Configuration;
034import org.apache.hadoop.hbase.HBaseTestingUtil;
035import org.apache.hadoop.hbase.HConstants;
036import org.apache.hadoop.hbase.security.HBaseKerberosUtils;
037import org.apache.hadoop.hbase.testclassification.ClientTests;
038import org.apache.hadoop.hbase.testclassification.LargeTests;
039import org.apache.hadoop.hbase.thrift.generated.Hbase;
040import org.apache.hadoop.hbase.util.SimpleKdcServerUtil;
041import org.apache.hadoop.security.authentication.util.KerberosName;
042import org.apache.http.HttpHeaders;
043import org.apache.http.auth.AuthSchemeProvider;
044import org.apache.http.auth.AuthScope;
045import org.apache.http.auth.KerberosCredentials;
046import org.apache.http.client.config.AuthSchemes;
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.CloseableHttpClient;
052import org.apache.http.impl.client.HttpClients;
053import org.apache.kerby.kerberos.kerb.client.JaasKrbUtil;
054import org.apache.kerby.kerberos.kerb.server.SimpleKdcServer;
055import org.apache.thrift.protocol.TBinaryProtocol;
056import org.apache.thrift.protocol.TProtocol;
057import org.apache.thrift.transport.THttpClient;
058import org.ietf.jgss.GSSCredential;
059import org.ietf.jgss.GSSManager;
060import org.ietf.jgss.GSSName;
061import org.ietf.jgss.Oid;
062import org.junit.jupiter.api.AfterAll;
063import org.junit.jupiter.api.BeforeAll;
064import org.junit.jupiter.api.Disabled;
065import org.junit.jupiter.api.Tag;
066import org.junit.jupiter.api.Test;
067import org.slf4j.Logger;
068import org.slf4j.LoggerFactory;
069
070/**
071 * Start the HBase Thrift HTTP server on a random port through the command-line interface and talk
072 * to it from client side with SPNEGO security enabled. Supplemental test to
073 * TestThriftSpnegoHttpServer which falls back to the original Kerberos principal and keytab
074 * configuration properties, not the separate SPNEGO-specific properties.
075 */
076@Tag(ClientTests.TAG)
077@Tag(LargeTests.TAG)
078public class TestThriftSpnegoHttpFallbackServer extends TestThriftHttpServerBase {
079
080  private static final Logger LOG =
081    LoggerFactory.getLogger(TestThriftSpnegoHttpFallbackServer.class);
082
083  private static SimpleKdcServer kdc;
084  private static File serverKeytab;
085  private static File clientKeytab;
086
087  private static String clientPrincipal;
088  private static String serverPrincipal;
089  private static String spnegoServerPrincipal;
090
091  private static void addSecurityConfigurations(Configuration conf) {
092    KerberosName.setRules("DEFAULT");
093
094    HBaseKerberosUtils.setKeytabFileForTesting(serverKeytab.getAbsolutePath());
095
096    conf.setBoolean(THRIFT_SUPPORT_PROXYUSER_KEY, true);
097    conf.setBoolean(Constants.USE_HTTP_CONF_KEY, true);
098
099    conf.set(Constants.THRIFT_KERBEROS_PRINCIPAL_KEY, serverPrincipal);
100    conf.set(Constants.THRIFT_KEYTAB_FILE_KEY, serverKeytab.getAbsolutePath());
101
102    HBaseKerberosUtils.setSecuredConfiguration(conf, spnegoServerPrincipal, spnegoServerPrincipal);
103    conf.set("hadoop.proxyuser.HTTP.hosts", "*");
104    conf.set("hadoop.proxyuser.HTTP.groups", "*");
105    conf.set(Constants.THRIFT_KERBEROS_PRINCIPAL_KEY, spnegoServerPrincipal);
106  }
107
108  @BeforeAll
109  public static void beforeAll() throws Exception {
110    kdc = SimpleKdcServerUtil.getRunningSimpleKdcServer(
111      new File(TEST_UTIL.getDataTestDir().toString()), HBaseTestingUtil::randomFreePort);
112
113    File keytabDir = Paths.get(TEST_UTIL.getRandomDir().toString()).toAbsolutePath().toFile();
114    assertTrue(keytabDir.mkdirs());
115
116    clientPrincipal = "client@" + kdc.getKdcConfig().getKdcRealm();
117    clientKeytab = new File(keytabDir, clientPrincipal + ".keytab");
118    kdc.createAndExportPrincipals(clientKeytab, clientPrincipal);
119
120    serverPrincipal = "hbase/" + HConstants.LOCALHOST + "@" + kdc.getKdcConfig().getKdcRealm();
121    serverKeytab = new File(keytabDir, serverPrincipal.replace('/', '_') + ".keytab");
122
123    spnegoServerPrincipal = "HTTP/" + HConstants.LOCALHOST + "@" + kdc.getKdcConfig().getKdcRealm();
124    // Add SPNEGO principal to server keytab
125    kdc.createAndExportPrincipals(serverKeytab, serverPrincipal, spnegoServerPrincipal);
126
127    TEST_UTIL.getConfiguration().setBoolean(Constants.USE_HTTP_CONF_KEY, true);
128    addSecurityConfigurations(TEST_UTIL.getConfiguration());
129
130    TestThriftHttpServerBase.setUpBeforeClass();
131  }
132
133  @AfterAll
134  public static void afterAll() throws Exception {
135    TestThriftHttpServerBase.tearDownAfterClass();
136
137    try {
138      if (null != kdc) {
139        kdc.stop();
140        kdc = null;
141      }
142    } catch (Exception e) {
143      LOG.info("Failed to stop mini KDC", e);
144    }
145  }
146
147  @Override
148  protected void talkToThriftServer(String url, int customHeaderSize) throws Exception {
149    // Close httpClient and THttpClient automatically on any failures
150    try (CloseableHttpClient httpClient = createHttpClient();
151      THttpClient tHttpClient = new THttpClient(url, httpClient)) {
152      tHttpClient.open();
153      if (customHeaderSize > 0) {
154        StringBuilder sb = new StringBuilder();
155        for (int i = 0; i < customHeaderSize; i++) {
156          sb.append("a");
157        }
158        tHttpClient.setCustomHeader(HttpHeaders.USER_AGENT, sb.toString());
159      }
160
161      TProtocol prot = new TBinaryProtocol(tHttpClient);
162      Hbase.Client client = new Hbase.Client(prot);
163      TestThriftServer.createTestTables(client);
164      TestThriftServer.checkTableList(client);
165      TestThriftServer.dropTestTables(client);
166    }
167  }
168
169  private CloseableHttpClient createHttpClient() throws Exception {
170    final Subject clientSubject = JaasKrbUtil.loginUsingKeytab(clientPrincipal, clientKeytab);
171    final Set<Principal> clientPrincipals = clientSubject.getPrincipals();
172    // Make sure the subject has a principal
173    assertFalse(clientPrincipals.isEmpty(), "Found no client principals in the clientSubject.");
174
175    // Get a TGT for the subject (might have many, different encryption types). The first should
176    // be the default encryption type.
177    Set<KerberosTicket> privateCredentials =
178      clientSubject.getPrivateCredentials(KerberosTicket.class);
179    assertFalse(privateCredentials.isEmpty(), "Found no private credentials in the clientSubject.");
180    KerberosTicket tgt = privateCredentials.iterator().next();
181    assertNotNull(tgt, "No kerberos ticket found.");
182
183    // The name of the principal
184    final String clientPrincipalName = clientPrincipals.iterator().next().getName();
185
186    return Subject.doAs(clientSubject, (PrivilegedExceptionAction<CloseableHttpClient>) () -> {
187      // Logs in with Kerberos via GSS
188      GSSManager gssManager = GSSManager.getInstance();
189      // jGSS Kerberos login constant
190      Oid oid = new Oid("1.2.840.113554.1.2.2");
191      GSSName gssClient = gssManager.createName(clientPrincipalName, GSSName.NT_USER_NAME);
192      GSSCredential credential = gssManager.createCredential(gssClient,
193        GSSCredential.DEFAULT_LIFETIME, oid, GSSCredential.INITIATE_ONLY);
194
195      Lookup<AuthSchemeProvider> authRegistry = RegistryBuilder.<AuthSchemeProvider> create()
196        .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, true)).build();
197
198      BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
199      credentialsProvider.setCredentials(AuthScope.ANY, new KerberosCredentials(credential));
200
201      return HttpClients.custom().setDefaultAuthSchemeRegistry(authRegistry)
202        .setDefaultCredentialsProvider(credentialsProvider).build();
203    });
204  }
205
206  @Override
207  protected Supplier<ThriftServer> getThriftServerSupplier() {
208    return () -> new ThriftServer(TEST_UTIL.getConfiguration());
209  }
210
211  /**
212   * Block call through to this method. It is a messy test that fails because of bad config and then
213   * succeeds only the first attempt adds a table which the second attempt doesn't want to be in
214   * place to succeed. Let the super impl of this test be responsible for verifying we fail if bad
215   * header size.
216   */
217  @Disabled
218  @Test
219  @Override
220  public void testRunThriftServerWithHeaderBufferLength() throws Exception {
221    super.testRunThriftServerWithHeaderBufferLength();
222  }
223}