blob: 1c84da7f0a43a3aa4ce848c4372773f6f48f4f23 (
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
|
{
lib,
pkgs,
config,
...
}:
let
cfg = config.services.drasl;
format = pkgs.formats.toml { };
filterAttrs = x: lib.filterAttrs (n: v: n != "ClientSecretFile" || v != null) x;
getIndex =
item:
builtins.toString (lib.lists.findFirstIndex (x: x == item) null cfg.settings.RegistrationOIDC);
secretFiles = lib.filter (
x: lib.hasAttr "ClientSecretFile" (filterAttrs x)
) cfg.settings.RegistrationOIDC;
settings = format.generate "drasl-config.toml" (
if cfg.settings.RegistrationOIDC == [ ] then
lib.filterAttrs (n: v: n != "RegistrationOIDC" || v != [ ]) cfg.settings
else
lib.recursiveUpdate cfg.settings {
RegistrationOIDC = map (
x:
if lib.hasAttr "ClientSecretFile" (filterAttrs x) then
lib.recursiveUpdate (filterAttrs x) {
ClientSecretFile = "$CREDENTIALS_DIRECTORY/${getIndex x}";
}
else
filterAttrs x
) cfg.settings.RegistrationOIDC;
}
);
in
{
options.services.drasl = {
enable = lib.mkEnableOption "Drasl";
package = lib.mkPackageOption pkgs "drasl" { };
enableDebug = lib.mkEnableOption "debugging";
settings = lib.mkOption {
description = ''
Configuration for Drasl. See the
[Drasl documentation](https://github.com/unmojang/drasl/blob/master/doc/configuration.md)
for possible options.
'';
type = lib.types.submodule {
freeformType = format.type;
options = {
RegistrationOIDC = lib.mkOption {
default = [ ];
description = "List of OpenID connect providers.";
type = lib.types.listOf (
lib.types.submodule {
freeformType = format.type;
options = {
ClientSecretFile = lib.mkOption {
default = null;
description = ''
Path to a file containing the OIDC client secret.
::: {.note}
The NixOS module will automatically load this file using
systemd's LoadCredential. Make sure this file is only
readable by the root user.
:::
'';
type = lib.types.nullOr lib.types.path;
};
};
}
);
};
};
};
};
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = lib.allUnique cfg.settings.RegistrationOIDC;
message = "All items in `services.drasl.settings.RegistrationOIDC` must be unique.";
}
{
assertion = lib.all (
x: (lib.hasAttr "ClientSecretFile" (filterAttrs x)) -> !(lib.hasAttr "ClientSecret" (filterAttrs x))
) cfg.settings.RegistrationOIDC;
message =
"Do not set both `services.drasl.settings.RegistrationOIDC.*.ClientSecret` "
+ "and `services.drasl.settings.RegistrationOIDC.*.ClientSecretFile`";
}
];
systemd.services.drasl = {
description = "Drasl";
after = [
"network-online.target"
"nss-lookup.target"
];
wants = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ];
environment = lib.mkIf cfg.enableDebug { DRASL_DEBUG = "1"; };
serviceConfig = {
ExecStart = "${lib.getExe cfg.package} -config ${settings}";
DynamicUser = true;
RuntimeDirectory = "drasl";
RuntimeDirectoryMode = "0700";
StateDirectory = "drasl";
LoadCredential = lib.mkIf (secretFiles != [ ]) (
map (x: "${getIndex x}:${x.ClientSecretFile}") secretFiles
);
# Hardening
LockPersonality = true;
MemoryDenyWriteExecute = true;
NoNewPrivileges = true;
PrivateDevices = true;
PrivateMounts = true;
ProtectClock = true;
ProtectControlGroups = true;
ProtectHome = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
RemoveIPC = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
CapabilityBoundingSet = "CAP_NET_BIND_SERVICE";
AmbientCapabilities = "CAP_NET_BIND_SERVICE";
PrivateTmp = "disconnected";
ProcSubset = "pid";
ProtectProc = "invisible";
ProtectSystem = "strict";
RestrictAddressFamilies = [
"AF_INET"
"AF_INET6"
];
RestrictNamespaces = [
"~cgroup"
"~ipc"
"~mnt"
"~net"
"~pid"
"~user"
"~uts"
];
SystemCallArchitectures = "native";
SystemCallFilter = [
"~@clock"
"~@cpu-emulation"
"~@debug"
"~@module"
"~@mount"
"~@obsolete"
"~@privileged"
"~@raw-io"
"~@reboot"
"~@resources"
"~@swap"
];
UMask = "0077";
};
};
};
meta.maintainers = with lib.maintainers; [
evan-goode
ungeskriptet
];
}
|