TestSASLAuth.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. // We need to disable the deprecation warnings as Apple has
  113. // decided to deprecate all of CyrusSASL's functions with OS 10.11
  114. // (see MESOS-3030, ZOOKEEPER-4201). We are using GCC pragmas also
  115. // for covering clang.
  116. #ifdef __APPLE__
  117. #pragma GCC diagnostic push
  118. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  119. #endif
  120. void testClientSASLHelper(const char *hostPorts, const char *path) {
  121. startServer();
  122. // Initialize Cyrus SASL.
  123. CPPUNIT_ASSERT_EQUAL(sasl_client_init(NULL), SASL_OK);
  124. // Initialize SASL parameters.
  125. zoo_sasl_params_t sasl_params = { 0 };
  126. sasl_params.service = "zookeeper";
  127. sasl_params.host = "zk-sasl-md5";
  128. sasl_params.mechlist = "DIGEST-MD5";
  129. sasl_params.callbacks = zoo_sasl_make_basic_callbacks(
  130. "myuser", NULL, "Zookeeper_SASLAuth.password");
  131. // Connect.
  132. watchctx_t ctx;
  133. int rc = 0;
  134. zhandle_t *zk = zookeeper_init_sasl(hostPorts, watcher, 10000, NULL,
  135. &ctx, /*flags*/0, /*log_callback*/NULL, &sasl_params);
  136. ctx.zh = zk;
  137. CPPUNIT_ASSERT(zk);
  138. // Wait for SASL auth to complete and handle to be connected.
  139. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  140. // Leave mark.
  141. char pathbuf[80];
  142. struct Stat stat_a = {0};
  143. rc = zoo_create2(zk, path, "", 0,
  144. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_a);
  145. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  146. // Stop and restart the server to test automatic reconnect & re-auth.
  147. stopServer();
  148. CPPUNIT_ASSERT(ctx.waitForDisconnected(zk));
  149. startServer();
  150. // Wait for automatic SASL re-auth to complete.
  151. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  152. // Check mark left above.
  153. rc = zoo_exists(zk, path, /*watch*/false, &stat_a);
  154. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  155. stopServer();
  156. }
  157. void testClientSASL() {
  158. testClientSASLHelper(hostPorts, "/clientSASL");
  159. }
  160. void testClientSASLOverIPv6() {
  161. const char *ipAndPort = "::1:22181";
  162. testClientSASLHelper(ipAndPort, "/clientSASLOverIPv6");
  163. }
  164. void testClientSASLReadOnly() {
  165. startServer(/*useJaasConf*/ true, /*readOnly*/ true);
  166. // Initialize Cyrus SASL.
  167. CPPUNIT_ASSERT_EQUAL(sasl_client_init(NULL), SASL_OK);
  168. // Initialize SASL parameters.
  169. zoo_sasl_params_t sasl_params = { 0 };
  170. sasl_params.service = "zookeeper";
  171. sasl_params.host = "zk-sasl-md5";
  172. sasl_params.mechlist = "DIGEST-MD5";
  173. sasl_params.callbacks = zoo_sasl_make_basic_callbacks(
  174. "myuser", NULL, "Zookeeper_SASLAuth.password");
  175. // Connect.
  176. watchctx_t ctx;
  177. int rc = 0;
  178. zhandle_t *zk = zookeeper_init_sasl(hostPorts, watcher, 10000, NULL,
  179. &ctx, /*flags*/ZOO_READONLY, /*log_callback*/NULL, &sasl_params);
  180. ctx.zh = zk;
  181. CPPUNIT_ASSERT(zk);
  182. // Wait for SASL auth to complete and handle to be connected.
  183. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  184. // Assert can read.
  185. char buf[1024];
  186. int len = sizeof(buf);
  187. rc = zoo_get(zk, "/", 0, buf, &len, 0);
  188. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  189. // Assert can not write.
  190. char path[1024];
  191. rc = zoo_create(zk, "/test", "hello", 5, &ZOO_OPEN_ACL_UNSAFE, 0, path, sizeof(path));
  192. CPPUNIT_ASSERT_EQUAL((int)ZNOTREADONLY, rc);
  193. stopServer();
  194. }
  195. void testClientSASLPacketOrder() {
  196. startServer();
  197. // Initialize Cyrus SASL.
  198. CPPUNIT_ASSERT_EQUAL(sasl_client_init(NULL), SASL_OK);
  199. // Initialize SASL parameters.
  200. zoo_sasl_params_t sasl_params = { 0 };
  201. sasl_params.service = "zookeeper";
  202. sasl_params.host = "zk-sasl-md5";
  203. sasl_params.mechlist = "DIGEST-MD5";
  204. sasl_params.callbacks = zoo_sasl_make_basic_callbacks(
  205. "myuser", NULL, "Zookeeper_SASLAuth.password");
  206. // Connect.
  207. watchctx_t ctx;
  208. int rc = 0;
  209. zhandle_t *zk = zookeeper_init_sasl(hostPorts, watcher, 10000, NULL,
  210. &ctx, /*flags*/0, /*log_callback*/NULL, &sasl_params);
  211. ctx.zh = zk;
  212. CPPUNIT_ASSERT(zk);
  213. // No wait: try and queue a packet before SASL auth is complete.
  214. char buf[1024];
  215. int len = sizeof(buf);
  216. rc = zoo_get(zk, "/", 0, buf, &len, 0);
  217. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  218. stopServer();
  219. }
  220. #ifdef __APPLE__
  221. #pragma GCC diagnostic pop
  222. #endif
  223. #endif /* HAVE_CYRUS_SASL_H */
  224. };
  225. const char Zookeeper_SASLAuth::hostPorts[] = "127.0.0.1:22181";
  226. const char Zookeeper_SASLAuth::jaasConf[] =
  227. "Server {\n"
  228. " org.apache.zookeeper.server.auth.DigestLoginModule required\n"
  229. " user_myuser=\"mypassword\";\n"
  230. "};\n";
  231. CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_SASLAuth);
  232. #endif /* THREADED */