/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.cli.validation;

import alluxio.PropertyKey;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Task for validating security configurations.
 */
public class SecureHdfsValidationTask implements ValidationTask {
  private static final String ALLUXIO_BIN = "./bin/alluxio";
  private static final String GETCONF_CMD = "getConf";
  /**
   * Regular expression to parse principal used by Alluxio to connect to secure
   * HDFS.
   *
   * @see <a href="https://web.mit.edu/kerberos/krb5-1.5/krb5-1.5.4/doc/krb5-user/What-is-a-Kerberos-Principal_003f.html">Kerberos documentation</a>
   * for more details.
   */
  private static final Pattern PRINCIPAL_PATTERN =
      Pattern.compile("(?<primary>[\\w][\\w-]*\\$?)(/(?<instance>[\\w]+))?(@(?<realm>[\\w]+))?");

  private final String mProcess;

  /**
   * Constructor of {@link SecureHdfsValidationTask}.
   *
   * @param process type of the process on behalf of which this validation task is run
   */
  public SecureHdfsValidationTask(String process) {
    mProcess = process.toLowerCase();
  }

  @Override
  public boolean validate() {
    // Check whether can login with specified principal and keytab
    String principal = "";
    String keytab = "";
    String primary;
    String instance;
    String realm;
    if (mProcess.equals("master")) {
      principal = Utils.getResultFromProcess(new String[] {ALLUXIO_BIN, GETCONF_CMD,
          PropertyKey.MASTER_PRINCIPAL.getName()}).getOutput();
      keytab = Utils.getResultFromProcess(new String[] {ALLUXIO_BIN, GETCONF_CMD,
          PropertyKey.MASTER_KEYTAB_KEY_FILE.getName()}).getOutput();
    } else if (mProcess.equals("worker")) {
      principal = Utils.getResultFromProcess(new String[] {ALLUXIO_BIN, GETCONF_CMD,
          PropertyKey.WORKER_PRINCIPAL.getName()}).getOutput();
      keytab = Utils.getResultFromProcess(new String[] {ALLUXIO_BIN, GETCONF_CMD,
          PropertyKey.WORKER_KEYTAB_FILE.getName()}).getOutput();
    }
    Matcher matchPrincipal = PRINCIPAL_PATTERN.matcher(principal);
    if (!matchPrincipal.matches()) {
      System.err.format("Principal %s is not in the right format.%n", principal);
      return false;
    }
    primary = matchPrincipal.group("primary");
    instance = matchPrincipal.group("instance");
    realm = matchPrincipal.group("realm");

    // Login with principal and keytab
    int exitVal =
        Utils.getResultFromProcess(new String[] {"kinit", "-kt", keytab, principal}).getExitValue();
    if (exitVal != 0) {
      System.err.format("Kerberos login failed for %s with keytab %s with exit value %d.%n",
          principal, keytab, exitVal);
      System.err.format("Primary is %s, instance is %s and realm is %s.%n",
          primary, instance, realm);
      return false;
    }
    return true;
  }
}

@@ -540,16 +540,46 @@
 ation;%0A%0A
+import alluxio.Configuration;%0A
 import a
@@ -1897,231 +1897,90 @@
 l = 
-Utils.getResultFromProcess(new String%5B%5D %7BALLUXIO_BIN, GETCONF_CMD,%0A          PropertyKey.MASTER_PRINCIPAL.getName()%7D).getOutput();%0A      keytab = Utils.getResultFromProcess(new String%5B%5D %7BALLUXIO_BIN, GETCONF_CMD,%0A          
+Configuration.get(PropertyKey.MASTER_PRINCIPAL);%0A      keytab = Configuration.get(
 Prop
@@ -2009,39 +2009,16 @@
 KEY_FILE
-.getName()%7D).getOutput(
 );%0A    %7D
@@ -2078,231 +2078,90 @@
 l = 
-Utils.getResultFromProcess(new String%5B%5D %7BALLUXIO_BIN, GETCONF_CMD,%0A          PropertyKey.WORKER_PRINCIPAL.getName()%7D).getOutput();%0A      keytab = Utils.getResultFromProcess(new String%5B%5D %7BALLUXIO_BIN, GETCONF_CMD,%0A          
+Configuration.get(PropertyKey.WORKER_PRINCIPAL);%0A      keytab = Configuration.get(
 Prop
@@ -2190,31 +2190,8 @@
 FILE
-.getName()%7D).getOutput(
 );%0A 

Use Configuration to obtain value of properties.