50_access.t 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. use Storable qw(dclone);
  21. BEGIN { use_ok('Net::ZooKeeper', qw(:all)) };
  22. my $test_dir;
  23. (undef, $test_dir, undef) = File::Spec->splitpath($0);
  24. require File::Spec->catfile($test_dir, 'util.pl');
  25. my($hosts, $root_path, $node_path) = zk_test_setup(0);
  26. my($username, $password, $digest) = zk_acl_test_setup();
  27. SKIP: {
  28. my $zkh = Net::ZooKeeper->new($hosts);
  29. my $path = $zkh->create($node_path, 'foo',
  30. 'acl' => ZOO_OPEN_ACL_UNSAFE) if (defined($zkh));
  31. skip 'no connection to ZooKeeper', 36 unless
  32. (defined($path) and $path eq $node_path);
  33. ## _zk_acl_constant()
  34. my $no_read_acl = ZOO_OPEN_ACL_UNSAFE;
  35. ok((ref($no_read_acl) eq 'ARRAY' and
  36. @{$no_read_acl} == 1 and
  37. ref($no_read_acl->[0]) eq 'HASH' and
  38. keys(%{$no_read_acl->[0]}) == 3 and
  39. $no_read_acl->[0]->{'perms'} == ZOO_PERM_ALL),
  40. '_zk_acl_constant(): returned default ACL');
  41. my $zoo_read_acl_unsafe = ZOO_READ_ACL_UNSAFE;
  42. ok((ref($zoo_read_acl_unsafe) eq 'ARRAY' and
  43. @{$zoo_read_acl_unsafe} == 1 and
  44. ref($zoo_read_acl_unsafe->[0]) eq 'HASH' and
  45. keys(%{$zoo_read_acl_unsafe->[0]}) == 3 and
  46. $zoo_read_acl_unsafe->[0]->{'perms'} == ZOO_PERM_READ),
  47. '_zk_acl_constant(): returned good ACL');
  48. my $zoo_creator_all_acl = ZOO_CREATOR_ALL_ACL;
  49. ok((ref($zoo_creator_all_acl) eq 'ARRAY' and
  50. @{$zoo_creator_all_acl} == 1 and
  51. ref($zoo_creator_all_acl->[0]) eq 'HASH' and
  52. keys(%{$zoo_creator_all_acl->[0]}) == 3 and
  53. $zoo_creator_all_acl->[0]->{'perms'} == ZOO_PERM_ALL),
  54. '_zk_acl_constant(): returned good ACL');
  55. $no_read_acl->[0]->{'perms'} &= ~ZOO_PERM_READ;
  56. is($no_read_acl->[0]->{'perms'}, ((ZOO_PERM_ALL) & ~ZOO_PERM_READ),
  57. 'assign: altered default ACL');
  58. is(ZOO_OPEN_ACL_UNSAFE->[0]->{'perms'}, ZOO_PERM_ALL,
  59. '_zk_acl_constant(): returned unaltered default ACL');
  60. my $copy_no_read_acl = $no_read_acl;
  61. is_deeply($copy_no_read_acl, $no_read_acl,
  62. 'assign: copied default ACL');
  63. undef $no_read_acl;
  64. ok(!defined($no_read_acl),
  65. 'undef: released original default ACL');
  66. is($copy_no_read_acl->[0]->{'perms'}, ((ZOO_PERM_ALL) & ~ZOO_PERM_READ),
  67. 'undef: no change to copied default ACL');
  68. $no_read_acl = $copy_no_read_acl;
  69. is_deeply($no_read_acl, $copy_no_read_acl,
  70. 'assign: re-copied default ACL');
  71. ## create()
  72. my $acl_node_path = "$node_path/a1";
  73. $path = $zkh->create($acl_node_path, 'foo', 'acl' => $no_read_acl);
  74. is($path, $acl_node_path,
  75. 'create(): created node with no-read ACL');
  76. my $node = $zkh->get($acl_node_path);
  77. my $skip_acl;
  78. if (defined($node) and $node eq 'foo') {
  79. $skip_acl = 1;
  80. }
  81. elsif(!defined($node) and $zkh->get_error() == ZNOAUTH) {
  82. $skip_acl = 0;
  83. }
  84. else {
  85. $skip_acl = -1;
  86. diag(sprintf('unable to get node with no-read ACL %s: %d, %s',
  87. $acl_node_path, $zkh->get_error(), $!));
  88. }
  89. my $ret = $zkh->delete($acl_node_path);
  90. diag(sprintf('unable to delete node with no-read ACL %s: %d, %s',
  91. $acl_node_path, $zkh->get_error(), $!)) unless ($ret);
  92. my $digest_acl = [
  93. {
  94. 'perms' => ZOO_PERM_READ,
  95. 'scheme' => 'world',
  96. 'id' => 'anyone'
  97. },
  98. {
  99. 'perms' => (ZOO_PERM_WRITE | ZOO_PERM_ADMIN),
  100. 'scheme' => 'digest',
  101. 'id' => "$username:$digest"
  102. }
  103. ];
  104. $path = $zkh->create($acl_node_path, 'foo', 'acl' => $digest_acl);
  105. is($path, $acl_node_path,
  106. 'create(): created node with digest auth ACL');
  107. SKIP: {
  108. skip 'ZooKeeper skipping ACLs', 1 unless (!$skip_acl);
  109. my $acl_node_path = "$node_path/a2";
  110. my $path = $zkh->create($acl_node_path, 'foo', 'acl' => [
  111. {
  112. 'perms' => ZOO_PERM_WRITE,
  113. 'scheme' => 'foo',
  114. 'id' => 'bar'
  115. }
  116. ]);
  117. ok((!defined($path) and $zkh->get_error() == ZINVALIDACL and $! eq ''),
  118. 'create(): undef when attempting to create node with invalid ACL');
  119. }
  120. ## get_acl()
  121. my @acl = ('abc');
  122. @acl = $zkh->get_acl($node_path . '/NONE');
  123. ok((@acl == 0 and $zkh->get_error() == ZNONODE and $! eq ''),
  124. 'get_acl(): empty list returned for non-extant node');
  125. $num_acl_entries = $zkh->get_acl($node_path . '/NONE');
  126. ok((!defined($num_acl_entries) and $zkh->get_error() == ZNONODE and
  127. $! eq ''),
  128. 'get_acl(): undef returned for non-extant node');
  129. # The test is not running as ADMIN, which means that the server
  130. # returns "redacted" ACLs (see ZOOKEEPER-1392 and OpCode.getACL in
  131. # FinalRequestProcessor). We must do the same for the comparison
  132. # to succeed.
  133. my $redacted_digest_acl = dclone($digest_acl);
  134. $redacted_digest_acl->[1]->{id} =~ s/:.*/:x/;
  135. @acl = ('abc');
  136. @acl = $zkh->get_acl($acl_node_path);
  137. is_deeply(\@acl, $redacted_digest_acl,
  138. 'get_acl(): retrieved digest ACL');
  139. my $stat = $zkh->stat();
  140. @acl = ('abc');
  141. @acl = $zkh->get_acl($node_path, 'stat' => $stat);
  142. is_deeply(\@acl, ZOO_OPEN_ACL_UNSAFE,
  143. 'get_acl(): retrieved ACL');
  144. is($stat->{'data_len'}, 3,
  145. 'get_acl(): retrieved ACL with stat handle');
  146. SKIP: {
  147. skip 'ZooKeeper not skipping ACLs', 3 unless ($skip_acl > 0);
  148. my $acl_node_path = "$node_path/a2";
  149. my $path = $zkh->create($acl_node_path, 'foo', 'acl' => []);
  150. is($path, $acl_node_path,
  151. 'create(): created node with empty ACL');
  152. my @acl = ('abc');
  153. @acl = $zkh->get_acl($acl_node_path);
  154. ok((@acl == 0 and $zkh->get_error() == ZOK),
  155. 'get_acl(): retrieved empty ACL');
  156. my $num_acl_entries = $zkh->get_acl($acl_node_path);
  157. ok((defined($num_acl_entries) and $num_acl_entries == 0),
  158. 'get_acl(): retrieved zero count of ACL entries');
  159. my $ret = $zkh->delete($acl_node_path);
  160. diag(sprintf('unable to delete node with empty ACL %s: %d, %s',
  161. $acl_node_path, $zkh->get_error(), $!)) unless ($ret);
  162. }
  163. ## set_acl()
  164. SKIP: {
  165. skip 'ZooKeeper skipping ACLs', 2 unless (!$skip_acl);
  166. my $ret = $zkh->set_acl($acl_node_path, [
  167. {
  168. 'perms' => ZOO_PERM_CREATE,
  169. 'scheme' => 'foo',
  170. 'id' => 'bar'
  171. }
  172. ]);
  173. ok((!$ret and $zkh->get_error() == ZINVALIDACL and $! eq ''),
  174. 'set_acl(): invalid ACL');
  175. push @{$digest_acl}, {
  176. 'perms' => (ZOO_PERM_CREATE | ZOO_PERM_DELETE),
  177. 'scheme' => 'ip',
  178. 'id' => '0.0.0.0'
  179. };
  180. $ret = $zkh->set_acl($acl_node_path, $digest_acl);
  181. ok((!$ret and $zkh->get_error() == ZNOAUTH and $! eq ''),
  182. 'set_acl(): ACL unchanged if no auth');
  183. }
  184. ## add_auth(), set_acl()
  185. $ret = $zkh->add_auth('digest', '');
  186. ok($ret,
  187. 'add_auth(): empty digest cert');
  188. SKIP: {
  189. skip 'ZooKeeper skipping ACLs', 1 unless (!$skip_acl);
  190. my $ret = $zkh->set($acl_node_path, 'foo');
  191. ok((!$ret and $zkh->get_error() == ZNOAUTH and $! eq ''),
  192. 'set(): node value unchanged if no auth');
  193. }
  194. $ret = $zkh->add_auth('digest', "$username:$password");
  195. ok($ret,
  196. 'add_auth(): valid digest cert');
  197. SKIP: {
  198. skip 'ZooKeeper skipping ACLs', 13 unless (!$skip_acl);
  199. my $ret = $zkh->set($acl_node_path, 'baz');
  200. ok($ret,
  201. 'set(): set node value with auth');
  202. my $node = $zkh->get($acl_node_path);
  203. is($node, 'baz',
  204. 'get(): retrieved node value with auth');
  205. $ret = $zkh->set_acl($acl_node_path, $digest_acl);
  206. ok($ret,
  207. 'set_acl(): set digest ACL with auth');
  208. my $stat = $zkh->stat();
  209. my @acl = ('abc');
  210. @acl = $zkh->get_acl($acl_node_path, 'stat' => $stat);
  211. is_deeply(\@acl, $digest_acl,
  212. 'get_acl(): retrieved digest ACL with auth');
  213. is($stat->{'data_len'}, 3,
  214. 'get_acl(): retrieved digest ACL with stat handle and auth');
  215. SKIP: {
  216. skip 'invalid node data', 2 unless ($stat->{'version'} == 1);
  217. my $ret = $zkh->set_acl($acl_node_path, $digest_acl,
  218. 'version' => $stat->{'version'});
  219. ok($ret,
  220. 'set_acl(): set digest ACL with matching version with auth');
  221. $ret = $zkh->set_acl($acl_node_path, $digest_acl,
  222. 'version' => $stat->{'version'});
  223. ok((!$ret and $zkh->get_error() == ZBADVERSION and $! eq ''),
  224. 'set_acl(): ACL unchanged if non-matching version');
  225. }
  226. my $child_node_path = "$acl_node_path/c1";
  227. my $path = $zkh->create($child_node_path, 'foo',
  228. 'acl' => ZOO_OPEN_ACL_UNSAFE);
  229. ok((!defined($path) and $zkh->get_error() == ZNOAUTH and $! eq ''),
  230. 'create(): undef when attempting to create node if no auth');
  231. $digest_acl->[1]->{'perms'} |= ZOO_PERM_CREATE;
  232. $digest_acl->[2]->{'perms'} &= ~ZOO_PERM_CREATE;
  233. $ret = $zkh->set_acl($acl_node_path, $digest_acl);
  234. ok($ret,
  235. 'set_acl(): set changed digest ACL with auth');
  236. $path = $zkh->create($child_node_path, 'foo',
  237. 'acl' => ZOO_OPEN_ACL_UNSAFE);
  238. is($path, $child_node_path,
  239. 'create(): created node with auth');
  240. $ret = $zkh->delete($child_node_path);
  241. ok((!$ret and $zkh->get_error() == ZNOAUTH and $! eq ''),
  242. 'delete(): no deletion of node if no auth');
  243. $digest_acl->[1]->{'perms'} |= ZOO_PERM_DELETE;
  244. pop @{$digest_acl};
  245. $ret = $zkh->set_acl($acl_node_path, $digest_acl);
  246. ok($ret,
  247. 'set_acl(): set reduced digest ACL with auth');
  248. $ret = $zkh->delete($child_node_path);
  249. ok($ret,
  250. 'delete(): deleted node with auth');
  251. }
  252. ## cleanup
  253. $ret = $zkh->delete($acl_node_path);
  254. diag(sprintf('unable to delete node with digest auth ACL %s: %d, %s',
  255. $acl_node_path, $zkh->get_error(), $!)) unless ($ret);
  256. $ret = $zkh->delete($node_path);
  257. diag(sprintf('unable to delete node %s: %d, %s',
  258. $node_path, $zkh->get_error(), $!)) unless ($ret);
  259. }
  260. SKIP: {
  261. my $zkh = Net::ZooKeeper->new($hosts);
  262. my $ret = $zkh->exists($root_path) if (defined($zkh));
  263. skip 'no connection to ZooKeeper', 1 unless
  264. (defined($ret) and $ret);
  265. ## add_auth()
  266. $ret = $zkh->add_auth('foo', 'bar');
  267. my $err = $zkh->get_error();
  268. ok((!$ret and
  269. ($err == ZAUTHFAILED or
  270. $err == ZCONNECTIONLOSS or
  271. $err == ZSESSIONEXPIRED)
  272. and $! eq ''),
  273. 'set_acl(): invalid scheme');
  274. }