記事ディレクトリ
1. 公式サイトの説明
認証/セキュリティ設定
HiveServer2 は、SASL の有無にかかわらず匿名 (認証なし)、Kerberos (GSSAPI)、パススルー LDAP、プラグイン可能なカスタム認証、およびプラグイン可能な認証モジュール (PAM、Hive 0.13 以降でサポート) をサポートします。
認証モード:
hive.server2.authentication – 認証モード、デフォルトは NONE。オプションは、NONE (プレーン SASL を使用)、NOSASL、KERBEROS、LDAP、PAM、および CUSTOM です。
CUSTOM モードでは次のように設定します。
hive.server2.custom.authentication.class – org.apache.hive.service.auth.PasswdAuthenticationProvider インターフェイスを実装するカスタム認証クラス。
公式サイト:SettingUpHiveServer2-Authentication/SecurityConfiguration
二つ、悟る
ロジックを実現します。
- org.apache.hive.service.auth.PasswdAuthenticationProvider インターフェースを実装し、パッケージ化して >${hive_home}/lib に配置します。
- 各 hiveserver インストールで hive-site.xml を構成する
- ハイサーバーを再起動します
実装クラス:
ロジックは非常に単純で、hive.jdbc_passwd.auth 構成を読み取り、ユーザーとパスワードを識別します。設定がない場合、または一致が失敗した場合は、例外がスローされます。
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hive.service.auth.PasswdAuthenticationProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.security.sasl.AuthenticationException;
/**
* @Description TODO
* @Author lianggao
* @Date 2023/6/12 下午3:45
*/
public class CustomPasswdAuthenticator implements PasswdAuthenticationProvider {
private Logger LOG = LoggerFactory.getLogger(CustomPasswdAuthenticator.class);
private static final String HIVE_JDBC_PASSWD_AUTH_PREFIX = "hive.jdbc_passwd.auth.%s";
private Configuration conf = null;
public CustomPasswdAuthenticator() {
}
public void Authenticate(String userName, String passwd) throws AuthenticationException {
LOG.info("user: " + userName + " try login.");
String passwdConf = this.getConf().get(String.format("hive.jdbc_passwd.auth.%s", userName));
String message;
if (passwdConf == null) {
message = "user's ACL configration is not found. user:" + userName;
LOG.info(message);
throw new AuthenticationException(message);
} else if (!passwd.equals(passwdConf)) {
message = "user name and password is mismatch. user:" + userName;
throw new AuthenticationException(message);
}
}
public Configuration getConf() {
if (this.conf == null) {
this.conf = new Configuration(new HiveConf());
}
return this.conf;
}
public void setConf(Configuration conf) {
this.conf = conf;
}
}
修改hive-site.xml
<!--自定义远程连接用户名和密码-->
<property>
<name>hive.server2.authentication</name>
<value>CUSTOM</value><!--默认为none,修改成CUSTOM-->
</property>
<!--指定解析jar包-->
<property>
<name>hive.server2.custom.authentication.class</name>
<value>org.apache.hadoop.hive.contrib.auth.CustomPasswdAuthenticator</value>
</property>
<!--设置用户名和密码-->
<property>
<name>hive.jdbc_passwd.auth.usename1111</name><!--用户名识别为:usename1111-->
<value>pswd1111</value><!--密码-->
</property>
ハイサーバーを再起動します
jps
kill -9 hive_server_pid
nohup ./hive --service hiveserver2 >> /tmp/hiveserver2.log 2>&1
ハイブ テストをビーライン経由で接続します。
beeline -u jdbc:hive2://hostname:10000 -n usename1111 -p pswd1111
3. 2回目の実装
ユーザー名とパスワードを実装クラスにあらかじめ記述しておくと、hive-site.xmlの構成がより簡潔になります。
package hive.test;
import java.util.Hashtable;
import javax.security.sasl.AuthenticationException;
import org.apache.hive.service.auth.PasswdAuthenticationProvider;
/*
javac -cp $HIVE_HOME/lib/hive-service-0.11-mapr.jar SampleAuthenticator.java -d .
jar cf sampleauth.jar hive
cp sampleauth.jar $HIVE_HOME/lib/.
*/
public class SampleAuthenticator implements PasswdAuthenticationProvider {
Hashtable<String, String> store = null;
public SampleAuthenticator () {
store = new Hashtable<String, String>();
store.put("user1", "passwd1");
store.put("user2", "passwd2");
}
@Override
public void Authenticate(String user, String password)
throws AuthenticationException {
String storedPasswd = store.get(user);
if (storedPasswd != null && storedPasswd.equals(password))
return;
throw new AuthenticationException("SampleAuthenticator: Error validating user");
}
}
ハイブサイト.xml
<property>
<name>hive.server2.authentication</name>
<value>CUSTOM</value>
</property>
<property>
<name>hive.server2.custom.authentication.class</name>
<value>hive.test.SampleAuthenticator</value>
</property>
参考:
https://docs.ezmeral.hpe.com/datafabric-customer-managed/72/Hive/HiveServer2-CustomAuth.html