spark-submit cluster模式业务开发 --解决 配置文件读取不到、 安全认证不 ...

打印 上一主题 下一主题

主题 861|帖子 861|积分 2593

个人主页:500佰
个人专栏:大数据组件性能调优-登上高阶
#spark #spark-submit #spark-sql #spark业务开发 #大数据 #生产环境真实案例 #整理 #履历总结
阐明:本专题:spark-submit cluster模式业务开发 含完备代码
业务场景:spark-sql类型任务

本案例:可以解决 配置文件读取不到(如keytab、krb5.conf、spark配置文件)、 安全认证不过等题目
本案例目标:使用spark-submit cluster模式 提交spark-sql类型任务


  • cluster 模式优势:
    相对client模式,driver端在集群内部,资源更好管理,减少垮节点IO
  • cluster模式常见题目:
    相对client模式,存在读取不到配置文件及安全认证不过 √
    本案例:可以解决 配置文件读取不到、 安全认证不过等题目
  • 调用slala-sdk版本要求:2.12.14



  • 调用示例:
        userSparkSQL.sh 示例如下:
  1. #!/bin/bash
  2. function envInit(){
  3. spark_submit_path=$(dirname "${BASH_SOURCE-$0}")
  4. spark_submit_path=$(cd "$spark_submit_path"; pwd)
  5. client=$(cd "$spark_submit_path"/../../../;pwd)
  6. source "$client"/bigdata_env
  7. }
  8. envInit
  9. ${spark_submit_path}/spark-submit \
  10. --master yarn \
  11. --deploy-mode cluster \
  12. --conf spark.sql.autoBroadcastJoinThreshold=-1 \
  13. --conf spark.driver.memory=50g \
  14. --conf spark.logOptimization.enable=false \
  15. --conf spark.sql.shuffle.partitions=400 \
  16. --conf spark.serializer=org.apache.spark.serializer.KryoSerializer \
  17. --conf spark.network.timeout=300 \
  18. --num-executors 12 \
  19. --executor-memory 6g \
  20. --executor-cores 10 \
  21. --name ExecSqlc\
  22. --files /opt/client2/Spark2x/spark/bin/conf/user.keytab,/opt/client2/Spark2x/spark/bin/conf/krb5.conf,/opt/client2/Spark2x/spark/bin/conf/2.hql \
  23. --class com.xx.spark.security.bigdata.ExecSqlc /opt/client2/Spark2x/spark/bin/userSparkSQL.jar \
  24. --userPrincipal=hcqs \
  25. --sparkSqlPath=/opt/client2/Spark2x/spark/bin/conf/2.hql \
  26. --params="cloumn1=0201,cloumn2=2059881123"
复制代码
        Hive SQL文件(示例的x.hql文件)内里的参数示例:
  1. select * from tb where cloumn1=$cloumn1 and cloumn2=$cloumn2
复制代码
业务代码

