tillrohrmann commented on a change in pull request #11189: [FLINK-16236][yarn][hotfix] fix Yarn secure test not loading correct context factory URL: https://github.com/apache/flink/pull/11189#discussion_r386453071
########## File path: flink-yarn-tests/src/test/java/org/apache/flink/yarn/util/TestHadoopSecurityContextFactory.java ########## @@ -0,0 +1,63 @@ +/* + * 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.util; + +import org.apache.flink.runtime.security.SecurityConfiguration; +import org.apache.flink.runtime.security.contexts.HadoopSecurityContext; +import org.apache.flink.runtime.security.contexts.HadoopSecurityContextFactory; +import org.apache.flink.runtime.security.contexts.SecurityContext; +import org.apache.flink.runtime.security.contexts.SecurityContextFactory; + +import org.apache.hadoop.security.UserGroupInformation; + +/** + * Test hadoop context module factory associated with {@link TestHadoopModuleFactory}. + */ +public class TestHadoopSecurityContextFactory implements SecurityContextFactory { + + @Override + public boolean isCompatibleWith(SecurityConfiguration securityConfig) { + if (!securityConfig.getSecurityModuleFactories().contains(TestHadoopModuleFactory.class.getCanonicalName())) { + return false; + } + try { + Class.forName( + "org.apache.hadoop.security.UserGroupInformation", + false, + HadoopSecurityContextFactory.class.getClassLoader()); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } + + @Override + public SecurityContext createContext(SecurityConfiguration securityConfig) { + try { + Class.forName( + "org.apache.hadoop.security.UserGroupInformation", + false, + TestHadoopSecurityContextFactory.class.getClassLoader()); + UserGroupInformation loginUser = UserGroupInformation.getLoginUser(); + return new HadoopSecurityContext(loginUser); + } catch (Exception e) { + return null; Review comment: I think this should be better expressed in the interface. E.g. one could add a `@Nullable` to the the return type stating that this method returns `null` if it cannot instantiate a `Context`. Using `Optional` would be even better. However, the problem with this kind of approach is that we are losing the context of `e`. `e` might contain helpful information why the context could not be instantiated. Hence, I would suggest to make the contract so that `createContext` always needs to return something. If not, then it should thrown an exception. How the service loader implementation reacts to it, should not be the problem of this class here. ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services