cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

pgwire: CockroachDB user/pass authentication doesn't work.

Open mbeattie opened this issue 8 years ago • 15 comments

This page: https://www.cockroachlabs.com/docs/stable/create-user.html Has this as example:

Secure Clusters with Passwords Users with passwords can authenticate their access by entering their password at the command prompt instead of using their client certificate and key.

cockroach sql --user=jpointsman

However, the actual experience is that you can not connect to a secure cluster with a username and password.

$ ./cockroach sql --certs-dir=certs/
# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
#
# Server version: CockroachDB CCL v1.1.5 (linux amd64, built 2018/02/05 17:40:57, go1.8.3) (same version as client)
# Cluster ID: 78480959-8218-4e7e-8d0f-f201ab771e7e
#
# Enter \? for a brief introduction.
#
root@:26257/> CREATE USER test WITH PASSWORD 'abc123';
CREATE USER

Time: 232.662504ms

root@:26257/> ^D
$ ./cockroach sql --user=test
# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
#
Enter password:
Error: problem using security settings, did you mean to use --insecure?: problem with CA certificate: not found
Failed running "sql"

The user and pass do seem to exist:

$ ./cockroach user get test --certs-dir=certs/
# Server version: CockroachDB CCL v1.1.5 (linux amd64, built 2018/02/05 17:40:57, go1.8.3) (same version as client)
# Cluster ID: 78480959-8218-4e7e-8d0f-f201ab771e7e
+----------+--------------------------------------------------------------+
| username |                        hashedPassword                        |
+----------+--------------------------------------------------------------+
| test     | $2a$10$tRqFsnqWTtiveB8ZWYtNvO3NMNP.W35TCeigHmYuaWfNE3kBJ12pm |
+----------+--------------------------------------------------------------+
(1 row)

It seems you always need certs and user/pass auth doesn't work.

mbeattie avatar Feb 09 '18 03:02 mbeattie

In fact the client doesn't even seem to attempt to connect. The same behavior happens even if the cluster is down.

mbeattie avatar Feb 09 '18 03:02 mbeattie

Have you tried ./cockroach sql --user=test --certs-dir=certs/?

knz avatar Feb 09 '18 05:02 knz

That works, however its not consistent with the documentation. Also, if you're using certs why bother with the password?

mbeattie avatar Feb 09 '18 11:02 mbeattie

Client certificates can be used to authenticate the user, in which case you do not use the password.

However, you can still talk to the server over TLS without a client certificate, in which case you still need a way to log in.

The --certs-dir flag defaults to ~/.cockroach-certs. We should probably clarify this in the docs, and in cases such as secure connection, show the explicit flag (or mention the default again). @jseldess

mberhault avatar Feb 09 '18 11:02 mberhault

Ok, the docs mention 2 kinds of certificates: https://www.cockroachlabs.com/docs/stable/secure-a-cluster.html

One is used to authenticate a user

The third command creates the client certificate and key, in this case for the root 
user: client.root.crt and client.root.key. These files will be used to secure communication 
between the built-in SQL shell and the cluster (see step 4).

The other are used to authenticate nodes between each other:

The fourth command creates the node certificate and key: node.crt and node.key. 
These files will be used to secure communication between nodes. Typically, you would 
generate these separately for each node since each node has unique addresses; in this 
case, however, since all nodes will be running locally, you need to generate only one 
node certificate and key.

There's no mention of needing a TLS client cert for a client to server connection.

mbeattie avatar Feb 09 '18 11:02 mbeattie

The "client certificate" can be used to authenticate a client when talking TLS to a server as it contains the user name in the certificate's Subject field.

When using this client certificate, authentication is performed by verifying that the certificate was signed by the CA. We then match the username in the certificate against the requested username.

The alternative is using passwords. You still need to use TLS to talk to the server, but you can do so without a client certificate.

Both possibilities are listed in https://www.cockroachlabs.com/docs/stable/create-user.html#user-authentication

mberhault avatar Feb 09 '18 12:02 mberhault

The alternative is using passwords. You still need to use TLS to talk to the server, but you can do so without a client certificate.

This is the part that I have demonstrated is false.

mbeattie avatar Feb 09 '18 12:02 mbeattie

You still need the CA certificate to know who to trust, which is why you need the certs-dir, but you definitely don't need a client certificate. You can even skip the CA check if you want to live dangerously, in which case the client needs no certificates whatsoever.

