50_access.t 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. # Net::ZooKeeper - Perl extension for Apache ZooKeeper
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. The ASF licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. use File::Spec;
  19. use Test::More tests => 40;
  20. BEGIN { use_ok('Net::ZooKeeper', qw(:all)) };
  21. my $test_dir;
  22. (undef, $test_dir, undef) = File::Spec->splitpath($0);
  23. require File::Spec->catfile($test_dir, 'util.pl');
  24. my($hosts, $root_path, $node_path) = zk_test_setup(0);
  25. my($username, $password, $digest) = zk_acl_test_setup();
  26. SKIP: {
  27. my $zkh = Net::ZooKeeper->new($hosts);
  28. my $path = $zkh->create($node_path, 'foo',
  29. 'acl' => ZOO_OPEN_ACL_UNSAFE) if (defined($zkh));
  30. skip 'no connection to ZooKeeper', 36 unless
  31. (defined($path) and $path eq $node_path);
  32. ## _zk_acl_constant()
  33. my $no_read_acl = ZOO_OPEN_ACL_UNSAFE;
  34. ok((ref($no_read_acl) eq 'ARRAY' and
  35. @{$no_read_acl} == 1 and
  36. ref($no_read_acl->[0]) eq 'HASH' and
  37. keys(%{$no_read_acl->[0]}) == 3 and
  38. $no_read_acl->[0]->{'perms'} == ZOO_PERM_ALL),
  39. '_zk_acl_constant(): returned default ACL');
  40. my $zoo_read_acl_unsafe = ZOO_READ_ACL_UNSAFE;
  41. ok((ref($zoo_read_acl_unsafe) eq 'ARRAY' and
  42. @{$zoo_read_acl_unsafe} == 1 and
  43. ref($zoo_read_acl_unsafe->[0]) eq 'HASH' and
  44. keys(%{$zoo_read_acl_unsafe->[0]}) == 3 and
  45. $zoo_read_acl_unsafe->[0]->{'perms'} == ZOO_PERM_READ),
  46. '_zk_acl_constant(): returned good ACL');
  47. my $zoo_creator_all_acl = ZOO_CREATOR_ALL_ACL;
  48. ok((ref($zoo_creator_all_acl) eq 'ARRAY' and
  49. @{$zoo_creator_all_acl} == 1 and
  50. ref($zoo_creator_all_acl->[0]) eq 'HASH' and
  51. keys(%{$zoo_creator_all_acl->[0]}) == 3 and
  52. $zoo_creator_all_acl->[0]->{'perms'} == ZOO_PERM_ALL),
  53. '_zk_acl_constant(): returned good ACL');
  54. $no_read_acl->[0]->{'perms'} &= ~ZOO_PERM_READ;
  55. is($no_read_acl->[0]->{'perms'}, ((ZOO_PERM_ALL) & ~ZOO_PERM_READ),
  56. 'assign: altered default ACL');
  57. is(ZOO_OPEN_ACL_UNSAFE->[0]->{'perms'}, ZOO_PERM_ALL,
  58. '_zk_acl_constant(): returned unaltered default ACL');
  59. my $copy_no_read_acl = $no_read_acl;
  60. is_deeply($copy_no_read_acl, $no_read_acl,
  61. 'assign: copied default ACL');
  62. undef $no_read_acl;
  63. ok(!defined($no_read_acl),
  64. 'undef: released original default ACL');
  65. is($copy_no_read_acl->[0]->{'perms'}, ((ZOO_PERM_ALL) & ~ZOO_PERM_READ),
  66. 'undef: no change to copied default ACL');
  67. $no_read_acl = $copy_no_read_acl;
  68. is_deeply($no_read_acl, $copy_no_read_acl,
  69. 'assign: re-copied default ACL');
  70. ## create()
  71. my $acl_node_path = "$node_path/a1";
  72. $path = $zkh->create($acl_node_path, 'foo', 'acl' => $no_read_acl);
  73. is($path, $acl_node_path,
  74. 'create(): created node with no-read ACL');
  75. my $node = $zkh->get($acl_node_path);
  76. my $skip_acl;
  77. if (defined($node) and $node eq 'foo') {
  78. $skip_acl = 1;
  79. }
  80. elsif(!defined($node) and $zkh->get_error() == ZNOAUTH) {
  81. $skip_acl = 0;
  82. }
  83. else {
  84. $skip_acl = -1;
  85. diag(sprintf('unable to get node with no-read ACL %s: %d, %s',
  86. $acl_node_path, $zkh->get_error(), $!));
  87. }
  88. my $ret = $zkh->delete($acl_node_path);
  89. diag(sprintf('unable to delete node with no-read ACL %s: %d, %s',
  90. $acl_node_path, $zkh->get_error(), $!)) unless ($ret);
  91. my $digest_acl = [
  92. {
  93. 'perms' => ZOO_PERM_READ,
  94. 'scheme' => 'world',
  95. 'id' => 'anyone'
  96. },
  97. {
  98. 'perms' => (ZOO_PERM_WRITE | ZOO_PERM_ADMIN),
  99. 'scheme' => 'digest',
  100. 'id' => "$username:$digest"
  101. }
  102. ];
  103. $path = $zkh->create($acl_node_path, 'foo', 'acl' => $digest_acl);
  104. is($path, $acl_node_path,
  105. 'create(): created node with digest auth ACL');
  106. SKIP: {
  107. skip 'ZooKeeper skipping ACLs', 1 unless (!$skip_acl);
  108. my $acl_node_path = "$node_path/a2";
  109. my $path = $zkh->create($acl_node_path, 'foo', 'acl' => [
  110. {
  111. 'perms' => ZOO_PERM_WRITE,
  112. 'scheme' => 'foo',
  113. 'id' => 'bar'
  114. }
  115. ]);
  116. ok((!defined($path) and $zkh->get_error() == ZINVALIDACL and $! eq ''),
  117. 'create(): undef when attempting to create node with invalid ACL');
  118. }
  119. ## get_acl()
  120. my @acl = ('abc');
  121. @acl = $zkh->get_acl($node_path . '/NONE');
  122. ok((@acl == 0 and $zkh->get_error() == ZNONODE and $! eq ''),
  123. 'get_acl(): empty list returned for non-extant node');
  124. $num_acl_entries = $zkh->get_acl($node_path . '/NONE');
  125. ok((!defined($num_acl_entries) and $zkh->get_error() == ZNONODE and
  126. $! eq ''),
  127. 'get_acl(): undef returned for non-extant node');
  128. @acl = ('abc');
  129. @acl = $zkh->get_acl($acl_node_path);
  130. is_deeply(\@acl, $digest_acl,
  131. 'get_acl(): retrieved digest ACL');
  132. my $stat = $zkh->stat();
  133. @acl = ('abc');
  134. @acl = $zkh->get_acl($node_path, 'stat' => $stat);
  135. is_deeply(\@acl, ZOO_OPEN_ACL_UNSAFE,
  136. 'get_acl(): retrieved ACL');
  137. is($stat->{'data_len'}, 3,
  138. 'get_acl(): retrieved ACL with stat handle');
  139. SKIP: {
  140. skip 'ZooKeeper not skipping ACLs', 3 unless ($skip_acl > 0);
  141. my $acl_node_path = "$node_path/a2";
  142. my $path = $zkh->create($acl_node_path, 'foo', 'acl' => []);
  143. is($path, $acl_node_path,
  144. 'create(): created node with empty ACL');
  145. my @acl = ('abc');
  146. @acl = $zkh->get_acl($acl_node_path);
  147. ok((@acl == 0 and $zkh->get_error() == ZOK),
  148. 'get_acl(): retrieved empty ACL');
  149. my $num_acl_entries = $zkh->get_acl($acl_node_path);
  150. ok((defined($num_acl_entries) and $num_acl_entries == 0),
  151. 'get_acl(): retrieved zero count of ACL entries');
  152. my $ret = $zkh->delete($acl_node_path);
  153. diag(sprintf('unable to delete node with empty ACL %s: %d, %s',
  154. $acl_node_path, $zkh->get_error(), $!)) unless ($ret);
  155. }
  156. ## set_acl()
  157. SKIP: {
  158. skip 'ZooKeeper skipping ACLs', 2 unless (!$skip_acl);
  159. my $ret = $zkh->set_acl($acl_node_path, [
  160. {
  161. 'perms' => ZOO_PERM_CREATE,
  162. 'scheme' => 'foo',
  163. 'id' => 'bar'
  164. }
  165. ]);
  166. ok((!$ret and $zkh->get_error() == ZINVALIDACL and $! eq ''),
  167. 'set_acl(): invalid ACL');
  168. push @{$digest_acl}, {
  169. 'perms' => (ZOO_PERM_CREATE | ZOO_PERM_DELETE),
  170. 'scheme' => 'ip',
  171. 'id' => '0.0.0.0'
  172. };
  173. $ret = $zkh->set_acl($acl_node_path, $digest_acl);
  174. ok((!$ret and $zkh->get_error() == ZNOAUTH and $! eq ''),
  175. 'set_acl(): ACL unchanged if no auth');
  176. }
  177. ## add_auth(), set_acl()
  178. $ret = $zkh->add_auth('digest', '');
  179. ok($ret,
  180. 'add_auth(): empty digest cert');
  181. SKIP: {
  182. skip 'ZooKeeper skipping ACLs', 1 unless (!$skip_acl);
  183. my $ret = $zkh->set($acl_node_path, 'foo');
  184. ok((!$ret and $zkh->get_error() == ZNOAUTH and $! eq ''),
  185. 'set(): node value unchanged if no auth');
  186. }
  187. $ret = $zkh->add_auth('digest', "$username:$password");
  188. ok($ret,
  189. 'add_auth(): valid digest cert');
  190. SKIP: {
  191. skip 'ZooKeeper skipping ACLs', 13 unless (!$skip_acl);
  192. my $ret = $zkh->set($acl_node_path, 'baz');
  193. ok($ret,
  194. 'set(): set node value with auth');
  195. my $node = $zkh->get($acl_node_path);
  196. is($node, 'baz',
  197. 'get(): retrieved node value with auth');
  198. $ret = $zkh->set_acl($acl_node_path, $digest_acl);
  199. ok($ret,
  200. 'set_acl(): set digest ACL with auth');
  201. my $stat = $zkh->stat();
  202. my @acl = ('abc');
  203. @acl = $zkh->get_acl($acl_node_path, 'stat' => $stat);
  204. is_deeply(\@acl, $digest_acl,
  205. 'get_acl(): retrieved digest ACL with auth');
  206. is($stat->{'data_len'}, 3,
  207. 'get_acl(): retrieved digest ACL with stat handle and auth');
  208. SKIP: {
  209. skip 'invalid node data', 2 unless ($stat->{'version'} == 1);
  210. my $ret = $zkh->set_acl($acl_node_path, $digest_acl,
  211. 'version' => $stat->{'version'});
  212. ok($ret,
  213. 'set_acl(): set digest ACL with matching version with auth');
  214. $ret = $zkh->set_acl($acl_node_path, $digest_acl,
  215. 'version' => $stat->{'version'});
  216. ok((!$ret and $zkh->get_error() == ZBADVERSION and $! eq ''),
  217. 'set_acl(): ACL unchanged if non-matching version');
  218. }
  219. my $child_node_path = "$acl_node_path/c1";
  220. my $path = $zkh->create($child_node_path, 'foo',
  221. 'acl' => ZOO_OPEN_ACL_UNSAFE);
  222. ok((!defined($path) and $zkh->get_error() == ZNOAUTH and $! eq ''),
  223. 'create(): undef when attempting to create node if no auth');
  224. $digest_acl->[1]->{'perms'} |= ZOO_PERM_CREATE;
  225. $digest_acl->[2]->{'perms'} &= ~ZOO_PERM_CREATE;
  226. $ret = $zkh->set_acl($acl_node_path, $digest_acl);
  227. ok($ret,
  228. 'set_acl(): set changed digest ACL with auth');
  229. $path = $zkh->create($child_node_path, 'foo',
  230. 'acl' => ZOO_OPEN_ACL_UNSAFE);
  231. is($path, $child_node_path,
  232. 'create(): created node with auth');
  233. $ret = $zkh->delete($child_node_path);
  234. ok((!$ret and $zkh->get_error() == ZNOAUTH and $! eq ''),
  235. 'delete(): no deletion of node if no auth');
  236. $digest_acl->[1]->{'perms'} |= ZOO_PERM_DELETE;
  237. pop @{$digest_acl};
  238. $ret = $zkh->set_acl($acl_node_path, $digest_acl);
  239. ok($ret,
  240. 'set_acl(): set reduced digest ACL with auth');
  241. $ret = $zkh->delete($child_node_path);
  242. ok($ret,
  243. 'delete(): deleted node with auth');
  244. }
  245. ## cleanup
  246. $ret = $zkh->delete($acl_node_path);
  247. diag(sprintf('unable to delete node with digest auth ACL %s: %d, %s',
  248. $acl_node_path, $zkh->get_error(), $!)) unless ($ret);
  249. $ret = $zkh->delete($node_path);
  250. diag(sprintf('unable to delete node %s: %d, %s',
  251. $node_path, $zkh->get_error(), $!)) unless ($ret);
  252. }
  253. SKIP: {
  254. my $zkh = Net::ZooKeeper->new($hosts);
  255. my $ret = $zkh->exists($root_path) if (defined($zkh));
  256. skip 'no connection to ZooKeeper', 1 unless
  257. (defined($ret) and $ret);
  258. ## add_auth()
  259. $ret = $zkh->add_auth('foo', 'bar');
  260. my $err = $zkh->get_error();
  261. ok((!$ret and
  262. ($err == ZAUTHFAILED or
  263. $err == ZCONNECTIONLOSS or
  264. $err == ZSESSIONEXPIRED)
  265. and $! eq ''),
  266. 'set_acl(): invalid scheme');
  267. }