Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/clickhouse-test-runner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`. |
Expand Down
28 changes: 28 additions & 0 deletions tests/clickhouse-test-runner/args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,34 @@ Options ParseArgs(const std::vector<std::string>& argv) {
return opts;
}

std::vector<std::string> RedactPassword(const std::vector<std::string>& 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<std::string> 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
Expand Down
9 changes: 9 additions & 0 deletions tests/clickhouse-test-runner/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ struct Options {
/// std::invalid_argument for a bad value or a missing argument.
Options ParseArgs(const std::vector<std::string>& 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<std::string> RedactPassword(const std::vector<std::string>& 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
Expand Down
2 changes: 1 addition & 1 deletion tests/clickhouse-test-runner/shim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int RunMain(const std::vector<std::string>& 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);
Expand Down
27 changes: 27 additions & 0 deletions tests/clickhouse-test-runner/ut/args_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using namespace clickhouse_test_runner;

using Settings = std::vector<std::pair<std::string, std::string>>;
using Argv = std::vector<std::string>;

TEST(ParseArgs, SQLTestCommandLine) {
// The command line that tests/clickhouse-test uses for a .sql test.
Expand Down Expand Up @@ -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<Case> 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;
Expand Down
39 changes: 39 additions & 0 deletions tests/clickhouse-test-runner/ut/live_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
#include <sys/socket.h>
#include <unistd.h>

#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <optional>
#include <sstream>
#include <string>
#include <vector>
Expand Down Expand Up @@ -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<std::string> previous = env ? std::optional<std::string>(env) : std::nullopt;
setenv("CLICKHOUSE_CLIENT_CLI_LOG", path.c_str(), 1);
for (const auto& form : std::vector<std::vector<std::string>>{{"--password", password}, {"--password=" + inline_password}}) {
std::vector<std::string> 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<char>(file)), std::istreambuf_iterator<char>());
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.
Expand Down
Loading