-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pgwire: properly support unix socket clients with authentication
tldr: this patch makes unix sockets more production-ready, by enabling clients to use unix sockets in secure mode and enabling authentication over unix sockets. **Motivation:** [Unix domain sockets](https://en.wikipedia.org/wiki/Unix_domain_socket) are a way for a server process to accept direct in-memory connections from processes running on the same machine as the server. They are simpler and faster as they avoid the TCP/IP stack entirely. Unix sockets are used both to provide a local client interface for administrator users operating the system; as well as setting up more complex authentication systems using the following topology: ``` client ^ | (non-standard protocol) | .----------|--------------(server machine)--------------------------. | v | | ,----------------------. ,--------------------. | | | connection proxy | | server process | | | | and transport-level |<--(unix socket)-->| and authentication | | | | security | | (e.g. crdb) | | | `----------------------' `--------------------' | `-------------------------------------------------------------------' ``` **Description of this change:** CockroachDB already supports setting up a unix socket for use by clients running on the same machine, subject to regular Unix permission checks. Prior to this patch, support for unix sockets was incomplete: - it would work properly for insecure nodes/clusters; however, ... - ... in secure mode, it would also require a TLS handshake over the unix socket, which is neither supported by pg clients nor meaningful: unix domain sockets have transport-level security already. This patch extends/fixes support for unix sockets as follows: - it properly accepts client connections without TLS over unix sockets; - it subjects incoming unix socket connections to the standard HBA rule-based authentication selection (via the cluster setting `server.host_based_authentication.configuration`); - it changes the default HBA configuration to contain a default `local` rule that requires password authentication, in a way compatible with PostgreSQL; - it un-hides the `--socket` parameter from the output of `cockroach start --help`. Release note (cli change): Connections using Unix sockets are now accepted even when the server is running in secure more. (Consult `cockroach start --help` for details about the `--socket` parameter.) Release note (security): Connections using unix sockets are now subject to the HBA rules defined via the setting `server.host_based_authentication.configuration`, in a way compatible with PostgreSQL: incoming unix connections match `local` rules, whereas incoming TCP connections match `host` rules. The default HBA configuration used when the cluster setting is empty is now: host all root all cert host all all all cert-password local all all password
- Loading branch information
Showing
15 changed files
with
379 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env bash | ||
|
||
CERTS_DIR=${CERTS_DIR:-/certs} | ||
crdb=$1 | ||
trap "set -x; killall cockroach cockroachshort" EXIT HUP | ||
|
||
set -euo pipefail | ||
|
||
# Disable automatic network access by psql. | ||
unset PGHOST | ||
unset PGPORT | ||
# Use root access. | ||
export PGUSER=root | ||
|
||
echo "Testing Unix socket connection via insecure server." | ||
set -x | ||
|
||
# Start an insecure CockroachDB server. | ||
# We use a different port number from standard for an extra guarantee that | ||
# "psql" is not going to find it. | ||
"$crdb" start-single-node --background --insecure \ | ||
--socket=/tmp/.s.PGSQL.1111 \ | ||
--listen-addr=:12345 | ||
|
||
# Wait for server ready. | ||
"$crdb" sql --insecure -e "select 1" -p 12345 | ||
|
||
# Verify that psql can connect to the server. | ||
psql -h /tmp -p 1111 -c "select 1" | grep "1 row" | ||
|
||
# It worked. | ||
"$crdb" quit --insecure -p 12345 | ||
sleep 1; killall -9 cockroach cockroachshort || true | ||
|
||
set +x | ||
echo "Testing Unix socket connection via secure server." | ||
set -x | ||
|
||
# Restart the server in secure mode. | ||
"$crdb" start-single-node --background \ | ||
--certs-dir="$CERTS_DIR" --socket=/tmp/.s.PGSQL.1111 \ | ||
--listen-addr=:12345 | ||
|
||
# Wait for server ready; also create a user that can log in. | ||
"$crdb" sql --certs-dir="$CERTS_DIR" -e "create user foo with password 'pass'" -p 12345 | ||
|
||
# Also verify that psql can connect to the server. | ||
env PGPASSWORD=pass psql -U foo -h /tmp -p 1111 -c "select 1" | grep "1 row" | ||
|
||
set +x | ||
# Done. | ||
"$crdb" quit --certs-dir="$CERTS_DIR" -p 12345 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.