MAPREDUCE-2473. Add "mapred groups" command to query the server-side groups resolved for a user. Contributed by Aaron T. Myers.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/mapreduce/trunk@1102515 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index 72d3436..266d338 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -6,6 +6,9 @@
 
   NEW FEATURES
 
+    MAPREDUCE-2473. Add "mapred groups" command to query the server-side groups
+    resolved for a user. (Aaron T. Myers via todd)
+
   IMPROVEMENTS
 
     MAPREDUCE-2153. Bring in more job configuration properties in to the trace 
diff --git a/bin/mapred b/bin/mapred
index c56a19d..171f052 100755
--- a/bin/mapred
+++ b/bin/mapred
@@ -29,6 +29,7 @@
   echo "  pipes                run a Pipes job"
   echo "  job                  manipulate MapReduce jobs"
   echo "  queue                get information regarding JobQueues"
+  echo "  groups               get the groups which users belong to"
   echo ""
   echo "Most commands print help when invoked w/o parameters."
 }
@@ -60,6 +61,9 @@
 elif [ "$COMMAND" = "sampler" ] ; then
   CLASS=org.apache.hadoop.mapred.lib.InputSampler
   HADOOP_OPTS="$HADOOP_OPTS $HADOOP_CLIENT_OPTS"
+elif [ "$COMMAND" = "groups" ] ; then
+  CLASS=org.apache.hadoop.mapred.tools.GetGroups
+  HADOOP_OPTS="$HADOOP_OPTS $HADOOP_CLIENT_OPTS"
 else
   echo $COMMAND - invalid command
   print_usage
diff --git a/src/java/org/apache/hadoop/mapred/JobTracker.java b/src/java/org/apache/hadoop/mapred/JobTracker.java
index 4bf11b0..3e35849 100644
--- a/src/java/org/apache/hadoop/mapred/JobTracker.java
+++ b/src/java/org/apache/hadoop/mapred/JobTracker.java
@@ -114,6 +114,7 @@
 import org.apache.hadoop.security.authorize.RefreshAuthorizationPolicyProtocol;
 import org.apache.hadoop.security.authorize.ServiceAuthorizationManager;
 import org.apache.hadoop.security.token.Token;
+import org.apache.hadoop.tools.GetUserMappingsProtocol;
 import org.apache.hadoop.util.HostsFileReader;
 import org.apache.hadoop.util.ReflectionUtils;
 import org.apache.hadoop.util.StringUtils;
@@ -128,7 +129,8 @@
 @InterfaceStability.Unstable
 public class JobTracker implements MRConstants, InterTrackerProtocol,
     ClientProtocol, TaskTrackerManager, RefreshUserMappingsProtocol,