Let me demonstrate:

Create the CA, node, and client cert (for user root):

# I have the following directories to use as the server and client certs dir respectively:
$ mkdir server-certs client-certs

# Create the CA certificate:
$ cockroach cert create-ca --certs-dir=server-certs --ca-key=server-certs/ca.key

# Create the node certificate:
$ cockroach cert create-node --certs-dir=server-certs --ca-key=server-certs/ca.key localhost 127.0.0.1
# Examine directory:
$ ls server-certs/
ca.crt  ca.key  client.root.crt  client.root.key  node.crt  node.key

# Start the node:
$ cockroach start --certs-dir=server-certs

Connect as root and create a user:

# Copy the CA and root client certificate and key to the client certs directory:
$ cp server-certs/ca.crt server-certs/client.root.* client-certs/

# List it:
$ ls client-certs/
ca.crt  client.root.crt  client.root.key

# Connect as root, specifying the certs directory:
$ cockroach sql --certs-dir=client-certs
root@:26257/> CREATE USER marc WITH PASSWORD foo;
CREATE USER 1
Time: 67.196923ms

Connect as me, I haven't generated certificates for user marc: however, I still need the CA certificate from the client-certs dir:

$ cockroach sql --certs-dir=client-certs --user=marc
Enter password: # Here, I enter my password: "foo".
marc@:26257/> show databases;
+--------------------+
|      Database      |
+--------------------+
| crdb_internal      |
| information_schema |
| pg_catalog         |
+--------------------+
(3 rows)

Time: 1.379652ms

I can go further and connect without validating that the server certificate was signed by a known CA certificate. This must be done through the --url as it offers no MITM protection. Notice how I am no longer specifying the certs directory. I also need to pass the password through the URL because the use of --url supercedes other connection flags: from the flag help message: The connection URL. If you use this flag, do not set any other connection flags.

$ cockroach sql --url='postgresql://marc:foo@localhost:26257/?sslmode=require'
marc@localhost:26257/> show databases;
+--------------------+
|      Database      |
+--------------------+
| crdb_internal      |
| information_schema |
| pg_catalog         |
+--------------------+
(3 rows)

Time: 1.319817ms

You can read more about the possible sslmode values, and the risks of some of them in the postgres docs.

mberhault avatar Feb 09 '18 12:02 mberhault

I am not saying the functionality doesn't work or is broken, just that it is not consistent with the documentation. Specifically this:

I can go further and connect without validating that the server certificate was signed by a known CA certificate. This must be done through the --url as it offers no MITM protection. Notice how I am no longer specifying the certs directory. I also need to pass the password through the URL.

Is not indicated and the fact that the client (cockroach sql) will prompt for a password leads a user to believe that you can supply the password when asked.

mbeattie avatar Feb 09 '18 12:02 mbeattie

Ah, sorry, I think I'm on the same page now. You're talking about the fact that you can't use password authentication if client certificates are present?

If so, I agree, we should at least fix the docs for this. In the longer term, I've had #15318 filed for a while to make our connection/security flags more sane (and more in line with psql). I just haven't gotten around to it. Could you take a look at it and see if it would address the issue?

mberhault avatar Feb 09 '18 17:02 mberhault

Basically yes. If you are using another DB (like postgres) you can have TLS enabled on the server side and not need to explicitly have some client certificate... you just end up validating the cert itself and then authenticating the user with the username/password. Cockroach works differently in that it wants any node that connects to have a client certificate to "authorize" that node to even make connections to the cluster. That is an independent action from the user authentication itself. It's not possible to connect a client to a "secure" cluster regardless of your validation of the server's certificate.

Anyway, looking at that issue, I think that the first point in the original post covers what I am trying to say.

mbeattie avatar Feb 10 '18 00:02 mbeattie

cc @tim-o @awoods187 for next actions

knz avatar Jul 23 '18 10:07 knz

CC @jseldess for docs updates

awoods187 avatar Jul 23 '18 14:07 awoods187

Created https://github.com/cockroachdb/docs/issues/3417. @awoods187 - let me know if you want to leave the issue open to track the compatibility with postgres.

tim-o avatar Jul 23 '18 14:07 tim-o

Nah i'm fine closing it with the docs issue filed

awoods187 avatar Jul 23 '18 15:07 awoods187