aboutsummaryrefslogtreecommitdiff
path: root/content/blog/nix/migrating-vaultwarden.md
blob: a095070d45237e3f7f029c033e6fb06fb37a84c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
---
title: Migrating vaultwarden to nixos
description: How I migrated my vaultwarden installation to nixos
date: 2023-12-20
tags:
- nix
- vaultwarden
---

## Introduction

I am migrating several services from a k3s kubernetes cluster to a nixos server. Here is how I performed the operation with my [vaultwarden](https://github.com/dani-garcia/vaultwarden) password manager.

## Vaultwarden with nixos

Vaultwarden is packaged on nixos, but I am used to the hosting the container image and upgrading it at my own pace so I am sticking with it for now.

Here is the module I wrote to deploy a vaultwarden container, configure postgresql and borg backups in `apps/vaultwarden/app.nix`:
```nix
{ config, lib, pkgs, ... }:
{
	imports = [
	  ../../lib/nginx.nix
	  ../../lib/postgresql.nix
	];
	environment.etc = {
		"borg-vaultwarden-db.key" = {
			mode = "0400";
			source = ./borg-db.key;
		};
		"borg-vaultwarden-storage.key" = {
			mode = "0400";
			source = ./borg-storage.key;
		};
	};
	services = {
		borgbackup.jobs = let defaults = {
			compression = "auto,zstd";
			doInit = true;
			encryption.mode = "none";
			prune.keep = {
				daily = 14;
				weekly = 4;
				monthly = 3;
			};
			startAt = "daily";
		}; in {
			"vaultwarden-db" = defaults // {
				environment.BORG_RSH = "ssh -i /etc/borg-vaultwarden-db.key";
				paths = "/tmp/vaultwarden.sql";
				postHook = "rm -f /tmp/vaultwarden.sql";
				preHook = ''rm -f /tmp/vaultwarden.sql; /run/current-system/sw/bin/pg_dump -h localhost -U vaultwarden -d vaultwarden > /tmp/vaultwarden.sql'';
				repo = "ssh://borg@gcp.adyxax.org/srv/borg/vaultwarden-db";
			};
			"vaultwarden-storage" = defaults // {
				environment.BORG_RSH = "ssh -i /etc/borg-vaultwarden-storage.key";
				paths = "/srv/vaultwarden";
				repo = "ssh://borg@gcp.adyxax.org/srv/borg/vaultwarden-storage";
			};
		};
		nginx.virtualHosts = let commons = {
			forceSSL = true;
			locations = {
			  "/" = {
			    proxyPass = "http://127.0.0.1:8083";
			  };
			};
		}; in {
			"pass.adyxax.org" = commons // {
				sslCertificate = "/etc/nginx/adyxax.org.crt";
				sslCertificateKey = "/etc/nginx/adyxax.org.key";
			};
		};
		postgresql = {
			ensureUsers = [{
				name = "vaultwarden";
				ensureDBOwnership = true;
			}];
			ensureDatabases = ["vaultwarden"];
		};
	};
	virtualisation.oci-containers.containers = {
		vaultwarden = {
			environment = {
				ADMIN_TOKEN = builtins.readFile ./argon-token.key;
				DATABASE_MAX_CONNS = "2";
				DATABASE_URL = "postgres://vaultwarden:" + (lib.removeSuffix "\n" (builtins.readFile ./database-password.key)) + "@10.88.0.1/vaultwarden?sslmode=disable";
			};
			image = "vaultwarden/server:1.30.1";
			ports = ["127.0.0.1:8083:80"];
			volumes = [ "/srv/vaultwarden/:/data" ];
		};
	};
}
```

## Dependencies

### Borg

Borg needs to be running on another server with the following configuration stored in my `apps/vaultwarden/borg.nix` file:
```nix
{ config, pkgs, ... }:
{
        imports = [
          ../../lib/borg.nix
        ];
        users.users.borg.openssh.authorizedKeys.keys = [
                ("command=\"borg serve --restrict-to-path /srv/borg/vaultwarden-db\",restrict " + (builtins.readFile ./borg-db.key.pub))
                ("command=\"borg serve --restrict-to-path /srv/borg/vaultwarden-storage\",restrict " + (builtins.readFile ./borg-storage.key.pub))
        ];
}
```

### PostgreSQL

My postgreSQL module defines the following global configuration:
```nix
{ config, lib, pkgs, ... }:
{
	networking.firewall.interfaces."podman0".allowedTCPPorts = [ 5432 ];
	services.postgresql = {
		enable = true;
		enableTCPIP = true;
		package = pkgs.postgresql_15;
		authentication = pkgs.lib.mkOverride 10 ''
			#type database  DBuser                 auth-method
			local all       all                    trust
			# podman
			host  all       all     10.88.0.0/16   scram-sha-256
		'';
	};
}
```

Since for now I am running nothing outside of containers on this server, I am trusting the unix socket connections. Depending on what you are doing you might want a stronger auth-method there.

### Nginx

My nginx module defines the following global configuration:
```nix
{ config, lib, pkgs, ... }:
{
	environment.etc = let permissions = { mode = "0400"; uid= config.ids.uids.nginx; }; in {
		"nginx/adyxax.org.crt" = permissions // { source = ../../01-legacy/adyxax.org.crt; };
		"nginx/adyxax.org.key" = permissions // { source = ../../01-legacy/adyxax.org.key; };
	};
	networking.firewall.allowedTCPPorts = [ 80 443 ];
	services.nginx = {
		clientMaxBodySize = "40M";
		enable = true;
		enableReload = true;
		recommendedGzipSettings = true;
		recommendedOptimisation = true;
		recommendedProxySettings = true;
	};
}
```

### Secrets

There are several secrets referenced in the configuration, these are all git-crypted files:
- argon-token.key
- borg-db.key
- borg-storage.key
- database-password.key

## Migration process

The first step is obviously to deploy this new configuration to the server, then I need to login and manually restore the backups.
```sh
make run host=myth.adyxax.org
```

The container will be failing because no password is set on the database user yet, so I stop it:
```sh
systemctl stop podman-vaultwarden
```

There are two backup jobs for vaultwarden: one for its storage and the second one for the database.
```sh
export BORG_RSH="ssh -i /etc/borg-vaultwarden-storage.key"
borg list ssh://borg@gcp.adyxax.org/srv/borg/vaultwarden-storage
borg extract ssh://borg@gcp.adyxax.org/srv/borg/vaultwarden-storage::dalinar-vaultwarden-storage-2023-11-19T00:00:01
mv srv/vaultwarden /srv/
```

```sh
export BORG_RSH="ssh -i /etc/borg-vaultwarden-db.key"
borg list ssh://borg@gcp.adyxax.org/srv/borg/vaultwarden-db
borg extract ssh://borg@gcp.adyxax.org/srv/borg/vaultwarden-db::dalinar-vaultwarden-db-2023-11-19T00:00:01
```

Restoring the data itself is done with the psql shell:
```sh
psql -h localhost -U postgres -d vaultwarden
ALTER USER vaultwarden WITH PASSWORD 'XXXXX';
\i tmp/vaultwarden.sql
```

Afterwards I clean up the database dump and restart vaultwarden:
```sh
rm -rf tmp/
systemctl start podman-vaultwarden
```

To wrap this up I migrate the DNS records to the new host, update my monitoring system and clean up the namespace on the k3s server.

## Conclusion

Automating things with nixos is satisfying, but it does not abstract all the sysadmin's work away.

I am not quite satisfied with my borg configuration entries. I should be able to write this more elegantly when I find the time, but it works.