ranger-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mad...@apache.org
Subject [05/27] incubator-ranger git commit: Ranger-201: Addition changes to process user group JSON array file format in the FileSource
Date Wed, 07 Jan 2015 19:32:04 GMT
Ranger-201: Addition changes to process user group JSON array file format in the FileSource


Project: http://git-wip-us.apache.org/repos/asf/incubator-ranger/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ranger/commit/1d0c1490
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ranger/tree/1d0c1490
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ranger/diff/1d0c1490

Branch: refs/heads/stack
Commit: 1d0c14908e9a1c4e8ceb5f27dcd7e7392f3f06bc
Parents: 2203ffb
Author: rmani <rmani@hortonworks.com>
Authored: Mon Jan 5 00:02:56 2015 -0800
Committer: rmani <rmani@hortonworks.com>
Committed: Mon Jan 5 00:02:56 2015 -0800

----------------------------------------------------------------------
 .../process/FileSourceUserGroupBuilder.java     | 190 ++++++++++++-------
 1 file changed, 118 insertions(+), 72 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/1d0c1490/ugsync/src/main/java/org/apache/ranger/unixusersync/process/FileSourceUserGroupBuilder.java
----------------------------------------------------------------------
diff --git a/ugsync/src/main/java/org/apache/ranger/unixusersync/process/FileSourceUserGroupBuilder.java
b/ugsync/src/main/java/org/apache/ranger/unixusersync/process/FileSourceUserGroupBuilder.java
index 8ebe71c..65f8e6c 100644
--- a/ugsync/src/main/java/org/apache/ranger/unixusersync/process/FileSourceUserGroupBuilder.java
+++ b/ugsync/src/main/java/org/apache/ranger/unixusersync/process/FileSourceUserGroupBuilder.java
@@ -22,39 +22,43 @@ package org.apache.ranger.unixusersync.process;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
 import org.apache.log4j.Logger;
 import org.apache.ranger.unixusersync.config.UserGroupSyncConfig;
