TestSASLAuth.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. CPPUNIT_TEST(testClientSASLWithPasswordFileNewline);
  32. CPPUNIT_TEST(testClientSASLWithPasswordFileFirstLine);
  33. CPPUNIT_TEST(testClientSASLWithPasswordEncryptedText);
  34. CPPUNIT_TEST(testClientSASLWithPasswordEncryptedBinary);
  35. #ifdef ZOO_IPV6_ENABLED
  36. CPPUNIT_TEST(testClientSASLOverIPv6);
  37. #endif/* ZOO_IPV6_ENABLED */
  38. CPPUNIT_TEST(testClientSASLReadOnly);
  39. CPPUNIT_TEST(testClientSASLPacketOrder);
  40. #endif /* HAVE_CYRUS_SASL_H */
  41. CPPUNIT_TEST_SUITE_END();
  42. FILE *logfile;
  43. static const char hostPorts[];
  44. static const char jaasConf[];
  45. static void watcher(zhandle_t *, int type, int state, const char *path,void*v){
  46. watchctx_t *ctx = (watchctx_t*)v;
  47. if (state == ZOO_CONNECTED_STATE || state == ZOO_READONLY_STATE) {
  48. ctx->connected = true;
  49. } else {
  50. ctx->connected = false;
  51. }
  52. if (type != ZOO_SESSION_EVENT) {
  53. evt_t evt;
  54. evt.path = path;
  55. evt.type = type;
  56. ctx->putEvent(evt);
  57. }
  58. }
  59. public:
  60. Zookeeper_SASLAuth() {
  61. logfile = openlogfile("Zookeeper_SASLAuth");
  62. }
  63. ~Zookeeper_SASLAuth() {
  64. if (logfile) {
  65. fflush(logfile);
  66. fclose(logfile);
  67. logfile = 0;
  68. }
  69. }
  70. void setUp() {
  71. zoo_set_log_stream(logfile);
  72. // Create SASL configuration file for server.
  73. FILE *conff = fopen("Zookeeper_SASLAuth.jaas.conf", "wt");
  74. CPPUNIT_ASSERT(conff);
  75. size_t confLen = strlen(jaasConf);
  76. CPPUNIT_ASSERT_EQUAL(fwrite(jaasConf, 1, confLen, conff), confLen);
  77. CPPUNIT_ASSERT_EQUAL(fclose(conff), 0);
  78. conff = NULL;
  79. // Create password file for client.
  80. FILE *passf = fopen("Zookeeper_SASLAuth.password", "wt");
  81. CPPUNIT_ASSERT(passf);
  82. CPPUNIT_ASSERT(fputs("mypassword", passf) > 0);
  83. CPPUNIT_ASSERT_EQUAL(fclose(passf), 0);
  84. passf = NULL;
  85. }
  86. void startServer(bool useJaasConf = true, bool readOnly = false) {
  87. char cmd[1024];
  88. sprintf(cmd, "%s startRequireSASLAuth %s %s",
  89. ZKSERVER_CMD,
  90. useJaasConf ? "Zookeeper_SASLAuth.jaas.conf" : "",
  91. readOnly ? "true" : "");
  92. CPPUNIT_ASSERT(system(cmd) == 0);
  93. }
  94. void stopServer() {
  95. char cmd[1024];
  96. sprintf(cmd, "%s stop", ZKSERVER_CMD);
  97. CPPUNIT_ASSERT(system(cmd) == 0);
  98. }
  99. void testServerRequireClientSASL() {
  100. startServer(false);
  101. watchctx_t ctx;
  102. int rc = 0;
  103. zhandle_t *zk = zookeeper_init(hostPorts, watcher, 10000, 0, &ctx, 0);
  104. ctx.zh = zk;
  105. CPPUNIT_ASSERT(zk);
  106. // Wait for handle to be connected.
  107. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  108. char pathbuf[80];
  109. struct Stat stat_a = {0};
  110. rc = zoo_create2(zk, "/serverRequireClientSASL", "", 0,
  111. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_a);
  112. CPPUNIT_ASSERT_EQUAL((int)ZSESSIONCLOSEDREQUIRESASLAUTH, rc);
  113. stopServer();
  114. }
  115. #ifdef HAVE_CYRUS_SASL_H
  116. // We need to disable the deprecation warnings as Apple has
  117. // decided to deprecate all of CyrusSASL's functions with OS 10.11
  118. // (see MESOS-3030, ZOOKEEPER-4201). We are using GCC pragmas also
  119. // for covering clang.
  120. #ifdef __APPLE__
  121. #pragma GCC diagnostic push
  122. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  123. #endif
  124. void testClientSASLHelper(const char *hostPorts, const char *path,
  125. const sasl_callback_t *callbacks) {
  126. startServer();
  127. // Initialize Cyrus SASL.
  128. CPPUNIT_ASSERT_EQUAL(sasl_client_init(NULL), SASL_OK);
  129. // Initialize SASL parameters.
  130. zoo_sasl_params_t sasl_params = { 0 };
  131. sasl_params.service = "zookeeper";
  132. sasl_params.host = "zk-sasl-md5";
  133. sasl_params.mechlist = "DIGEST-MD5";
  134. sasl_params.callbacks = callbacks;
  135. // Connect.
  136. watchctx_t ctx;
  137. int rc = 0;
  138. zhandle_t *zk = zookeeper_init_sasl(hostPorts, watcher, 10000, NULL,
  139. &ctx, /*flags*/0, /*log_callback*/NULL, &sasl_params);
  140. ctx.zh = zk;
  141. CPPUNIT_ASSERT(zk);
  142. // Wait for SASL auth to complete and handle to be connected.
  143. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  144. // Leave mark.
  145. char pathbuf[80];
  146. struct Stat stat_a = {0};
  147. rc = zoo_create2(zk, path, "", 0,
  148. &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_a);
  149. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  150. // Stop and restart the server to test automatic reconnect & re-auth.
  151. stopServer();
  152. CPPUNIT_ASSERT(ctx.waitForDisconnected(zk));
  153. startServer();
  154. // Wait for automatic SASL re-auth to complete.
  155. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  156. // Check mark left above.
  157. rc = zoo_exists(zk, path, /*watch*/false, &stat_a);
  158. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  159. stopServer();
  160. }
  161. void testClientSASLHelper(const char *hostPorts, const char *path,
  162. const char *password_file) {
  163. const sasl_callback_t *callbacks = zoo_sasl_make_basic_callbacks(
  164. "myuser", NULL, password_file);
  165. testClientSASLHelper(hostPorts, path, callbacks);
  166. }
  167. void testClientSASLHelper(const char *hostPorts, const char *path) {
  168. testClientSASLHelper(hostPorts, path, "Zookeeper_SASLAuth.password");
  169. }
  170. void testClientSASLHelper(const char *hostPorts, const char *path,
  171. zoo_sasl_password_t *password) {
  172. const sasl_callback_t *callbacks = zoo_sasl_make_password_callbacks(
  173. "myuser", NULL, password);
  174. testClientSASLHelper(hostPorts, path, callbacks);
  175. }
  176. void testClientSASL() {
  177. testClientSASLHelper(hostPorts, "/clientSASL");
  178. }
  179. void testClientSASL(const char *password_file, const char *content, size_t content_len,
  180. const char *path, zoo_sasl_password_callback_t callback) {
  181. // Create password file for client.
  182. FILE *passf = fopen(password_file, "wt");
  183. CPPUNIT_ASSERT(passf);
  184. // Write the specified content into the file.
  185. size_t len = fwrite(content, sizeof(content[0]), content_len, passf);
  186. CPPUNIT_ASSERT_EQUAL(len, content_len);
  187. CPPUNIT_ASSERT_EQUAL(fclose(passf), 0);
  188. passf = NULL;
  189. zoo_sasl_password_t passwd = {password_file, this, callback};
  190. testClientSASLHelper(hostPorts, path, &passwd);
  191. }
  192. void testClientSASLWithPasswordFileNewline() {
  193. // Insert a newline immediately after the correct password.
  194. const char content[] = "mypassword\nabc";
  195. testClientSASL("Zookeeper_SASLAuth.password.newline",
  196. content,
  197. sizeof(content) - 1,
  198. "/clientSASLWithPasswordFileNewline",
  199. NULL);
  200. }
  201. void testClientSASLWithPasswordFileFirstLine() {
  202. // Insert multiple newlines and check if only the first line is accepted as the
  203. // actual password.
  204. const char content[] = "mypassword\nabc\nxyz";
  205. testClientSASL("Zookeeper_SASLAuth.password.firstline",
  206. content,
  207. sizeof(content) - 1,
  208. "/clientSASLWithPasswordFileFirstLine",
  209. NULL);
  210. }
  211. int decryptPassword(const char *content, size_t content_len,
  212. char incr, char *buf, size_t buf_len,
  213. size_t *passwd_len) {
  214. CPPUNIT_ASSERT(content_len <= buf_len);
  215. // A simple decryption that only increases each character by a fixed value.
  216. for (size_t i = 0; i < content_len; ++i) {
  217. buf[i] = content[i] + incr;
  218. }
  219. *passwd_len = content_len;
  220. // Since null terminator has not been appended to buf, use memcmp.
  221. CPPUNIT_ASSERT_EQUAL(memcmp(buf, "mypassword", *passwd_len), 0);
  222. return SASL_OK;
  223. }
  224. static int textPasswordCallback(const char *content, size_t content_len,
  225. void *context, char *buf, size_t buf_len,
  226. size_t *passwd_len) {
  227. Zookeeper_SASLAuth *auth = static_cast<Zookeeper_SASLAuth *>(context);
  228. return auth->decryptPassword(content, content_len, 1, buf, buf_len, passwd_len);
  229. }
  230. void testClientSASLWithPasswordEncryptedText() {
  231. // Encrypt "mypassword" by subtracting 1 from each character in it as plain text.
  232. const char content[] = {0x6C, 0x78, 0x6F, 0x60, 0x72, 0x72, 0x76, 0x6E, 0x71, 0x63};
  233. testClientSASL("Zookeeper_SASLAuth.password.encrypted.text",
  234. content,
  235. sizeof(content),
  236. "/clientSASLWithPasswordEncryptedText",
  237. textPasswordCallback);
  238. }
  239. static int binaryPasswordCallback(const char *content, size_t content_len,
  240. void *context, char *buf, size_t buf_len,
  241. size_t *passwd_len) {
  242. Zookeeper_SASLAuth *auth = static_cast<Zookeeper_SASLAuth *>(context);
  243. return auth->decryptPassword(content, content_len, 'a', buf, buf_len, passwd_len);
  244. }
  245. void testClientSASLWithPasswordEncryptedBinary() {
  246. // Encrypt "mypassword" by subtracting 'a' from each character in it as binary format.
  247. const char content[] = {0x0C, 0x18, 0x0F, 0x00, 0x12, 0x12, 0x16, 0x0E, 0x11, 0x03};
  248. testClientSASL("Zookeeper_SASLAuth.password.encrypted.binary",
  249. content,
  250. sizeof(content),
  251. "/clientSASLWithPasswordEncryptedBinary",
  252. binaryPasswordCallback);
  253. }
  254. void testClientSASLOverIPv6() {
  255. const char *ipAndPort = "::1:22181";
  256. testClientSASLHelper(ipAndPort, "/clientSASLOverIPv6");
  257. }
  258. void testClientSASLReadOnly() {
  259. startServer(/*useJaasConf*/ true, /*readOnly*/ true);
  260. // Initialize Cyrus SASL.
  261. CPPUNIT_ASSERT_EQUAL(sasl_client_init(NULL), SASL_OK);
  262. // Initialize SASL parameters.
  263. zoo_sasl_params_t sasl_params = { 0 };
  264. sasl_params.service = "zookeeper";
  265. sasl_params.host = "zk-sasl-md5";
  266. sasl_params.mechlist = "DIGEST-MD5";
  267. sasl_params.callbacks = zoo_sasl_make_basic_callbacks(
  268. "myuser", NULL, "Zookeeper_SASLAuth.password");
  269. // Connect.
  270. watchctx_t ctx;
  271. int rc = 0;
  272. zhandle_t *zk = zookeeper_init_sasl(hostPorts, watcher, 10000, NULL,
  273. &ctx, /*flags*/ZOO_READONLY, /*log_callback*/NULL, &sasl_params);
  274. ctx.zh = zk;
  275. CPPUNIT_ASSERT(zk);
  276. // Wait for SASL auth to complete and handle to be connected.
  277. CPPUNIT_ASSERT(ctx.waitForConnected(zk));
  278. // Assert can read.
  279. char buf[1024];
  280. int len = sizeof(buf);
  281. rc = zoo_get(zk, "/", 0, buf, &len, 0);
  282. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  283. // Assert can not write.
  284. char path[1024];
  285. rc = zoo_create(zk, "/test", "hello", 5, &ZOO_OPEN_ACL_UNSAFE, 0, path, sizeof(path));
  286. CPPUNIT_ASSERT_EQUAL((int)ZNOTREADONLY, rc);
  287. stopServer();
  288. }
  289. void testClientSASLPacketOrder() {
  290. startServer();
  291. // Initialize Cyrus SASL.
  292. CPPUNIT_ASSERT_EQUAL(sasl_client_init(NULL), SASL_OK);
  293. // Initialize SASL parameters.
  294. zoo_sasl_params_t sasl_params = { 0 };
  295. sasl_params.service = "zookeeper";
  296. sasl_params.host = "zk-sasl-md5";
  297. sasl_params.mechlist = "DIGEST-MD5";
  298. sasl_params.callbacks = zoo_sasl_make_basic_callbacks(
  299. "myuser", NULL, "Zookeeper_SASLAuth.password");
  300. // Connect.
  301. watchctx_t ctx;
  302. int rc = 0;
  303. zhandle_t *zk = zookeeper_init_sasl(hostPorts, watcher, 10000, NULL,
  304. &ctx, /*flags*/0, /*log_callback*/NULL, &sasl_params);
  305. ctx.zh = zk;
  306. CPPUNIT_ASSERT(zk);
  307. // No wait: try and queue a packet before SASL auth is complete.
  308. char buf[1024];
  309. int len = sizeof(buf);
  310. rc = zoo_get(zk, "/", 0, buf, &len, 0);
  311. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  312. stopServer();
  313. }
  314. #ifdef __APPLE__
  315. #pragma GCC diagnostic pop
  316. #endif
  317. #endif /* HAVE_CYRUS_SASL_H */
  318. };
  319. const char Zookeeper_SASLAuth::hostPorts[] = "127.0.0.1:22181";
  320. const char Zookeeper_SASLAuth::jaasConf[] =
  321. "Server {\n"
  322. " org.apache.zookeeper.server.auth.DigestLoginModule required\n"
  323. " user_myuser=\"mypassword\";\n"
  324. "};\n";
  325. CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_SASLAuth);
  326. #endif /* THREADED */