# Secrets on disk

A server that connects to other systems has to keep their passwords: the login of a partner's SFTP server in a user's folder, the password of the mail server, of a keystore, of a job's database connection. It must be able to **use** them, so it cannot hash them the way it hashes its users' passwords. It stores them encrypted, and the question is: with which key?

## Three ways

| Way | Where the key is | Who can read the stored secrets | Start of the server |
|---|---|---|---|
| **built in** | in the program, the same in every copy | anybody who has the server's files | by itself |
| **key file** | a file `secret.key` in the server's settings folder, made for this server | whoever has that file **and** the other files | by itself |
| **passphrase** | nowhere on disk: the key is locked with a passphrase an administrator types | whoever knows the passphrase and has the files | waits until an administrator has typed the passphrase |

A new server, and one that was upgraded, uses the built-in way until an administrator chooses another. With the built-in key, the encryption only keeps secrets from being read by accident: a copy of `prefs.XML`, of a user's file or of a job that reaches the wrong hands gives up every secret in it.

With a key of the server's own, only that server can read them.

## Choosing

The choice is made on the card **Stored Secrets** in [How passwords are stored](Prefs-Encryption-Passwords.md#stored-secrets), with the button **Apply on this server**. It is an action on this one server, not a setting that is saved and replicated.

| Choose | When |
|---|---|
| **key file** | for most servers. Nothing changes in daily life, and a copy of the settings alone is no longer enough. |
| **passphrase** | when even a full copy of the server's disk must not give up the secrets. The price: after every restart, nothing works until an administrator types the passphrase. |
| built in | only while you test |

The passphrase must have 12 characters or more.

## What changes, and what does not

- A secret is sealed with the new key **when it is next saved**. Nothing goes through the files to convert what is there: an old user file keeps its old form until somebody saves that user. One exception: the password of the PGP key ring is sealed at once, when the key is chosen or given and at every start (see [PGP in the browser](Web-PGP.md#general-settings)).
- Both forms are read side by side. Changing the way never makes a stored secret unreadable.
- It is one key in all three cases. Going from the passphrase to the key file writes that same key into the file; going back locks the same key again.
- Going back to the built-in way only stops new secrets from being sealed. What was sealed stays readable as long as the key is there.

## The key file is the crown jewel

- **Back it up**, and **apart** from the settings. A backup that holds both is as readable as before. A backup of the settings without the key cannot be restored into a working server.
- Keep it readable for the server's account only. The server makes it so.
- When you move the server to another machine, the file goes along.

## More than one server

| Other machine | Gets the key |
|---|---|
| a **DMZ server** | from the main server, over their protected link, in memory only. Nothing to do. |
| the other servers of a **cluster** | **not** by itself. With a key file, copy `secret.key` to each server by hand. With a passphrase, each server asks for it at its start. |
| a job engine on the **same** machine | from the server |
| a job engine on **another** machine | not at all. A job whose task holds a sealed secret runs on the server's own machine. |

A server whose settings say "key file" and that has no file says so in its log and reads nothing that is sealed.

## Exports and copies

A user, a job or a settings file taken to another server carries sealed secrets that the other server cannot read, unless it has the same key. The other server's log says what is missing. Enter the passwords again there, or give both servers the same key on purpose.

## After a restart, with a passphrase

The admin pages open with a notice that the passphrase is owed: see [Notices at start](Admin-Startup-Notices.md). Until it is given, listeners that need a keystore password are not up, and folders and jobs that need a stored login fail. A right passphrase answers "Activated" and the listeners start. A wrong one is refused after a short pause.

Plan for it: more than one person must know the passphrase, and a restart at night needs somebody awake.

## What this does not cover

- **Users' own passwords** are hashed, not encrypted, unless you chose to store them readable: see [Passwords and password rules](Security-Passwords.md).
- **Files** that users upload: see [PGP](Prefs-Encryption-PGP.md) and the file encryption of [Encryption](Prefs-Encryption.md).
- **Not keeping the secret at all** is stronger than any way of keeping it: a [key vault](VFS-Special-Types.md#keyvault) for passwords, keys and tokens instead of passwords, machine identities in a cloud.
- **Administrators.** Whoever may read the settings through the admin pages sees what is there. A full administrator can read stored secrets; role holders get them masked. Give out administration accordingly: [Roles and access](Security-Roles.md).
