/*
 * 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.security;

import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.getClientKeytabForTesting;
import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.getClientPrincipalForTesting;
import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.getKeytabFileForTesting;
import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.getPrincipalForTesting;
import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.getSecuredConfiguration;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.AuthUtil;
import org.apache.hadoop.hbase.HBaseTestingUtility;
import org.apache.hadoop.hbase.testclassification.SecurityTests;
import org.apache.hadoop.hbase.testclassification.SmallTests;
import org.apache.hadoop.minikdc.MiniKdc;
import org.apache.hadoop.security.UserGroupInformation;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag(SecurityTests.TAG)
@Tag(SmallTests.TAG)
public class TestUsersOperationsWithSecureHadoop {

  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
  private static final File KEYTAB_FILE =
    new File(TEST_UTIL.getDataTestDir("keytab").toUri().getPath());

  private static MiniKdc KDC;

  private static String HOST = "localhost";

  private static String PRINCIPAL;

  private static String CLIENT_NAME;

  private static String OTHER_CLIENT_NAME;

  @BeforeAll
  public static void setUp() throws Exception {
    KDC = TEST_UTIL.setupMiniKdc(KEYTAB_FILE);
    PRINCIPAL = "hbase/" + HOST;
    CLIENT_NAME = "foo";
    OTHER_CLIENT_NAME = "bar";
    KDC.createPrincipal(KEYTAB_FILE, PRINCIPAL, CLIENT_NAME, OTHER_CLIENT_NAME);
    HBaseKerberosUtils.setPrincipalForTesting(PRINCIPAL + "@" + KDC.getRealm());
    HBaseKerberosUtils.setKeytabFileForTesting(KEYTAB_FILE.getAbsolutePath());
    HBaseKerberosUtils.setClientPrincipalForTesting(CLIENT_NAME + "@" + KDC.getRealm());
    HBaseKerberosUtils.setClientKeytabForTesting(KEYTAB_FILE.getAbsolutePath());
  }

  @AfterAll
  public static void tearDown() throws IOException {
    if (KDC != null) {
      KDC.stop();
    }
    TEST_UTIL.cleanupTestDir();
  }

  /**
   * test login with security enabled configuration To run this test, we must specify the following
   * system properties:
   * <p>
   * <b> hbase.regionserver.kerberos.principal </b>
   * <p>
   * <b> hbase.regionserver.keytab.file </b>
   */
  @Test
  public void testUserLoginInSecureHadoop() throws Exception {
    // Default login is system user.
    UserGroupInformation defaultLogin = UserGroupInformation.getCurrentUser();

    String nnKeyTab = getKeytabFileForTesting();
    String dnPrincipal = getPrincipalForTesting();

    assertNotNull("KerberosKeytab was not specified", nnKeyTab);
    assertNotNull("KerberosPrincipal was not specified", dnPrincipal);

    Configuration conf = getSecuredConfiguration();
    UserGroupInformation.setConfiguration(conf);

    User.login(conf, HBaseKerberosUtils.KRB_KEYTAB_FILE, HBaseKerberosUtils.KRB_PRINCIPAL,
      "localhost");
    UserGroupInformation successLogin = UserGroupInformation.getLoginUser();
    assertFalse(defaultLogin.equals(successLogin),
      "ugi should be different in in case success login");
  }

  @Test
  public void testLoginWithUserKeytabAndPrincipal() throws Exception {
    String clientKeytab = getClientKeytabForTesting();
    String clientPrincipal = getClientPrincipalForTesting();
    assertNotNull("Path for client keytab is not specified.", clientKeytab);
    assertNotNull("Client principal is not specified.", clientPrincipal);

    Configuration conf = getSecuredConfiguration();
    conf.set(AuthUtil.HBASE_CLIENT_KEYTAB_FILE, clientKeytab);
    conf.set(AuthUtil.HBASE_CLIENT_KERBEROS_PRINCIPAL, clientPrincipal);
    UserGroupInformation.setConfiguration(conf);

    UserProvider provider = UserProvider.instantiate(conf);
    assertTrue(provider.shouldLoginFromKeytab(), "Client principal or keytab is empty");

    provider.login(AuthUtil.HBASE_CLIENT_KEYTAB_FILE, AuthUtil.HBASE_CLIENT_KERBEROS_PRINCIPAL);
    User loginUser = provider.getCurrent();
    assertEquals(CLIENT_NAME, loginUser.getShortName());
    assertEquals(getClientPrincipalForTesting(), loginUser.getName());
  }

  @Test
  public void testAuthUtilLoginWithExistingLoginUser() throws Exception {
    String clientKeytab = getClientKeytabForTesting();
    String clientPrincipal = getClientPrincipalForTesting();
    Configuration conf = getSecuredConfiguration();
    conf.set(AuthUtil.HBASE_CLIENT_KEYTAB_FILE, clientKeytab);
    conf.set(AuthUtil.HBASE_CLIENT_KERBEROS_PRINCIPAL, clientPrincipal);
    UserGroupInformation.setConfiguration(conf);

    UserGroupInformation.loginUserFromKeytab(CLIENT_NAME, clientKeytab);

    User user = AuthUtil.loginClient(conf);
    assertTrue(user.isLoginFromKeytab());
    assertEquals(CLIENT_NAME, user.getShortName());
    assertEquals(getClientPrincipalForTesting(), user.getName());
  }

  @Test
  public void testAuthUtilLoginWithDifferentExistingUser() throws Exception {
    String clientKeytab = getClientKeytabForTesting();
    String clientPrincipal = getClientPrincipalForTesting();
    Configuration conf = getSecuredConfiguration();
    conf.set(AuthUtil.HBASE_CLIENT_KEYTAB_FILE, clientKeytab);
    conf.set(AuthUtil.HBASE_CLIENT_KERBEROS_PRINCIPAL, clientPrincipal);
    UserGroupInformation.setConfiguration(conf);

    // Login with other principal first
    String otherPrincipal = OTHER_CLIENT_NAME + "@" + KDC.getRealm();
    UserGroupInformation.loginUserFromKeytab(otherPrincipal, clientKeytab);

    User user = AuthUtil.loginClient(conf);
    assertTrue(user.isLoginFromKeytab());
    // The existing login user (bar) doesn't match the principal configured in
    // HBASE_CLIENT_KERBEROS_PRINCIPAL (foo), so loginClient should re-login
    // with the configured principal.
    assertEquals(CLIENT_NAME, user.getShortName());
    assertEquals(getClientPrincipalForTesting(), user.getName());

    conf.set(AuthUtil.HBASE_CLIENT_KERBEROS_PRINCIPAL, otherPrincipal);

    user = AuthUtil.loginClient(conf);
    assertTrue(user.isLoginFromKeytab());
    // After updating HBASE_CLIENT_KERBEROS_PRINCIPAL to bar, loginClient should re-login with bar.
    assertEquals(OTHER_CLIENT_NAME, user.getShortName());
    assertEquals(otherPrincipal, user.getName());
  }
}
