forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix](multi-catalog)fix getting ugi methods and unify them (apache#30844
- Loading branch information
Showing
14 changed files
with
305 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...n/src/main/java/org/apache/doris/common/security/authentication/AuthenticationConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// 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.doris.common.security.authentication; | ||
|
||
import lombok.Data; | ||
import org.apache.hadoop.conf.Configuration; | ||
|
||
@Data | ||
public abstract class AuthenticationConfig { | ||
|
||
public static String HADOOP_USER_NAME = "hadoop.username"; | ||
public static String HADOOP_SECURITY_AUTHENTICATION = "hadoop.security.authentication"; | ||
public static String HADOOP_KERBEROS_PRINCIPAL = "hadoop.kerberos.principal"; | ||
public static String HADOOP_KERBEROS_AUTHORIZATION = "hadoop.security.authorization"; | ||
public static String HADOOP_KERBEROS_KEYTAB = "hadoop.kerberos.keytab"; | ||
public static String HIVE_KERBEROS_PRINCIPAL = "hive.metastore.kerberos.principal"; | ||
public static String HIVE_KERBEROS_KEYTAB = "hive.metastore.kerberos.keytab.file"; | ||
|
||
private boolean isValid; | ||
|
||
/** | ||
* get kerberos config from hadoop conf | ||
* @param conf config | ||
* @return ugi | ||
*/ | ||
public static AuthenticationConfig getKerberosConfig(Configuration conf) { | ||
return AuthenticationConfig.getKerberosConfig(conf, HADOOP_KERBEROS_PRINCIPAL, HADOOP_KERBEROS_KEYTAB); | ||
} | ||
|
||
/** | ||
* get kerberos config from hadoop conf | ||
* @param conf config | ||
* @param krbPrincipalKey principal key | ||
* @param krbKeytabKey keytab key | ||
* @return ugi | ||
*/ | ||
public static AuthenticationConfig getKerberosConfig(Configuration conf, | ||
String krbPrincipalKey, | ||
String krbKeytabKey) { | ||
String authentication = conf.get(HADOOP_SECURITY_AUTHENTICATION, null); | ||
if (AuthType.KERBEROS.getDesc().equals(authentication)) { | ||
KerberosAuthenticationConfig krbConfig = new KerberosAuthenticationConfig(); | ||
krbConfig.setKerberosPrincipal(conf.get(krbPrincipalKey)); | ||
krbConfig.setKerberosKeytab(conf.get(krbKeytabKey)); | ||
krbConfig.setConf(conf); | ||
return krbConfig; | ||
} else { | ||
// AuthType.SIMPLE | ||
SimpleAuthenticationConfig simpleAuthenticationConfig = new SimpleAuthenticationConfig(); | ||
simpleAuthenticationConfig.setUsername(conf.get(HADOOP_USER_NAME)); | ||
return simpleAuthenticationConfig; | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
fe/fe-common/src/main/java/org/apache/doris/common/security/authentication/HadoopUGI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// 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.doris.common.security.authentication; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.security.UserGroupInformation; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import java.io.IOException; | ||
|
||
public class HadoopUGI { | ||
private static final Logger LOG = LogManager.getLogger(HadoopUGI.class); | ||
|
||
/** | ||
* login and return hadoop ugi | ||
* @param config auth config | ||
* @return ugi | ||
*/ | ||
public static UserGroupInformation loginWithUGI(AuthenticationConfig config) { | ||
UserGroupInformation ugi; | ||
if (config instanceof KerberosAuthenticationConfig) { | ||
KerberosAuthenticationConfig krbConfig = (KerberosAuthenticationConfig) config; | ||
Configuration hadoopConf = krbConfig.getConf(); | ||
hadoopConf.set(AuthenticationConfig.HADOOP_KERBEROS_AUTHORIZATION, "true"); | ||
UserGroupInformation.setConfiguration(hadoopConf); | ||
String principal = krbConfig.getKerberosPrincipal(); | ||
try { | ||
// login hadoop with keytab and try checking TGT | ||
ugi = UserGroupInformation.getLoginUser(); | ||
LOG.debug("Current login user: {}", ugi.getUserName()); | ||
if (ugi.hasKerberosCredentials() && StringUtils.equals(ugi.getUserName(), principal)) { | ||
// if the current user is logged by kerberos and is the same user | ||
// just use checkTGTAndReloginFromKeytab because this method will only relogin | ||
// when the TGT is expired or is close to expiry | ||
ugi.checkTGTAndReloginFromKeytab(); | ||
return ugi; | ||
} | ||
} catch (IOException e) { | ||
LOG.warn("A SecurityException occurs with kerberos, do login immediately.", e); | ||
} | ||
try { | ||
ugi = UserGroupInformation.loginUserFromKeytabAndReturnUGI(principal, krbConfig.getKerberosKeytab()); | ||
UserGroupInformation.setLoginUser(ugi); | ||
LOG.debug("Login by kerberos authentication with principal: {}", principal); | ||
return ugi; | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} else { | ||
String hadoopUserName = ((SimpleAuthenticationConfig) config).getUsername(); | ||
if (hadoopUserName == null) { | ||
hadoopUserName = "hadoop"; | ||
LOG.debug(AuthenticationConfig.HADOOP_USER_NAME + " is unset, use default user: hadoop"); | ||
} | ||
ugi = UserGroupInformation.createRemoteUser(hadoopUserName); | ||
UserGroupInformation.setLoginUser(ugi); | ||
LOG.debug("Login by proxy user, hadoop.username: {}", hadoopUserName); | ||
return ugi; | ||
} | ||
} | ||
|
||
/** | ||
* use for HMSExternalCatalog to login | ||
* @param config auth config | ||
*/ | ||
public static void tryKrbLogin(String catalogName, AuthenticationConfig config) { | ||
if (config instanceof KerberosAuthenticationConfig) { | ||
KerberosAuthenticationConfig krbConfig = (KerberosAuthenticationConfig) config; | ||
try { | ||
/** | ||
* Because metastore client is created by using | ||
* {@link org.apache.hadoop.hive.metastore.RetryingMetaStoreClient#getProxy} | ||
* it will relogin when TGT is expired, so we don't need to relogin manually. | ||
*/ | ||
UserGroupInformation.loginUserFromKeytab(krbConfig.getKerberosPrincipal(), | ||
krbConfig.getKerberosKeytab()); | ||
} catch (IOException e) { | ||
throw new RuntimeException("login with kerberos auth failed for catalog: " + catalogName, e); | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...in/java/org/apache/doris/common/security/authentication/KerberosAuthenticationConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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.doris.common.security.authentication; | ||
|
||
import lombok.Data; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.hadoop.conf.Configuration; | ||
|
||
@Data | ||
public class KerberosAuthenticationConfig extends AuthenticationConfig { | ||
private String kerberosPrincipal; | ||
private String kerberosKeytab; | ||
private Configuration conf; | ||
|
||
@Override | ||
public boolean isValid() { | ||
return StringUtils.isNotEmpty(kerberosPrincipal) && StringUtils.isNotEmpty(kerberosKeytab); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...main/java/org/apache/doris/common/security/authentication/SimpleAuthenticationConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 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.doris.common.security.authentication; | ||
|
||
import lombok.Data; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
@Data | ||
public class SimpleAuthenticationConfig extends AuthenticationConfig { | ||
private String username; | ||
|
||
@Override | ||
public boolean isValid() { | ||
return StringUtils.isNotEmpty(username); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.