about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/security/bitwarden-directory-connector-cli.nix
blob: 18c02e22fd7e665ee8cf14b8e3695687dbe053e3 (plain) (blame)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
{
  config,
  lib,
  pkgs,
  ...
}:
with lib; let
  cfg = config.services.bitwarden-directory-connector-cli;
in {
  options.services.bitwarden-directory-connector-cli = {
    enable = mkEnableOption "Bitwarden Directory Connector";

    package = mkPackageOption pkgs "bitwarden-directory-connector-cli" {};

    domain = mkOption {
      type = types.str;
      description = lib.mdDoc "The domain the Bitwarden/Vaultwarden is accessible on.";
      example = "https://vaultwarden.example.com";
    };

    user = mkOption {
      type = types.str;
      description = lib.mdDoc "User to run the program.";
      default = "bwdc";
    };

    interval = mkOption {
      type = types.str;
      default = "*:0,15,30,45";
      description = lib.mdDoc "The interval when to run the connector. This uses systemd's OnCalendar syntax.";
    };

    ldap = mkOption {
      description = lib.mdDoc ''
        Options to configure the LDAP connection.
        If you used the desktop application to test the configuration you can find the settings by searching for `ldap` in `~/.config/Bitwarden\ Directory\ Connector/data.json`.
      '';
      default = {};
      type = types.submodule ({
        config,
        options,
        ...
      }: {
        freeformType = types.attrsOf (pkgs.formats.json {}).type;

        config.finalJSON = builtins.toJSON (removeAttrs config (filter (x: x == "finalJSON" || ! options.${x}.isDefined or false) (attrNames options)));

        options = {
          finalJSON = mkOption {
            type = (pkgs.formats.json {}).type;
            internal = true;
            readOnly = true;
            visible = false;
          };

          ssl = mkOption {
            type = types.bool;
            default = false;
            description = lib.mdDoc "Whether to use TLS.";
          };
          startTls = mkOption {
            type = types.bool;
            default = false;
            description = lib.mdDoc "Whether to use STARTTLS.";
          };

          hostname = mkOption {
            type = types.str;
            description = lib.mdDoc "The host the LDAP is accessible on.";
            example = "ldap.example.com";
          };

          port = mkOption {
            type = types.port;
            default = 389;
            description = lib.mdDoc "Port LDAP is accessible on.";
          };

          ad = mkOption {
            type = types.bool;
            default = false;
            description = lib.mdDoc "Whether the LDAP Server is an Active Directory.";
          };

          pagedSearch = mkOption {
            type = types.bool;
            default = false;
            description = lib.mdDoc "Whether the LDAP server paginates search results.";
          };

          rootPath = mkOption {
            type = types.str;
            description = lib.mdDoc "Root path for LDAP.";
            example = "dc=example,dc=com";
          };

          username = mkOption {
            type = types.str;
            description = lib.mdDoc "The user to authenticate as.";
            example = "cn=admin,dc=example,dc=com";
          };
        };
      });
    };

    sync = mkOption {
      description = lib.mdDoc ''
        Options to configure what gets synced.
        If you used the desktop application to test the configuration you can find the settings by searching for `sync` in `~/.config/Bitwarden\ Directory\ Connector/data.json`.
      '';
      default = {};
      type = types.submodule ({
        config,
        options,
        ...
      }: {
        freeformType = types.attrsOf (pkgs.formats.json {}).type;

        config.finalJSON = builtins.toJSON (removeAttrs config (filter (x: x == "finalJSON" || ! options.${x}.isDefined or false) (attrNames options)));

        options = {
          finalJSON = mkOption {
            type = (pkgs.formats.json {}).type;
            internal = true;
            readOnly = true;
            visible = false;
          };

          removeDisabled = mkOption {
            type = types.bool;
            default = true;
            description = lib.mdDoc "Remove users from bitwarden groups if no longer in the ldap group.";
          };

          overwriteExisting = mkOption {
            type = types.bool;
            default = false;
            description =
              lib.mdDoc "Remove and re-add users/groups, See https://bitwarden.com/help/user-group-filters/#overwriting-syncs for more details.";
          };

          largeImport = mkOption {
            type = types.bool;
            default = false;
            description = lib.mdDoc "Enable if you are syncing more than 2000 users/groups.";
          };

          memberAttribute = mkOption {
            type = types.str;
            description = lib.mdDoc "Attribute that lists members in a LDAP group.";
            example = "uniqueMember";
          };

          creationDateAttribute = mkOption {
            type = types.str;
            description = lib.mdDoc "Attribute that lists a user's creation date.";
            example = "whenCreated";
          };

          useEmailPrefixSuffix = mkOption {
            type = types.bool;
            default = false;
            description = lib.mdDoc "If a user has no email address, combine a username prefix with a suffix value to form an email.";
          };
          emailPrefixAttribute = mkOption {
            type = types.str;
            description = lib.mdDoc "The attribute that contains the users username.";
            example = "accountName";
          };
          emailSuffix = mkOption {
            type = types.str;
            description = lib.mdDoc "Suffix for the email, normally @example.com.";
            example = "@example.com";
          };

          users = mkOption {
            type = types.bool;
            default = false;
            description = lib.mdDoc "Sync users.";
          };
          userPath = mkOption {
            type = types.str;
            description = lib.mdDoc "User directory, relative to root.";
            default = "ou=users";
          };
          userObjectClass = mkOption {
            type = types.str;
            description = lib.mdDoc "Class that users must have.";
            default = "inetOrgPerson";
          };
          userEmailAttribute = mkOption {
            type = types.str;
            description = lib.mdDoc "Attribute for a users email.";
            default = "mail";
          };
          userFilter = mkOption {
            type = types.str;
            description = lib.mdDoc "LDAP filter for users.";
            example = "(memberOf=cn=sales,ou=groups,dc=example,dc=com)";
            default = "";
          };

          groups = mkOption {
            type = types.bool;
            default = false;
            description = lib.mdDoc "Whether to sync ldap groups into BitWarden.";
          };
          groupPath = mkOption {
            type = types.str;
            description = lib.mdDoc "Group directory, relative to root.";
            default = "ou=groups";
          };
          groupObjectClass = mkOption {
            type = types.str;
            description = lib.mdDoc "A class that groups will have.";
            default = "groupOfNames";
          };
          groupNameAttribute = mkOption {
            type = types.str;
            description = lib.mdDoc "Attribute for a name of group.";
            default = "cn";
          };
          groupFilter = mkOption {
            type = types.str;
            description = lib.mdDoc "LDAP filter for groups.";
            example = "(cn=sales)";
            default = "";
          };
        };
      });
    };

    secrets = {
      ldap = mkOption {
        type = types.str;
        description = "Path to file that contains LDAP password for user in {option}`ldap.username";
      };

      bitwarden = {
        client_path_id = mkOption {
          type = types.str;
          description = "Path to file that contains Client ID.";
        };
        client_path_secret = mkOption {
          type = types.str;
          description = "Path to file that contains Client Secret.";
        };
      };
    };
  };

  config = mkIf cfg.enable {
    users.groups."${cfg.user}" = {};
    users.users."${cfg.user}" = {
      isSystemUser = true;
      group = cfg.user;
    };

    systemd = {
      timers.bitwarden-directory-connector-cli = {
        description = "Sync timer for Bitwarden Directory Connector";
        wantedBy = ["timers.target"];
        after = ["network-online.target"];
        timerConfig = {
          OnCalendar = cfg.interval;
          Unit = "bitwarden-directory-connector-cli.service";
          Persistent = true;
        };
      };

      services.bitwarden-directory-connector-cli = {
        description = "Main process for Bitwarden Directory Connector";
        path = [pkgs.jq];

        environment = {
          BITWARDENCLI_CONNECTOR_APPDATA_DIR = "/tmp";
          BITWARDENCLI_CONNECTOR_PLAINTEXT_SECRETS = "true";
        };

        serviceConfig = {
          Type = "oneshot";
          User = "${cfg.user}";
          PrivateTmp = true;
          preStart = ''
            set -eo pipefail

            # create the config file
            ${lib.getExe cfg.package} data-file
            touch /tmp/data.json.tmp
            chmod 600 /tmp/data.json{,.tmp}

            ${lib.getExe cfg.package} config server ${cfg.domain}

            # now login to set credentials
            export BW_CLIENTID="$(< ${escapeShellArg cfg.secrets.bitwarden.client_path_id})"
            export BW_CLIENTSECRET="$(< ${escapeShellArg cfg.secrets.bitwarden.client_path_secret})"
            ${lib.getExe cfg.package} login

            jq '.authenticatedAccounts[0] as $account
              | .[$account].directoryConfigurations.ldap |= $ldap_data
              | .[$account].directorySettings.organizationId |= $orgID
              | .[$account].directorySettings.sync |= $sync_data' \
              --argjson ldap_data ${escapeShellArg cfg.ldap.finalJSON} \
              --arg orgID "''${BW_CLIENTID//organization.}" \
              --argjson sync_data ${escapeShellArg cfg.sync.finalJSON} \
              /tmp/data.json \
              > /tmp/data.json.tmp

            mv -f /tmp/data.json.tmp /tmp/data.json

            # final config
            ${lib.getExe cfg.package} config directory 0
            ${lib.getExe cfg.package} config ldap.password --secretfile ${cfg.secrets.ldap}
          '';

          ExecStart = "${lib.getExe cfg.package} sync";
        };
      };
    };
  };

  meta.maintainers = with maintainers; [Silver-Golden];
}