TestSASLAuth.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifdef THREADED
  19. #include <cppunit/extensions/HelperMacros.h>
  20. #include "CppAssertHelper.h"
  21. #include <sys/socket.h>
  22. #include <unistd.h>
  23. #include <zookeeper.h>
  24. #include "Util.h"
  25. #include "WatchUtil.h"
  26. class Zookeeper_SASLAuth : public CPPUNIT_NS::TestFixture {
  27. CPPUNIT_TEST_SUITE(Zookeeper_SASLAuth);
  28. CPPUNIT_TEST(testServerRequireClientSASL);
  29. #ifdef HAVE_CYRUS_SASL_H
  30. CPPUNIT_TEST(testClientSASL);
  31. #ifdef ZOO_IPV6_ENABLED
  32. CPPUNIT_TEST(testClientSASLOverIPv6);
  33. #endif/* ZOO_IPV6_ENABLED */
  34. CPPUNIT_TEST(testClientSASLReadOnly);
  35. CPPUNIT_TEST(testClientSASLPacketOrder);
  36. #endif /* HAVE_CYRUS_SASL_H */
  37. CPPUNIT_TEST_SUITE_END();
  38. FILE *logfile;
  39. static const char hostPorts[];
  40. static const char jaasConf[];
  41. static void watcher(zhandle_t *, int type, int state, const char *path,void*v){
  42. watchctx_t *ctx = (watchctx_t*)v;
  43. if (state == ZOO_CONNECTED_STATE || state == ZOO_READONLY_STATE) {
  44. ctx->connected = true;
  45. } else {
  46. ctx->connected = false;
  47. }
  48. if (type != ZOO_SESSION_EVENT) {
  49. evt_t evt;
  50. evt.path = path;
  51. evt.type = type;
  52. ctx->putEvent(evt);
  53. }
  54. }
  55. public:
  56. Zookeeper_SASLAuth() {
  57. logfile = openlogfile("Zookeeper_SASLAuth");
  58. }
  59. ~Zookeeper_SASLAuth() {
  60. if (logfile) {
  61. fflush(logfile);
  62. fclose(logfile);
  63. logfile = 0;
  64. }
  65. }
  66. void setUp() {
  67. zoo_set_log_stream(logfile);
  68. // Create SASL configuration file for server.
  69. FILE *conff = fopen("Zookeeper_SASLAuth.jaas.conf", "wt");
  70. CPPUNIT_ASSERT(conff);
  71. size_t confLen = strlen(jaasConf);
  72. CPPUNIT_ASSERT_EQUAL(fwrite(jaasConf, 1, confLen, conff), confLen);
  73. CPPUNIT_ASSERT_EQUAL(fclose(conff), 0);
  74. conff = NULL;
  75. // Create password file for client.
  76. FILE *passf = fopen("Zookeeper_SASLAuth.password", "wt");
  77. CPPUNIT_ASSERT(passf);
  78. CPPUNIT_ASSERT(fputs("mypassword", passf) > 0);
  79. CPPUNIT_ASSERT_EQUAL(fclose(passf), 0);
  80. passf = NULL;
  81. }
  82. void startServer(bool useJaasConf = true, bool readOnly = false) {
  83. char cmd[1024];
  84. sprintf(cmd, "%s startRequireSASLAuth %s %s",
  85. ZKSERVER_CMD,
  86. useJaasConf ? "Zookeeper_SASLAuth.jaas.conf" : "",
  87. readOnly ? "true" : "");
  88. CPPUNIT_ASSERT(system(cmd) == 0);
  89. }
  90. void stopServer() {
  91. char cmd[1024];
  92. sprintf(cmd, "%s stop", ZKSERVER_CMD);
  93. CPPUNIT_ASSERT(system(cmd) == 0);
  94. }
  95. void testServerRequireClientSASL() {
  96. startServer(false);
  97. watchctx_t ctx;
  98. int rc = 0;
  99. zhandle_t *zk = zookeeper_init(hostPorts, watcher, 10000, 0, &ctx, 0);
  100. ctx.zh = zk;
  101. CPPUNIT_ASSERT(zk);
  102. // Wait for handle to be connected.
  103. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  104. char pathbuf[80];
  105. struct Stat stat_a = {0};
  106. rc = zoo_create2(zk, "/serverRequireClientSASL", "", 0,
  107. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_a);
  108. CPPUNIT_ASSERT_EQUAL((int)ZSESSIONCLOSEDREQUIRESASLAUTH, rc);
  109. stopServer();
  110. }
  111. #ifdef HAVE_CYRUS_SASL_H
  112. void testClientSASLHelper(const char *hostPorts, const char *path) {
  113. startServer();
  114. // Initialize Cyrus SASL.
  115. CPPUNIT_ASSERT_EQUAL(sasl_client_init(NULL), SASL_OK);
  116. // Initialize SASL parameters.
  117. zoo_sasl_params_t sasl_params = { 0 };
  118. sasl_params.service = "zookeeper";
  119. sasl_params.host = "zk-sasl-md5";
  120. sasl_params.mechlist = "DIGEST-MD5";
  121. sasl_params.callbacks = zoo_sasl_make_basic_callbacks(
  122. "myuser", NULL, "Zookeeper_SASLAuth.password");
  123. // Connect.
  124. watchctx_t ctx;
  125. int rc = 0;
  126. zhandle_t *zk = zookeeper_init_sasl(hostPorts, watcher, 10000, NULL,
  127. &ctx, /*flags*/0, /*log_callback*/NULL, &sasl_params);
  128. ctx.zh = zk;
  129. CPPUNIT_ASSERT(zk);
  130. // Wait for SASL auth to complete and handle to be connected.
  131. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  132. // Leave mark.
  133. char pathbuf[80];
  134. struct Stat stat_a = {0};
  135. rc = zoo_create2(zk, path, "", 0,
  136. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_a);
  137. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  138. // Stop and restart the server to test automatic reconnect & re-auth.
  139. stopServer();
  140. CPPUNIT_ASSERT(ctx.waitForDisconnected(zk));
  141. startServer();
  142. // Wait for automatic SASL re-auth to complete.
  143. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  144. // Check mark left above.
  145. rc = zoo_exists(zk, path, /*watch*/false, &stat_a);
  146. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  147. stopServer();
  148. }
  149. void testClientSASL() {
  150. testClientSASLHelper(hostPorts, "/clientSASL");
  151. }
  152. void testClientSASLOverIPv6() {
  153. const char *ipAndPort = "::1:22181";
  154. testClientSASLHelper(ipAndPort, "/clientSASLOverIPv6");
  155. }
  156. void testClientSASLReadOnly() {
  157. startServer(/*useJaasConf*/ true, /*readOnly*/ true);
  158. // Initialize Cyrus SASL.
  159. CPPUNIT_ASSERT_EQUAL(sasl_client_init(NULL), SASL_OK);
  160. // Initialize SASL parameters.
  161. zoo_sasl_params_t sasl_params = { 0 };
  162. sasl_params.service = "zookeeper";
  163. sasl_params.host = "zk-sasl-md5";
  164. sasl_params.mechlist = "DIGEST-MD5";
  165. sasl_params.callbacks = zoo_sasl_make_basic_callbacks(
  166. "myuser", NULL, "Zookeeper_SASLAuth.password");
  167. // Connect.
  168. watchctx_t ctx;
  169. int rc = 0;
  170. zhandle_t *zk = zookeeper_init_sasl(hostPorts, watcher, 10000, NULL,
  171. &ctx, /*flags*/ZOO_READONLY, /*log_callback*/NULL, &sasl_params);
  172. ctx.zh = zk;
  173. CPPUNIT_ASSERT(zk);
  174. // Wait for SASL auth to complete and handle to be connected.
  175. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  176. // Assert can read.
  177. char buf[1024];
  178. int len = sizeof(buf);
  179. rc = zoo_get(zk, "/", 0, buf, &len, 0);
  180. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  181. // Assert can not write.
  182. char path[1024];
  183. rc = zoo_create(zk, "/test", "hello", 5, &ZOO_OPEN_ACL_UNSAFE, 0, path, sizeof(path));
  184. CPPUNIT_ASSERT_EQUAL((int)ZNOTREADONLY, rc);
  185. stopServer();
  186. }
  187. void testClientSASLPacketOrder() {
  188. startServer();
  189. // Initialize Cyrus SASL.
  190. CPPUNIT_ASSERT_EQUAL(sasl_client_init(NULL), SASL_OK);
  191. // Initialize SASL parameters.
  192. zoo_sasl_params_t sasl_params = { 0 };
  193. sasl_params.service = "zookeeper";
  194. sasl_params.host = "zk-sasl-md5";
  195. sasl_params.mechlist = "DIGEST-MD5";
  196. sasl_params.callbacks = zoo_sasl_make_basic_callbacks(
  197. "myuser", NULL, "Zookeeper_SASLAuth.password");
  198. // Connect.
  199. watchctx_t ctx;
  200. int rc = 0;
  201. zhandle_t *zk = zookeeper_init_sasl(hostPorts, watcher, 10000, NULL,
  202. &ctx, /*flags*/0, /*log_callback*/NULL, &sasl_params);
  203. ctx.zh = zk;
  204. CPPUNIT_ASSERT(zk);
  205. // No wait: try and queue a packet before SASL auth is complete.
  206. char buf[1024];
  207. int len = sizeof(buf);
  208. rc = zoo_get(zk, "/", 0, buf, &len, 0);
  209. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  210. stopServer();
  211. }
  212. #endif /* HAVE_CYRUS_SASL_H */
  213. };
  214. const char Zookeeper_SASLAuth::hostPorts[] = "127.0.0.1:22181";
  215. const char Zookeeper_SASLAuth::jaasConf[] =
  216. "Server {\n"
  217. " org.apache.zookeeper.server.auth.DigestLoginModule required\n"
  218. " user_myuser=\"mypassword\";\n"
  219. "};\n";
  220. CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_SASLAuth);
  221. #endif /* THREADED */