Need some good insight on User Impersonation

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Need some good insight on User Impersonation

Niranjan Subramanian-2
Hi All, 

I’m trying to access HDFS on behalf of another user. I’m trying this with the following application 

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.log4j.Logger;
import org.apache.hadoop.fs.FSDataOutputStream;

public class HDFSProxyTest {

    public static void main (String[] args) throws Exception {
        String hadoopConfigurationPath = "/etc/hadoop/conf/";
       final  Configuration hdfsConfiguration = new Configuration();
       FileSystem localFileSystem = FileSystem.getLocal(hdfsConfiguration);
         Path coreSitePath = new Path(hadoopConfigurationPath+"core-site.xml");
         Path hdfsSitePath = new Path(hadoopConfigurationPath+"hdfs-site.xml");
        UserGroupInformation.loginUserFromKeytab("[hidden email]", "/home/striim/striim1_client.keytab");
         UserGroupInformation ugi =
            UserGroupInformation.createProxyUser("joy", UserGroupInformation.getLoginUser());
        FileSystem hadoopFileSystem =ugi.doAs(new PrivilegedExceptionAction<FileSystem>() {
      public FileSystem run() throws Exception {
         return  FileSystem.get(hdfsConfiguration);

        FSDataOutputStream fsDataOutputStream = hadoopFileSystem.create(new Path("/user/striim1/hdfsproxy.csv"));
        fsDataOutputStream.write("This is niranjan!!! testing this\n".getBytes());

Here this app execution user is striim and the super user I’m trying to emulate is striim1 who has Kerberos credentials and joy is the user on whose behalf I’m trying to access HDFS. 

I end up with this exception. 

2017-05-19 02:45:34,843 - WARN main org.apache.hadoop.util.NativeCodeLoader.<clinit> ( Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Exception in thread "main" Permission denied: user=joy, access=WRITE, inode="/user/striim1":striim1:striim1:drwxr-xr-x
at org.apache.hadoop.hdfs.server.namenode.DefaultAuthorizationProvider.checkFsPermission(
at org.apache.hadoop.hdfs.server.namenode.DefaultAuthorizationProvider.check(
at org.apache.hadoop.hdfs.server.namenode.DefaultAuthorizationProvider.check(
at org.apache.hadoop.hdfs.server.namenode.DefaultAuthorizationProvider.checkPermission(
at org.apache.sentry.hdfs.SentryAuthorizationProvider.checkPermission(
at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkPermission(
at org.apache.hadoop.hdfs.server.namenode.FSDirectory.checkPermission(
at org.apache.hadoop.hdfs.server.namenode.FSDirectory.checkPermission(
at org.apache.hadoop.hdfs.server.namenode.FSDirectory.checkAncestorAccess(
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.checkAncestorAccess(
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.startFileInternal(
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.startFileInt(
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.startFile(
at org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.create(
at org.apache.hadoop.hdfs.server.namenode.AuthorizationProviderProxyClientProtocol.create(
at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolServerSideTranslatorPB.create(
at org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos$ClientNamenodeProtocol$2.callBlockingMethod(
at org.apache.hadoop.ipc.ProtobufRpcEngine$Server$
at org.apache.hadoop.ipc.RPC$
at org.apache.hadoop.ipc.Server$Handler$
at org.apache.hadoop.ipc.Server$Handler$
at Method)
at org.apache.hadoop.ipc.Server$

This is my configuration on core-site.xml 

This is the permission setting of folder I’m trying to access 

drwxr-xr-x   - striim1       striim1                0 2017-05-19 02:50 /user/striim1

I need explanation for the following questions 
1) Even though I pass super user’s UGI to the proxy user joy. Why is the client trying to create the file in the context of user joy? 
2) In my cluster deployment, “striim1”  is just a user who has Kerberos credentials and not really a super-user as per this definition. Would impersonation work only if “striim1” is a superuser or added to the group of super user?
3) Should the name of the user I’m trying to impersonate be a valid OS user? If not what would happen and what validation is done in this respect? 
4) What should be the permission setting of directory I’m trying to write using this impersonated user? Should be it some location that is owned or part of super-user group? 
5) Should UGI.createProxyUser be called explicitly in my application? Say I execute my application from the user whom I want to impersonate using super-user and I pass proxy user configuration (basically passing core-site.xm) to my application? Would this suffice? (l’m expecting something like createProxyUser being called internally by taking current app executing user as the user to be impersonated).

Also in general let me know other internals of how this User impersonation works. I want to understand this well and I’m interested in knowing the answers of these 5 questions. 

Thanks in advance.