TestSASLAuth.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #endif /* HAVE_CYRUS_SASL_H */
  36. CPPUNIT_TEST_SUITE_END();
  37. FILE *logfile;
  38. static const char hostPorts[];
  39. static const char jaasConf[];
  40. static void watcher(zhandle_t *, int type, int state, const char *path,void*v){
  41. watchctx_t *ctx = (watchctx_t*)v;
  42. if (state == ZOO_CONNECTED_STATE || state == ZOO_READONLY_STATE) {
  43. ctx->connected = true;
  44. } else {
  45. ctx->connected = false;
  46. }
  47. if (type != ZOO_SESSION_EVENT) {
  48. evt_t evt;
  49. evt.path = path;
  50. evt.type = type;
  51. ctx->putEvent(evt);
  52. }
  53. }
  54. public:
  55. Zookeeper_SASLAuth() {
  56. logfile = openlogfile("Zookeeper_SASLAuth");
  57. }
  58. ~Zookeeper_SASLAuth() {
  59. if (logfile) {
  60. fflush(logfile);
  61. fclose(logfile);
  62. logfile = 0;
  63. }
  64. }
  65. void setUp() {
  66. zoo_set_log_stream(logfile);
  67. // Create SASL configuration file for server.
  68. FILE *conff = fopen("Zookeeper_SASLAuth.jaas.conf", "wt");
  69. CPPUNIT_ASSERT(conff);
  70. size_t confLen = strlen(jaasConf);
  71. CPPUNIT_ASSERT_EQUAL(fwrite(jaasConf, 1, confLen, conff), confLen);
  72. CPPUNIT_ASSERT_EQUAL(fclose(conff), 0);
  73. conff = NULL;
  74. // Create password file for client.
  75. FILE *passf = fopen("Zookeeper_SASLAuth.password", "wt");
  76. CPPUNIT_ASSERT(passf);
  77. CPPUNIT_ASSERT(fputs("mypassword", passf) > 0);
  78. CPPUNIT_ASSERT_EQUAL(fclose(passf), 0);
  79. passf = NULL;
  80. }
  81. void startServer(bool useJaasConf = true, bool readOnly = false) {
  82. char cmd[1024];
  83. sprintf(cmd, "%s startRequireSASLAuth %s %s",
  84. ZKSERVER_CMD,
  85. useJaasConf ? "Zookeeper_SASLAuth.jaas.conf" : "",
  86. readOnly ? "true" : "");
  87. CPPUNIT_ASSERT(system(cmd) == 0);
  88. }
  89. void stopServer() {
  90. char cmd[1024];
  91. sprintf(cmd, "%s stop", ZKSERVER_CMD);
  92. CPPUNIT_ASSERT(system(cmd) == 0);
  93. }
  94. void testServerRequireClientSASL() {
  95. startServer(false);
  96. watchctx_t ctx;
  97. int rc = 0;
  98. zhandle_t *zk = zookeeper_init(hostPorts, watcher, 10000, 0, &ctx, 0);
  99. ctx.zh = zk;
  100. CPPUNIT_ASSERT(zk);
  101. // Wait for handle to be connected.
  102. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  103. char pathbuf[80];
  104. struct Stat stat_a = {0};
  105. rc = zoo_create2(zk, "/serverRequireClientSASL", "", 0,
  106. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_a);
  107. CPPUNIT_ASSERT_EQUAL((int)ZSESSIONCLOSEDREQUIRESASLAUTH, rc);
  108. stopServer();
  109. }
  110. #ifdef HAVE_CYRUS_SASL_H
  111. void testClientSASLHelper(const char *hostPorts, const char *path) {
  112. startServer();
  113. // Initialize Cyrus SASL.
  114. CPPUNIT_ASSERT_EQUAL(sasl_client_init(NULL), SASL_OK);
  115. // Initialize SASL parameters.
  116. zoo_sasl_params_t sasl_params = { 0 };
  117. sasl_params.service = "zookeeper";
  118. sasl_params.host = "zk-sasl-md5";
  119. sasl_params.mechlist = "DIGEST-MD5";
  120. sasl_params.callbacks = zoo_sasl_make_basic_callbacks(
  121. "myuser", NULL, "Zookeeper_SASLAuth.password");
  122. // Connect.
  123. watchctx_t ctx;
  124. int rc = 0;
  125. zhandle_t *zk = zookeeper_init_sasl(hostPorts, watcher, 10000, NULL,
  126. &ctx, /*flags*/0, /*log_callback*/NULL, &sasl_params);
  127. ctx.zh = zk;
  128. CPPUNIT_ASSERT(zk);
  129. // Wait for SASL auth to complete and handle to be connected.
  130. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  131. // Leave mark.
  132. char pathbuf[80];
  133. struct Stat stat_a = {0};
  134. rc = zoo_create2(zk, path, "", 0,
  135. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_a);
  136. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  137. // Stop and restart the server to test automatic reconnect & re-auth.
  138. stopServer();
  139. CPPUNIT_ASSERT(ctx.waitForDisconnected(zk));
  140. startServer();
  141. // Wait for automatic SASL re-auth to complete.
  142. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  143. // Check mark left above.
  144. rc = zoo_exists(zk, path, /*watch*/false, &stat_a);
  145. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  146. stopServer();
  147. }
  148. void testClientSASL() {
  149. testClientSASLHelper(hostPorts, "/clientSASL");
  150. }
  151. void testClientSASLOverIPv6() {
  152. const char *ipAndPort = "::1:22181";
  153. testClientSASLHelper(ipAndPort, "/clientSASLOverIPv6");
  154. }
  155. void testClientSASLReadOnly() {
  156. startServer(/*useJaasConf*/ true, /*readOnly*/ true);
  157. // Initialize Cyrus SASL.
  158. CPPUNIT_ASSERT_EQUAL(sasl_client_init(NULL), SASL_OK);
  159. // Initialize SASL parameters.
  160. zoo_sasl_params_t sasl_params = { 0 };
  161. sasl_params.service = "zookeeper";
  162. sasl_params.host = "zk-sasl-md5";
  163. sasl_params.mechlist = "DIGEST-MD5";
  164. sasl_params.callbacks = zoo_sasl_make_basic_callbacks(
  165. "myuser", NULL, "Zookeeper_SASLAuth.password");
  166. // Connect.
  167. watchctx_t ctx;
  168. int rc = 0;
  169. zhandle_t *zk = zookeeper_init_sasl(hostPorts, watcher, 10000, NULL,
  170. &ctx, /*flags*/ZOO_READONLY, /*log_callback*/NULL, &sasl_params);
  171. ctx.zh = zk;
  172. CPPUNIT_ASSERT(zk);
  173. // Wait for SASL auth to complete and handle to be connected.
  174. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  175. // Assert can read.
  176. char buf[1024];
  177. int len = sizeof(buf);
  178. rc = zoo_get(zk, "/", 0, buf, &len, 0);
  179. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  180. // Assert can not write.
  181. char path[1024];
  182. rc = zoo_create(zk, "/test", "hello", 5, &ZOO_OPEN_ACL_UNSAFE, 0, path, sizeof(path));
  183. CPPUNIT_ASSERT_EQUAL((int)ZNOTREADONLY, rc);
  184. stopServer();
  185. }
  186. #endif /* HAVE_CYRUS_SASL_H */
  187. };
  188. const char Zookeeper_SASLAuth::hostPorts[] = "127.0.0.1:22181";
  189. const char Zookeeper_SASLAuth::jaasConf[] =
  190. "Server {\n"
  191. " org.apache.zookeeper.server.auth.DigestLoginModule required\n"
  192. " user_myuser=\"mypassword\";\n"
  193. "};\n";
  194. CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_SASLAuth);
  195. #endif /* THREADED */