主类 object ExecSqlc:

  1. package com.mrs.spark.security.bigdata
  2. import com.mrs.spark.security.hadoop.LoginUtil
  3. import org.apache.log4j.Logger
  4. import org.apache.spark.sql.{DataFrame, SparkSession}
  5. import org.apache.spark.storage.StorageLevel
  6. import java.io.File
  7. import scala.io.Source
  8. /**
  9. * 执行spark-sql类
  10. * spark-submit driver cluster
  11. *
  12. * @param userPrincipal ,userKeytabPath,krb5ConfPath
  13. * @return
  14. * @author 500佰
  15. */
  16. object ExecSqlc {
  17.   val logger = LoginUtil.LOG
  18.   // Table structure
  19.   def readSparkSQLFile(sparkSqlPath: String, variables: Map[String, String], sparkSession: SparkSession): String = {
  20.     val source = Source.fromFile(sparkSqlPath) // driver client model
  21.     try {
  22.       val sqlString = source.getLines().map { line =>
  23.         variables.foldLeft(line) { (acc, entry) =>
  24.           acc.replaceAllLiterally(s"$$${entry._1}", entry._2)
  25.         }
  26.       }.mkString("\n")
  27.       sqlString
  28.     } finally {
  29.       source.close()
  30.     }
  31.   }
  32.   def main(args: Array[String]) {
  33.     val logger = Logger.getLogger(getClass.getName)
  34.     val paramMap = args.map(_.split("=", 2)).collect {
  35.       case Array(key, value) if key.startsWith("--") => key.stripPrefix("--") -> value.trim
  36.     }.toMap
  37.     val userPrincipal = paramMap.getOrElse("userPrincipal", "")
  38.     //    val userKeytabPath = paramMap.getOrElse("userKeytabPath", "")
  39.     //    val krb5ConfPath = paramMap.getOrElse("krb5ConfPath", "")
  40.     val sparkSqlPath = paramMap.getOrElse("sparkSqlPath", "")
  41.     val filePath = System.getProperty("user.dir") + File.separator + sparkSqlPath.split("/").last
  42.     val params = paramMap.getOrElse("params", "").split(",").map(_.split("=")).map(arr => arr(0) -> arr(1)).toMap
  43.     //    try {
  44.     LoginUtil.loginWithUserKeytab(userPrincipal)
  45.     logger.info("认证成功!")
  46.     // Configure SparkSession & Spark application name
  47.     val appName = sparkSqlPath.split("/").last.split("\\.")(0)
  48.     val spark = SparkSession
  49.       .builder()
  50.       .appName(s"ExecUserSqlc-${appName}")
  51.       .getOrCreate()
  52.     //sparkSql Content
  53.     val sqlContent = readSparkSQLFile(filePath, params, spark)
  54.     val sqlc: String = sqlContent.stripMargin //format
  55.     logger.info(s"执行语句:${sqlc}")
  56.     logger.info(s"开始执行:${sparkSqlPath}")
  57.     val sqlStatements: Array[String] = sqlc.split(";")
  58.     //遍历顺序执行
  59.     for (statement <- sqlStatements) {
  60.       val Info: DataFrame = spark.sql(statement).distinct().persist(StorageLevel.MEMORY_AND_DISK)
  61.       logger.info(s"执行结果:${Info.show(2)}")
  62.     }
  63.     spark.stop()
  64.     //      System.exit(0)
  65.     //    } catch {
  66.     //      case ex: Exception => logger.error("发生了异常:" + ex.getMessage)
  67.     //        System.exit(1)
  68.     //    }
  69.   }
  70. }
复制代码
其他代码:

com.xx.spark.security.hadoop.KerberosUtil类:

  1. package com.mrs.spark.security.hadoop;
  2. import org.apache.log4j.Logger;
  3. import java.lang.reflect.Method;
  4. public class KerberosUtil {
  5.     private static Logger logger = Logger.getLogger(KerberosUtil.class);
  6.     public static final String JAVA_VENDER = "java.vendor";
  7.     public static final String IBM_FLAG = "IBM";
  8.     public static final String CONFIG_CLASS_FOR_IBM = "com.ibm.security.krb5.internal.Config";
  9.     public static final String CONFIG_CLASS_FOR_SUN = "sun.security.krb5.Config";
  10.     public static final String METHOD_GET_INSTANCE = "getInstance";
  11.     public static final String METHOD_GET_DEFAULT_REALM = "getDefaultRealm";
  12.     public static final String DEFAULT_REALM = "HADOOP.COM";
  13.     // UUID principal
  14.     // principal=spark2x/hadoop.868023f3-49f9-4be4-ae96-bc723d246362.com@868023F3-49F9-4BE4-AE96-BC723D246362.COM
  15.     // Default principal principal=spark2x/hadoop.hadoop.com@HADOOP.COM
  16.     public static String getKrb5DomainRealm() {
  17.         Class<?> krb5ConfClass;
  18.         String peerRealm;
  19.         try {
  20.             if (System.getProperty(JAVA_VENDER).contains(IBM_FLAG)) {
  21.                 krb5ConfClass = Class.forName(CONFIG_CLASS_FOR_IBM);
  22.             } else {
  23.                 krb5ConfClass = Class.forName(CONFIG_CLASS_FOR_SUN);
  24.             }
  25.             Method getInstanceMethod = krb5ConfClass.getMethod(METHOD_GET_INSTANCE);
  26.             Object kerbConf = getInstanceMethod.invoke(krb5ConfClass);
  27.             Method getDefaultRealmMethod = krb5ConfClass.getDeclaredMethod(METHOD_GET_DEFAULT_REALM);
  28.             peerRealm = (String) getDefaultRealmMethod.invoke(kerbConf);
  29.             logger.info("Get default realm successfully, the realm is : " + peerRealm);
  30.         } catch (Exception e) {
  31.             peerRealm = DEFAULT_REALM;
  32.             logger.warn("Get default realm failed, use default value : " + DEFAULT_REALM);
  33.         }
  34.         return peerRealm;
  35.     }
  36. }
