josephxsxn

JAAS Java LoginContext UserGroupInformation

Mar 20th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.IOException;
  4.  
  5. import javax.security.auth.Subject;
  6. import javax.security.auth.login.LoginContext;
  7. import javax.security.auth.login.LoginException;
  8.  
  9. import org.apache.hadoop.conf.Configuration;
  10. import org.apache.hadoop.security.UserGroupInformation;
  11.  
  12. public class GetTest2 {
  13.    
  14.  
  15. /* JAAS.CONF
  16. com.sun.security.jgss.krb5.initiate {
  17.     com.sun.security.auth.module.Krb5LoginModule required
  18.     doNotPrompt=true
  19.     principal="[email protected]"
  20.     useKeyTab=true
  21.     keyTab="./conf/devp.keytab"
  22.     storeKey=false
  23.     debug=false
  24.     useTicketCache=false;
  25. };
  26. */
  27.  
  28.     public static void main(String[] args) throws IOException, LoginException {
  29.        
  30.         if(args.length != 6){
  31.             System.out.println(String.join(",", args));
  32.             System.out.println("Classpath Jars found from: export jars=$(hadoop classpath):($hbase classpath)");
  33.             System.out.println("Usage: /usr/jdk64/jdk1.8.0_60/bin/java -cp $jars:FSHBaseTEST.jar test.GetTest2 HDFSFolder HBaseTable HBaseKey HBaseColFam HBaseCell HBaseVal");
  34.             System.out.println("conf/ folder should contain jaas.conf, hbase-site.xml, core-site.xml, hdfs-site.xml");
  35.             return;
  36.         }
  37.        
  38.         System.out.println();
  39.         System.setProperty("sun.security.krb5.debug", "false");
  40.         System.setProperty("java.security.auth.login.config", "./conf/jaas.conf");
  41.         System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
  42.         System.setProperty("java.security.krb5.conf", "./conf/krb5.conf");
  43.  
  44.         LoginContext context = new LoginContext("com.sun.security.jgss.krb5.initiate");
  45.         context.login();
  46.         Subject subject = context.getSubject();
  47.        
  48.         Configuration conf = new Configuration();
  49.         conf.addResource(new Path("./conf/hdfs-site.xml"));
  50.         conf.addResource(new Path("./conf/core-site.xml"));
  51.         conf.addResource(new Path("./conf/hbase-site.xml"));
  52.  
  53.  
  54.         UserGroupInformation.setConfiguration(conf);
  55.         UserGroupInformation.loginUserFromSubject(subject);
  56.        
  57.         System.out.println("NAME -> " + conf.get("dfs.namenode.http-address"));
  58.         System.out.println("SECURITY -> " + conf.get("hadoop.security.authentication"));
  59.         System.out.println("defaultFS -> " + conf.get("fs.defaultFS"));
  60.        
  61.         FileSystem fs = FileSystem.get(conf);
  62.         RemoteIterator<LocatedFileStatus> lfs = fs.listFiles(new Path(args[0]), false);
  63.  
  64.         while (lfs.hasNext()){
  65.             System.out.println(lfs.next().getPath());
  66.         }
  67.        
  68.         //HBASE TEST
  69.         Connection connection = ConnectionFactory.createConnection(conf);
  70.         Table table = connection.getTable(TableName.valueOf(args[1]));
  71.         Put pp = new Put(Bytes.toBytes(args[2]));
  72.         pp.add(Bytes.toBytes(args[3]), Bytes.toBytes(args[4]), Bytes.toBytes(args[5]));
  73.         table.put(pp);
  74.        
  75.        
  76.         Get g = new Get(Bytes.toBytes(args[2]));
  77.         Result result = table.get(g);
  78.         //System.out.println(result.toString());
  79.         byte[] value = result.getValue(Bytes.toBytes(args[3]), Bytes.toBytes(args[4]));
  80.  
  81.         // Printing the values
  82.         String stuff = Bytes.toString(value);
  83.         System.out.println("stuff " + stuff);
  84.    
  85.     }
  86.    
  87.  
  88.    
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment