From 683c6b4c7bc1cb3a85b9279bee801ac23f94a1cc Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Fri, 6 Nov 2026 13:40:18 -0800 Subject: [PATCH 1/2] ZOOKEEPER-5124 (C client) Chroot path validation fails to isolate namespaces On a chrooted handle, the C client added the chroot to the client path before it validated the path. The check then incorrectly considered the full path, so a path that did not start with "/" would be silently accepted. Also, sub_string removed the chroot from server paths by length only rather than at path component boundaries. prepend_chroot now returns NULL for a NULL, empty, or relative client path on a chrooted handle. The isValidPath check of the caller then returns ZBADARGUMENTS, as on a plain handle. sub_string removes the chroot path only on a path component boundary and returns other paths unchanged. Sync config reads now use zoo_awgetconfig with a completion to standardize handling on one path. New tests in Zookeeper_simpleSystem: testChrootPathValidation, testChrootGetConfig, and testChrootServerPathMapping. Co-Authored-By: Claude Opus 5.5 --- .../zookeeper-client-c/src/zookeeper.c | 105 ++++++++--- .../zookeeper-client-c/tests/TestClient.cc | 166 ++++++++++++++++++ 2 files changed, 247 insertions(+), 24 deletions(-) diff --git a/zookeeper-client/zookeeper-client-c/src/zookeeper.c b/zookeeper-client/zookeeper-client-c/src/zookeeper.c index c8429003b29..86d1cb60ff7 100644 --- a/zookeeper-client/zookeeper-client-c/src/zookeeper.c +++ b/zookeeper-client/zookeeper-client-c/src/zookeeper.c @@ -1706,42 +1706,73 @@ void free_duplicate_path(const char *free_path, const char* path) { } /** - prepend the chroot path if available else return the path -*/ -static char* prepend_string(zhandle_t *zh, const char* client_path) { + * Map a client path to a server path under the chroot. + * + * A valid client path is absolute. The chroot prefix does not make an + * invalid path valid. A sequential create of the client root stays in + * the chroot namespace. A null result is an invalid path or a failed + * allocation. + */ +static char* prepend_chroot(zhandle_t *zh, const char* client_path, int mode) { + size_t chroot_len; + size_t path_len; char *ret_str; if (zh == NULL || zh->chroot == NULL) return (char *) client_path; - // handle the chroot itself, client_path = "/" - if (strlen(client_path) == 1) { + if (client_path == NULL || client_path[0] != '/') + return NULL; + /* + * The client root is the chroot node. A sequential create of the root + * keeps a slash at the end of the server path. The server adds the + * sequence number as a child of the chroot, not as a sibling. + */ + if (client_path[1] == '\0' && !ZOOKEEPER_IS_SEQUENCE(mode)) { return strdup(zh->chroot); } - ret_str = (char *) malloc(strlen(zh->chroot) + strlen(client_path) + 1); - strcpy(ret_str, zh->chroot); - return strcat(ret_str, client_path); + chroot_len = strlen(zh->chroot); + path_len = strlen(client_path); + ret_str = (char *) malloc(chroot_len + path_len + 1); + if (ret_str == NULL) + return NULL; + memcpy(ret_str, zh->chroot, chroot_len); + memcpy(ret_str + chroot_len, client_path, path_len + 1); + return ret_str; +} + +/** + * Map a client path to a server path when the request does not create a + * sequential node. + */ +static char* prepend_string(zhandle_t *zh, const char* client_path) { + return prepend_chroot(zh, client_path, 0); } /** - strip off the chroot string from the server path - if there is one else return the exact path + * Map a server path to a client path. + * + * The chroot prefix matches only at a path component boundary, f.e. a chroot + * of /app does not match the path /app2. Cluster configuration is not in + * the chroot namespace, so it is not affected by the chroot prefix. */ char* sub_string(zhandle_t *zh, const char* server_path) { - char *ret_str; - if (zh->chroot == NULL) + size_t chroot_len; + if (zh->chroot == NULL || server_path == NULL) return (char *) server_path; //ZOOKEEPER-1027 - if (strncmp(server_path, zh->chroot, strlen(zh->chroot)) != 0) { + chroot_len = strlen(zh->chroot); + if (strncmp(server_path, zh->chroot, chroot_len) == 0) { + if (server_path[chroot_len] == '\0') { + return strdup("/"); + } + if (server_path[chroot_len] == '/') { + return strdup(server_path + chroot_len); + } + } + if (strcmp(server_path, ZOO_CONFIG_NODE) != 0) { LOG_ERROR(LOGCALLBACK(zh), "server path %s does not include chroot path %s", server_path, zh->chroot); - return (char *) server_path; - } - if (strlen(server_path) == strlen(zh->chroot)) { - //return "/" - ret_str = strdup("/"); - return ret_str; } - ret_str = strdup(server_path + strlen(zh->chroot)); - return ret_str; + return (char *) server_path; } static buffer_list_t *allocate_buffer(char *buff, int len) @@ -4115,7 +4146,7 @@ static int Request_path_init(zhandle_t *zh, int mode, { assert(path_out); - *path_out = prepend_string(zh, path); + *path_out = prepend_chroot(zh, path, mode); /* * Restore *path_out to caller's original path pointer on validation * failure so caller-side free_duplicate_path() remains safe. @@ -5678,13 +5709,39 @@ int zoo_wget(zhandle_t *zh, const char *path, int zoo_getconfig(zhandle_t *zh, int watch, char *buffer, int* buffer_len, struct Stat *stat) { - return zoo_wget(zh,ZOO_CONFIG_NODE,watch?zh->watcher:0,zh->context, buffer,buffer_len,stat); + return zoo_wgetconfig(zh,watch?zh->watcher:0,zh->context, buffer,buffer_len,stat); } +/* + * Cluster configuration is not in the chroot namespace. This function + * uses the asynchronous configuration call so that the synchronous and + * asynchronous APIs will both read the same configuration node. + */ int zoo_wgetconfig(zhandle_t *zh, watcher_fn watcher, void* watcherCtx, char *buffer, int* buffer_len, struct Stat *stat) { - return zoo_wget(zh, ZOO_CONFIG_NODE, watcher, watcherCtx, buffer, buffer_len, stat); + struct sync_completion *sc; + int rc=0; + + if(buffer_len==NULL) + return ZBADARGUMENTS; + if((sc=alloc_sync_completion())==NULL) + return ZSYSTEMERROR; + + sc->u.data.buffer = buffer; + sc->u.data.buff_len = *buffer_len; + rc=zoo_awgetconfig(zh, watcher, watcherCtx, SYNCHRONOUS_MARKER, sc); + if(rc==ZOK){ + wait_sync_completion(sc); + rc = sc->rc; + if (rc == 0) { + if(stat) + *stat = sc->u.data.stat; + *buffer_len = sc->u.data.buff_len; + } + } + free_sync_completion(sc); + return rc; } diff --git a/zookeeper-client/zookeeper-client-c/tests/TestClient.cc b/zookeeper-client/zookeeper-client-c/tests/TestClient.cc index 36739e11f04..5e6c9f70280 100644 --- a/zookeeper-client/zookeeper-client-c/tests/TestClient.cc +++ b/zookeeper-client/zookeeper-client-c/tests/TestClient.cc @@ -51,6 +51,9 @@ struct buff_struct_2 { // it needs a overhaul to work properly with both threaded and single // threaded (ZOOKEEPER-2640) #ifdef THREADED +// The client uses this internal function to map a server path to a client path. +extern "C" char* sub_string(zhandle_t *zh, const char* server_path); + // For testing LogMessage Callback functionality list logMessages; void logMessageHandler(const char* message) { @@ -225,6 +228,9 @@ class Zookeeper_simpleSystem : public CPPUNIT_NS::TestFixture CPPUNIT_TEST(testPing); CPPUNIT_TEST(testAcl); CPPUNIT_TEST(testChroot); + CPPUNIT_TEST(testChrootPathValidation); + CPPUNIT_TEST(testChrootGetConfig); + CPPUNIT_TEST(testChrootServerPathMapping); CPPUNIT_TEST(testAuth); CPPUNIT_TEST(testCloseInAuthCompletion); CPPUNIT_TEST(testHangingClient); @@ -1205,6 +1211,166 @@ class Zookeeper_simpleSystem : public CPPUNIT_NS::TestFixture CPPUNIT_ASSERT(count == 0); } + // A handle with a chroot and a handle with no chroot reject the same + // invalid client paths. Invalid paths do not reach nodes outside the + // chroot. + void testChrootPathValidation() { + watchctx_t ctx, ctx_ch; + struct Stat stat; + struct String_vector children; + char buf[64]; + int len; + zhandle_t *zk = createClient(&ctx); + CPPUNIT_ASSERT(zk); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_create(zk, "/tenant", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_create(zk, "/tenant2", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_create(zk, "/tenantx", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0)); + zhandle_t *zk_ch = createchClient(&ctx_ch, "127.0.0.1:22181/tenant"); + CPPUNIT_ASSERT(zk_ch); + + const char *bad[] = { NULL, "", "x", "2", "2/x", "x/y" }; + for (size_t i = 0; i < sizeof(bad) / sizeof(bad[0]); i++) { + const char *path = bad[i]; + zhandle_t *handles[] = { zk, zk_ch }; + int multi_rc[2]; + for (size_t j = 0; j < 2; j++) { + zhandle_t *zh = handles[j]; + verifyCreateFails(path, zh); + verifyCreateFailsSeq(path, zh); + CPPUNIT_ASSERT_EQUAL((int)ZBADARGUMENTS, + zoo_exists(zh, path, 0, &stat)); + len = sizeof(buf); + CPPUNIT_ASSERT_EQUAL((int)ZBADARGUMENTS, + zoo_get(zh, path, 0, buf, &len, &stat)); + CPPUNIT_ASSERT_EQUAL((int)ZBADARGUMENTS, + zoo_set(zh, path, "x", 1, -1)); + CPPUNIT_ASSERT_EQUAL((int)ZBADARGUMENTS, + zoo_delete(zh, path, -1)); + CPPUNIT_ASSERT_EQUAL((int)ZBADARGUMENTS, + zoo_get_children(zh, path, 0, &children)); + CPPUNIT_ASSERT_EQUAL((int)ZBADARGUMENTS, + zoo_remove_all_watches(zh, path, ZWATCHTYPE_ANY, 1)); + zoo_op_t op; + zoo_op_result_t result; + zoo_delete_op_init(&op, path, -1); + multi_rc[j] = zoo_multi(zh, 1, &op, &result); + } + // zoo_multi reports a sub-request error as ZMARSHALLINGERROR. + // The two handles give the same error. + CPPUNIT_ASSERT(multi_rc[0] != ZOK); + CPPUNIT_ASSERT_EQUAL(multi_rc[0], multi_rc[1]); + } + + // Invalid paths did not change the chroot node or its sibling nodes. + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_exists(zk, "/tenant", 0, &stat)); + CPPUNIT_ASSERT_EQUAL(0, stat.version); + CPPUNIT_ASSERT_EQUAL(0, stat.numChildren); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_exists(zk, "/tenant2", 0, &stat)); + CPPUNIT_ASSERT_EQUAL(0, stat.numChildren); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_exists(zk, "/tenantx", 0, &stat)); + CPPUNIT_ASSERT_EQUAL(0, stat.version); + + // A sequential create of the client root makes a child of the chroot + // node. A sequential create of "/" on a handle with no chroot has + // the same result at the cluster root. + char path_buffer[64]; + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_create(zk_ch, "/", "", 0, + &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE, path_buffer, + sizeof(path_buffer))); + CPPUNIT_ASSERT_EQUAL((size_t)11, strlen(path_buffer)); + CPPUNIT_ASSERT_EQUAL('/', path_buffer[0]); + string server_path = string("/tenant") + path_buffer; + CPPUNIT_ASSERT_EQUAL((int)ZOK, + zoo_exists(zk, server_path.c_str(), 0, &stat)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_get_children(zk, "/", 0, &children)); + for (int i = 0; i < children.count; i++) { + CPPUNIT_ASSERT(strncmp(children.data[i], "tenant0", 7) != 0); + } + deallocate_String_vector(&children); + + // Other operations on the client root use the chroot node. + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_exists(zk_ch, "/", 0, &stat)); + CPPUNIT_ASSERT_EQUAL(1, stat.numChildren); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_get_children(zk_ch, "/", 0, &children)); + CPPUNIT_ASSERT_EQUAL(1, (int)children.count); + CPPUNIT_ASSERT_EQUAL(string(path_buffer + 1), string(children.data[0])); + deallocate_String_vector(&children); + } + + // Cluster configuration is not in the chroot namespace. Synchronous and + // asynchronous reads get the same node as a handle with no chroot. + void testChrootGetConfig() { + watchctx_t ctx, ctx_ch; + char buf[1024], buf_ch[1024]; + int len = sizeof(buf); + int len_ch = sizeof(buf_ch); + struct Stat stat, stat_ch; + zhandle_t *zk = createClient(&ctx); + CPPUNIT_ASSERT(zk); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_create(zk, "/cfgchroot", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0)); + zhandle_t *zk_ch = createchClient(&ctx_ch, "127.0.0.1:22181/cfgchroot"); + CPPUNIT_ASSERT(zk_ch); + + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_getconfig(zk, 0, buf, &len, &stat)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, + zoo_getconfig(zk_ch, 0, buf_ch, &len_ch, &stat_ch)); + CPPUNIT_ASSERT_EQUAL(len, len_ch); + CPPUNIT_ASSERT(len <= 0 || memcmp(buf, buf_ch, len) == 0); + CPPUNIT_ASSERT_EQUAL(stat.czxid, stat_ch.czxid); + CPPUNIT_ASSERT_EQUAL(stat.mzxid, stat_ch.mzxid); + + len_ch = sizeof(buf_ch); + memset(&stat_ch, 0, sizeof(stat_ch)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_wgetconfig(zk_ch, watcher, &ctx_ch, + buf_ch, &len_ch, &stat_ch)); + CPPUNIT_ASSERT_EQUAL(len, len_ch); + CPPUNIT_ASSERT_EQUAL(stat.mzxid, stat_ch.mzxid); + } + + static bool hasChrootPathError() { + for (list::iterator it = logMessages.begin(); + it != logMessages.end(); ++it) { + if (it->find("does not include chroot path") != string::npos) { + return true; + } + } + return false; + } + + // The chroot prefix of a server path ends at a path-component boundary. + // Cluster configuration that is not in the chroot does not cause an + // error. + void testChrootServerPathMapping() { + watchctx_t ctx; + logMessages.clear(); + zhandle_t *zh = zookeeper_init2("127.0.0.1:22181/zoo", watcher, 10000, + 0, &ctx, 0, logMessageHandler); + CPPUNIT_ASSERT(zh); + ctx.zh = zh; + + char *client_path = sub_string(zh, "/zoo"); + CPPUNIT_ASSERT_EQUAL(string("/"), string(client_path)); + free(client_path); + client_path = sub_string(zh, "/zoo/a/b"); + CPPUNIT_ASSERT_EQUAL(string("/a/b"), string(client_path)); + free(client_path); + CPPUNIT_ASSERT(!hasChrootPathError()); + + const char *config_path = ZOO_CONFIG_NODE; + CPPUNIT_ASSERT(sub_string(zh, config_path) == config_path); + CPPUNIT_ASSERT(!hasChrootPathError()); + + const char *sibling_path = "/zoo2/a"; + CPPUNIT_ASSERT(sub_string(zh, sibling_path) == sibling_path); + CPPUNIT_ASSERT(hasChrootPathError()); + + CPPUNIT_ASSERT(sub_string(zh, NULL) == NULL); + } + // Test creating normal handle via zookeeper_init then explicitly setting callback void testLogCallbackSet() { From 96374311f0c0a641f3fab4836155d43c17dc6d0d Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Fri, 20 Nov 2026 14:19:21 -0800 Subject: [PATCH 2/2] ZOOKEEPER-5124: Cover async and watched config reads on a chrooted handle Address review feedback: - Read the config with zoo_agetconfig in testChrootGetConfig, and check that the result matches the sync read. - Add TestReconfigServer::testChrootConfigWatch. It sets a config watch on a chrooted handle, runs a reconfig, and checks the event type and path. It also checks that the client logs no chroot path error. - Return ZSYSTEMERROR when prepend_chroot fails to allocate. - Share one helper between zoo_wget and zoo_wgetconfig. - Document the /zookeeper chroot case in sub_string. Co-Authored-By: Claude Opus 5.5 --- .../zookeeper-client-c/src/zookeeper.c | 66 +++++++++----- .../zookeeper-client-c/tests/TestClient.cc | 30 ++++++ .../tests/TestReconfigServer.cc | 91 +++++++++++++++++++ 3 files changed, 163 insertions(+), 24 deletions(-) diff --git a/zookeeper-client/zookeeper-client-c/src/zookeeper.c b/zookeeper-client/zookeeper-client-c/src/zookeeper.c index 86d1cb60ff7..06eb07688f5 100644 --- a/zookeeper-client/zookeeper-client-c/src/zookeeper.c +++ b/zookeeper-client/zookeeper-client-c/src/zookeeper.c @@ -1747,12 +1747,23 @@ static char* prepend_string(zhandle_t *zh, const char* client_path) { return prepend_chroot(zh, client_path, 0); } +/** + * Give the error for a null result of prepend_chroot. An absolute client + * path maps to null only if the allocation fails. + */ +static int prepend_error(const char* client_path) { + return (client_path != NULL && client_path[0] == '/') ? + ZSYSTEMERROR : ZBADARGUMENTS; +} + /** * Map a server path to a client path. * * The chroot prefix matches only at a path component boundary, f.e. a chroot * of /app does not match the path /app2. Cluster configuration is not in * the chroot namespace, so it is not affected by the chroot prefix. + * A chroot of /zookeeper is the exception. The configuration node is then + * inside the chroot, so the client reports its path as /config. */ char* sub_string(zhandle_t *zh, const char* server_path) { size_t chroot_len; @@ -4151,6 +4162,10 @@ static int Request_path_init(zhandle_t *zh, int mode, * Restore *path_out to caller's original path pointer on validation * failure so caller-side free_duplicate_path() remains safe. */ + if (*path_out == NULL) { + *path_out = (char *)path; + return prepend_error(path); + } if (zh == NULL || !isValidPath(*path_out, mode)) { free_duplicate_path(*path_out, path); *path_out = (char *)path; @@ -4254,6 +4269,9 @@ int zoo_awgetconfig(zhandle_t *zh, watcher_fn watcher, void* watcherCtx, struct GetDataRequest req = { (char*)server_path, watcher!=0 }; int rc; + if (server_path == NULL) { + return prepend_error(path); + } if (zh==0 || !isValidPath(server_path, 0)) { free_duplicate_path(server_path, path); return ZBADARGUMENTS; @@ -5033,6 +5051,10 @@ static int aremove_watches( WatchesRequest req; watcher_deregistration_t *wdo; + if (server_path == NULL) { + rc = prepend_error(path); + goto done; + } if (!zh || !isValidPath(server_path, 0)) { rc = ZBADARGUMENTS; goto done; @@ -5678,7 +5700,11 @@ int zoo_get(zhandle_t *zh, const char *path, int watch, char *buffer, buffer,buffer_len,stat); } -int zoo_wget(zhandle_t *zh, const char *path, +/* + * Read a node and wait for the result. A configuration read uses the + * configuration call, because that node is not in the chroot namespace. + */ +static int wget_sync(zhandle_t *zh, int config, const char *path, watcher_fn watcher, void* watcherCtx, char *buffer, int* buffer_len, struct Stat *stat) { @@ -5692,7 +5718,11 @@ int zoo_wget(zhandle_t *zh, const char *path, sc->u.data.buffer = buffer; sc->u.data.buff_len = *buffer_len; - rc=zoo_awget(zh, path, watcher, watcherCtx, SYNCHRONOUS_MARKER, sc); + if (config) { + rc=zoo_awgetconfig(zh, watcher, watcherCtx, SYNCHRONOUS_MARKER, sc); + } else { + rc=zoo_awget(zh, path, watcher, watcherCtx, SYNCHRONOUS_MARKER, sc); + } if(rc==ZOK){ wait_sync_completion(sc); rc = sc->rc; @@ -5706,6 +5736,14 @@ int zoo_wget(zhandle_t *zh, const char *path, return rc; } +int zoo_wget(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + char *buffer, int* buffer_len, struct Stat *stat) +{ + return wget_sync(zh, 0, path, watcher, watcherCtx, buffer, buffer_len, + stat); +} + int zoo_getconfig(zhandle_t *zh, int watch, char *buffer, int* buffer_len, struct Stat *stat) { @@ -5720,28 +5758,8 @@ int zoo_getconfig(zhandle_t *zh, int watch, char *buffer, int zoo_wgetconfig(zhandle_t *zh, watcher_fn watcher, void* watcherCtx, char *buffer, int* buffer_len, struct Stat *stat) { - struct sync_completion *sc; - int rc=0; - - if(buffer_len==NULL) - return ZBADARGUMENTS; - if((sc=alloc_sync_completion())==NULL) - return ZSYSTEMERROR; - - sc->u.data.buffer = buffer; - sc->u.data.buff_len = *buffer_len; - rc=zoo_awgetconfig(zh, watcher, watcherCtx, SYNCHRONOUS_MARKER, sc); - if(rc==ZOK){ - wait_sync_completion(sc); - rc = sc->rc; - if (rc == 0) { - if(stat) - *stat = sc->u.data.stat; - *buffer_len = sc->u.data.buff_len; - } - } - free_sync_completion(sc); - return rc; + return wget_sync(zh, 1, NULL, watcher, watcherCtx, buffer, buffer_len, + stat); } diff --git a/zookeeper-client/zookeeper-client-c/tests/TestClient.cc b/zookeeper-client/zookeeper-client-c/tests/TestClient.cc index 5e6c9f70280..475636ff58a 100644 --- a/zookeeper-client/zookeeper-client-c/tests/TestClient.cc +++ b/zookeeper-client/zookeeper-client-c/tests/TestClient.cc @@ -1300,6 +1300,27 @@ class Zookeeper_simpleSystem : public CPPUNIT_NS::TestFixture deallocate_String_vector(&children); } + // Result of one asynchronous configuration read. + struct ConfigResult { + ConfigResult(): rc(ZAPIERROR), mzxid(0) {} + int rc; + string value; + int64_t mzxid; + }; + + static void configCompletion(int rc, const char *value, int value_len, + const struct Stat *stat, const void *data) { + ConfigResult *res = (ConfigResult *)data; + res->rc = rc; + if (rc == ZOK) { + if (value != NULL && value_len > 0) { + res->value.assign(value, value_len); + } + res->mzxid = stat->mzxid; + } + count++; + } + // Cluster configuration is not in the chroot namespace. Synchronous and // asynchronous reads get the same node as a handle with no chroot. void testChrootGetConfig() { @@ -1329,6 +1350,15 @@ class Zookeeper_simpleSystem : public CPPUNIT_NS::TestFixture buf_ch, &len_ch, &stat_ch)); CPPUNIT_ASSERT_EQUAL(len, len_ch); CPPUNIT_ASSERT_EQUAL(stat.mzxid, stat_ch.mzxid); + + ConfigResult res; + count = 0; + CPPUNIT_ASSERT_EQUAL((int)ZOK, + zoo_agetconfig(zk_ch, 0, configCompletion, &res)); + waitForCreateCompletion(10); + CPPUNIT_ASSERT_EQUAL((int)ZOK, res.rc); + CPPUNIT_ASSERT_EQUAL(string(buf, len > 0 ? len : 0), res.value); + CPPUNIT_ASSERT_EQUAL(stat.mzxid, res.mzxid); } static bool hasChrootPathError() { diff --git a/zookeeper-client/zookeeper-client-c/tests/TestReconfigServer.cc b/zookeeper-client/zookeeper-client-c/tests/TestReconfigServer.cc index 4df7571d1dd..27ce5f8eb25 100644 --- a/zookeeper-client/zookeeper-client-c/tests/TestReconfigServer.cc +++ b/zookeeper-client/zookeeper-client-c/tests/TestReconfigServer.cc @@ -15,7 +15,10 @@ * the License. */ #include +#include +#include #include +#include #include #include #include @@ -33,6 +36,7 @@ class TestReconfigServer : public CPPUNIT_NS::TestFixture { CPPUNIT_TEST(testRemoveFollower); CPPUNIT_TEST(testReconfigFailureWithoutAuth); CPPUNIT_TEST(testReconfigFailureWithoutServerSuperuserPasswordConfigured); + CPPUNIT_TEST(testChrootConfigWatch); CPPUNIT_TEST_SUITE_END(); public: @@ -45,6 +49,7 @@ class TestReconfigServer : public CPPUNIT_NS::TestFixture { void testRemoveFollower(); void testReconfigFailureWithoutAuth(); void testReconfigFailureWithoutServerSuperuserPasswordConfigured(); + void testChrootConfigWatch(); private: static const uint32_t NUM_SERVERS; FILE* logfile_; @@ -59,6 +64,31 @@ class TestReconfigServer : public CPPUNIT_NS::TestFixture { const uint32_t TestReconfigServer::NUM_SERVERS = 3; +// The client threads write these and the test thread reads them. +static std::mutex configWatchMx; +static bool configWatchFired; +static int configWatchType; +static std::string configWatchPath; +static bool chrootPathError; + +static void configWatcher(zhandle_t*, int type, int, const char* path, + void*) { + std::lock_guard lock(configWatchMx); + if (type == ZOO_SESSION_EVENT || configWatchFired) { + return; + } + configWatchFired = true; + configWatchType = type; + configWatchPath = path ? path : ""; +} + +static void chrootLogCallback(const char* message) { + if (strstr(message, "does not include chroot path") != NULL) { + std::lock_guard lock(configWatchMx); + chrootPathError = true; + } +} + TestReconfigServer:: TestReconfigServer() : logfile_(openlogfile("TestReconfigServer")) { @@ -140,6 +170,67 @@ waitForConnected(zhandle_t* zh, uint32_t timeout_sec) { return false; } +/** + * 1. Set a configuration watch on a handle with a chroot. + * 2. Remove a follower using incremental reconfig. + * 3. The watch event has the configuration path, because that node is not + * in the chroot namespace. The client logs no chroot path error. + */ +void TestReconfigServer:: +testChrootConfigWatch() { + struct Stat stat; + int len = 1024; + char buf[len]; + + { + std::lock_guard lock(configWatchMx); + configWatchFired = false; + configWatchType = 0; + configWatchPath.clear(); + chrootPathError = false; + } + std::size_t leader = getLeader(); + CPPUNIT_ASSERT(leader >= 0); + std::string host = cluster_[leader]->getHostPort(); + zhandle_t* zk = zookeeper_init(host.c_str(), NULL, 10000, NULL, NULL, 0); + CPPUNIT_ASSERT_EQUAL(true, waitForConnected(zk, 10)); + std::string chrootHost = host + "/app"; + zhandle_t* zk_ch = zookeeper_init2(chrootHost.c_str(), NULL, 10000, NULL, + NULL, 0, chrootLogCallback); + CPPUNIT_ASSERT_EQUAL(true, waitForConnected(zk_ch, 10)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_wgetconfig(zk_ch, configWatcher, NULL, + buf, &len, &stat)); + + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:test", 10, NULL,(void*)ZOK)); + std::vector followers = getFollowers(); + CPPUNIT_ASSERT(!followers.empty()); + std::stringstream ss; + ss << followers[0]; + len = 1024; + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_reconfig(zk, NULL, ss.str().c_str(), + NULL, -1, buf, &len, &stat)); + + bool fired = false; + for (int i = 0; i < 100 && !fired; i++) { + { + std::lock_guard lock(configWatchMx); + fired = configWatchFired; + } + if (!fired) { + usleep(100000); + } + } + { + std::lock_guard lock(configWatchMx); + CPPUNIT_ASSERT(configWatchFired); + CPPUNIT_ASSERT_EQUAL((int)ZOO_CHANGED_EVENT, configWatchType); + CPPUNIT_ASSERT_EQUAL(std::string(ZOO_CONFIG_NODE), configWatchPath); + CPPUNIT_ASSERT(!chrootPathError); + } + zookeeper_close(zk_ch); + zookeeper_close(zk); +} + /** * 1. Connect to the leader. * 2. Remove a follower using incremental reconfig.