public virtual void TestStaticMapping() { Assume.AssumeTrue(!Shell.Windows); IDictionary <int, int> uidStaticMap = new ShellBasedIdMapping.PassThroughMap <int>( ); IDictionary <int, int> gidStaticMap = new ShellBasedIdMapping.PassThroughMap <int>( ); uidStaticMap[11501] = 10; gidStaticMap[497] = 200; // Maps for id to name map BiMap <int, string> uMap = HashBiMap.Create(); BiMap <int, string> gMap = HashBiMap.Create(); string GetAllUsersCmd = "echo \"atm:x:1000:1000:Aaron T. Myers,,,:/home/atm:/bin/bash\n" + "hdfs:x:11501:10787:Grid Distributed File System:/home/hdfs:/bin/bash\"" + " | cut -d: -f1,3"; string GetAllGroupsCmd = "echo \"hdfs:*:11501:hrt_hdfs\n" + "mapred:x:497\n" + "mapred2:x:498\"" + " | cut -d: -f1,3"; ShellBasedIdMapping.UpdateMapInternal(uMap, "user", GetAllUsersCmd, ":", uidStaticMap ); ShellBasedIdMapping.UpdateMapInternal(gMap, "group", GetAllGroupsCmd, ":", gidStaticMap ); Assert.Equal("hdfs", uMap[10]); Assert.Equal(10, (int)uMap.Inverse()["hdfs"]); Assert.Equal("atm", uMap[1000]); Assert.Equal(1000, (int)uMap.Inverse()["atm"]); Assert.Equal("hdfs", gMap[11501]); Assert.Equal(11501, (int)gMap.Inverse()["hdfs"]); Assert.Equal("mapred", gMap[200]); Assert.Equal(200, (int)gMap.Inverse()["mapred"]); Assert.Equal("mapred2", gMap[498]); Assert.Equal(498, (int)gMap.Inverse()["mapred2"]); }
/// <exception cref="System.IO.IOException"/> public virtual int GetUid(string user) { lock (this) { CheckAndUpdateMaps(); int id = uidNameMap.Inverse()[user]; if (id == null) { UpdateMapIncr(user, false); id = uidNameMap.Inverse()[user]; if (id == null) { throw new IOException("User just deleted?:" + user); } } return(id); } }
/// <exception cref="System.IO.IOException"/> public virtual int GetGid(string group) { lock (this) { CheckAndUpdateMaps(); int id = gidNameMap.Inverse()[group]; if (id == null) { UpdateMapIncr(group, true); id = gidNameMap.Inverse()[group]; if (id == null) { throw new IOException("No such group:" + group); } } return(id); } }
public static bool UpdateMapInternal(BiMap <int, string> map, string mapName, string command, string regex, IDictionary <int, int> staticMapping) { bool updated = false; BufferedReader br = null; try { SystemProcess process = Runtime.GetRuntime().Exec(new string[] { "bash", "-c", command }); br = new BufferedReader(new InputStreamReader(process.GetInputStream(), Encoding. Default)); string line = null; while ((line = br.ReadLine()) != null) { string[] nameId = line.Split(regex); if ((nameId == null) || (nameId.Length != 2)) { throw new IOException("Can't parse " + mapName + " list entry:" + line); } Log.Debug("add to " + mapName + "map:" + nameId[0] + " id:" + nameId[1]); // HDFS can't differentiate duplicate names with simple authentication int key = staticMapping[ParseId(nameId[1])]; string value = nameId[0]; if (map.Contains(key)) { string prevValue = map[key]; if (value.Equals(prevValue)) { // silently ignore equivalent entries continue; } ReportDuplicateEntry("Got multiple names associated with the same id: ", key, value , key, prevValue); continue; } if (map.ContainsValue(value)) { int prevKey = map.Inverse()[value]; ReportDuplicateEntry("Got multiple ids associated with the same name: ", key, value , prevKey, value); continue; } map[key] = value; updated = true; } Log.Debug("Updated " + mapName + " map size: " + map.Count); } catch (IOException e) { Log.Error("Can't update " + mapName + " map"); throw; } finally { if (br != null) { try { br.Close(); } catch (IOException e1) { Log.Error("Can't close BufferedReader of command result", e1); } } } return(updated); }