diff --git a/tests/clickhouse-test-runner/README.md b/tests/clickhouse-test-runner/README.md index 2aed73e5..307fb808 100644 --- a/tests/clickhouse-test-runner/README.md +++ b/tests/clickhouse-test-runner/README.md @@ -141,7 +141,7 @@ CLICKHOUSE_TEST_RUNNER_LIVE=1 tests/clickhouse-test-runner/.build/tests/clickhou | `CLICKHOUSE_TEST_RUNNER_BIN` | (none) | The absolute path of a program that is already built; the run script then does not build. | | `CLICKHOUSE_TEST_RUNNER_BUILD_DIR` | `.build` in this directory | The CMake build directory of `scripts/build.sh`, as an absolute path. | | `CMAKE_BUILD_TYPE`, `CMAKE_BUILD_PARALLEL_LEVEL` | `Release`, `4` | The build type (used only when the build directory is new) and the number of parallel jobs of `scripts/build.sh`. | -| `CLICKHOUSE_CLIENT_CLI_LOG` | (none) | The absolute path of a file. Each invocation of the program adds its arguments, statements and errors to it. | +| `CLICKHOUSE_CLIENT_CLI_LOG` | (none) | The absolute path of a file. Each invocation of the program adds its arguments (the `--password` value hidden), statements and errors to it. | | `CLICKHOUSE_TEST_RUNNER_RECEIVE_TIMEOUT` | `60` | Seconds that the program waits for a packet of the server (`ClientOptions::SetConnectionRecvTimeout`); `0` waits for ever. | | `CLICKHOUSE_HOST`, `CLICKHOUSE_PORT_TCP`, `CLICKHOUSE_PORT_HTTP` | `localhost`, `9000`, `8123` | The server. The runner gives `--host` and `--port=$CLICKHOUSE_PORT_TCP`; the run script and the live tests use the HTTP port to read the server version and the server's own output. | | `CLICKHOUSE_CONTAINER` | `clickhouse` | The server container in which `run-with-real-client.sh` runs the real `clickhouse-client`. | diff --git a/tests/clickhouse-test-runner/args.cpp b/tests/clickhouse-test-runner/args.cpp index d39aed13..ba9dcf12 100644 --- a/tests/clickhouse-test-runner/args.cpp +++ b/tests/clickhouse-test-runner/args.cpp @@ -179,6 +179,34 @@ Options ParseArgs(const std::vector& argv) { return opts; } +std::vector RedactPassword(const std::vector& argv) { + // A setting (`--max_threads 1`) takes the next argument as its value only + // when it does not start with `-` or is a negative number. Such a value is + // never an option, so the walk can read it as an argument of its own. + std::vector result; + result.reserve(argv.size()); + for (size_t i = 0; i < argv.size(); ++i) { + const std::string& arg = argv[i]; + const OptionSpec* o = nullptr; + if (arg.rfind("--", 0) == 0) { + const auto eq = arg.find('='); + if (eq != std::string::npos) { + result.push_back(arg.compare(2, eq - 2, "password") == 0 ? "--password=***" : arg); + continue; + } + o = FindOption(arg.substr(2), ""); + } else if (arg.size() > 1 && arg[0] == '-') { + o = FindOption("", arg.substr(1)); + } + result.push_back(arg); + if (o && o->has_arg && i + 1 < argv.size()) { + ++i; + result.push_back(std::string(o->long_name) == "password" ? "***" : argv[i]); + } + } + return result; +} + const char kUsageText[] = R"(usage: clickhouse-client [options] A clickhouse-client imitation that runs queries through clickhouse-cpp, so diff --git a/tests/clickhouse-test-runner/args.h b/tests/clickhouse-test-runner/args.h index 4fca2665..18350ddb 100644 --- a/tests/clickhouse-test-runner/args.h +++ b/tests/clickhouse-test-runner/args.h @@ -30,6 +30,15 @@ struct Options { /// std::invalid_argument for a bad value or a missing argument. Options ParseArgs(const std::vector& argv); +/// The command line with each `--password` value replaced by `***`, for the +/// log: `--password X` gives `--password ***`, `--password=X` gives +/// `--password=***`. It reads the options as ParseArgs does, so it finds the +/// value also after an option that takes `--password` as its own value +/// (`-u --password --password X`). A last `--password` stays as it is. Unlike +/// ParseArgs, it does not stop at `--`: a `--password` value after it is also +/// hidden. +std::vector RedactPassword(const std::vector& argv); + /// The settings of the command line that the shim sends to the server: /// `log_comment`, `max_insert_threads`, `max_threads` and `send_logs_level`, /// and the feature-tag overrides of `tests/clickhouse-test`: for a test with diff --git a/tests/clickhouse-test-runner/shim.cpp b/tests/clickhouse-test-runner/shim.cpp index 1ec80bfc..f95b4e8c 100644 --- a/tests/clickhouse-test-runner/shim.cpp +++ b/tests/clickhouse-test-runner/shim.cpp @@ -51,7 +51,7 @@ int RunMain(const std::vector& argv, std::istream& in, std::ostream InvocationLog log; { std::string joined; - for (const auto& a : argv) { + for (const auto& a : RedactPassword(argv)) { joined += (joined.empty() ? "" : " ") + a; } log.Add("argv", joined); diff --git a/tests/clickhouse-test-runner/ut/args_ut.cpp b/tests/clickhouse-test-runner/ut/args_ut.cpp index 9bddf119..1a385faa 100644 --- a/tests/clickhouse-test-runner/ut/args_ut.cpp +++ b/tests/clickhouse-test-runner/ut/args_ut.cpp @@ -10,6 +10,7 @@ using namespace clickhouse_test_runner; using Settings = std::vector>; +using Argv = std::vector; TEST(ParseArgs, SQLTestCommandLine) { // The command line that tests/clickhouse-test uses for a .sql test. @@ -76,6 +77,32 @@ TEST(ParseArgs, Errors) { EXPECT_THROW(ParseArgs({"--query"}), std::invalid_argument); } +TEST(RedactPassword, HidesThePasswordOnly) { + EXPECT_EQ((Argv{"--user", "u", "--password", "***", "--password=***", "--query", "SELECT 1", "--password"}), + RedactPassword({"--user", "u", "--password", "secret", "--password=secret2", "--query", "SELECT 1", "--password"})); + + struct Case { + Argv argv; + std::string password; ///< The password that ParseArgs reads. + Argv want; + }; + const std::vector cases = { + // An option that takes a value can take `--password` as its value. A + // setting (`--max_threads`) never does. + {{"-u", "--password", "--password", "secret"}, "secret", {"-u", "--password", "--password", "***"}}, + {{"--host", "--password", "--password", "secret"}, "secret", {"--host", "--password", "--password", "***"}}, + {{"--max_threads", "--password", "secret"}, "secret", {"--max_threads", "--password", "***"}}, + {{"--max_threads", "-1", "--password", "secret"}, "secret", {"--max_threads", "-1", "--password", "***"}}, + // Contrast: the value of another option stays. + {{"-u", "--password", "x"}, "", {"-u", "--password", "x"}}, + {{"--host", "--password=x"}, "", {"--host", "--password=x"}}, + }; + for (const auto& c : cases) { + EXPECT_EQ(c.password, ParseArgs(c.argv).password) << testing::PrintToString(c.argv); + EXPECT_EQ(c.want, RedactPassword(c.argv)) << testing::PrintToString(c.argv); + } +} + TEST(RunMain, ExtractFromConfig) { std::istringstream in; std::ostringstream out, err; diff --git a/tests/clickhouse-test-runner/ut/live_ut.cpp b/tests/clickhouse-test-runner/ut/live_ut.cpp index 615b7be7..acf527b5 100644 --- a/tests/clickhouse-test-runner/ut/live_ut.cpp +++ b/tests/clickhouse-test-runner/ut/live_ut.cpp @@ -12,7 +12,11 @@ #include #include +#include #include +#include +#include +#include #include #include #include @@ -244,6 +248,41 @@ TEST(Live, StatementsWithoutResult) { EXPECT_EQ("x\nUInt8\n1\nx\n", got.out); } +TEST(Live, LogHidesPassword) { + REQUIRE_LIVE(); + // The log of CLICKHOUSE_CLIENT_CLI_LOG has no password value, in no record. + // The test checks only the log: the server can refuse the password. + std::string path = testing::TempDir() + "clickhouse-test-runner-log-XXXXXX"; + const int fd = mkstemp(path.data()); + ASSERT_GE(fd, 0) << path; + close(fd); + const std::string password = "pw-7f3a9c"; + const std::string inline_password = "pw-2b8e4d"; + const char* env = std::getenv("CLICKHOUSE_CLIENT_CLI_LOG"); + const std::optional previous = env ? std::optional(env) : std::nullopt; + setenv("CLICKHOUSE_CLIENT_CLI_LOG", path.c_str(), 1); + for (const auto& form : std::vector>{{"--password", password}, {"--password=" + inline_password}}) { + std::vector argv = {"--host", Env("CLICKHOUSE_HOST", "localhost"), "--port", Env("CLICKHOUSE_PORT_TCP", "9000")}; + argv.insert(argv.end(), form.begin(), form.end()); + argv.insert(argv.end(), {"--query", "SELECT 1"}); + std::istringstream in; + std::ostringstream out, err; + RunMain(argv, in, out, err); + } + if (previous) { + setenv("CLICKHOUSE_CLIENT_CLI_LOG", previous->c_str(), 1); + } else { + unsetenv("CLICKHOUSE_CLIENT_CLI_LOG"); + } + std::ifstream file(path); + const std::string log((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + std::remove(path.c_str()); + EXPECT_EQ(std::string::npos, log.find(password)) << log; + EXPECT_EQ(std::string::npos, log.find(inline_password)) << log; + EXPECT_NE(std::string::npos, log.find(" --password *** ")) << log; + EXPECT_NE(std::string::npos, log.find(" --password=*** ")) << log; +} + TEST(Live, InsertWithoutDataFailsAtOnce) { REQUIRE_LIVE(); // The server waits for the INSERT data; Client::Execute never sends it.