nixpkgs/nixos/modules/services/web-apps/peering-manager.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

368 lines
12 KiB
Nix
Raw Normal View History

2022-12-01 16:31:48 +00:00
{ config, lib, pkgs, buildEnv, ... }:
let
cfg = config.services.peering-manager;
pythonFmt = pkgs.formats.pythonVars {};
settingsFile = pythonFmt.generate "peering-manager-settings.py" cfg.settings;
extraConfigFile = pkgs.writeTextFile {
name = "peering-manager-extraConfig.py";
text = cfg.extraConfig;
2022-12-01 16:31:48 +00:00
};
configFile = pkgs.concatText "configuration.py" [ settingsFile extraConfigFile ];
2022-12-01 16:31:48 +00:00
pkg = (pkgs.peering-manager.overrideAttrs (old: {
postInstall = ''
ln -s ${configFile} $out/opt/peering-manager/peering_manager/configuration.py
'' + lib.optionalString cfg.enableLdap ''
2022-12-01 16:31:48 +00:00
ln -s ${cfg.ldapConfigPath} $out/opt/peering-manager/peering_manager/ldap_config.py
'' + lib.optionalString cfg.enableOidc ''
ln -s ${cfg.oidcConfigPath} $out/opt/peering-manager/peering_manager/oidc_config.py
2022-12-01 16:31:48 +00:00
'';
})).override {
inherit (cfg) plugins;
};
peeringManagerManageScript = pkgs.writeScriptBin "peering-manager-manage" ''
#!${pkgs.stdenv.shell}
2022-12-01 16:31:48 +00:00
export PYTHONPATH=${pkg.pythonPath}
sudo -u peering-manager ${pkg}/bin/peering-manager "$@"
'';
2022-12-01 16:31:48 +00:00
in {
options.services.peering-manager = with lib; {
2022-12-01 16:31:48 +00:00
enable = mkOption {
type = types.bool;
2022-12-01 16:31:48 +00:00
default = false;
description = ''
2022-12-01 16:31:48 +00:00
Enable Peering Manager.
This module requires a reverse proxy that serves `/static` separately.
See this [example](https://github.com/peering-manager/contrib/blob/main/nginx.conf on how to configure this.
'';
};
enableScheduledTasks = mkOption {
type = types.bool;
default = true;
description = ''
Set up [scheduled tasks](https://peering-manager.readthedocs.io/en/stable/setup/8-scheduled-tasks/)
2022-12-01 16:31:48 +00:00
'';
};
listenAddress = mkOption {
type = types.str;
default = "[::1]";
description = ''
2022-12-01 16:31:48 +00:00
Address the server will listen on.
'';
};
port = mkOption {
type = types.port;
default = 8001;
description = ''
2022-12-01 16:31:48 +00:00
Port the server will listen on.
'';
};
plugins = mkOption {
type = types.functionTo (types.listOf types.package);
default = _: [];
defaultText = literalExpression ''
python3Packages: with python3Packages; [];
'';
description = ''
2022-12-01 16:31:48 +00:00
List of plugin packages to install.
'';
};
secretKeyFile = mkOption {
type = types.path;
description = ''
2022-12-01 16:31:48 +00:00
Path to a file containing the secret key.
'';
};
peeringdbApiKeyFile = mkOption {
type = with types; nullOr path;
default = null;
description = ''
2022-12-01 16:31:48 +00:00
Path to a file containing the PeeringDB API key.
'';
};
settings = lib.mkOption {
description = ''
Configuration options to set in `configuration.py`.
See the [documentation](https://peering-manager.readthedocs.io/en/stable/configuration/optional-settings/) for more possible options.
'';
default = { };
type = lib.types.submodule {
freeformType = pythonFmt.type;
options = {
ALLOWED_HOSTS = lib.mkOption {
type = with lib.types; listOf str;
default = ["*"];
description = ''
A list of valid fully-qualified domain names (FQDNs) and/or IP
addresses that can be used to reach the peering manager service.
'';
};
};
};
};
2022-12-01 16:31:48 +00:00
extraConfig = mkOption {
type = types.lines;
default = "";
description = ''
2022-12-01 16:31:48 +00:00
Additional lines of configuration appended to the `configuration.py`.
See the [documentation](https://peering-manager.readthedocs.io/en/stable/configuration/optional-settings/) for more possible options.
'';
};
enableLdap = mkOption {
type = types.bool;
default = false;
description = ''
2022-12-01 16:31:48 +00:00
Enable LDAP-Authentication for Peering Manager.
This requires a configuration file being pass through `ldapConfigPath`.
'';
};
ldapConfigPath = mkOption {
type = types.path;
description = ''
2022-12-18 00:31:14 +00:00
Path to the Configuration-File for LDAP-Authentication, will be loaded as `ldap_config.py`.
2022-12-01 16:31:48 +00:00
See the [documentation](https://peering-manager.readthedocs.io/en/stable/setup/6-ldap/#configuration) for possible options.
'';
};
enableOidc = mkOption {
type = types.bool;
default = false;
description = ''
Enable OIDC-Authentication for Peering Manager.
This requires a configuration file being pass through `oidcConfigPath`.
'';
};
oidcConfigPath = mkOption {
type = types.path;
description = ''
Path to the Configuration-File for OIDC-Authentication, will be loaded as `oidc_config.py`.
See the [documentation](https://peering-manager.readthedocs.io/en/stable/setup/6b-oidc/#configuration) for possible options.
'';
};
2022-12-01 16:31:48 +00:00
};
config = lib.mkIf cfg.enable {
services.peering-manager = {
settings = {
DATABASE = {
NAME = "peering-manager";
USER = "peering-manager";
HOST = "/run/postgresql";
};
# Redis database settings. Redis is used for caching and for queuing background tasks such as webhook events. A separate
# configuration exists for each. Full connection details are required in both sections, and it is strongly recommended
# to use two separate database IDs.
REDIS = {
tasks = {
UNIX_SOCKET_PATH = config.services.redis.servers.peering-manager.unixSocket;
DATABASE = 0;
};
caching = {
UNIX_SOCKET_PATH = config.services.redis.servers.peering-manager.unixSocket;
DATABASE = 1;
};
};
};
extraConfig = ''
with open("${cfg.secretKeyFile}", "r") as file:
SECRET_KEY = file.readline()
'' + lib.optionalString (cfg.peeringdbApiKeyFile != null) ''
with open("${cfg.peeringdbApiKeyFile}", "r") as file:
PEERINGDB_API_KEY = file.readline()
'';
plugins = (ps:
(lib.optionals cfg.enableLdap [ ps.django-auth-ldap ]) ++
(lib.optionals cfg.enableOidc (with ps; [ mozilla-django-oidc pyopenssl josepy ]))
);
};
2022-12-01 16:31:48 +00:00
system.build.peeringManagerPkg = pkg;
services.redis.servers.peering-manager.enable = true;
services.postgresql = {
enable = true;
ensureDatabases = [ "peering-manager" ];
ensureUsers = [
{
name = "peering-manager";
nixos/postgresql: drop ensurePermissions, fix ensureUsers for postgresql15 Closes #216989 First of all, a bit of context: in PostgreSQL, newly created users don't have the CREATE privilege on the public schema of a database even with `ALL PRIVILEGES` granted via `ensurePermissions` which is how most of the DB users are currently set up "declaratively"[1]. This means e.g. a freshly deployed Nextcloud service will break early because Nextcloud itself cannot CREATE any tables in the public schema anymore. The other issue here is that `ensurePermissions` is a mere hack. It's effectively a mixture of SQL code (e.g. `DATABASE foo` is relying on how a value is substituted in a query. You'd have to parse a subset of SQL to actually know which object are permissions granted to for a user). After analyzing the existing modules I realized that in every case with a single exception[2] the UNIX system user is equal to the db user is equal to the db name and I don't see a compelling reason why people would change that in 99% of the cases. In fact, some modules would even break if you'd change that because the declarations of the system user & the db user are mixed up[3]. So I decided to go with something new which restricts the ways to use `ensure*` options rather than expanding those[4]. Effectively this means that * The DB user _must_ be equal to the DB name. * Permissions are granted via `ensureDBOwnerhip` for an attribute-set in `ensureUsers`. That way, the user is actually the owner and can perform `CREATE`. * For such a postgres user, a database must be declared in `ensureDatabases`. For anything else, a custom state management should be implemented. This can either be `initialScript`, doing it manual, outside of the module or by implementing proper state management for postgresql[5], but the current state of `ensure*` isn't even declarative, but a convergent tool which is what Nix actually claims to _not_ do. Regarding existing setups: there are effectively two options: * Leave everything as-is (assuming that system user == db user == db name): then the DB user will automatically become the DB owner and everything else stays the same. * Drop the `createDatabase = true;` declarations: nothing will change because a removal of `ensure*` statements is ignored, so it doesn't matter at all whether this option is kept after the first deploy (and later on you'd usually restore from backups anyways). The DB user isn't the owner of the DB then, but for an existing setup this is irrelevant because CREATE on the public schema isn't revoked from existing users (only not granted for new users). [1] not really declarative though because removals of these statements are simply ignored for instance: https://github.com/NixOS/nixpkgs/issues/206467 [2] `services.invidious`: I removed the `ensure*` part temporarily because it IMHO falls into the category "manage the state on your own" (see the commit message). See also https://github.com/NixOS/nixpkgs/pull/265857 [3] e.g. roundcube had `"DATABASE ${cfg.database.username}" = "ALL PRIVILEGES";` [4] As opposed to other changes that are considered a potential fix, but also add more things like collation for DBs or passwords that are _never_ touched again when changing those. [5] As suggested in e.g. https://github.com/NixOS/nixpkgs/issues/206467
2023-11-08 11:50:09 +00:00
ensureDBOwnership = true;
2022-12-01 16:31:48 +00:00
}
];
};
environment.systemPackages = [ peeringManagerManageScript ];
systemd.targets.peering-manager = {
description = "Target for all Peering Manager services";
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
2022-12-01 16:31:48 +00:00
after = [ "network-online.target" "redis-peering-manager.service" ];
};
systemd.services = let
defaults = {
environment = {
PYTHONPATH = pkg.pythonPath;
};
serviceConfig = {
WorkingDirectory = "/var/lib/peering-manager";
User = "peering-manager";
Group = "peering-manager";
StateDirectory = "peering-manager";
StateDirectoryMode = "0750";
Restart = "on-failure";
};
2022-12-01 16:31:48 +00:00
};
in {
peering-manager-migration = lib.recursiveUpdate defaults {
2022-12-01 16:31:48 +00:00
description = "Peering Manager migrations";
wantedBy = [ "peering-manager.target" ];
serviceConfig = {
2022-12-01 16:31:48 +00:00
Type = "oneshot";
ExecStart = "${pkg}/bin/peering-manager migrate";
2022-12-01 16:31:48 +00:00
};
};
peering-manager = lib.recursiveUpdate defaults {
2022-12-01 16:31:48 +00:00
description = "Peering Manager WSGI Service";
wantedBy = [ "peering-manager.target" ];
after = [ "peering-manager-migration.service" ];
preStart = ''
${pkg}/bin/peering-manager remove_stale_contenttypes --no-input
'';
serviceConfig = {
2022-12-01 16:31:48 +00:00
ExecStart = ''
${pkg.python.pkgs.gunicorn}/bin/gunicorn peering_manager.wsgi \
--bind ${cfg.listenAddress}:${toString cfg.port} \
--pythonpath ${pkg}/opt/peering-manager
'';
};
};
peering-manager-rq = lib.recursiveUpdate defaults {
2022-12-01 16:31:48 +00:00
description = "Peering Manager Request Queue Worker";
wantedBy = [ "peering-manager.target" ];
after = [ "peering-manager.service" ];
serviceConfig.ExecStart = "${pkg}/bin/peering-manager rqworker high default low";
};
2022-12-01 16:31:48 +00:00
peering-manager-housekeeping = lib.recursiveUpdate defaults {
description = "Peering Manager housekeeping job";
after = [ "peering-manager.service" ];
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkg}/bin/peering-manager housekeeping";
2022-12-01 16:31:48 +00:00
};
};
2022-12-01 16:31:48 +00:00
peering-manager-peeringdb-sync = lib.recursiveUpdate defaults {
description = "PeeringDB sync";
after = [ "peering-manager.service" ];
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkg}/bin/peering-manager peeringdb_sync";
2022-12-01 16:31:48 +00:00
};
};
peering-manager-prefix-fetch = lib.recursiveUpdate defaults {
description = "Fetch IRR AS-SET prefixes";
2022-12-01 16:31:48 +00:00
after = [ "peering-manager.service" ];
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkg}/bin/peering-manager grab_prefixes";
};
};
2022-12-01 16:31:48 +00:00
peering-manager-configuration-deployment = lib.recursiveUpdate defaults {
description = "Push configuration to routers";
after = [ "peering-manager.service" ];
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkg}/bin/peering-manager configure_routers";
2022-12-01 16:31:48 +00:00
};
};
2022-12-01 16:31:48 +00:00
peering-manager-session-poll = lib.recursiveUpdate defaults {
description = "Poll peering sessions from routers";
after = [ "peering-manager.service" ];
serviceConfig = {
2022-12-01 16:31:48 +00:00
Type = "oneshot";
ExecStart = "${pkg}/bin/peering-manager poll_bgp_sessions --all";
2022-12-01 16:31:48 +00:00
};
};
};
systemd.timers = {
peering-manager-housekeeping = {
description = "Run Peering Manager housekeeping job";
wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = "daily";
};
peering-manager-peeringdb-sync = {
enable = lib.mkDefault cfg.enableScheduledTasks;
description = "Sync PeeringDB at 2:30";
wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = "02:30:00";
};
peering-manager-prefix-fetch = {
enable = lib.mkDefault cfg.enableScheduledTasks;
description = "Fetch IRR AS-SET prefixes at 4:30";
wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = "04:30:00";
};
peering-manager-configuration-deployment = {
enable = lib.mkDefault cfg.enableScheduledTasks;
description = "Push router configuration every hour 5 minutes before full hour";
wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = "*:55:00";
};
2022-12-01 16:31:48 +00:00
peering-manager-session-poll = {
enable = lib.mkDefault cfg.enableScheduledTasks;
description = "Poll peering sessions from routers every hour";
wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = "*:00:00";
2022-12-01 16:31:48 +00:00
};
};
users.users.peering-manager = {
home = "/var/lib/peering-manager";
isSystemUser = true;
group = "peering-manager";
};
users.groups.peering-manager = {};
users.groups."${config.services.redis.servers.peering-manager.user}".members = [ "peering-manager" ];
};
meta.maintainers = with lib.maintainers; [ yuka ];
2022-12-01 16:31:48 +00:00
}