-    RefreshAuthorizationPolicyProtocol, AdminOperationsProtocol, JTConfig {
+    RefreshAuthorizationPolicyProtocol, AdminOperationsProtocol, 
+    GetUserMappingsProtocol, JTConfig {
 
   static{
     ConfigUtil.loadResources();
@@ -321,6 +323,8 @@
       return AdminOperationsProtocol.versionID;
     } else if (protocol.equals(RefreshUserMappingsProtocol.class.getName())){
       return RefreshUserMappingsProtocol.versionID;
+    } else if (protocol.equals(GetUserMappingsProtocol.class.getName())){
+      return GetUserMappingsProtocol.versionID;
     } else {
       throw new IOException("Unknown protocol to job tracker: " + protocol);
     }
@@ -4491,6 +4495,14 @@
 
     ProxyUsers.refreshSuperUserGroupsConfiguration();
   }
+  
+  @Override
+  public String[] getGroupsForUser(String user) throws IOException {
+    if (LOG.isDebugEnabled()) {
+      LOG.debug("Getting groups for user " + user);
+    }
+    return UserGroupInformation.createRemoteUser(user).getGroupNames();
+  }
 
   @Override
   public void refreshUserToGroupsMappings() throws IOException {
diff --git a/src/java/org/apache/hadoop/mapred/MapReducePolicyProvider.java b/src/java/org/apache/hadoop/mapred/MapReducePolicyProvider.java
index 5b49489..18c40f3 100644
--- a/src/java/org/apache/hadoop/mapred/MapReducePolicyProvider.java
+++ b/src/java/org/apache/hadoop/mapred/MapReducePolicyProvider.java
@@ -24,6 +24,7 @@
 import org.apache.hadoop.security.authorize.PolicyProvider;
 import org.apache.hadoop.security.authorize.RefreshAuthorizationPolicyProtocol;
 import org.apache.hadoop.security.authorize.Service;
+import org.apache.hadoop.tools.GetUserMappingsProtocol;
 
 /**
  * {@link PolicyProvider} for Map-Reduce protocols.
@@ -45,6 +46,8 @@
                   RefreshUserMappingsProtocol.class),
       new Service("security.admin.operations.protocol.acl", 
                   AdminOperationsProtocol.class),
+      new Service("security.get.user.mappings.protocol.acl",
+                  GetUserMappingsProtocol.class)
   };
   
   @Override
diff --git a/src/java/org/apache/hadoop/mapred/tools/GetGroups.java b/src/java/org/apache/hadoop/mapred/tools/GetGroups.java
new file mode 100644
index 0000000..a2be335
--- /dev/null
+++ b/src/java/org/apache/hadoop/mapred/tools/GetGroups.java
@@ -0,0 +1,58 @@
+/**
+ * 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.hadoop.mapred.tools;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import java.net.InetSocketAddress;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.mapred.JobTracker;
+import org.apache.hadoop.tools.GetGroupsBase;
+import org.apache.hadoop.util.ToolRunner;
+
+/**
+ * MR implementation of a tool for getting the groups which a given user
+ * belongs to.
+ */
+public class GetGroups extends GetGroupsBase {
+
+  static {
+    Configuration.addDefaultResource("mapred-default.xml");
+    Configuration.addDefaultResource("mapred-site.xml");
+  }
+  
+  GetGroups(Configuration conf) {
+    super(conf);
+  }
+  
+  GetGroups(Configuration conf, PrintStream out) {
+    super(conf, out);
+  }
+
+  @Override
+  protected InetSocketAddress getProtocolAddress(Configuration conf)
+      throws IOException {
+    return JobTracker.getAddress(conf);
+  }
+
+  public static void main(String[] argv) throws Exception {
+    int res = ToolRunner.run(new GetGroups(new Configuration()), argv);
+    System.exit(res);
+  }
+}
\ No newline at end of file
diff --git a/src/test/mapred/org/apache/hadoop/mapred/tools/TestGetGroups.java b/src/test/mapred/org/apache/hadoop/mapred/tools/TestGetGroups.java
new file mode 100644
index 0000000..26da853
--- /dev/null
+++ b/src/test/mapred/org/apache/hadoop/mapred/tools/TestGetGroups.java
@@ -0,0 +1,53 @@
+/**
+ * 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.hadoop.mapred.tools;
+
+import java.io.IOException;
+import java.io.PrintStream;
+
+import org.apache.hadoop.mapred.MiniMRCluster;
+import org.apache.hadoop.mapred.tools.GetGroups;
+import org.apache.hadoop.tools.GetGroupsTestBase;
+import org.apache.hadoop.util.Tool;
+import org.junit.After;
+import org.junit.Before;
+
+/**
+ * Tests for the MR implementation of {@link GetGroups}
+ */
+public class TestGetGroups extends GetGroupsTestBase {
+  
+  private MiniMRCluster cluster;
+
+  @Before
+  public void setUpJobTracker() throws IOException, InterruptedException {
+    cluster = new MiniMRCluster(0, "file:///", 1);
+    conf = cluster.createJobConf();
+  }
+  
+  @After
+  public void tearDownJobTracker() throws IOException {
+    cluster.shutdown();
+  }
+
+  @Override
+  protected Tool getTool(PrintStream o) {
+    return new GetGroups(conf, o);
+  }
+
+}