|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package com.dtstack.flinkx.launcher; |
| 19 | + |
| 20 | +import com.dtstack.flinkx.util.ExceptionUtil; |
| 21 | +import org.apache.commons.lang3.StringUtils; |
| 22 | +import org.apache.flink.configuration.Configuration; |
| 23 | +import org.apache.flink.configuration.SecurityOptions; |
| 24 | +import org.apache.flink.runtime.util.HadoopUtils; |
| 25 | +import org.apache.hadoop.security.SecurityUtil; |
| 26 | +import org.apache.hadoop.security.UserGroupInformation; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +import java.io.File; |
| 31 | +import java.io.IOException; |
| 32 | + |
| 33 | +/** |
| 34 | + * KerberosInfo |
| 35 | + * |
| 36 | + |
| 37 | + * @Date 2020/8/21 |
| 38 | + */ |
| 39 | +public class KerberosInfo { |
| 40 | + |
| 41 | + private static final Logger LOG = LoggerFactory.getLogger(KerberosInfo.class); |
| 42 | + |
| 43 | + |
| 44 | + private final String krb5confPath; |
| 45 | + private final String keytab; |
| 46 | + private final String principal; |
| 47 | + private final Configuration config; |
| 48 | + private final org.apache.hadoop.conf.Configuration hadoopConfiguration; |
| 49 | + |
| 50 | + public KerberosInfo(String krb5confPath, String keytab, String principal, Configuration config) { |
| 51 | + this.krb5confPath = krb5confPath; |
| 52 | + this.config = config; |
| 53 | + this.hadoopConfiguration = HadoopUtils.getHadoopConfiguration(this.config); |
| 54 | + |
| 55 | + //keytab, launcherOptions.getKeytab() 比flinkConfiguration里配置的优先级高 |
| 56 | + if (StringUtils.isBlank(keytab)) { |
| 57 | + this.keytab = this.config.getString(SecurityOptions.KERBEROS_LOGIN_KEYTAB); |
| 58 | + } else { |
| 59 | + this.keytab = keytab; |
| 60 | + } |
| 61 | + //principal信息, launcherOptions.getPrincipal() 比flinkConfiguration里配置的优先级高 |
| 62 | + if (StringUtils.isBlank(principal)) { |
| 63 | + this.principal = this.config.getString(SecurityOptions.KERBEROS_LOGIN_PRINCIPAL); |
| 64 | + } else { |
| 65 | + this.principal = principal; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public void verify() { |
| 70 | + if (!isVerify()) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + check(); |
| 75 | + |
| 76 | + //如果指定了Krb5conf位置 |
| 77 | + if (StringUtils.isNotBlank(this.getKrb5confPath())) { |
| 78 | + System.setProperty("java.security.krb5.conf", this.getKrb5confPath()); |
| 79 | + } |
| 80 | + |
| 81 | + String keyTabpath; |
| 82 | + try { |
| 83 | + keyTabpath = (new File(keytab)).getAbsolutePath(); |
| 84 | + } catch (Exception e) { |
| 85 | + String message = String.format("can not get the file 【%s】,error info-> %s ", |
| 86 | + keytab, |
| 87 | + ExceptionUtil.getErrorMessage(e)); |
| 88 | + LOG.error("{}", message); |
| 89 | + throw new RuntimeException(message, e); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + LOG.info("kerberos info:Krb5confPath ->{}, Principal ->{}, keytab->{}", System.getProperty("java.security.krb5.conf"), principal, keyTabpath); |
| 94 | + |
| 95 | + //开始kerberos验证 |
| 96 | + UserGroupInformation.setConfiguration(hadoopConfiguration); |
| 97 | + try { |
| 98 | + UserGroupInformation.getCurrentUser().setAuthenticationMethod(UserGroupInformation.AuthenticationMethod.KERBEROS); |
| 99 | + } catch (IOException e) { |
| 100 | + String message = "UserGroupInformation getCurrentUser has error," + ExceptionUtil.getErrorMessage(e); |
| 101 | + LOG.error("{}", message); |
| 102 | + throw new RuntimeException(message, e); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + try { |
| 107 | + UserGroupInformation.loginUserFromKeytab(principal, keyTabpath); |
| 108 | + } catch (IOException e) { |
| 109 | + String message = String.format("Unable to set the Hadoop login principal【%s】,keytab 【%s】error info-> %s ", |
| 110 | + principal, |
| 111 | + keyTabpath, |
| 112 | + ExceptionUtil.getErrorMessage(e)); |
| 113 | + LOG.error("{}", message); |
| 114 | + throw new RuntimeException(message, e); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + //是否需要kerberos验证 |
| 119 | + public boolean isVerify() { |
| 120 | + UserGroupInformation.AuthenticationMethod authenticationMethod = SecurityUtil.getAuthenticationMethod(hadoopConfiguration); |
| 121 | + return UserGroupInformation.AuthenticationMethod.SIMPLE != authenticationMethod; |
| 122 | + } |
| 123 | + |
| 124 | + protected void check() { |
| 125 | + if (StringUtils.isBlank(getKeytab())) { |
| 126 | + throw new RuntimeException("keytabPath can not be null"); |
| 127 | + } |
| 128 | + |
| 129 | + if (StringUtils.isBlank(getPrincipal())) { |
| 130 | + throw new RuntimeException("principal can not be null"); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + public String getKrb5confPath() { |
| 136 | + return krb5confPath; |
| 137 | + } |
| 138 | + |
| 139 | + public String getKeytab() { |
| 140 | + return keytab; |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + public String getPrincipal() { |
| 145 | + return principal; |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | +} |
0 commit comments