You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by om...@apache.org on 2011/03/08 05:35:38 UTC

svn commit: r1079098 - in /hadoop/common/branches/yahoo-merge: CHANGES.txt src/java/org/apache/hadoop/fs/FileContext.java src/test/core/org/apache/hadoop/fs/FileContextPermissionBase.java

Author: omalley
Date: Tue Mar  8 04:35:38 2011
New Revision: 1079098

URL: http://svn.apache.org/viewvc?rev=1079098&view=rev
Log:
commit 3e13f12d5651ca808a2977eee689c35e5accefda
Author: Jitendra Nath Pandey <jitendra@sufferhome-lm.(none)>
Date:   Thu Oct 21 11:33:22 2010 -0700

     from

Modified:
    hadoop/common/branches/yahoo-merge/CHANGES.txt
    hadoop/common/branches/yahoo-merge/src/java/org/apache/hadoop/fs/FileContext.java
    hadoop/common/branches/yahoo-merge/src/test/core/org/apache/hadoop/fs/FileContextPermissionBase.java

Modified: hadoop/common/branches/yahoo-merge/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/branches/yahoo-merge/CHANGES.txt?rev=1079098&r1=1079097&r2=1079098&view=diff
==============================================================================
--- hadoop/common/branches/yahoo-merge/CHANGES.txt (original)
+++ hadoop/common/branches/yahoo-merge/CHANGES.txt Tue Mar  8 04:35:38 2011
@@ -38,6 +38,8 @@ Trunk (unreleased changes)
     HADOOP-6996. Allow CodecFactory to return a codec object given a codec'
     class name. (hairong)
 
+    HADOOP-7171. Support Ugi in the FileContext. (jitendra)
+
   IMPROVEMENTS
 
     HADOOP-6644. util.Shell getGROUPS_FOR_USER_COMMAND method name 

Modified: hadoop/common/branches/yahoo-merge/src/java/org/apache/hadoop/fs/FileContext.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/yahoo-merge/src/java/org/apache/hadoop/fs/FileContext.java?rev=1079098&r1=1079097&r2=1079098&view=diff
==============================================================================
--- hadoop/common/branches/yahoo-merge/src/java/org/apache/hadoop/fs/FileContext.java (original)
+++ hadoop/common/branches/yahoo-merge/src/java/org/apache/hadoop/fs/FileContext.java Tue Mar  8 04:35:38 2011
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URI;
+import java.security.PrivilegedExceptionAction;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.EnumSet;
@@ -49,6 +50,7 @@ import org.apache.hadoop.ipc.RpcServerEx
 import org.apache.hadoop.ipc.UnexpectedServerException;
 import org.apache.hadoop.fs.InvalidPathException;
 import org.apache.hadoop.security.AccessControlException;
+import org.apache.hadoop.security.UserGroupInformation;
 
 /**
  * The FileContext class provides an interface to the application writer for
@@ -194,12 +196,20 @@ public final class FileContext {
   private Path workingDir;          // Fully qualified
   private FsPermission umask;
   private final Configuration conf;
+  private final UserGroupInformation ugi;
 
   private FileContext(final AbstractFileSystem defFs,
     final FsPermission theUmask, final Configuration aConf) {
     defaultFS = defFs;
     umask = FsPermission.getUMask(aConf);
     conf = aConf;
+    try {
+      ugi = UserGroupInformation.getCurrentUser();
+    } catch (IOException e) {
+      LOG.error("Exception in getCurrentUser: "+e);
+      throw new RuntimeException("Failed to get the current user " +
+      		"while creating a FileContext");
+    }
     /*
      * Init the wd.
      * WorkingDir is implemented at the FileContext layer 
@@ -288,7 +298,25 @@ public final class FileContext {
       defaultFS.checkPath(absOrFqPath);
       return defaultFS;
     } catch (Exception e) { // it is different FileSystem
-      return AbstractFileSystem.get(absOrFqPath.toUri(), conf);
+      try {
+        return ugi.doAs(new PrivilegedExceptionAction<AbstractFileSystem>() {
+
+          public AbstractFileSystem run() throws UnsupportedFileSystemException {
+            return AbstractFileSystem.get(absOrFqPath.toUri(), conf);
+          }
+
+        });
+      } catch (InterruptedException ex) {
+        LOG.error(ex);
+        throw new RuntimeException(
+            "Failed to get the AbstractFileSystem for path: " + absOrFqPath);
+      } catch (UnsupportedFileSystemException ex) {
+        throw ex;
+      } catch (IOException ex) {
+        LOG.error("IOException in doAs: " + ex);
+        throw new RuntimeException(
+            "Failed to get the AbstractFileSystem for path: " + absOrFqPath);
+      }
     }
   }
   
@@ -464,6 +492,14 @@ public final class FileContext {
   }
   
   /**
+   * Gets the ugi in the file-context
+   * @return UserGroupInformation
+   */
+  public UserGroupInformation getUgi() {
+    return ugi;
+  }
+  
+  /**
    * 
    * @return the umask of this FileContext
    */

Modified: hadoop/common/branches/yahoo-merge/src/test/core/org/apache/hadoop/fs/FileContextPermissionBase.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/yahoo-merge/src/test/core/org/apache/hadoop/fs/FileContextPermissionBase.java?rev=1079098&r1=1079097&r2=1079098&view=diff
==============================================================================
--- hadoop/common/branches/yahoo-merge/src/test/core/org/apache/hadoop/fs/FileContextPermissionBase.java (original)
+++ hadoop/common/branches/yahoo-merge/src/test/core/org/apache/hadoop/fs/FileContextPermissionBase.java Tue Mar  8 04:35:38 2011
@@ -18,6 +18,7 @@
 package org.apache.hadoop.fs;
 
 import java.io.IOException;
+import java.security.PrivilegedExceptionAction;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.StringTokenizer;
@@ -25,6 +26,7 @@ import java.util.StringTokenizer;
 import junit.framework.Assert;
 
 import org.apache.hadoop.fs.permission.FsPermission;
+import org.apache.hadoop.security.UserGroupInformation;
 import org.apache.hadoop.util.Shell;
 import org.apache.hadoop.util.StringUtils;
 import org.junit.After;
@@ -32,6 +34,7 @@ import org.junit.Before;
 import org.junit.Test;
 
 import static org.apache.hadoop.fs.FileContextTestHelper.*;
+import static org.junit.Assert.assertEquals;
 
 /**
  * <p>
@@ -162,6 +165,22 @@ public abstract class FileContextPermiss
     } 
     finally {cleanupFile(fc, f);}
   }
+  
+  @Test
+  public void testUgi() throws IOException, InterruptedException {
+    
+    UserGroupInformation otherUser = UserGroupInformation
+        .createRemoteUser("otherUser");
+    FileContext newFc = otherUser.doAs(new PrivilegedExceptionAction<FileContext>() {
+
+      public FileContext run() throws Exception {
+        FileContext newFc = FileContext.getFileContext();
+        return newFc;
+      }
+      
+    });
+    assertEquals("otherUser",newFc.getUgi().getUserName());
+  }
 
   static List<String> getGroups() throws IOException {
     List<String> a = new ArrayList<String>();