复制代码
com.xx.spark.security.hadoop.LoginUtil类:

  1. package com.mrs.spark.security.hadoop;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.security.UserGroupInformation;
  4. import org.apache.hadoop.security.authentication.util.KerberosUtil;
  5. import org.apache.log4j.Logger;
  6. import javax.security.auth.login.AppConfigurationEntry;
  7. import javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag;
  8. import java.io.File;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. public class LoginUtil {
  14.     public enum Module {
  15.         STORM("StormClient"),
  16.         KAFKA("KafkaClient"),
  17.         ZOOKEEPER("Client");
  18.         private String name;
  19.         private Module(String name) {
  20.             this.name = name;
  21.         }
  22.         public String getName() {
  23.             return name;
  24.         }
  25.     }
  26.     public static final Logger LOG = Logger.getLogger(LoginUtil.class);
  27.     /**
  28.      * line operator string
  29.      */
  30.     private static final String LINE_SEPARATOR = System.getProperty("line.separator");
  31.     /**
  32.      * jaas file postfix
  33.      */
  34.     private static final String JAAS_POSTFIX = ".jaas.conf";
  35.     /**
  36.      * IBM jdk login module
  37.      */
  38.     private static final String IBM_LOGIN_MODULE = "com.ibm.security.auth.module.Krb5LoginModule required";
  39.     /**
  40.      * oracle jdk login module
  41.      */
  42.     private static final String SUN_LOGIN_MODULE = "com.sun.security.auth.module.Krb5LoginModule required";
  43.     /**
  44.      * java security login file path
  45.      */
  46.     public static final String JAVA_SECURITY_LOGIN_CONF_KEY = "java.security.auth.login.config";
  47.     private static final String JAVA_SECURITY_KRB5_CONF_KEY = "java.security.krb5.conf";
  48.     private static final String ZOOKEEPER_SERVER_PRINCIPAL_KEY = "zookeeper.server.principal";
  49.     private static final String LOGIN_FAILED_CAUSE_PASSWORD_WRONG =
  50.             "(wrong password) keytab file and user not match, you can kinit -k -t keytab user in client server to"
  51.                     + " check";
  52.     private static final String LOGIN_FAILED_CAUSE_TIME_WRONG =
  53.             "(clock skew) time of local server and remote server not match, please check ntp to remote server";
  54.     private static final String LOGIN_FAILED_CAUSE_AES256_WRONG =
  55.             "(aes256 not support) aes256 not support by default jdk/jre, need copy local_policy.jar and"
  56.                     + " US_export_policy.jar from remote server in path /opt/huawei/Bigdata/jdk/jre/lib/security";
  57.     private static final String LOGIN_FAILED_CAUSE_PRINCIPAL_WRONG =
  58.             "(no rule) principal format not support by default, need add property hadoop.security.auth_to_local(in"
  59.                     + " core-site.xml) value RULE:[1:$1] RULE:[2:$1]";
  60.     private static final String LOGIN_FAILED_CAUSE_TIME_OUT =
  61.             "(time out) can not connect to kdc server or there is fire wall in the network";
  62.     private static final boolean IS_IBM_JDK = System.getProperty("java.vendor").contains("IBM");
  63.     public static synchronized void login(
  64.             String userPrincipal, String userKeytabPath, String krb5ConfPath, Configuration conf) throws IOException {
  65.         // 1.check input parameters
  66.         if ((userPrincipal == null) || (userPrincipal.length() <= 0)) {
  67.             LOG.error("input userPrincipal is invalid.");
  68.             throw new IOException("input userPrincipal is invalid.");
  69.         }
  70.         if ((userKeytabPath == null) || (userKeytabPath.length() <= 0)) {
  71.             LOG.error("input userKeytabPath is invalid.");
  72.             throw new IOException("input userKeytabPath is invalid.");
  73.         }
  74.         if ((krb5ConfPath == null) || (krb5ConfPath.length() <= 0)) {
  75.             LOG.error("input krb5ConfPath is invalid.");
  76.             throw new IOException("input krb5ConfPath is invalid.");
  77.         }
  78.         if ((conf == null)) {
  79.             LOG.error("input conf is invalid.");
  80.             throw new IOException("input conf is invalid.");
  81.         }
  82.         // 2.check file exsits
  83.         File userKeytabFile = new File(userKeytabPath);
  84.         if (!userKeytabFile.exists()) {
  85.             LOG.error("userKeytabFile(" + userKeytabFile.getAbsolutePath() + ") does not exsit.");
  86.             throw new IOException("userKeytabFile(" + userKeytabFile.getAbsolutePath() + ") does not exsit.");
  87.         }
  88.         if (!userKeytabFile.isFile()) {
  89.             LOG.error("userKeytabFile(" + userKeytabFile.getAbsolutePath() + ") is not a file.");
  90.             throw new IOException("userKeytabFile(" + userKeytabFile.getAbsolutePath() + ") is not a file.");
  91.         }
  92.         File krb5ConfFile = new File(krb5ConfPath);
  93.         if (!krb5ConfFile.exists()) {
  94.             LOG.error("krb5ConfFile(" + krb5ConfFile.getAbsolutePath() + ") does not exsit.");
  95.             throw new IOException("krb5ConfFile(" + krb5ConfFile.getAbsolutePath() + ") does not exsit.");
  96.         }
  97.         if (!krb5ConfFile.isFile()) {
  98.             LOG.error("krb5ConfFile(" + krb5ConfFile.getAbsolutePath() + ") is not a file.");
  99.             throw new IOException("krb5ConfFile(" + krb5ConfFile.getAbsolutePath() + ") is not a file.");
  100.         }
  101.         // 3.set and check krb5config
  102.         setKrb5Config(krb5ConfFile.getAbsolutePath());
  103.         setConfiguration(conf);
  104.         // 4.login and check for hadoop
  105.         loginHadoop(userPrincipal, userKeytabFile.getAbsolutePath());
  106.         LOG.info("Login success!!!!!!!!!!!!!!");
  107.     }
  108.     private static void setConfiguration(Configuration conf) throws IOException {
  109.         UserGroupInformation.setConfiguration(conf);
  110.     }
  111.     private static boolean checkNeedLogin(String principal) throws IOException {
  112.         if (!UserGroupInformation.isSecurityEnabled()) {
  113.             LOG.error(
  114.                     "UserGroupInformation is not SecurityEnabled, please check if core-site.xml exists in classpath.");
  115.             throw new IOException(
  116.                     "UserGroupInformation is not SecurityEnabled, please check if core-site.xml exists in classpath.");
  117.         }
  118.         UserGroupInformation currentUser = UserGroupInformation.getCurrentUser();
  119.         if ((currentUser != null) && (currentUser.hasKerberosCredentials())) {
  120.             if (checkCurrentUserCorrect(principal)) {
  121.                 LOG.info("current user is " + currentUser + "has logined.");
  122.                 if (!currentUser.isFromKeytab()) {
  123.                     LOG.error("current user is not from keytab.");
  124.                     throw new IOException("current user is not from keytab.");
  125.                 }
  126.                 return false;
  127.             } else {
  128.                 LOG.error(
  129.                         "current user is "
  130.                                 + currentUser
  131.                                 + "has logined. please check your enviroment , especially when it used IBM JDK or"
  132.                                 + " kerberos for OS count login!!");
  133.                 throw new IOException(
  134.                         "current user is " + currentUser + " has logined. And please check your enviroment!!");
  135.             }
  136.         }
  137.         return true;
  138.     }
  139.     public static void setKrb5Config(String krb5ConfFile) throws IOException {
  140.         System.setProperty(JAVA_SECURITY_KRB5_CONF_KEY, krb5ConfFile);
  141.         String ret = System.getProperty(JAVA_SECURITY_KRB5_CONF_KEY);
  142.         if (ret == null) {
  143.             LOG.error(JAVA_SECURITY_KRB5_CONF_KEY + " is null.");
  144.             throw new IOException(JAVA_SECURITY_KRB5_CONF_KEY + " is null.");
  145.         }
  146.         if (!ret.equals(krb5ConfFile)) {
  147.             LOG.error(JAVA_SECURITY_KRB5_CONF_KEY + " is " + ret + " is not " + krb5ConfFile + ".");
  148.             throw new IOException(JAVA_SECURITY_KRB5_CONF_KEY + " is " + ret + " is not " + krb5ConfFile + ".");
  149.         }
  150.     }
  151.     public static void setJaasFile(String principal, String keytabPath) throws IOException {
  152.         String jaasPath =
  153.                 new File(System.getProperty("java.io.tmpdir"))
  154.                         + File.separator
  155.                         + System.getProperty("user.name")
  156.                         + JAAS_POSTFIX;
  157.         // windows路径下分隔符替换
  158.         jaasPath = jaasPath.replace("\", "\\\");
  159.         keytabPath = keytabPath.replace("\", "\\\");
  160.         // 删除jaas文件
  161.         deleteJaasFile(jaasPath);
  162.         writeJaasFile(jaasPath, principal, keytabPath);
  163.         System.setProperty(JAVA_SECURITY_LOGIN_CONF_KEY, jaasPath);
  164.     }
  165.     private static void writeJaasFile(String jaasPath, String principal, String keytabPath) throws IOException {
  166.         FileWriter writer = new FileWriter(new File(jaasPath));
  167.         try {
  168.             writer.write(getJaasConfContext(principal, keytabPath));
  169.             writer.flush();
  170.         } catch (IOException e) {
  171.             throw new IOException("Failed to create jaas.conf File");
  172.         } finally {
  173.             writer.close();
  174.         }
  175.     }
  176.     private static void deleteJaasFile(String jaasPath) throws IOException {
  177.         File jaasFile = new File(jaasPath);
  178.         if (jaasFile.exists()) {
  179.             if (!jaasFile.delete()) {
  180.                 throw new IOException("Failed to delete exists jaas file.");
  181.             }
  182.         }
  183.     }
  184.     private static String getJaasConfContext(String principal, String keytabPath) {
  185.         Module[] allModule = Module.values();
  186.         StringBuilder builder = new StringBuilder();
  187.         for (Module modlue : allModule) {
  188.             builder.append(getModuleContext(principal, keytabPath, modlue));
  189.         }
  190.         return builder.toString();
  191.     }
  192.     private static String getModuleContext(String userPrincipal, String keyTabPath, Module module) {
  193.         StringBuilder builder = new StringBuilder();
  194.         if (IS_IBM_JDK) {
  195.             builder.append(module.getName()).append(" {").append(LINE_SEPARATOR);
  196.             builder.append(IBM_LOGIN_MODULE).append(LINE_SEPARATOR);
  197.             builder.append("credsType=both").append(LINE_SEPARATOR);
  198.             builder.append("principal="" + userPrincipal + """).append(LINE_SEPARATOR);
  199.             builder.append("useKeytab="" + keyTabPath + """).append(LINE_SEPARATOR);
  200.             builder.append("debug=true;").append(LINE_SEPARATOR);
  201.             builder.append("};").append(LINE_SEPARATOR);
  202.         } else {
  203.             builder.append(module.getName()).append(" {").append(LINE_SEPARATOR);
  204.             builder.append(SUN_LOGIN_MODULE).append(LINE_SEPARATOR);
  205.             builder.append("useKeyTab=true").append(LINE_SEPARATOR);
  206.             builder.append("keyTab="" + keyTabPath + """).append(LINE_SEPARATOR);
  207.             builder.append("principal="" + userPrincipal + """).append(LINE_SEPARATOR);
  208.             builder.append("useTicketCache=false").append(LINE_SEPARATOR);
  209.             builder.append("storeKey=true").append(LINE_SEPARATOR);
  210.             builder.append("debug=true;").append(LINE_SEPARATOR);
  211.             builder.append("};").append(LINE_SEPARATOR);
  212.         }
  213.         return builder.toString();
  214.     }
  215.     public static void setJaasConf(String loginContextName, String principal, String keytabFile) throws IOException {
  216.         if ((loginContextName == null) || (loginContextName.length() <= 0)) {
  217.             LOG.error("input loginContextName is invalid.");
  218.             throw new IOException("input loginContextName is invalid.");
  219.         }
  220.         if ((principal == null) || (principal.length() <= 0)) {
  221.             LOG.error("input principal is invalid.");
  222.             throw new IOException("input principal is invalid.");
  223.         }
  224.         if ((keytabFile == null) || (keytabFile.length() <= 0)) {
  225.             LOG.error("input keytabFile is invalid.");
  226.             throw new IOException("input keytabFile is invalid.");
  227.         }
  228.         File userKeytabFile = new File(keytabFile);
  229.         if (!userKeytabFile.exists()) {
  230.             LOG.error("userKeytabFile(" + userKeytabFile.getAbsolutePath() + ") does not exsit.");
  231.             throw new IOException("userKeytabFile(" + userKeytabFile.getAbsolutePath() + ") does not exsit.");
  232.         }
  233.         javax.security.auth.login.Configuration.setConfiguration(
  234.                 new JaasConfiguration(loginContextName, principal, userKeytabFile.getAbsolutePath()));
  235.         javax.security.auth.login.Configuration conf = javax.security.auth.login.Configuration.getConfiguration();
  236.         if (!(conf instanceof JaasConfiguration)) {
  237.             LOG.error("javax.security.auth.login.Configuration is not JaasConfiguration.");
  238.             throw new IOException("javax.security.auth.login.Configuration is not JaasConfiguration.");
  239.         }
  240.         AppConfigurationEntry[] entrys = conf.getAppConfigurationEntry(loginContextName);
  241.         if (entrys == null) {
  242.             LOG.error(
  243.                     "javax.security.auth.login.Configuration has no AppConfigurationEntry named "
  244.                             + loginContextName
  245.                             + ".");
  246.             throw new IOException(
  247.                     "javax.security.auth.login.Configuration has no AppConfigurationEntry named "
  248.                             + loginContextName
  249.                             + ".");
  250.         }
  251.         boolean checkPrincipal = false;
  252.         boolean checkKeytab = false;
  253.         for (int i = 0; i < entrys.length; i++) {
  254.             if (entrys[i].getOptions().get("principal").equals(principal)) {
  255.                 checkPrincipal = true;
  256.             }
  257.             if (IS_IBM_JDK) {
  258.                 if (entrys[i].getOptions().get("useKeytab").equals(keytabFile)) {
  259.                     checkKeytab = true;
  260.                 }
  261.             } else {
  262.                 if (entrys[i].getOptions().get("keyTab").equals(keytabFile)) {
  263.                     checkKeytab = true;
  264.                 }
  265.             }
  266.         }
  267.         if (!checkPrincipal) {
  268.             LOG.error(
  269.                     "AppConfigurationEntry named "
  270.                             + loginContextName
  271.                             + " does not have principal value of "
  272.                             + principal
  273.                             + ".");
  274.             throw new IOException(
  275.                     "AppConfigurationEntry named "
  276.                             + loginContextName
  277.                             + " does not have principal value of "
  278.                             + principal
  279.                             + ".");
  280.         }
  281.         if (!checkKeytab) {
  282.             LOG.error(
  283.                     "AppConfigurationEntry named "
  284.                             + loginContextName
  285.                             + " does not have keyTab value of "
  286.                             + keytabFile
  287.                             + ".");
  288.             throw new IOException(
  289.                     "AppConfigurationEntry named "
  290.                             + loginContextName
  291.                             + " does not have keyTab value of "
  292.                             + keytabFile
  293.                             + ".");
  294.         }
  295.     }
  296.     public static void setZookeeperServerPrincipal(String zkServerPrincipal) throws IOException {
  297.         System.setProperty(ZOOKEEPER_SERVER_PRINCIPAL_KEY, zkServerPrincipal);
  298.         String ret = System.getProperty(ZOOKEEPER_SERVER_PRINCIPAL_KEY);
  299.         if (ret == null) {
  300.             LOG.error(ZOOKEEPER_SERVER_PRINCIPAL_KEY + " is null.");
  301.             throw new IOException(ZOOKEEPER_SERVER_PRINCIPAL_KEY + " is null.");
  302.         }
  303.         if (!ret.equals(zkServerPrincipal)) {
  304.             LOG.error(ZOOKEEPER_SERVER_PRINCIPAL_KEY + " is " + ret + " is not " + zkServerPrincipal + ".");
  305.             throw new IOException(ZOOKEEPER_SERVER_PRINCIPAL_KEY + " is " + ret + " is not " + zkServerPrincipal + ".");
  306.         }
  307.     }
  308.     @Deprecated
  309.     public static void setZookeeperServerPrincipal(String zkServerPrincipalKey, String zkServerPrincipal)
  310.             throws IOException {
  311.         System.setProperty(zkServerPrincipalKey, zkServerPrincipal);
  312.         String ret = System.getProperty(zkServerPrincipalKey);
  313.         if (ret == null) {
  314.             LOG.error(zkServerPrincipalKey + " is null.");
  315.             throw new IOException(zkServerPrincipalKey + " is null.");
  316.         }
  317.         if (!ret.equals(zkServerPrincipal)) {
  318.             LOG.error(zkServerPrincipalKey + " is " + ret + " is not " + zkServerPrincipal + ".");
  319.             throw new IOException(zkServerPrincipalKey + " is " + ret + " is not " + zkServerPrincipal + ".");
  320.         }
  321.     }
  322.     private static void loginHadoop(String principal, String keytabFile) throws IOException {
  323.         try {
  324.             UserGroupInformation.loginUserFromKeytab(principal, keytabFile);
  325.         } catch (IOException e) {
  326.             LOG.error("login failed with " + principal + " and " + keytabFile + ".");
  327.             LOG.error("perhaps cause 1 is " + LOGIN_FAILED_CAUSE_PASSWORD_WRONG + ".");
  328.             LOG.error("perhaps cause 2 is " + LOGIN_FAILED_CAUSE_TIME_WRONG + ".");
  329.             LOG.error("perhaps cause 3 is " + LOGIN_FAILED_CAUSE_AES256_WRONG + ".");
  330.             LOG.error("perhaps cause 4 is " + LOGIN_FAILED_CAUSE_PRINCIPAL_WRONG + ".");
  331.             LOG.error("perhaps cause 5 is " + LOGIN_FAILED_CAUSE_TIME_OUT + ".");
  332.             throw e;
  333.         }
  334.     }
  335.     private static void checkAuthenticateOverKrb() throws IOException {
  336.         UserGroupInformation loginUser = UserGroupInformation.getLoginUser();
  337.         UserGroupInformation currentUser = UserGroupInformation.getCurrentUser();
  338.         if (loginUser == null) {
  339.             LOG.error("current user is " + currentUser + ", but loginUser is null.");
  340.             throw new IOException("current user is " + currentUser + ", but loginUser is null.");
  341.         }
  342.         if (!loginUser.equals(currentUser)) {
  343.             LOG.error("current user is " + currentUser + ", but loginUser is " + loginUser + ".");
  344.             throw new IOException("current user is " + currentUser + ", but loginUser is " + loginUser + ".");
  345.         }
  346.         if (!loginUser.hasKerberosCredentials()) {
  347.             LOG.error("current user is " + currentUser + " has no Kerberos Credentials.");
  348.             throw new IOException("current user is " + currentUser + " has no Kerberos Credentials.");
  349.         }
  350.         if (!UserGroupInformation.isLoginKeytabBased()) {
  351.             LOG.error("current user is " + currentUser + " is not Login Keytab Based.");
  352.             throw new IOException("current user is " + currentUser + " is not Login Keytab Based.");
  353.         }
  354.     }
  355.     private static boolean checkCurrentUserCorrect(String principal) throws IOException {
  356.         UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
  357.         if (ugi == null) {
  358.             LOG.error("current user still null.");
  359.             throw new IOException("current user still null.");
  360.         }
  361.         String defaultRealm = null;
  362.         try {
  363.             defaultRealm = KerberosUtil.getDefaultRealm();
  364.         } catch (Exception e) {
  365.             LOG.warn("getDefaultRealm failed.");
  366.             throw new IOException(e);
  367.         }
  368.         if ((defaultRealm != null) && (defaultRealm.length() > 0)) {
  369.             StringBuilder realm = new StringBuilder();
  370.             StringBuilder principalWithRealm = new StringBuilder();
  371.             realm.append("@").append(defaultRealm);
  372.             if (!principal.endsWith(realm.toString())) {
  373.                 principalWithRealm.append(principal).append(realm);
  374.                 principal = principalWithRealm.toString();
  375.             }
  376.         }
  377.         return principal.equals(ugi.getUserName());
  378.     }
  379.     public static void loginWithUserKeytab(String uPrincipal) throws IOException {
  380.         String userPrincipal = uPrincipal;
  381.         String userKeytabPath = System.getProperty("user.dir") + File.separator + "user.keytab";
  382.         String krb5ConfPath = System.getProperty("user.dir") + File.separator + "krb5.conf";
  383.         String principalName = com.mrs.spark.security.hadoop.KerberosUtil.getKrb5DomainRealm();
  384.         LOG.warn("login using user super. principal name is:" + principalName);
  385.         String ZKServerPrincipal = "zookeeper/hadoop." + principalName;
  386.         String ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME = "Client";
  387.         String ZOOKEEPER_SERVER_PRINCIPAL_KEY = "zookeeper.server.principal";
  388.         Configuration hadoopConf = new Configuration();
  389.         try {
  390.             //            setJaasConf(ZOOKEEPER_DEFAULT_LOGIN_CONTEXT_NAME, userPrincipal, userKeytabPath);
  391.             setJaasFile(userPrincipal, userKeytabPath);
  392.             setZookeeperServerPrincipal(ZOOKEEPER_SERVER_PRINCIPAL_KEY, ZKServerPrincipal);
  393.             login(userPrincipal, userKeytabPath, krb5ConfPath, hadoopConf);
  394.         } catch (IOException e) {
  395.             LOG.warn("Login with keytab failed.");
  396.             throw new IOException(e);
  397.         }
  398.     }
  399.     /**
  400.      * copy from hbase zkutil 0.94&0.98 A JAAS configuration that defines the login modules that we want to use for
  401.      * login.
  402.      */
  403.     private static class JaasConfiguration extends javax.security.auth.login.Configuration {
  404.         private static final Map<String, String> BASIC_JAAS_OPTIONS = new HashMap<String, String>();
  405.         static {
  406.             String jaasEnvVar = System.getenv("HBASE_JAAS_DEBUG");
  407.             if (jaasEnvVar != null && "true".equalsIgnoreCase(jaasEnvVar)) {
  408.                 BASIC_JAAS_OPTIONS.put("debug", "true");
  409.             }
  410.         }
  411.         private static final Map<String, String> KEYTAB_KERBEROS_OPTIONS = new HashMap<String, String>();
  412.         static {
  413.             if (IS_IBM_JDK) {
  414.                 KEYTAB_KERBEROS_OPTIONS.put("credsType", "both");
  415.             } else {
  416.                 KEYTAB_KERBEROS_OPTIONS.put("useKeyTab", "true");
  417.                 KEYTAB_KERBEROS_OPTIONS.put("useTicketCache", "false");
  418.                 KEYTAB_KERBEROS_OPTIONS.put("doNotPrompt", "true");
  419.                 KEYTAB_KERBEROS_OPTIONS.put("storeKey", "true");
  420.             }
  421.             KEYTAB_KERBEROS_OPTIONS.putAll(BASIC_JAAS_OPTIONS);
  422.         }
  423.         private static final AppConfigurationEntry KEYTAB_KERBEROS_LOGIN =
  424.                 new AppConfigurationEntry(
  425.                         KerberosUtil.getKrb5LoginModuleName(),
  426.                         LoginModuleControlFlag.REQUIRED,
  427.                         KEYTAB_KERBEROS_OPTIONS);
  428.         private static final AppConfigurationEntry[] KEYTAB_KERBEROS_CONF =
  429.                 new AppConfigurationEntry[] {KEYTAB_KERBEROS_LOGIN};
  430.         private javax.security.auth.login.Configuration baseConfig;
  431.         private final String loginContextName;
  432.         private final boolean useTicketCache;
  433.         private final String keytabFile;
  434.         private final String principal;
  435.         public JaasConfiguration(String loginContextName, String principal, String keytabFile) throws IOException {
  436.             this(loginContextName, principal, keytabFile, keytabFile == null || keytabFile.length() == 0);
  437.         }
  438.         private JaasConfiguration(String loginContextName, String principal, String keytabFile, boolean useTicketCache)
  439.                 throws IOException {
  440.             try {
  441.                 this.baseConfig = javax.security.auth.login.Configuration.getConfiguration();
  442.             } catch (SecurityException e) {
  443.                 this.baseConfig = null;
  444.             }
  445.             this.loginContextName = loginContextName;
  446.             this.useTicketCache = useTicketCache;
  447.             this.keytabFile = keytabFile;
  448.             this.principal = principal;
  449.             initKerberosOption();
  450.             LOG.info(
  451.                     "JaasConfiguration loginContextName="
  452.                             + loginContextName
  453.                             + " principal="
  454.                             + principal
  455.                             + " useTicketCache="
  456.                             + useTicketCache
  457.                             + " keytabFile="
  458.                             + keytabFile);
  459.         }
  460.         private void initKerberosOption() throws IOException {
  461.             if (!useTicketCache) {
  462.                 if (IS_IBM_JDK) {
  463.                     KEYTAB_KERBEROS_OPTIONS.put("useKeytab", keytabFile);
  464.                 } else {
  465.                     KEYTAB_KERBEROS_OPTIONS.put("keyTab", keytabFile);
  466.                     KEYTAB_KERBEROS_OPTIONS.put("useKeyTab", "true");
  467.                     KEYTAB_KERBEROS_OPTIONS.put("useTicketCache", useTicketCache ? "true" : "false");
  468.                 }
  469.             }
  470.             KEYTAB_KERBEROS_OPTIONS.put("principal", principal);
  471.         }
  472.         public AppConfigurationEntry[] getAppConfigurationEntry(String appName) {
  473.             if (loginContextName.equals(appName)) {
  474.                 return KEYTAB_KERBEROS_CONF;
  475.             }
  476.             if (baseConfig != null) return baseConfig.getAppConfigurationEntry(appName);
  477.             return (null);
  478.         }
  479.     }
  480. }
复制代码
最后
谢谢各人 @500佰


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

篮之新喜

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表