Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.kylin.tool.kerberos;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.security.Credentials;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.security.token.Token;
import org.apache.hadoop.security.token.TokenIdentifier;
import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenIdentifier;
import org.apache.hadoop.util.Shell;
import org.apache.kylin.common.KapConfig;
import org.apache.kylin.common.util.Unsafe;
import org.apache.kylin.engine.spark.utils.ThreadUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.security.InvalidParameterException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class DelegationTokenManager {
private String principal;
private String keytab;
private ScheduledExecutorService renewalExecutor;
private final KapConfig kapConf;
private static final String CONTEXT_NAME = "Client";
private static final Configuration CONFIGURATION = new Configuration();
private static final Logger logger = LoggerFactory.getLogger(DelegationTokenManager.class);
public DelegationTokenManager() {
this(KapConfig.getInstanceFromEnv());
}
public DelegationTokenManager(KapConfig kapConf) {
this.kapConf = kapConf;
}
public void start() {
if (Boolean.FALSE.equals(kapConf.isKerberosEnabled())) {
logger.info("Kerberos is not enabled.");
return;
}
principal = kapConf.getKerberosPrincipal();
keytab = kapConf.getKerberosKeytabPath();
preCheck();
renewalExecutor = //
ThreadUtils.newDaemonSingleThreadScheduledExecutor("Kylin Credential Renewal Thread");
// invoke from external?
Runtime.getRuntime().addShutdownHook(new Thread(this::stop));
// Login first before everything.
tryLogin();
// Schedule TGT renewal.
// Update credentials of FileSystem cached UGIs.
scheduleTGTRenewal();
// Schedule TGT cache renewal for ZK.
// ZK read tgt from external cache: KRB5CCNAME, not ugi credentials.
scheduleTGTCacheRenewal();
}
private void tryLogin() {
try {
doLogin();
} catch (IOException ioe) {
long retryInterval = kapConf.getKerberosTGTRetryInterval();
logger.error("Failed to login kerberos from principal: {}, keytab: {}," + //
" will try again in {} minutes." + //
" If this happens too often tasks will fail.", principal, keytab, retryInterval, ioe);
renewalExecutor.schedule(this::tryLogin, Math.max(0, retryInterval), TimeUnit.MINUTES);
}
}
private void scheduleTGTRenewal() {
final Runnable tgtRenewalTask = () -> {
try {
updateCredentials();
} catch (Exception e) {
logger.error("Failed to update UGI credentials.", e);
}
};
long renewalInternal = kapConf.getKerberosTGTRenewalInterval();
renewalExecutor.scheduleWithFixedDelay(tgtRenewalTask, renewalInternal, renewalInternal, TimeUnit.MINUTES);
}
// We wouldn't do UGI#loginUserFromKeytab again and again.
private void scheduleTGTCacheRenewal() {
final Runnable tgtCacheRenewalTask = () -> {
try {
doRenewTGTCache();
} catch (IOException ioe) {
logger.error("Failed to renew kerberos tgt cache at KRB5CCNAME.", ioe);
}
};
long renewalInternal = kapConf.getKerberosTicketRefreshInterval();
renewalExecutor.scheduleWithFixedDelay(tgtCacheRenewalTask, //
renewalInternal, renewalInternal, TimeUnit.MINUTES);
}
private void updateCredentials() throws IOException, NoSuchFieldException {
final UserGroupInformation current = UserGroupInformation.getCurrentUser();
current.checkTGTAndReloginFromKeytab();
Object fsCache = getFileSystemCache();
if (Objects.isNull(fsCache)) {
return;
}
// Extract old UGIs from cacheKeys.
final Collection