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: 2 additions & 0 deletions programs/keeper-client/KeeperClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <Common/filesystemHelpers.h>
#include <Common/ZooKeeper/ZooKeeper.h>
#include <Common/ErrnoException.h>
#include <Common/clearPasswordFromCommandLine.h>
#include <Parsers/TokenIterator.h>
#include <Poco/Util/HelpFormatter.h>

Expand Down Expand Up @@ -752,6 +753,7 @@ int mainEntryClickHouseKeeperClient(int argc, char ** argv)
{
DB::KeeperClient client;
client.init(argc, argv);
clearPasswordFromCommandLine(argc, argv);
return client.run();
}
catch (DB::Exception & e)
Expand Down
30 changes: 22 additions & 8 deletions src/Common/clearPasswordFromCommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,30 @@ using namespace std::literals;

void clearPasswordFromCommandLine(int argc, char ** argv)
{
for (int arg = 1; arg < argc; ++arg)
/// Options of the programs calling this function whose values are credentials.
static constexpr std::string_view secret_options[]
= {"--password"sv, "--jwt"sv, "--ssh-key-passphrase"sv, "--one-time-password"sv, "--oauth-client-secret"sv, "--identity"sv};

/// Backwards, so that an option is recognized before a preceding `--password` without a value zeroes it.
for (int arg = argc - 1; arg >= 1; --arg)
{
if (arg + 1 < argc && argv[arg] == "--password"sv)
{
++arg;
memset(argv[arg], 0, strlen(argv[arg]));
}
else if (0 == strncmp(argv[arg], "--password=", strlen("--password=")))
const std::string_view argument = argv[arg];
for (const auto option : secret_options)
{
memset(argv[arg] + strlen("--password="), 0, strlen(argv[arg]) - strlen("--password="));
if (argument == option)
{
/// clickhouse-client also accepts `--option = value`.
const int value = (arg + 2 < argc && argv[arg + 1] == "="sv) ? arg + 2 : arg + 1;
if (value < argc)
memset(argv[value], 0, strlen(argv[value]));
break;
}
if (argument.size() > option.size() && argument.starts_with(option)
&& (argument[option.size()] == '=' || argument[option.size()] == ':'))
{
memset(argv[arg] + option.size() + 1, 0, argument.size() - option.size() - 1);
break;
}
}
}
}
4 changes: 2 additions & 2 deletions src/Common/clearPasswordFromCommandLine.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

/** If there are --password=... or --password ... arguments in command line, replace their values with zero bytes.
* This is needed to prevent password exposure in 'ps' and similar tools.
/** Replace with zero bytes the values of the options carrying credentials (`--password`, `--jwt`, ...), given as
* `--option value`, `--option=value`, `--option = value` or `--option:value`, to prevent their exposure in `ps` and similar tools.
*/
void clearPasswordFromCommandLine(int argc, char ** argv);
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
client, separate values
--jwt
--password
--ssh-key-passphrase
--one-time-password
client, values after =
--jwt=
--password=
--ssh-key-passphrase=
--one-time-password=
client, values after spaced =
--jwt
--password
--ssh-key-passphrase
--one-time-password
client, --password without a value
--password
keeper-client
--password
--identity=
keeper-client, values after :
--password:
--identity:
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env bash
# Tags: no-fasttest
# no-fasttest: `--jwt` needs a build with jwt-cpp and SSL.

CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=../shell_config.sh
. "$CUR_DIR"/../shell_config.sh

# The values of the options carrying credentials must not be visible in `ps` and `/proc/<pid>/cmdline`.
# The programs connect to a listener that never answers, so they stay alive after parsing their options.

LISTENER_SCRIPT="${CLICKHOUSE_TMP}/${CLICKHOUSE_TEST_UNIQUE_NAME}_listener.py"
PORT_FILE="${CLICKHOUSE_TMP}/${CLICKHOUSE_TEST_UNIQUE_NAME}.port"
ACCEPTED_FILE="${CLICKHOUSE_TMP}/${CLICKHOUSE_TEST_UNIQUE_NAME}.accepted"
rm -f "$PORT_FILE"
: > "$ACCEPTED_FILE"

cat > "$LISTENER_SCRIPT" <<'EOF'
import os
import socket
import sys

listener = socket.socket()
listener.bind(("127.0.0.1", 0))
listener.listen(16)
with open(sys.argv[1] + ".tmp", "w") as f:
f.write(str(listener.getsockname()[1]))
os.rename(sys.argv[1] + ".tmp", sys.argv[1])

connections = []
while True:
connection, _ = listener.accept()
connections.append(connection)
with open(sys.argv[2], "a") as f:
f.write("accepted\n")
EOF

python3 "$LISTENER_SCRIPT" "$PORT_FILE" "$ACCEPTED_FILE" &
LISTENER_PID=$!
trap 'kill $LISTENER_PID 2>/dev/null; wait $LISTENER_PID 2>/dev/null' EXIT

while [ ! -s "$PORT_FILE" ] && kill -0 "$LISTENER_PID" 2>/dev/null; do sleep 0.1; done
PORT=$(cat "$PORT_FILE")

if [ -x "${CLICKHOUSE_BINARY}-keeper-client" ]; then
KEEPER_CLIENT="${CLICKHOUSE_BINARY}-keeper-client"
else
KEEPER_CLIENT="${CLICKHOUSE_BINARY} keeper-client"
fi

CONNECTIONS=0
function show_credential_options()
{
"$@" > /dev/null 2>&1 &
local pid=$!
CONNECTIONS=$((CONNECTIONS + 1))
# A connected program has parsed its options.
while [ "$(wc -l < "$ACCEPTED_FILE")" -lt "$CONNECTIONS" ] && kill -0 "$pid" 2>/dev/null; do sleep 0.1; done
tr '\0' '\n' < "/proc/$pid/cmdline" | grep -E '^--(password|jwt|ssh-key-passphrase|one-time-password|identity)|SECRET'
kill "$pid" 2>/dev/null
wait "$pid" 2>/dev/null || true
}

JWT='eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0In0.SECRET'

echo "client, separate values"
show_credential_options ${CLICKHOUSE_CLIENT_BINARY} --host 127.0.0.1 --port "$PORT" --query "SELECT 1" \
--jwt "$JWT" --password SECRET --ssh-key-passphrase SECRET --one-time-password SECRET

echo "client, values after ="
show_credential_options ${CLICKHOUSE_CLIENT_BINARY} --host 127.0.0.1 --port "$PORT" --query "SELECT 1" \
--jwt="$JWT" --password=SECRET --ssh-key-passphrase=SECRET --one-time-password=SECRET

echo "client, values after spaced ="
show_credential_options ${CLICKHOUSE_CLIENT_BINARY} --host 127.0.0.1 --port "$PORT" --query "SELECT 1" \
--jwt = "$JWT" --password = SECRET --ssh-key-passphrase = SECRET --one-time-password = SECRET

echo "client, --password without a value"
show_credential_options ${CLICKHOUSE_CLIENT_BINARY} --host 127.0.0.1 --port "$PORT" --query "SELECT 1" \
--password --jwt "$JWT"

echo "keeper-client"
show_credential_options ${KEEPER_CLIENT} --host 127.0.0.1 --port "$PORT" --connection-timeout 60 --session-timeout 60 \
--operation-timeout 60 --query "ls" --password SECRET --identity=user:SECRET

echo "keeper-client, values after :"
show_credential_options ${KEEPER_CLIENT} --host 127.0.0.1 --port "$PORT" --connection-timeout 60 --session-timeout 60 \
--operation-timeout 60 --query "ls" --password:SECRET --identity:user:SECRET
Loading