summary refs log tree commit diff
path: root/nixos/modules/services/web-apps/nifi.nix
blob: f643e24d81d99ca3244d9e94ce8dc647bc96080e (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
{ lib, pkgs, config, options, ... }:

let
  cfg = config.services.nifi;
  opt = options.services.nifi;

  env = {
    NIFI_OVERRIDE_NIFIENV = "true";
    NIFI_HOME = "/var/lib/nifi";
    NIFI_PID_DIR = "/run/nifi";
    NIFI_LOG_DIR = "/var/log/nifi";
  };

  envFile = pkgs.writeText "nifi.env" (lib.concatMapStrings (s: s + "\n") (
    (lib.concatLists (lib.mapAttrsToList (name: value:
      if value != null then [
        "${name}=\"${toString value}\""
      ] else []
    ) env))));

  nifiEnv = pkgs.writeShellScriptBin "nifi-env" ''
    set -a
    source "${envFile}"
    eval -- "\$@"
  '';

in {
  options = {
    services.nifi = {
      enable = lib.mkEnableOption (lib.mdDoc "Apache NiFi");

      package = lib.mkOption {
        type = lib.types.package;
        default = pkgs.nifi;
        defaultText = lib.literalExpression "pkgs.nifi";
        description = lib.mdDoc "Apache NiFi package to use.";
      };

      user = lib.mkOption {
        type = lib.types.str;
        default = "nifi";
        description = lib.mdDoc "User account where Apache NiFi runs.";
      };

      group = lib.mkOption {
        type = lib.types.str;
        default = "nifi";
        description = lib.mdDoc "Group account where Apache NiFi runs.";
      };

      enableHTTPS = lib.mkOption {
        type = lib.types.bool;
        default = true;
        description = lib.mdDoc "Enable HTTPS protocol. Don`t use in production.";
      };

      listenHost = lib.mkOption {
        type = lib.types.str;
        default = if cfg.enableHTTPS then "0.0.0.0" else "127.0.0.1";
        defaultText = lib.literalExpression ''
          if config.${opt.enableHTTPS}
          then "0.0.0.0"
          else "127.0.0.1"
        '';
        description = lib.mdDoc "Bind to an ip for Apache NiFi web-ui.";
      };

      listenPort = lib.mkOption {
        type = lib.types.int;
        default = if cfg.enableHTTPS then 8443 else 8080;
        defaultText = lib.literalExpression ''
          if config.${opt.enableHTTPS}
          then "8443"
          else "8000"
        '';
        description = lib.mdDoc "Bind to a port for Apache NiFi web-ui.";
      };

      proxyHost = lib.mkOption {
        type = lib.types.nullOr lib.types.str;
        default = if cfg.enableHTTPS then "0.0.0.0" else null;
        defaultText = lib.literalExpression ''
          if config.${opt.enableHTTPS}
          then "0.0.0.0"
          else null
        '';
        description = lib.mdDoc "Allow requests from a specific host.";
      };

      proxyPort = lib.mkOption {
        type = lib.types.nullOr lib.types.int;
        default = if cfg.enableHTTPS then 8443 else null;
        defaultText = lib.literalExpression ''
          if config.${opt.enableHTTPS}
          then "8443"
          else null
        '';
        description = lib.mdDoc "Allow requests from a specific port.";
      };

      initUser = lib.mkOption {
        type = lib.types.nullOr lib.types.str;
        default = null;
        description = lib.mdDoc "Initial user account for Apache NiFi. Username must be at least 4 characters.";
      };

      initPasswordFile = lib.mkOption {
        type = lib.types.nullOr lib.types.path;
        default = null;
        example = "/run/keys/nifi/password-nifi";
        description = lib.mdDoc "nitial password for Apache NiFi. Password must be at least 12 characters.";
      };

      initJavaHeapSize = lib.mkOption {
        type = lib.types.nullOr lib.types.int;
        default = null;
        example = 1024;
        description = lib.mdDoc "Set the initial heap size for the JVM in MB.";
      };

      maxJavaHeapSize = lib.mkOption {
        type = lib.types.nullOr lib.types.int;
        default = null;
        example = 2048;
        description = lib.mdDoc "Set the initial heap size for the JVM in MB.";
      };
    };
  };

  config = lib.mkIf cfg.enable {
    assertions = [
      { assertion = cfg.initUser!=null || cfg.initPasswordFile==null;
          message = ''
            <option>services.nifi.initUser</option> needs to be set if <option>services.nifi.initPasswordFile</option> enabled.
          '';
      }
      { assertion = cfg.initUser==null || cfg.initPasswordFile!=null;
          message = ''
            <option>services.nifi.initPasswordFile</option> needs to be set if <option>services.nifi.initUser</option> enabled.
          '';
      }
      { assertion = cfg.proxyHost==null || cfg.proxyPort!=null;
          message = ''
            <option>services.nifi.proxyPort</option> needs to be set if <option>services.nifi.proxyHost</option> value specified.
          '';
      }
      { assertion = cfg.proxyHost!=null || cfg.proxyPort==null;
          message = ''
            <option>services.nifi.proxyHost</option> needs to be set if <option>services.nifi.proxyPort</option> value specified.
          '';
      }
      { assertion = cfg.initJavaHeapSize==null || cfg.maxJavaHeapSize!=null;
          message = ''
            <option>services.nifi.maxJavaHeapSize</option> needs to be set if <option>services.nifi.initJavaHeapSize</option> value specified.
          '';
      }
      { assertion = cfg.initJavaHeapSize!=null || cfg.maxJavaHeapSize==null;
          message = ''
            <option>services.nifi.initJavaHeapSize</option> needs to be set if <option>services.nifi.maxJavaHeapSize</option> value specified.
          '';
      }
    ];

    warnings = lib.optional (cfg.enableHTTPS==false) ''
      Please do not disable HTTPS mode in production. In this mode, access to the nifi is opened without authentication.
    '';

    systemd.tmpfiles.rules = [
      "d '/var/lib/nifi/conf' 0750 ${cfg.user} ${cfg.group}"
      "L+ '/var/lib/nifi/lib' - - - - ${cfg.package}/lib"
    ];


    systemd.services.nifi = {
      description = "Apache NiFi";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      environment = env;
      path = [ pkgs.gawk ];

      serviceConfig = {
        Type = "forking";
        PIDFile = "/run/nifi/nifi.pid";
        ExecStartPre = pkgs.writeScript "nifi-pre-start.sh" ''
          #!/bin/sh
          umask 077
          test -f '/var/lib/nifi/conf/authorizers.xml'                      || (cp '${cfg.package}/share/nifi/conf/authorizers.xml' '/var/lib/nifi/conf/' && chmod 0640 '/var/lib/nifi/conf/authorizers.xml')
          test -f '/var/lib/nifi/conf/bootstrap.conf'                       || (cp '${cfg.package}/share/nifi/conf/bootstrap.conf' '/var/lib/nifi/conf/' && chmod 0640 '/var/lib/nifi/conf/bootstrap.conf')
          test -f '/var/lib/nifi/conf/bootstrap-hashicorp-vault.conf'       || (cp '${cfg.package}/share/nifi/conf/bootstrap-hashicorp-vault.conf' '/var/lib/nifi/conf/' && chmod 0640 '/var/lib/nifi/conf/bootstrap-hashicorp-vault.conf')
          test -f '/var/lib/nifi/conf/bootstrap-notification-services.xml'  || (cp '${cfg.package}/share/nifi/conf/bootstrap-notification-services.xml' '/var/lib/nifi/conf/' && chmod 0640 '/var/lib/nifi/conf/bootstrap-notification-services.xml')
          test -f '/var/lib/nifi/conf/logback.xml'                          || (cp '${cfg.package}/share/nifi/conf/logback.xml' '/var/lib/nifi/conf/' && chmod 0640 '/var/lib/nifi/conf/logback.xml')
          test -f '/var/lib/nifi/conf/login-identity-providers.xml'         || (cp '${cfg.package}/share/nifi/conf/login-identity-providers.xml' '/var/lib/nifi/conf/' && chmod 0640 '/var/lib/nifi/conf/login-identity-providers.xml')
          test -f '/var/lib/nifi/conf/nifi.properties'                      || (cp '${cfg.package}/share/nifi/conf/nifi.properties' '/var/lib/nifi/conf/' && chmod 0640 '/var/lib/nifi/conf/nifi.properties')
          test -f '/var/lib/nifi/conf/stateless-logback.xml'                || (cp '${cfg.package}/share/nifi/conf/stateless-logback.xml' '/var/lib/nifi/conf/' && chmod 0640 '/var/lib/nifi/conf/stateless-logback.xml')
          test -f '/var/lib/nifi/conf/stateless.properties'                 || (cp '${cfg.package}/share/nifi/conf/stateless.properties' '/var/lib/nifi/conf/' && chmod 0640 '/var/lib/nifi/conf/stateless.properties')
          test -f '/var/lib/nifi/conf/state-management.xml'                 || (cp '${cfg.package}/share/nifi/conf/state-management.xml' '/var/lib/nifi/conf/' && chmod 0640 '/var/lib/nifi/conf/state-management.xml')
          test -f '/var/lib/nifi/conf/zookeeper.properties'                 || (cp '${cfg.package}/share/nifi/conf/zookeeper.properties' '/var/lib/nifi/conf/' && chmod 0640 '/var/lib/nifi/conf/zookeeper.properties')
          test -d '/var/lib/nifi/docs/html'                                 || (mkdir -p /var/lib/nifi/docs && cp -r '${cfg.package}/share/nifi/docs/html' '/var/lib/nifi/docs/html')
          ${lib.optionalString ((cfg.initUser != null) && (cfg.initPasswordFile != null)) ''
            awk -F'[<|>]' '/property name="Username"/ {if ($3!="") f=1} END{exit !f}' /var/lib/nifi/conf/login-identity-providers.xml || ${cfg.package}/bin/nifi.sh set-single-user-credentials ${cfg.initUser} $(cat ${cfg.initPasswordFile})
          ''}
          ${lib.optionalString (cfg.enableHTTPS == false) ''
            sed -i /var/lib/nifi/conf/nifi.properties \
              -e 's|nifi.remote.input.secure=.*|nifi.remote.input.secure=false|g' \
              -e 's|nifi.web.http.host=.*|nifi.web.http.host=${cfg.listenHost}|g' \
              -e 's|nifi.web.http.port=.*|nifi.web.http.port=${(toString cfg.listenPort)}|g' \
              -e 's|nifi.web.https.host=.*|nifi.web.https.host=|g' \
              -e 's|nifi.web.https.port=.*|nifi.web.https.port=|g' \
              -e 's|nifi.security.keystore=.*|nifi.security.keystore=|g' \
              -e 's|nifi.security.keystoreType=.*|nifi.security.keystoreType=|g' \
              -e 's|nifi.security.truststore=.*|nifi.security.truststore=|g' \
              -e 's|nifi.security.truststoreType=.*|nifi.security.truststoreType=|g' \
              -e '/nifi.security.keystorePasswd/s|^|#|' \
              -e '/nifi.security.keyPasswd/s|^|#|' \
              -e '/nifi.security.truststorePasswd/s|^|#|'
          ''}
          ${lib.optionalString (cfg.enableHTTPS == true) ''
            sed -i /var/lib/nifi/conf/nifi.properties \
              -e 's|nifi.remote.input.secure=.*|nifi.remote.input.secure=true|g' \
              -e 's|nifi.web.http.host=.*|nifi.web.http.host=|g' \
              -e 's|nifi.web.http.port=.*|nifi.web.http.port=|g' \
              -e 's|nifi.web.https.host=.*|nifi.web.https.host=${cfg.listenHost}|g' \
              -e 's|nifi.web.https.port=.*|nifi.web.https.port=${(toString cfg.listenPort)}|g' \
              -e 's|nifi.security.keystore=.*|nifi.security.keystore=./conf/keystore.p12|g' \
              -e 's|nifi.security.keystoreType=.*|nifi.security.keystoreType=PKCS12|g' \
              -e 's|nifi.security.truststore=.*|nifi.security.truststore=./conf/truststore.p12|g' \
              -e 's|nifi.security.truststoreType=.*|nifi.security.truststoreType=PKCS12|g' \
              -e '/nifi.security.keystorePasswd/s|^#\+||' \
              -e '/nifi.security.keyPasswd/s|^#\+||' \
              -e '/nifi.security.truststorePasswd/s|^#\+||'
          ''}
          ${lib.optionalString ((cfg.enableHTTPS == true) && (cfg.proxyHost != null) && (cfg.proxyPort != null)) ''
            sed -i /var/lib/nifi/conf/nifi.properties \
              -e 's|nifi.web.proxy.host=.*|nifi.web.proxy.host=${cfg.proxyHost}:${(toString cfg.proxyPort)}|g'
          ''}
          ${lib.optionalString ((cfg.enableHTTPS == false) || (cfg.proxyHost == null) && (cfg.proxyPort == null)) ''
            sed -i /var/lib/nifi/conf/nifi.properties \
              -e 's|nifi.web.proxy.host=.*|nifi.web.proxy.host=|g'
          ''}
          ${lib.optionalString ((cfg.initJavaHeapSize != null) && (cfg.maxJavaHeapSize != null))''
            sed -i /var/lib/nifi/conf/bootstrap.conf \
              -e 's|java.arg.2=.*|java.arg.2=-Xms${(toString cfg.initJavaHeapSize)}m|g' \
              -e 's|java.arg.3=.*|java.arg.3=-Xmx${(toString cfg.maxJavaHeapSize)}m|g'
          ''}
          ${lib.optionalString ((cfg.initJavaHeapSize == null) && (cfg.maxJavaHeapSize == null))''
            sed -i /var/lib/nifi/conf/bootstrap.conf \
              -e 's|java.arg.2=.*|java.arg.2=-Xms512m|g' \
              -e 's|java.arg.3=.*|java.arg.3=-Xmx512m|g'
          ''}
        '';
        ExecStart = "${cfg.package}/bin/nifi.sh start";
        ExecStop = "${cfg.package}/bin/nifi.sh stop";
        # User and group
        User = cfg.user;
        Group = cfg.group;
        # Runtime directory and mode
        RuntimeDirectory = "nifi";
        RuntimeDirectoryMode = "0750";
        # State directory and mode
        StateDirectory = "nifi";
        StateDirectoryMode = "0750";
        # Logs directory and mode
        LogsDirectory = "nifi";
        LogsDirectoryMode = "0750";
        # Proc filesystem
        ProcSubset = "pid";
        ProtectProc = "invisible";
        # Access write directories
        ReadWritePaths = [ cfg.initPasswordFile ];
        UMask = "0027";
        # Capabilities
        CapabilityBoundingSet = "";
        # Security
        NoNewPrivileges = true;
        # Sandboxing
        ProtectSystem = "strict";
        ProtectHome = true;
        PrivateTmp = true;
        PrivateDevices = true;
        PrivateIPC = true;
        PrivateUsers = true;
        ProtectHostname = true;
        ProtectClock = true;
        ProtectKernelTunables = true;
        ProtectKernelModules = true;
        ProtectKernelLogs = true;
        ProtectControlGroups = true;
        RestrictAddressFamilies = [ "AF_INET AF_INET6" ];
        RestrictNamespaces = true;
        LockPersonality = true;
        MemoryDenyWriteExecute  = false;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        RemoveIPC = true;
        PrivateMounts = true;
        # System Call Filtering
        SystemCallArchitectures = "native";
        SystemCallFilter = [ "~@cpu-emulation @debug @keyring @memlock @mount @obsolete @resources @privileged @setuid" "@chown" ];
      };
    };

    users.users = lib.mkMerge [
      (lib.mkIf (cfg.user == "nifi") {
        nifi = {
          group = cfg.group;
          isSystemUser = true;
          home = cfg.package;
        };
      })
      (lib.attrsets.setAttrByPath [ cfg.user "packages" ] [ cfg.package nifiEnv ])
    ];

    users.groups = lib.optionalAttrs (cfg.group == "nifi") {
      nifi = { };
    };
  };
}