summaryrefslogtreecommitdiffstats
path: root/nixos/tests/vaultwarden.nix
blob: 1304f2fa9b780c503e39715bd2836551b50c5414 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# These tests will:
#  * Set up a vaultwarden server
#  * Have Firefox use the web vault to create an account, log in, and save a password to the vault
#  * Have the bw cli log in and read that password from the vault
#
# Note that Firefox must be on the same machine as the server for WebCrypto APIs to be available (or HTTPS must be configured)
#
# The same tests should work without modification on the official bitwarden server, if we ever package that.

let
  certs = import ./common/acme/server/snakeoil-certs.nix;
  makeVaultwardenTest =
    name:
    {
      backend ? name,
      withClient ? true,
      testScript ? null,
    }:
    import ./make-test-python.nix (
      { lib, pkgs, ... }:
      let
        dbPassword = "please_dont_hack";
        userEmail = "meow@example.com";
        userPassword = "also_super_secret_ZJWpBKZi668QGt"; # Must be complex to avoid interstitial warning on the signup page
        storedPassword = "seeeecret";

        testRunner =
          pkgs.writers.writePython3Bin "test-runner"
            {
              libraries = [ pkgs.python3Packages.selenium ];
              flakeIgnore = [ "E501" ];
            }
            # python
            ''
              import shutil
              from selenium.webdriver.common.by import By
              from selenium.webdriver import Firefox
              from selenium.webdriver.firefox.options import Options
              from selenium.webdriver.firefox.service import Service
              from selenium.webdriver.support.ui import WebDriverWait
              from selenium.webdriver.support import expected_conditions as EC
              from selenium.common.exceptions import ElementClickInterceptedException


              def click_when_unobstructed(mark):
                  while True:
                      try:
                          wait.until(EC.element_to_be_clickable(mark)).click()
                          break
                      except ElementClickInterceptedException:
                          continue


              service = Service(shutil.which("geckodriver"))

              options = Options()
              options.add_argument('--headless')
              driver = Firefox(options=options, service=service)

              driver.implicitly_wait(20)
              driver.get('https://localhost/#/signup')

              wait = WebDriverWait(driver, 10)

              wait.until(EC.title_contains("Vaultwarden Web"))

              driver.find_element(By.CSS_SELECTOR, 'input#register-start_form_input_email').send_keys(
                  '${userEmail}'
              )
              driver.find_element(By.CSS_SELECTOR, 'input#register-start_form_input_name').send_keys(
                  'A Cat'
              )
              driver.find_element(By.XPATH, "//button[contains(., 'Continue')]").click()
              driver.find_element(By.CSS_SELECTOR, 'input#input-password-form_new-password').send_keys(
                  '${userPassword}'
              )
              driver.find_element(By.CSS_SELECTOR, 'input#input-password-form_new-password-confirm').send_keys(
                  '${userPassword}'
              )
              if driver.find_element(By.XPATH, '//input[@formcontrolname="checkForBreaches"]').is_selected():
                  driver.find_element(By.XPATH, '//input[@formcontrolname="checkForBreaches"]').click()

              driver.find_element(By.XPATH, "//button[contains(., 'Create account')]").click()

              wait.until_not(EC.title_contains("Set a strong password"))

              wait.until_not(EC.title_contains("Join organization"))

              # NOTE: When testing this locally, the Bitwarden browser extension must not be installed, otherwise this screen does not appear
              click_when_unobstructed((By.XPATH, "//button[contains(., 'Add it later')]"))

              click_when_unobstructed((By.XPATH, "//a[contains(., 'Skip to web app')]"))

              # Skip the tour on first login
              click_when_unobstructed((By.XPATH, "//button[contains(., 'Skip')]"))

              click_when_unobstructed((By.XPATH, "//button[contains(., 'New item')]"))

              driver.find_element(By.XPATH, '//input[@formcontrolname="name"]').send_keys(
                  'secrets'
              )
              driver.find_element(By.XPATH, '//input[@formcontrolname="password"]').send_keys(
                  '${storedPassword}'
              )

              driver.find_element(By.XPATH, "//button[contains(., 'Save')]").click()
            '';
      in
      {
        inherit name;

        meta = {
          maintainers = with pkgs.lib.maintainers; [
            dotlambda
            SuperSandro2000
          ];
        };

        nodes = {
          server =
            { pkgs, ... }:
            lib.mkMerge [
              {
                mysql = {
                  services.mysql = {
                    enable = true;
                    initialScript = pkgs.writeText "mysql-init.sql" ''
                      CREATE DATABASE bitwarden;
                      CREATE USER 'bitwardenuser'@'localhost' IDENTIFIED BY '${dbPassword}';
                      GRANT ALL ON `bitwarden`.* TO 'bitwardenuser'@'localhost';
                      FLUSH PRIVILEGES;
                    '';
                    package = pkgs.mariadb;
                  };

                  services.vaultwarden.config.databaseUrl = "mysql://bitwardenuser:${dbPassword}@localhost/bitwarden";

                  systemd.services.vaultwarden.after = [ "mysql.service" ];
                };

                postgresql = {
                  services.postgresql = {
                    enable = true;
                    ensureDatabases = [ "vaultwarden" ];
                    ensureUsers = [
                      {
                        name = "vaultwarden";
                        ensureDBOwnership = true;
                      }
                    ];
                  };

                  services.vaultwarden.config.databaseUrl = "postgresql:///vaultwarden?host=/run/postgresql";

                  systemd.services.vaultwarden.after = [ "postgresql.target" ];
                };

                sqlite = {
                  services.vaultwarden.backupDir = "/srv/backups/vaultwarden";

                  environment.systemPackages = [ pkgs.sqlite ];
                };
              }
              .${backend}

              {
                networking.hosts."::1" = [ certs.domain ];
                services.vaultwarden = {
                  enable = true;
                  dbBackend = backend;
                  config = {
                    rocketAddress = "::";
                    rocketPort = 8080;
                  };
                };
                services.nginx = {
                  enable = true;
                  virtualHosts."${certs.domain}" = {
                    sslCertificate = certs.${certs.domain}.cert;
                    sslCertificateKey = certs.${certs.domain}.key;
                    enableACME = false;
                    forceSSL = true;
                    locations."/" = {
                      proxyPass = "http://[::1]:8080";
                    };
                  };
                };

                networking.firewall.allowedTCPPorts = [
                  80
                  443
                ];

                environment.systemPackages = [
                  pkgs.firefox-unwrapped
                  pkgs.geckodriver
                  testRunner
                ];
              }
            ];
        }
        // lib.optionalAttrs withClient {
          client =
            {
              nodes,
              pkgs,
              ...
            }:
            {
              networking.hosts."${nodes.server.networking.primaryIPAddress}" = [ certs.domain ];
              environment.systemPackages = [ pkgs.bitwarden-cli ];
              security.pki.certificateFiles = [ certs.ca.cert ];
            };
        };

        testScript =
          if testScript != null then
            testScript
          else
            ''
              # import json

              start_all()
              server.wait_for_unit("vaultwarden.service")
              server.wait_for_open_port(443)

              with subtest("configure the cli"):
                  client.succeed("bw --nointeraction config server https://${certs.domain}")

              with subtest("can't login to nonexistent account"):
                  client.fail(
                      "bw --nointeraction --raw login ${userEmail} ${userPassword}"
                  )

              with subtest("use the web interface to sign up, log in, and save a password"):
                  server.succeed("PYTHONUNBUFFERED=1 systemd-cat -t test-runner test-runner")

              # Upstreams sees offline usage as a new feature...
              # https://github.com/bitwarden/clients/issues/18110
              # with subtest("log in with the cli"):
              #     key = client.succeed(
              #         "bw --nointeraction --raw login ${userEmail} ${userPassword}"
              #     ).strip()

              # with subtest("sync with the cli"):
              #     client.succeed(f"bw --nointeraction --raw --session {key} sync -f")

              # with subtest("get the password with the cli"):
              #     output = json.loads(client.succeed(f"bw --nointeraction --raw --session {key} list items"))

              #     assert output[0]['login']['password'] == "${storedPassword}"
            '';
      }
    );
in
builtins.mapAttrs (k: v: makeVaultwardenTest k v) {
  mysql = { };
  postgresql = { };
  sqlite = { };
  sqlite-backup = {
    backend = "sqlite";
    withClient = false;

    testScript = ''
      start_all()
      server.wait_for_unit("vaultwarden.service")
      server.wait_for_open_port(443)

      with subtest("Set up vaultwarden"):
          server.succeed("PYTHONUNBUFFERED=1 test-runner | systemd-cat -t test-runner")

      with subtest("Run the backup script"):
          server.start_job("backup-vaultwarden.service")

      with subtest("Check that backup exists"):
          server.succeed('[ -d "/srv/backups/vaultwarden" ]')
          server.succeed('[ -f "/srv/backups/vaultwarden/db.sqlite3" ]')
          server.succeed('[ -f "/srv/backups/vaultwarden/rsa_key.pem" ]')
          # Ensure only the db backed up with the backup command exists and not the other db files.
          server.succeed('[ ! -f "/srv/backups/vaultwarden/db.sqlite3-shm" ]')
    '';
  };
}