2023-11-06 04:16:33 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
2023-11-25 22:41:48 +00:00
|
|
|
pkgs,
|
2023-11-06 04:16:33 +00:00
|
|
|
...
|
|
|
|
}:
|
|
|
|
with lib; let
|
2023-11-25 22:41:48 +00:00
|
|
|
cfg = config.services.bitwarden-directory-connector;
|
2023-11-06 04:16:33 +00:00
|
|
|
in {
|
2023-11-25 22:41:48 +00:00
|
|
|
options.services.bitwarden-directory-connector = {
|
2023-11-06 04:16:33 +00:00
|
|
|
enable = mkEnableOption "Bitwarden Directory Connector";
|
|
|
|
|
2023-11-25 22:41:48 +00:00
|
|
|
package = mkPackageOption pkgs "bitwarden-directory-connector" {};
|
2023-11-12 03:27:30 +00:00
|
|
|
|
2023-11-06 04:16:33 +00:00
|
|
|
domain = mkOption {
|
|
|
|
type = types.str;
|
2023-11-25 22:41:48 +00:00
|
|
|
description = lib.mdDoc "The domain the Bitwarden/Vaultwarden is accessible on.";
|
2023-11-06 04:16:33 +00:00
|
|
|
example = "https://vaultwarden.example.com";
|
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = lib.mdDoc "User to run the program.";
|
|
|
|
default = "bwdc";
|
|
|
|
};
|
|
|
|
|
2023-11-13 00:54:23 +00:00
|
|
|
interval = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "*:0,15,30,45";
|
2023-11-25 22:41:48 +00:00
|
|
|
description = lib.mdDoc "The interval when to run the connector. This uses systemd's OnCalendar syntax.";
|
2023-11-06 04:16:33 +00:00
|
|
|
};
|
|
|
|
|
2023-11-13 00:54:23 +00:00
|
|
|
ldap = mkOption {
|
2023-12-27 15:48:58 +00:00
|
|
|
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`.
|
|
|
|
'';
|
2023-11-25 22:41:48 +00:00
|
|
|
default = {};
|
|
|
|
|
|
|
|
type = types.submodule ({
|
|
|
|
config,
|
|
|
|
options,
|
|
|
|
...
|
|
|
|
}: {
|
2023-11-16 01:12:09 +00:00
|
|
|
freeformType = types.attrsOf (pkgs.formats.json {}).type;
|
2023-11-13 00:54:23 +00:00
|
|
|
|
2023-11-25 22:41:48 +00:00
|
|
|
config.finalJSON = builtins.toJSON (removeAttrs config (filter (x: x == "finalJSON" || ! options.${x}.isDefined or false) (attrNames options)));
|
|
|
|
|
2023-11-13 00:54:23 +00:00
|
|
|
options = {
|
2023-11-25 22:41:48 +00:00
|
|
|
finalJSON = mkOption {
|
|
|
|
type = (pkgs.formats.json {}).type;
|
|
|
|
internal = true;
|
|
|
|
readOnly = true;
|
|
|
|
visible = false;
|
|
|
|
};
|
|
|
|
|
2023-11-13 00:54:23 +00:00
|
|
|
ssl = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2023-11-25 22:41:48 +00:00
|
|
|
description = lib.mdDoc "Whether to use TLS.";
|
2023-11-13 00:54:23 +00:00
|
|
|
};
|
|
|
|
startTls = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2023-11-25 22:41:48 +00:00
|
|
|
description = lib.mdDoc "Whether to use STARTTLS.";
|
2023-11-13 00:54:23 +00:00
|
|
|
};
|
2023-11-25 22:41:48 +00:00
|
|
|
|
|
|
|
hostname = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = lib.mdDoc "The host the LDAP is accessible on.";
|
|
|
|
example = "ldap.example.com";
|
2023-11-13 00:54:23 +00:00
|
|
|
};
|
2023-11-25 22:41:48 +00:00
|
|
|
|
2023-11-13 00:54:23 +00:00
|
|
|
port = mkOption {
|
2023-11-25 22:41:48 +00:00
|
|
|
type = types.port;
|
2023-11-13 00:54:23 +00:00
|
|
|
default = 389;
|
2023-11-25 22:41:48 +00:00
|
|
|
description = lib.mdDoc "Port LDAP is accessible on.";
|
2023-11-13 00:54:23 +00:00
|
|
|
};
|
2023-11-25 22:41:48 +00:00
|
|
|
|
2023-11-13 00:54:23 +00:00
|
|
|
ad = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2023-11-25 22:41:48 +00:00
|
|
|
description = lib.mdDoc "Whether the LDAP Server is an Active Directory.";
|
2023-11-13 00:54:23 +00:00
|
|
|
};
|
2023-11-25 22:41:48 +00:00
|
|
|
|
2023-11-13 00:54:23 +00:00
|
|
|
pagedSearch = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2023-11-25 22:41:48 +00:00
|
|
|
description = lib.mdDoc "Whether the LDAP server paginates search results.";
|
2023-11-13 00:54:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
rootPath = mkOption {
|
|
|
|
type = types.str;
|
2023-11-25 22:41:48 +00:00
|
|
|
description = lib.mdDoc "Root path for LDAP.";
|
2023-11-13 00:54:23 +00:00
|
|
|
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";
|
|
|
|
};
|
2023-11-06 04:16:33 +00:00
|
|
|
};
|
2023-11-25 22:41:48 +00:00
|
|
|
});
|
2023-11-13 00:54:23 +00:00
|
|
|
};
|
2023-11-06 04:16:33 +00:00
|
|
|
|
2023-11-13 00:54:23 +00:00
|
|
|
sync = mkOption {
|
2023-12-27 15:48:58 +00:00
|
|
|
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`.
|
|
|
|
'';
|
2023-11-25 22:41:48 +00:00
|
|
|
default = {};
|
|
|
|
|
|
|
|
type = types.submodule ({
|
|
|
|
config,
|
|
|
|
options,
|
|
|
|
...
|
|
|
|
}: {
|
2023-11-16 01:12:09 +00:00
|
|
|
freeformType = types.attrsOf (pkgs.formats.json {}).type;
|
2023-11-13 00:54:23 +00:00
|
|
|
|
2023-11-25 22:41:48 +00:00
|
|
|
config.finalJSON = builtins.toJSON (removeAttrs config (filter (x: x == "finalJSON" || ! options.${x}.isDefined or false) (attrNames options)));
|
|
|
|
|
2023-11-13 00:54:23 +00:00
|
|
|
options = {
|
2023-11-25 22:41:48 +00:00
|
|
|
finalJSON = mkOption {
|
|
|
|
type = (pkgs.formats.json {}).type;
|
|
|
|
internal = true;
|
|
|
|
readOnly = true;
|
|
|
|
visible = false;
|
|
|
|
};
|
|
|
|
|
2023-11-13 00:54:23 +00:00
|
|
|
removeDisabled = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = lib.mdDoc "Remove users from bitwarden groups if no longer in the ldap group.";
|
|
|
|
};
|
2023-11-25 22:41:48 +00:00
|
|
|
|
2023-11-13 00:54:23 +00:00
|
|
|
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.";
|
|
|
|
};
|
2023-11-25 22:41:48 +00:00
|
|
|
|
2023-11-13 00:54:23 +00:00
|
|
|
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;
|
2023-11-25 22:41:48 +00:00
|
|
|
description = lib.mdDoc "Attribute that lists a user's creation date.";
|
2023-11-13 00:54:23 +00:00
|
|
|
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;
|
2023-11-25 22:41:48 +00:00
|
|
|
description = lib.mdDoc "The attribute that contains the users username.";
|
|
|
|
example = "accountName";
|
2023-11-13 00:54:23 +00:00
|
|
|
};
|
|
|
|
emailSuffix = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = lib.mdDoc "Suffix for the email, normally @example.com.";
|
2023-11-25 22:41:48 +00:00
|
|
|
example = "@example.com";
|
2023-11-13 00:54:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2023-11-25 22:41:48 +00:00
|
|
|
description = lib.mdDoc "Class that users must have.";
|
2023-11-13 00:54:23 +00:00
|
|
|
default = "inetOrgPerson";
|
|
|
|
};
|
|
|
|
userEmailAttribute = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = lib.mdDoc "Attribute for a users email.";
|
|
|
|
default = "mail";
|
|
|
|
};
|
|
|
|
userFilter = mkOption {
|
|
|
|
type = types.str;
|
2023-11-25 22:41:48 +00:00
|
|
|
description = lib.mdDoc "LDAP filter for users.";
|
2023-11-13 00:54:23 +00:00
|
|
|
example = "(memberOf=cn=sales,ou=groups,dc=example,dc=com)";
|
|
|
|
default = "";
|
|
|
|
};
|
|
|
|
|
|
|
|
groups = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2023-11-25 22:41:48 +00:00
|
|
|
description = lib.mdDoc "Whether to sync ldap groups into BitWarden.";
|
2023-11-13 00:54:23 +00:00
|
|
|
};
|
|
|
|
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;
|
2023-11-25 22:41:48 +00:00
|
|
|
description = lib.mdDoc "LDAP filter for groups.";
|
2023-11-13 00:54:23 +00:00
|
|
|
example = "(cn=sales)";
|
|
|
|
default = "";
|
|
|
|
};
|
2023-11-06 04:16:33 +00:00
|
|
|
};
|
2023-11-25 22:41:48 +00:00
|
|
|
});
|
2023-11-06 04:16:33 +00:00
|
|
|
};
|
|
|
|
|
2023-11-18 03:03:35 +00:00
|
|
|
secrets = {
|
2023-11-25 22:41:48 +00:00
|
|
|
ldap = mkOption {
|
2023-11-06 04:16:33 +00:00
|
|
|
type = types.str;
|
2023-11-25 22:41:48 +00:00
|
|
|
description = "Path to file that contains LDAP password for user in {option}`ldap.username";
|
2023-11-06 04:16:33 +00:00
|
|
|
};
|
2023-11-25 22:41:48 +00:00
|
|
|
|
2023-11-18 03:03:35 +00:00
|
|
|
bitwarden = {
|
2023-11-25 22:41:48 +00:00
|
|
|
client_path_id = mkOption {
|
2023-11-18 03:03:35 +00:00
|
|
|
type = types.str;
|
|
|
|
description = "Path to file that contains Client ID.";
|
|
|
|
};
|
2023-11-25 22:41:48 +00:00
|
|
|
client_path_secret = mkOption {
|
2023-11-18 03:03:35 +00:00
|
|
|
type = types.str;
|
|
|
|
description = "Path to file that contains Client Secret.";
|
|
|
|
};
|
2023-11-06 04:16:33 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
users.groups."${cfg.user}" = {};
|
|
|
|
users.users."${cfg.user}" = {
|
|
|
|
isSystemUser = true;
|
2023-11-25 22:41:48 +00:00
|
|
|
group = cfg.user;
|
2023-11-06 04:16:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd = {
|
2023-12-27 15:48:58 +00:00
|
|
|
timers.bitwarden-directory-connector = {
|
2023-11-12 03:27:30 +00:00
|
|
|
description = "Sync timer for Bitwarden Directory Connector";
|
2023-11-06 04:16:33 +00:00
|
|
|
wantedBy = ["timers.target"];
|
2023-11-25 22:41:48 +00:00
|
|
|
after = ["network-online.target"];
|
2023-11-06 04:16:33 +00:00
|
|
|
timerConfig = {
|
2023-11-13 00:54:23 +00:00
|
|
|
OnCalendar = cfg.interval;
|
2023-12-27 15:48:58 +00:00
|
|
|
Unit = "bitwarden-directory-connector.service";
|
2023-11-06 04:16:33 +00:00
|
|
|
Persistent = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-12-27 15:48:58 +00:00
|
|
|
services.bitwarden-directory-connector = {
|
2023-11-06 04:16:33 +00:00
|
|
|
description = "Main process for Bitwarden Directory Connector";
|
2023-11-12 21:58:27 +00:00
|
|
|
path = [pkgs.jq];
|
2023-11-06 04:16:33 +00:00
|
|
|
|
|
|
|
environment = {
|
2023-11-25 22:41:48 +00:00
|
|
|
BITWARDENCLI_CONNECTOR_APPDATA_DIR = "/tmp";
|
2023-11-06 04:16:33 +00:00
|
|
|
BITWARDENCLI_CONNECTOR_PLAINTEXT_SECRETS = "true";
|
|
|
|
};
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
User = "${cfg.user}";
|
2023-11-25 22:41:48 +00:00
|
|
|
PrivateTmp = true;
|
2023-12-27 15:48:58 +00:00
|
|
|
preStart = ''
|
2023-11-25 22:41:48 +00:00
|
|
|
set -eo pipefail
|
|
|
|
|
2023-11-06 04:16:33 +00:00
|
|
|
# create the config file
|
2023-11-25 22:41:48 +00:00
|
|
|
${lib.getExe cfg.package} data-file
|
|
|
|
touch /tmp/data.json.tmp
|
|
|
|
chmod 600 /tmp/data.json{,.tmp}
|
2023-11-06 04:16:33 +00:00
|
|
|
|
2023-11-25 22:41:48 +00:00
|
|
|
${lib.getExe cfg.package} config server ${cfg.domain}
|
2023-11-06 04:16:33 +00:00
|
|
|
|
|
|
|
# now login to set credentials
|
2023-11-18 03:03:35 +00:00
|
|
|
export BW_CLIENTID="$(< ${escapeShellArg cfg.secrets.bitwarden.client_path_id})"
|
|
|
|
export BW_CLIENTSECRET="$(< ${escapeShellArg cfg.secrets.bitwarden.client_path_secret})"
|
2023-11-25 22:41:48 +00:00
|
|
|
${lib.getExe cfg.package} login
|
2023-11-06 04:16:33 +00:00
|
|
|
|
2023-11-13 00:54:23 +00:00
|
|
|
jq '.authenticatedAccounts[0] as $account
|
|
|
|
| .[$account].directoryConfigurations.ldap |= $ldap_data
|
|
|
|
| .[$account].directorySettings.organizationId |= $orgID
|
|
|
|
| .[$account].directorySettings.sync |= $sync_data' \
|
2023-11-25 22:41:48 +00:00
|
|
|
--argjson ldap_data ${escapeShellArg cfg.ldap.finalJSON} \
|
2023-11-13 00:54:23 +00:00
|
|
|
--arg orgID "''${BW_CLIENTID//organization.}" \
|
2023-11-25 22:41:48 +00:00
|
|
|
--argjson sync_data ${escapeShellArg cfg.sync.finalJSON} \
|
|
|
|
/tmp/data.json \
|
|
|
|
> /tmp/data.json.tmp
|
2023-11-12 21:58:27 +00:00
|
|
|
|
2023-11-25 22:41:48 +00:00
|
|
|
mv -f /tmp/data.json.tmp /tmp/data.json
|
2023-11-06 04:16:33 +00:00
|
|
|
|
|
|
|
# final config
|
2023-11-25 22:41:48 +00:00
|
|
|
${lib.getExe cfg.package} config directory 0
|
|
|
|
${lib.getExe cfg.package} config ldap.password --secretfile ${cfg.secrets.ldap}
|
2023-11-06 04:16:33 +00:00
|
|
|
'';
|
|
|
|
|
2023-11-25 22:41:48 +00:00
|
|
|
ExecStart = "${lib.getExe cfg.package} sync";
|
2023-11-06 04:16:33 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2023-11-12 03:27:30 +00:00
|
|
|
|
2023-11-25 22:41:48 +00:00
|
|
|
meta.maintainers = with maintainers; [Silver-Golden];
|
2023-11-06 04:16:33 +00:00
|
|
|
}
|