22
0
mirror of https://github.com/SuperSandro2000/nixos-modules.git synced 2024-06-11 02:34:05 +02:00
nixos-modules/modules/portunus.nix

173 lines
6.1 KiB
Nix
Raw Normal View History

2023-12-24 19:38:04 +01:00
{ config, lib, options, pkgs, ... }:
let
cfg = config.services.portunus;
inherit (config.security) ldap;
in
{
options.services.portunus = {
2023-03-25 16:23:48 +01:00
# maybe based on $service.ldap.enable && services.portunus.enable?
2023-01-17 00:23:31 +01:00
addToHosts = lib.mkOption {
type = lib.types.bool;
default = false;
2023-01-17 02:14:18 +01:00
description = lib.mdDoc "Whether to add a hosts entry for the portunus domain pointing to externalIp";
2023-01-17 00:23:31 +01:00
};
2023-12-05 02:52:30 +01:00
configureOAuth2Proxy = lib.mkOption {
type = lib.types.bool;
default = false;
description = lib.mdDoc ''
Wether to configure OAuth2 Proxy with Portunus' Dex.
Use `services.oauth2_proxy.nginx.virtualHosts` to configure the nginx virtual hosts that should require authentication.
2023-12-24 19:38:04 +01:00
To properly function this requires the services.oauth2_proxy.nginx.domain option from <https://github.com/NixOS/nixpkgs/pull/273234>.
2023-12-05 02:52:30 +01:00
'';
};
2023-02-23 00:34:37 +01:00
internalIp4 = lib.mkOption {
type = with lib.types; nullOr str;
default = null;
description = lib.mdDoc "Internal IPv4 of portunus instance. This is used in the addToHosts option.";
};
2023-02-23 00:34:37 +01:00
internalIp6 = lib.mkOption {
type = with lib.types; nullOr str;
default = null;
description = lib.mdDoc "Internal IPv6 of portunus instance. This is used in the addToHosts option.";
};
2023-01-17 00:23:31 +01:00
ldapPreset = lib.mkOption {
type = lib.types.bool;
2023-01-17 00:56:46 +01:00
default = false;
2023-01-17 02:14:18 +01:00
description = lib.mdDoc "Whether to set config.security.ldap to portunus specific settings.";
};
removeAddGroup = lib.mkOption {
type = lib.types.bool;
default = false;
description = lib.mdDoc "When enabled, remove the function to add new Groups via the web ui, to enforce seeding usage.";
};
seedGroups = lib.mkOption {
type = lib.types.bool;
default = false;
description = lib.mdDoc "Wether to seed groups configured in services as not member managed groups.";
};
# TODO: upstream to nixos
2024-01-06 02:32:04 +01:00
# https://github.com/NixOS/nixpkgs/pull/279050
seedSettings = lib.mkOption {
type = with lib.types; nullOr (attrsOf (listOf (attrsOf anything)));
default = null;
description = lib.mdDoc ''
Seed settings for users and grousp.
See upstream for format <https://github.com/majewsky/portunus#seeding-users-and-groups-from-static-configuration>
'';
};
};
config = {
2023-12-05 02:52:30 +01:00
assertions = [
{
assertion = cfg.configureOAuth2Proxy -> config.services.oauth2_proxy.keyFile != null;
message = ''
Setting services.portunus.configureOAuth2Proxy to true requires to set service.oauth2_proxy.keyFile
to a file that contains `OAUTH2_PROXY_CLIENT_SECRET` and `OAUTH2_PROXY_COOKIE_SECRET`.
'';
}
2024-01-06 02:32:11 +01:00
{
assertion = lib.versionAtLeast config.services.portunus.package.version "2.0.0";
message = "Portunus 2.0.0 is required for this module!";
}
2023-12-05 02:52:30 +01:00
];
2023-01-17 00:23:31 +01:00
networking.hosts = lib.mkIf cfg.addToHosts {
2023-02-23 00:34:37 +01:00
${cfg.internalIp4} = [ cfg.domain ];
${cfg.internalIp6} = [ cfg.domain ];
2023-01-17 00:23:31 +01:00
};
nixpkgs.overlays = lib.mkIf cfg.enable [
2023-04-04 20:52:33 +02:00
(final: prev: with final; {
dex-oidc = prev.dex-oidc.override {
buildGoModule = args: buildGoModule (args // {
patches = args.patches or [ ] ++ [
# remember session
(fetchpatch {
2023-12-03 23:38:13 +01:00
url = "https://github.com/SuperSandro2000/dex/commit/d2fb6cdf8188e6973721ddac657a7c5d3daf6955.patch";
hash = "sha256-PKC7jsNyFN28qFZ7SLYgnd0s09G2cb+vBeFvRzyyLGQ=";
})
# Complain if the env set in SecretEnv cannot be found
(fetchpatch {
url = "https://github.com/dexidp/dex/commit/f25f72053c9282cfe22521cd508698a07dc5190f.patch";
hash = "sha256-dyo+UPpceHxL3gcBQaGaDAHJqmysDJw051gMG1aeh5o=";
})
];
vendorHash = "sha256-YIi67pPIcVndIjWk94ckv6X4WLELUe/J/03e+XWIdHE=";
});
};
2024-01-06 02:32:04 +01:00
portunus = prev.portunus.overrideAttrs ({ patches ? [ ], ... }: {
2023-11-26 22:31:32 +01:00
patches = patches
++ lib.optional cfg.removeAddGroup ./portunus-remove-add-group.diff;
2023-04-04 00:05:25 +02:00
});
})
];
2023-12-05 02:52:30 +01:00
services = let
callbackURL = "https://${cfg.domain}/oauth2/callback";
2023-12-10 00:55:54 +01:00
clientID = "oauth2_proxy"; # - is not allowed in environment variables
2023-12-05 02:52:30 +01:00
in {
dex = {
enable = lib.mkIf cfg.configureOAuth2Proxy true;
# the user has no other option to accept this and all clients are internal anyway
settings.oauth2.skipApprovalScreen = true;
};
oauth2_proxy = lib.mkIf cfg.configureOAuth2Proxy {
enable = true;
2023-12-10 00:55:54 +01:00
inherit clientID;
2023-12-26 00:06:56 +01:00
nginx = lib.optionalAttrs (options.services.oauth2-proxy.nginx.domain or null != null) {
inherit (config.services.portunus) domain;
};
2023-12-05 02:52:30 +01:00
provider = "oidc";
redirectURL = callbackURL;
reverseProxy = true;
upstream = "http://127.0.0.1:4181";
extraConfig = {
oidc-issuer-url = config.services.dex.settings.issuer;
provider-display-name = "Portunus";
};
};
portunus = {
dex.oidcClients = lib.mkIf cfg.configureOAuth2Proxy [{
inherit callbackURL;
2023-12-10 00:55:54 +01:00
id = clientID;
2023-12-05 02:52:30 +01:00
}];
seedPath = pkgs.writeText "seed.json" (builtins.toJSON cfg.seedSettings);
};
2023-12-03 23:19:30 +01:00
};
2023-01-17 00:23:31 +01:00
security.ldap = lib.mkIf cfg.ldapPreset {
domainName = cfg.domain;
2023-03-17 01:50:30 +01:00
givenNameField = "givenName";
groupFilter = group: "(&(objectclass=person)(isMemberOf=cn=${group},${ldap.roleBaseDN}))";
2023-03-17 01:50:30 +01:00
mailField = "mail";
port = 636;
2023-01-17 00:23:31 +01:00
roleBaseDN = "ou=groups";
roleField = "cn";
roleFilter = "(&(objectclass=groupOfNames)(member=%s))";
roleValue = "dn";
2023-07-02 19:30:24 +02:00
searchFilterWithGroupFilter = userFilterGroup: userFilter: if (userFilterGroup != null) then "(&${ldap.groupFilter userFilterGroup}${userFilter})" else userFilter;
2023-03-18 01:24:55 +01:00
sshPublicKeyField = "sshPublicKey";
2023-01-17 00:23:31 +01:00
searchUID = "search";
2023-03-17 01:50:30 +01:00
surnameField = "sn";
2023-01-17 00:23:31 +01:00
userField = "uid";
userFilter = replaceStr: "(&(objectclass=person)(|(uid=${replaceStr})(mail=${replaceStr})))";
2023-01-17 00:23:31 +01:00
userBaseDN = "ou=users";
};
};
}