-import org.apache.ranger.unixusersync.model.UserGroupList;
 import org.apache.ranger.usergroupsync.UserGroupSink;
 import org.apache.ranger.usergroupsync.UserGroupSource;
 
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
+import com.google.gson.stream.JsonReader;
 
 public class FileSourceUserGroupBuilder  implements UserGroupSource {
-
 	private static final Logger LOG = Logger.getLogger(FileSourceUserGroupBuilder.class) ;
-	
-	public static  String DEFAULT_USER_GROUP_FILE = null;
-	public static String USER_GROUP_FILE = null;
-	
-	private static boolean isManualRun=false;
-	private Map<String,List<String>>  user2GroupListMap = new HashMap<String,List<String>>();
-	private UserGroupSyncConfig config = UserGroupSyncConfig.getInstance() ;
-	private UserGroupSink ugSink     = null ;
-	private long  usergroupFileModified = 0 ;
-	
+
+	private Map<String,List<String>> user2GroupListMap     = new HashMap<String,List<String>>();
+	private UserGroupSyncConfig      config                = UserGroupSyncConfig.getInstance();
+	private String                   userGroupFilename     = null;
+	private long                     usergroupFileModified = 0 ;
+
+
 	public static void main(String[] args) throws Throwable {
+		FileSourceUserGroupBuilder filesourceUGBuilder = new FileSourceUserGroupBuilder() ;
+
 		if (args.length > 0) {
-			isManualRun=true;
-			USER_GROUP_FILE = args[0];
-		}	
-		FileSourceUserGroupBuilder  filesourceUGBuilder = new FileSourceUserGroupBuilder() ;
+			filesourceUGBuilder.setUserGroupFilename(args[0]);
+		}
+		
 		filesourceUGBuilder.init();
+
+		UserGroupSink ugSink = UserGroupSyncConfig.getInstance().getUserGroupSink();
+		LOG.info("initializing sink: " + ugSink.getClass().getName());
+		ugSink.init();
+
+		filesourceUGBuilder.updateSink(ugSink);
+		
 		if ( LOG.isDebugEnabled()) {
 			filesourceUGBuilder.print(); 
 		}
@@ -62,31 +66,16 @@ public class FileSourceUserGroupBuilder  implements UserGroupSource {
 	
 	@Override
 	public void init() throws Throwable {
-		DEFAULT_USER_GROUP_FILE = config.getUserSyncFileSource();
-		buildUserGroupInfo();
-		if (isManualRun) {
-			ugSink = UserGroupSyncConfig.getInstance().getUserGroupSink();
-			LOG.info("initializing sink: " + ugSink.getClass().getName());
-			ugSink.init();
-			updateSink(ugSink);
+		if(userGroupFilename == null) {
+			userGroupFilename = config.getUserSyncFileSource();
 		}
-	}
 
-	private void print() {
-		for(String user : user2GroupListMap.keySet()) {
-			LOG.debug("USER:" + user) ;
-			List<String> groups = user2GroupListMap.get(user) ;
-			if (groups != null) {
-				for(String group : groups) {
-					LOG.debug("\tGROUP: " + group) ;
-				}
-			}
-		}
+		buildUserGroupInfo();
 	}
 	
 	@Override
 	public boolean isChanged() {
-		long TempUserGroupFileModifedAt = new File(DEFAULT_USER_GROUP_FILE).lastModified() ;
+		long TempUserGroupFileModifedAt = new File(userGroupFilename).lastModified() ;
 		if (usergroupFileModified != TempUserGroupFileModifedAt) {
 			return true ;
 		}
@@ -95,16 +84,33 @@ public class FileSourceUserGroupBuilder  implements UserGroupSource {
 
 	@Override
 	public void updateSink(UserGroupSink sink) throws Throwable {
-		buildUserGroupInfo() ;
+		buildUserGroupInfo();
+
 		for (Map.Entry<String, List<String>> entry : user2GroupListMap.entrySet())
{
 		    String       user   = entry.getKey();
 		    List<String> groups = entry.getValue();
+
 		    sink.addOrUpdateUser(user, groups);
 		}
 	}
 
+	private void setUserGroupFilename(String filename) {
+		userGroupFilename = filename;
+	}
+
+	private void print() {
+		for(String user : user2GroupListMap.keySet()) {
+			LOG.debug("USER:" + user) ;
+			List<String> groups = user2GroupListMap.get(user) ;
+			if (groups != null) {
+				for(String group : groups) {
+					LOG.debug("\tGROUP: " + group) ;
+				}
+			}
+		}
+	}
+
 	public void buildUserGroupInfo() throws Throwable {
-		user2GroupListMap = new HashMap<String,List<String>>();
 		buildUserGroupList();
 		if ( LOG.isDebugEnabled()) {
 			print(); 
@@ -112,50 +118,90 @@ public class FileSourceUserGroupBuilder  implements UserGroupSource
{
 	}
 	
 	public void buildUserGroupList() throws Throwable {
-	
-		Gson gson = new GsonBuilder().create() ;
-		
-		UserGroupList usergrouplist = new UserGroupList();
-		
-		String usergroupFile = null;
-		
-		if (isManualRun) {
-			usergroupFile = USER_GROUP_FILE;
-		} else {
-			usergroupFile = DEFAULT_USER_GROUP_FILE;
-		}
-		
-		if (usergroupFile == null){
+		if (userGroupFilename == null){
 			throw new Exception("User Group Source File is not Configured. Please maintain in unixauthservice.properties
or pass it as command line argument for org.apache.ranger.unixusersync.process.FileSourceUserGroupBuilder");
 		}
+	
+		File f = new File(userGroupFilename);
 		
-		File f = new File(usergroupFile);
-		
-		if (f.exists()) {
+		if (f.exists() && f.canRead()) {
 			
-			BufferedReader bfr  = new BufferedReader(new FileReader(f));
+			Map<String,List<String>> tmpUser2GroupListMap = new HashMap<String,List<String>>();
 			
-			String line = null ;
+			JsonReader jsonReader = new JsonReader(new BufferedReader(new FileReader(f)));
 			
-			while ((line = bfr.readLine()) != null) {
+			jsonReader.setLenient(true);
 			
-				if (line.trim().isEmpty()) 
-					continue ;
-				
-				usergrouplist = gson.fromJson(line,UserGroupList.class);
-				
-				String user = usergrouplist.getUser();
-				
-				List<String> groups = usergrouplist.getGroups();
+			jsonReader.beginArray();
+			
+			while (jsonReader.hasNext() ) {
+				Map<String, List<String>> usergroupMap = getUserGroupMap(jsonReader);
 				
-				if ( user != null) {
-					user2GroupListMap.put(user, groups);
-				}
+				for(String user : usergroupMap.keySet()) {
+					List<String> groups = usergroupMap.get(user) ;
+					tmpUser2GroupListMap.put(user,groups);
+				}		
 			}
 			
-			bfr.close();
+			jsonReader.endArray();
+			
+			jsonReader.close();
+
+			user2GroupListMap     = tmpUser2GroupListMap;
+			
 			usergroupFileModified = f.lastModified() ;
+			
+		} else {
+			throw new Exception("User Group Source File " + userGroupFilename + "doesn't not exist
or readable");
 		}
-
+	}
+	
+	
+	public Map<String, List<String>> getUserGroupMap(JsonReader jsonReader) throws
Exception {
+		
+		Map<String, List<String>> ret = new HashMap<String, List<String>>();
+		String user = null ;
+		List<String> groups = new ArrayList<String>();
+		
+		jsonReader.beginObject();
+		
+		while ( jsonReader.hasNext()) {
+			
+			String name = jsonReader.nextName();
+			
+			if ( name.equals("user")) {
+				user = jsonReader.nextString();
+			} else if ( name.equals("groups")) {
+				groups = getGroups(jsonReader);
+			} else {
+				StringBuilder sb = new StringBuilder();
+				sb.append("User Group Source JSON array should have following **user** and **groups**
as name tag e.g");
+				sb.append("[ {\"user\":\"userid1\",\"groups\":[\"groupid1\",\"groupid2\"]},");
+				sb.append("[ {\"user\":\"userid2\",\"groups\":[\"groupid1\",\"groupid2\"]}..]");
+				throw new Exception(sb.toString());
+			}
+			
+			if ( user != null ) {
+				ret.put(user, groups);
+			}
+		}
+		
+		jsonReader.endObject();
+		
+		return ret;
+	}
+	
+	public List<String> getGroups(JsonReader reader) throws IOException {
+		List<String> ret = new ArrayList<String>();
+		
+		reader.beginArray();
+		
+		while(reader.hasNext()) {
+			ret.add(reader.nextString());
+		}
+		
+		reader.endArray();
+		
+		return ret;
 	}
 }
\ No newline at end of file


Mime
View raw message