Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Improvement] refactor krb5 avoid starting error in idea #3377

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.amoro.shade.guava32.com.google.common.collect.Maps;
import org.apache.amoro.shade.guava32.com.google.common.hash.Hashing;
import org.apache.amoro.shade.guava32.com.google.common.io.ByteStreams;
import org.apache.amoro.utils.ReflectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -35,7 +36,6 @@
import org.apache.hadoop.security.authentication.util.KerberosName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.security.krb5.KrbException;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
Expand Down Expand Up @@ -383,7 +383,7 @@ public UserGroupInformation getUGI() {
constructKerberosUgi();
}
LOG.info("Completed to build ugi {}", authInformation());
} catch (IOException | KrbException e) {
} catch (Exception e) {
throw new RuntimeException("Fail to init user group information", e);
}
} else {
Expand All @@ -407,13 +407,17 @@ public UserGroupInformation getUGI() {
return ugi;
}

private void constructKerberosUgi() throws IOException, KrbException {
private void constructKerberosUgi() throws Exception {
Path confPath = generateKrbConfPath();
String krbConfFile = saveConfInPath(confPath, KRB_CONF_FILE_NAME, krbConf);
String keyTabFile = saveConfInPath(confPath, KEY_TAB_FILE_NAME, krbKeyTab);
System.clearProperty(HADOOP_USER_PROPERTY);
System.setProperty(KRB5_CONF_PROPERTY, krbConfFile);
sun.security.krb5.Config.refresh();
if (System.getProperty("java.vendor").contains("IBM")) {
ReflectionUtils.invoke("com.ibm.security.krb5.internal.Config", "refresh");
} else {
ReflectionUtils.invoke("sun.security.krb5.Config", "refresh");
}
UserGroupInformation.setConfiguration(getConfiguration());
KerberosName.resetDefaultRealm();
this.ugi = UserGroupInformation.loginUserFromKeytabAndReturnUGI(krbPrincipal, keyTabFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.amoro.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class ReflectionUtils {

private static final Logger LOG = LoggerFactory.getLogger(ReflectionUtils.class);
private static final Map<String, Class> CLASS_MAP = new HashMap<>();

public static Object invoke(String className, String methodName)
throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
Class<?> classRef =
CLASS_MAP.computeIfAbsent(
className,
key -> {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
// ignore error
return null;
}
});
if (Objects.isNull(classRef)) {
LOG.warn("cannot load class {}, skip execute method {}", className, methodName);
return null;
}
Method method = classRef.getDeclaredMethod(methodName);
return method.invoke(null);
}
}
Loading