JackWangCS commented on a change in pull request #14841: URL: https://github.com/apache/flink/pull/14841#discussion_r633613001
########## File path: flink-yarn/src/main/java/org/apache/flink/yarn/security/HadoopFSDelegationTokenProvider.java ########## @@ -0,0 +1,126 @@ +/* + * 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.flink.yarn.security; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.configuration.ConfigUtils; +import org.apache.flink.runtime.security.delegationtokens.HadoopDelegationTokenConfiguration; +import org.apache.flink.runtime.security.delegationtokens.HadoopDelegationTokenProvider; +import org.apache.flink.util.FlinkRuntimeException; +import org.apache.flink.util.function.FunctionUtils; +import org.apache.flink.yarn.configuration.YarnConfigOptions; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.mapred.Master; +import org.apache.hadoop.security.Credentials; +import org.apache.hadoop.security.UserGroupInformation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +/** Delegation token provider implementation for Hadoop FileSystems. */ +public class HadoopFSDelegationTokenProvider implements HadoopDelegationTokenProvider { + + private static final Logger LOG = + LoggerFactory.getLogger(HadoopFSDelegationTokenProvider.class); + + private HadoopDelegationTokenConfiguration hadoopDelegationTokenConf; + + @Override + public String serviceName() { + return "hadoopfs"; + } + + @Override + public void init(final HadoopDelegationTokenConfiguration conf) { + this.hadoopDelegationTokenConf = conf; + } + + @Override + public boolean delegationTokensRequired() { + return UserGroupInformation.isSecurityEnabled(); + } + + @Override + public Optional<Long> obtainDelegationTokens(final Credentials credentials) { + try { + Set<FileSystem> fileSystemsToAccess = getFileSystemsToAccess(); + + final String renewer = getTokenRenewer(hadoopDelegationTokenConf.getHadoopConf()); + fileSystemsToAccess.forEach( + fs -> { + try { + LOG.info("Getting FS token for: {} with renewer {}", fs, renewer); + fs.addDelegationTokens(renewer, credentials); + } catch (IOException e) { + LOG.warn("Failed to get token for {}.", fs); Review comment: Hi @zuston , if the job fails to get the FS DTs, it's supposed to fail, so I will throw a runtime exception to fail fast. ########## File path: flink-yarn/src/main/java/org/apache/flink/yarn/security/HadoopFSDelegationTokenProvider.java ########## @@ -0,0 +1,126 @@ +/* + * 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.flink.yarn.security; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.configuration.ConfigUtils; +import org.apache.flink.runtime.security.delegationtokens.HadoopDelegationTokenConfiguration; +import org.apache.flink.runtime.security.delegationtokens.HadoopDelegationTokenProvider; +import org.apache.flink.util.FlinkRuntimeException; +import org.apache.flink.util.function.FunctionUtils; +import org.apache.flink.yarn.configuration.YarnConfigOptions; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.mapred.Master; +import org.apache.hadoop.security.Credentials; +import org.apache.hadoop.security.UserGroupInformation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +/** Delegation token provider implementation for Hadoop FileSystems. */ +public class HadoopFSDelegationTokenProvider implements HadoopDelegationTokenProvider { + + private static final Logger LOG = + LoggerFactory.getLogger(HadoopFSDelegationTokenProvider.class); + + private HadoopDelegationTokenConfiguration hadoopDelegationTokenConf; + + @Override + public String serviceName() { + return "hadoopfs"; + } + + @Override + public void init(final HadoopDelegationTokenConfiguration conf) { + this.hadoopDelegationTokenConf = conf; + } + + @Override + public boolean delegationTokensRequired() { + return UserGroupInformation.isSecurityEnabled(); + } + + @Override + public Optional<Long> obtainDelegationTokens(final Credentials credentials) { + try { + Set<FileSystem> fileSystemsToAccess = getFileSystemsToAccess(); + + final String renewer = getTokenRenewer(hadoopDelegationTokenConf.getHadoopConf()); + fileSystemsToAccess.forEach( + fs -> { + try { + LOG.info("Getting FS token for: {} with renewer {}", fs, renewer); Review comment: You're right, I will change this log to show the Filesystem's URI: `Getting FS token for: viewfs://clusterXX/ with renewer yarn/host@REALM`. ########## File path: flink-yarn/src/main/java/org/apache/flink/yarn/Utils.java ########## @@ -197,13 +196,19 @@ private static LocalResource registerLocalResource( } public static void setTokensFor( - ContainerLaunchContext amContainer, List<Path> paths, Configuration conf) + org.apache.flink.configuration.Configuration flinkConf, + ContainerLaunchContext amContainer, + Configuration hadoopConf) throws IOException { Credentials credentials = new Credentials(); - // for HDFS - TokenCache.obtainTokensForNamenodes(credentials, paths.toArray(new Path[0]), conf); - // for HBase - obtainTokenForHBase(credentials, conf); + + // obtain tokens from HadoopDelegationTokenProviders Review comment: Merged with FLINK-21700 ########## File path: flink-yarn/src/main/java/org/apache/flink/yarn/security/HadoopDelegationTokenManager.java ########## @@ -0,0 +1,96 @@ +/* + * 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.flink.yarn.security; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.runtime.security.delegationtokens.HadoopDelegationTokenConfiguration; +import org.apache.flink.runtime.security.delegationtokens.HadoopDelegationTokenProvider; + +import org.apache.hadoop.security.Credentials; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.ServiceLoader; + +/** + * HadoopDelegationTokenManager is responsible for managing delegation tokens. It can be used to + * obtain delegation tokens by calling `obtainDelegationTokens` method. + */ +public class HadoopDelegationTokenManager { + private static final Logger LOG = LoggerFactory.getLogger(HadoopDelegationTokenManager.class); + + private final HadoopDelegationTokenConfiguration hadoopDelegationTokenConf; + private final List<HadoopDelegationTokenProvider> delegationTokenProviders; + + public HadoopDelegationTokenManager( + HadoopDelegationTokenConfiguration hadoopDelegationTokenConf) { + this.hadoopDelegationTokenConf = hadoopDelegationTokenConf; + delegationTokenProviders = loadProviders(); + } + + /** + * Obtain delegation tokens using HadoopDelegationProviders, and store them in the give + * credentials. + * + * @param credentials Credentials object where to store the delegation tokens. + */ + public void obtainDelegationTokens(Credentials credentials) { + delegationTokenProviders.forEach( + provider -> { + if (provider.delegationTokensRequired()) { + provider.obtainDelegationTokens(credentials); + } else { + LOG.info( + "Service {} does not need to require a token,", + provider.serviceName()); + } + }); + } + + private List<HadoopDelegationTokenProvider> loadProviders() { + ServiceLoader<HadoopDelegationTokenProvider> serviceLoader = + ServiceLoader.load(HadoopDelegationTokenProvider.class); + + List<HadoopDelegationTokenProvider> providers = new ArrayList<>(); + + Iterator<HadoopDelegationTokenProvider> providerIterator = serviceLoader.iterator(); + providerIterator.forEachRemaining( + provider -> { + try { + provider.init(hadoopDelegationTokenConf); Review comment: I added a debug log for this, I don't want to print log in a verbose way. For each provider, it will print some information about this. ``` INFO org.apache.flink.yarn.Utils [] - Obtaining delegation tokens... DEBUG org.apache.flink.yarn.security.HadoopDelegationTokenManager [] - Delegation provider hbase loaded and initialized DEBUG org.apache.flink.yarn.security.HadoopDelegationTokenManager [] - Delegation provider hadoopfs loaded and initialized INFO org.apache.flink.yarn.security.HBaseDelegationTokenProvider [] - HBase is not available (not packaged with this application): ClassNotFoundException : "org.apache.hadoop.hbase.HBaseConfiguration". INFO org.apache.flink.yarn.security.HadoopDelegationTokenManager [] - Service hbase does not need to require a token. INFO org.apache.flink.yarn.security.HadoopFSDelegationTokenProvider [] - Getting FS token for: viewfs://cluster14/ with renewer yarn INFO org.apache.hadoop.hdfs.DFSClient [] - Created token for test: HDFS_DELEGATION_TOKEN owner=test, renewer=yarn, realUser=, issueDate=1621268501397, maxDate=1621873301397, sequenceNumber=26153859, masterKeyId=726 on ha-hdfs:apstore INFO org.apache.hadoop.hdfs.DFSClient [] - Created token for test: HDFS_DELEGATION_TOKEN owner=test, renewer=yarn, realUser=, issueDate=1621268501434, maxDate=1621873301434, sequenceNumber=10388618, masterKeyId=252 on ha-hdfs:warehousextab INFO org.apache.hadoop.hdfs.DFSClient [] - Created token for test: HDFS_DELEGATION_TOKEN owner=test, renewer=yarn, realUser=, issueDate=1621268501489, maxDate=1621873301489, sequenceNumber=86464794, masterKeyId=729 on ha-hdfs:warehousestore INFO org.apache.hadoop.hdfs.DFSClient [] - Created token for test: HDFS_DELEGATION_TOKEN owner=test, renewer=yarn, realUser=, issueDate=1621268501506, maxDate=1621873301506, sequenceNumber=10343224, masterKeyId=255 on ha-hdfs:warehousextac INFO org.apache.hadoop.hdfs.DFSClient [] - Created token for test: HDFS_DELEGATION_TOKEN owner=test, renewer=yarn, realUser=, issueDate=1621268501524, maxDate=1621873301524, sequenceNumber=10340707, masterKeyId=249 on ha-hdfs:warehousextaa INFO org.apache.hadoop.hdfs.DFSClient [] - Created token for test: HDFS_DELEGATION_TOKEN owner=test, renewer=yarn, realUser=, issueDate=1621268501528, maxDate=1621873301528, sequenceNumber=421001249, masterKeyId=714 on ha-hdfs:beaconstore INFO org.apache.flink.yarn.YarnClusterDescriptor [] - Submitting application master application_1620874570676_200951 ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
