【ZooKeeper Notes 7】使用super身份对有权限的节点进行操作

      转载请用注明:@ni掌柜 nileader@gmail.com

      如果客户端设置了权限,那么其它人如果没有授权,就无法对这个节点进行操作。但是对于管理员来说,有没有一种方法,可以对任意节点进行操作呢,答案是有的~

方法简单描述如下:

1. 确认是否开启zookeeper的superDigest模式。方法如下:

     首先配置如下启动参数,然后重启server

 
 
  1.  
  1. "-Dzookeeper.DigestAuthenticationProvider.superDigest=super:/7ahZf2EjED/untmtb2NRkHhVlA=" 


2. 在java代码中进行digest模式的授权,方法如下:

 
 
  1. zkClient.addAuthInfo( "digest""super:yinshi.nc-1988".getBytes() ); 

3. 具体样例参见下面:

 
 
  1. import java.util.ArrayList; 
  2. import java.util.List; 
  3.  
  4. import org.apache.zookeeper.WatchedEvent; 
  5. import org.apache.zookeeper.Watcher; 
  6. import org.apache.zookeeper.ZooDefs.Ids; 
  7. import org.apache.zookeeper.data.ACL; 
  8. import org.apache.zookeeper.server.auth.DigestAuthenticationProvider; 
  9.  
  10. /** 
  11.  * Description: ZooKeeper-Authentication Test 
  12.  * @author   nileader / nileader@gmail.com 
  13.  * @Date     Jul 12, 2012 
  14.  */ 
  15. public class DemoAuth2 implements Watcher { 
  16.  
  17.     final static String SERVER_LIST = "127.0.0.1:2181"
  18.      
  19.     final static String PATH = "/yinshi_auth_test"
  20.     final static String PATH_DEL = "/yinshi_auth_test/will_be_del"
  21.  
  22.     final static String authentication_type = "digest"
  23.  
  24.     final static String correctAuthentication = "taokeeper:true"
  25.     final static String badAuthentication = "taokeeper:errorCode"
  26.     final static String superAuthentication = "super:yinshi.nc-1988"
  27.  
  28.     static ZkClient zkClient = null
  29.  
  30.     public static void main( String[] args ) throws Exception { 
  31.  
  32.         System.out.println( DigestAuthenticationProvider.generateDigest( "super:yinshi.nc-1988" ) ); 
  33.          
  34.         List< ACL > acls = new ArrayList< ACL >( 1 ); 
  35.         for ( ACL ids_acl : Ids.CREATOR_ALL_ACL ) { 
  36.             acls.add( ids_acl ); 
  37.         } 
  38.  
  39.         try { 
  40.             zkClient = new ZkClient( SERVER_LIST, 50000); 
  41.             zkClient.addAuthInfo( authentication_type, correctAuthentication.getBytes() ); 
  42.         } catch ( Exception e ) { 
  43.             // TODO Auto-generated catch block 
  44.             e.printStackTrace(); 
  45.         } 
  46.  
  47.         try { 
  48.             zkClient.createPersistent( PATH, acls, "init content" ); 
  49.             System.out.println( "使用授权key:" + correctAuthentication + "创建节点:" + PATH + ", 初始内容是: init content" ); 
  50.         } catch ( Exception e ) { 
  51.             e.printStackTrace(); 
  52.         } 
  53.         try { 
  54.             zkClient.createPersistent( PATH_DEL, acls, "待删节点" ); 
  55.             System.out.println( "使用授权key:" + correctAuthentication + "创建节点:" + PATH_DEL + ", 初始内容是: init content" ); 
  56.         } catch ( Exception e ) { 
  57.             // TODO Auto-generated catch block 
  58.             e.printStackTrace(); 
  59.         } 
  60.  
  61.         // 获取数据 
  62.         getDataByNoAuthentication(); 
  63.         getDataByBadAuthentication(); 
  64.         getDataByCorrectAuthentication(); 
  65.         getDataByBadAuthentication(); 
  66.         getDataBySuperAuthentication(); 
  67. // 
  68. //      // 更新数据 
  69. //      updateDataByNoAuthentication(); 
  70. //      updateDataByBadAuthentication(); 
  71. //      updateDataByCorrectAuthentication(); 
  72. // 
  73. //      // 获取数据 
  74. //      getDataByNoAuthentication(); 
  75. //      getDataByBadAuthentication(); 
  76. //      getDataByCorrectAuthentication(); 
  77. // 
  78. //      //删除数据 
  79. //      deleteNodeByBadAuthentication(); 
  80. //      deleteNodeByNoAuthentication(); 
  81. //      deleteNodeByCorrectAuthentication(); 
  82. // 
  83. //      deleteParent(); 
  84.          
  85.         zkClient.close(); 
  86.     } 
  87.  
  88.     /** 获取数据:采用错误的密码 */ 
  89.     static void getDataByBadAuthentication() { 
  90.         String prefix = "[使用错误的授权信息]"
  91.         try { 
  92.             System.out.println( prefix + "获取数据:" + PATH ); 
  93.             zkClient = new ZkClient( SERVER_LIST, 50000); 
  94.             zkClient.addAuthInfo( authentication_type, badAuthentication.getBytes() ); 
  95.             System.out.println( prefix + "成功获取数据:" + zkClient.readData( PATH ) ); 
  96.         } catch ( Exception e ) { 
  97.             System.err.println( prefix + "获取数据失败,原因:" + e.getMessage() ); 
  98.         } 
  99.     } 
  100.  
  101.     /** 获取数据:不采用密码 */ 
  102.     static void getDataByNoAuthentication() { 
  103.         String prefix = "[不使用任何授权信息]"
  104.         try { 
  105.             System.out.println( prefix + "获取数据:" + PATH ); 
  106.             zkClient = new ZkClient( SERVER_LIST, 50000); 
  107.             System.out.println( prefix + "成功获取数据:" + zkClient.readData( PATH ) ); 
  108.         } catch ( Exception e ) { 
  109.             System.err.println( prefix + "获取数据失败,原因:" + e.getMessage() ); 
  110.         } 
  111.     } 
  112.  
  113.     /** 采用正确的密码 */ 
  114.     static void getDataByCorrectAuthentication() { 
  115.         String prefix = "[使用正确的授权信息]"
  116.         try { 
  117.             System.out.println( prefix + "获取数据:" + PATH ); 
  118.             zkClient = new ZkClient( SERVER_LIST, 50000); 
  119.             zkClient.addAuthInfo( authentication_type, correctAuthentication.getBytes() ); 
  120.             System.out.println( prefix + "成功获取数据:" + zkClient.readData( PATH ) ); 
  121.         } catch ( Exception e ) { 
  122.             System.out.println( prefix + "获取数据失败,原因:" + e.getMessage() ); 
  123.         } 
  124.     } 
  125.      
  126.     /** 采用超级用户的密码 */ 
  127.     static void getDataBySuperAuthentication() { 
  128.         String prefix = "[使用超级用户的授权信息]"
  129.         try { 
  130.             System.out.println( prefix + "获取数据:" + PATH ); 
  131.             zkClient = new ZkClient( SERVER_LIST, 50000); 
  132.             zkClient.addAuthInfo( authentication_type, superAuthentication.getBytes() ); 
  133.             System.out.println( prefix + "成功获取数据:" + zkClient.readData( PATH ) ); 
  134.         } catch ( Exception e ) { 
  135.             System.out.println( prefix + "获取数据失败,原因:" + e.getMessage() ); 
  136.         } 
  137.     } 
  138.  
  139.     /** 
  140.      * 更新数据:不采用密码 
  141.      */ 
  142.     static void updateDataByNoAuthentication() { 
  143.          
  144.         String prefix = "[不使用任何授权信息]"
  145.          
  146.         System.out.println( prefix + "更新数据: " + PATH ); 
  147.         try { 
  148.             zkClient = new ZkClient( SERVER_LIST, 50000); 
  149.             if( zkClient.exists( PATH ) ){ 
  150.                 zkClient.writeData( PATH, prefix ); 
  151.                 System.out.println( prefix + "更新成功" ); 
  152.             } 
  153.         } catch ( Exception e ) { 
  154.             System.err.println( prefix + "更新失败,原因是:" + e.getMessage() ); 
  155.         } 
  156.     } 
  157.  
  158.     /** 
  159.      * 更新数据:采用错误的密码 
  160.      */ 
  161.     static void updateDataByBadAuthentication() { 
  162.          
  163.         String prefix = "[使用错误的授权信息]"
  164.          
  165.         System.out.println( prefix + "更新数据:" + PATH ); 
  166.         try { 
  167.             zkClient = new ZkClient( SERVER_LIST, 50000); 
  168.             zkClient.addAuthInfo( authentication_type, badAuthentication.getBytes() ); 
  169.             if( zkClient.exists( PATH ) ){ 
  170.                 zkClient.writeData( PATH, prefix ); 
  171.                 System.out.println( prefix + "更新成功" ); 
  172.             } 
  173.         } catch ( Exception e ) { 
  174.             System.err.println( prefix + "更新失败,原因是:" + e.getMessage() ); 
  175.         } 
  176.     } 
  177.  
  178.     /** 
  179.      * 更新数据:采用正确的密码 
  180.      */ 
  181.     static void updateDataByCorrectAuthentication() { 
  182.          
  183.         String prefix = "[使用正确的授权信息]"
  184.          
  185.         System.out.println( prefix + "更新数据:" + PATH ); 
  186.         try { 
  187.             zkClient = new ZkClient( SERVER_LIST, 50000); 
  188.             zkClient.addAuthInfo( authentication_type, correctAuthentication.getBytes() ); 
  189.             if( zkClient.exists( PATH ) ){ 
  190.                 zkClient.writeData( PATH, prefix ); 
  191.                 System.out.println( prefix + "更新成功" ); 
  192.             } 
  193.         } catch ( Exception e ) { 
  194.             System.err.println( prefix + "更新失败,原因是:" + e.getMessage() ); 
  195.         } 
  196.     } 
  197.  
  198.      
  199.     /** 
  200.      * 不使用密码 删除节点 
  201.      */ 
  202.     static void deleteNodeByNoAuthentication() throws Exception { 
  203.          
  204.         String prefix = "[不使用任何授权信息]"
  205.          
  206.         try { 
  207.             System.out.println( prefix + "删除节点:" + PATH_DEL ); 
  208.             zkClient = new ZkClient( SERVER_LIST, 50000); 
  209.             if( zkClient.exists( PATH_DEL ) ){ 
  210.                 zkClient.delete( PATH_DEL ); 
  211.                 System.out.println( prefix + "删除成功" ); 
  212.             } 
  213.         } catch ( Exception e ) { 
  214.             System.err.println( prefix + "删除失败,原因是:" + e.getMessage() ); 
  215.         } 
  216.     } 
  217.      
  218.      
  219.      
  220.     /** 
  221.      * 采用错误的密码删除节点 
  222.      */ 
  223.     static void deleteNodeByBadAuthentication() throws Exception { 
  224.          
  225.         String prefix = "[使用错误的授权信息]"
  226.          
  227.         try { 
  228.             System.out.println( prefix + "删除节点:" + PATH_DEL ); 
  229.             zkClient = new ZkClient( SERVER_LIST, 50000); 
  230.             zkClient.addAuthInfo( authentication_type, badAuthentication.getBytes() ); 
  231.             if( zkClient.exists( PATH_DEL ) ){ 
  232.                 zkClient.delete( PATH_DEL ); 
  233.                 System.out.println( prefix + "删除成功" ); 
  234.             } 
  235.         } catch ( Exception e ) { 
  236.             System.err.println( prefix + "删除失败,原因是:" + e.getMessage() ); 
  237.         } 
  238.     } 
  239.  
  240.  
  241.  
  242.     /** 
  243.      * 使用正确的密码删除节点 
  244.      */ 
  245.     static void deleteNodeByCorrectAuthentication() throws Exception { 
  246.          
  247.         String prefix = "[使用正确的授权信息]"
  248.          
  249.         try { 
  250.             System.out.println( prefix + "删除节点:" + PATH_DEL ); 
  251.             zkClient = new ZkClient( SERVER_LIST, 50000); 
  252.             zkClient.addAuthInfo( authentication_type, correctAuthentication.getBytes() ); 
  253.             if( zkClient.exists( PATH_DEL ) ){ 
  254.                 zkClient.delete( PATH_DEL ); 
  255.                 System.out.println( prefix + "删除成功" ); 
  256.             } 
  257.         } catch ( Exception e ) { 
  258.             System.out.println( prefix + "删除失败,原因是:" + e.getMessage() ); 
  259.         } 
  260.     } 
  261.      
  262.      
  263.      
  264.     /** 
  265.      * 使用正确的密码删除节点 
  266.      */ 
  267.     static void deleteParent() throws Exception { 
  268.         try { 
  269.             zkClient = new ZkClient( SERVER_LIST, 50000); 
  270.             zkClient.addAuthInfo( authentication_type, correctAuthentication.getBytes() ); 
  271.             if( zkClient.exists( PATH ) ){ 
  272.                 zkClient.delete( PATH ); 
  273.             } 
  274.         } catch ( Exception e ) { 
  275.             e.printStackTrace(); 
  276.         } 
  277.     } 
  278.  
  279.     @Override 
  280.     public void process( WatchedEvent event ) { 
  281.         // TODO Auto-generated method stub 
  282.          
  283.     } 
  284.      
  285.      
  286.      
  287.      
  288.  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值