v0.7.0
Menu

#Authentication

A connection logs in once, and nothing else is answered until it has. There are two ways to log in: the admin token and a user with a password.

By default there is no login and the server only accepts connections on a loopback address. Once a token is set, or once any user has been created, every new connection has to log in.

#The token

Set FERRITE_TOKEN in the server's environment. It has to be 16 to 1024 characters, and a random one is best:

export FERRITE_TOKEN=$(openssl rand -hex 32)
ferrite_db 0.0.0.0:7878

It is read from the environment and not from an argument, because arguments show up in ps. A client logs in with:

{"cmd":"auth","token":"<your token>"}

The token logs in as an admin on every database. It is what creates the first user, it is what lets a server listen on a non-loopback address (the server refuses to start there without one), and it is the way back in if every admin password is lost.

Without TLS the token is sent as plain text. See TLS. The server prints a warning when a token is set, the address is not loopback and TLS is off.

Changing the token means restarting the server. Connections that logged in with the old one stay open.

#Roles

role allows
read find, get, count, aggregate, distinct, listCollections, listIndexes
readWrite everything in read, plus insert, update, delete, replace, findOneAndUpdate, findOneAndDelete, createIndex, dropIndex, drop
admin everything in readWrite, plus compact and dropDatabase

#Grants

A user has a list of grants. A grant is a database and a role, {"db":"shop","role":"readWrite"}, and "db":"*" means every database. A request is checked against the database it is for: the strongest grant on that database, or on *, has to be at least what the request needs.

  • use on a database with no grant is unauthorized, and listDatabases only lists databases the user has a grant on.
  • A grant can name a collection: {"db":"shop","collection":"orders","role":"read"}. The most specific grant wins (collection, then database, then *). A collection grant can only be read or readWrite, cannot be on *, and gives no access to the rest of the database. A user with only collection grants is refused listCollections, compact and dropDatabase.
  • A pipeline needs read on every collection it reads, including a $lookup target. $out and $merge need readWrite on the target collection. See aggregation.
  • A refused request gets unauthorized and the connection stays open.
  • An admin grant on one database allows compact and dropDatabase there only.

#Managing users

The user commands are for the whole server, so they need an admin grant on * (or the token). An admin of a single database cannot make users.

{"cmd":"createUser","user":"alice","password":"correct horse","grants":[{"db":"shop","role":"readWrite"},{"db":"logs","role":"read"}]}
{"cmd":"updateUser","user":"alice","password":"another one","grants":[{"db":"shop","role":"read"}]}
{"cmd":"dropUser","user":"alice"}
{"cmd":"users"}
  • A user name is 1 to 64 letters, digits or _ . - @. A password is 8 to 256 ASCII characters.
  • Grants: each database can appear once, the list cannot be empty, and database names follow the usual rules.
  • "role":"admin" instead of grants is shorthand for one grant on *.
  • updateUser takes a password, grants (or a role), or both. The grants you send replace the old ones.
  • users lists each user's grants.
  • The first user has to be an admin on *, and the last such admin cannot be removed.
  • Once there has been a user, every new connection has to log in, even if all the users are dropped again. The token is the way back in.
  • A connection of a user who is dropped, or whose grants or password change, is closed at its next request. The user logs in again to get the new grants.

createUser and updateUser send the password in the request. Use TLS for them.

Users are server level, not per database. They are stored in _users.log, which is synced on every write whatever the sync policy.

#SCRAM-SHA-256

A user logs in with SCRAM-SHA-256 as in RFC 5802 and RFC 7677, without channel binding. The password never crosses the connection, and the server stores a salt, an iteration count (15,000) and two derived keys, not the password. A stolen log can be guessed against slowly but cannot be used to log in.

The exchange is two commands. The strings in payload are the standard SCRAM messages, so any SCRAM library can do the client side:

{"cmd":"scramStart","payload":"n,,n=alice,r=<client nonce>"}
{"cmd":"scramFinish","payload":"c=biws,r=<server nonce>,p=<proof>"}

Each reply holds the server's message in payload: the first has r=, s= and i=, and the second has v=<server signature>, which a client should check. A wrong password, an unknown name and a wrong token all get authentication failed after 300 ms, and the connection is closed. An unknown name gets a stable fake salt, so scramStart looks the same for it.

Passwords are ASCII only, so no SASLprep step is needed.

A second login on one connection is refused with already logged in.

#Limits on logging in

  • A client has 10 seconds in all to log in, and the login lines are at most 4 KiB.
  • Until the login, every reply is authentication required, even for a line that is not JSON.
  • An address that gets 5 logins wrong (a token or a password), each within a minute of the one before, cannot log in for a minute after the last of them. The reply has the code limit.
  • There is no lockout per user, only that and the connection limits.

#Not handled

  • Whoever has the token can do everything, including compact.
  • The token sits in the process's environment, so the same user and root can read it from /proc.
  • A TLS client certificate says nothing about a role. It is a gate on the connection, and the token or a login still comes after it.