about summary refs log tree commit diff
path: root/nixos/modules/services/databases/influxdb2.nix
blob: 2a67d87d4bbb8c227e54af614e934fbad105457b (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
{ config, lib, pkgs, ... }:

let
  inherit
    (lib)
    any
    attrNames
    attrValues
    count
    escapeShellArg
    filterAttrs
    flatten
    flip
    getExe
    hasAttr
    hasInfix
    listToAttrs
    literalExpression
    mapAttrsToList
    mdDoc
    mkEnableOption
    mkPackageOption
    mkIf
    mkOption
    nameValuePair
    optional
    subtractLists
    types
    unique
    ;

  format = pkgs.formats.json { };
  cfg = config.services.influxdb2;
  configFile = format.generate "config.json" cfg.settings;

  validPermissions = [
    "authorizations"
    "buckets"
    "dashboards"
    "orgs"
    "tasks"
    "telegrafs"
    "users"
    "variables"
    "secrets"
    "labels"
    "views"
    "documents"
    "notificationRules"
    "notificationEndpoints"
    "checks"
    "dbrp"
    "annotations"
    "sources"
    "scrapers"
    "notebooks"
    "remotes"
    "replications"
  ];

  # Determines whether at least one active api token is defined
  anyAuthDefined =
    flip any (attrValues cfg.provision.organizations)
    (o: o.present && flip any (attrValues o.auths)
    (a: a.present && a.tokenFile != null));

  provisionState = pkgs.writeText "provision_state.json" (builtins.toJSON {
    inherit (cfg.provision) organizations users;
  });

  provisioningScript = pkgs.writeShellScript "post-start-provision" ''
    set -euo pipefail
    export INFLUX_HOST="http://"${escapeShellArg (
      if ! hasAttr "http-bind-address" cfg.settings
        || hasInfix "0.0.0.0" cfg.settings.http-bind-address
      then "localhost:8086"
      else cfg.settings.http-bind-address
    )}

    # Wait for the influxdb server to come online
    count=0
    while ! influx ping &>/dev/null; do
      if [ "$count" -eq 300 ]; then
        echo "Tried for 30 seconds, giving up..."
        exit 1
      fi

      if ! kill -0 "$MAINPID"; then
        echo "Main server died, giving up..."
        exit 1
      fi

      sleep 0.1
      count=$((count++))
    done

    # Do the initial database setup. Pass /dev/null as configs-path to
    # avoid saving the token as the active config.
    if test -e "$STATE_DIRECTORY/.first_startup"; then
      influx setup \
        --configs-path /dev/null \
        --org ${escapeShellArg cfg.provision.initialSetup.organization} \
        --bucket ${escapeShellArg cfg.provision.initialSetup.bucket} \
        --username ${escapeShellArg cfg.provision.initialSetup.username} \
        --password "$(< "$CREDENTIALS_DIRECTORY/admin-password")" \
        --token "$(< "$CREDENTIALS_DIRECTORY/admin-token")" \
        --retention ${toString cfg.provision.initialSetup.retention}s \
        --force >/dev/null

      rm -f "$STATE_DIRECTORY/.first_startup"
    fi

    provision_result=$(${getExe pkgs.influxdb2-provision} ${provisionState} "$INFLUX_HOST" "$(< "$CREDENTIALS_DIRECTORY/admin-token")")
    if [[ "$(jq '[.auths[] | select(.action == "created")] | length' <<< "$provision_result")" -gt 0 ]]; then
      echo "Created at least one new token, queueing service restart so we can manipulate secrets"
      touch "$STATE_DIRECTORY/.needs_restart"
    fi
  '';

  restarterScript = pkgs.writeShellScript "post-start-restarter" ''
    set -euo pipefail
    if test -e "$STATE_DIRECTORY/.needs_restart"; then
      rm -f "$STATE_DIRECTORY/.needs_restart"
      /run/current-system/systemd/bin/systemctl restart influxdb2
    fi
  '';

  organizationSubmodule = types.submodule (organizationSubmod: let
    org = organizationSubmod.config._module.args.name;
  in {
    options = {
      present = mkOption {
        description = mdDoc "Whether to ensure that this organization is present or absent.";
        type = types.bool;
        default = true;
      };

      description = mkOption {
        description = mdDoc "Optional description for the organization.";
        default = null;
        type = types.nullOr types.str;
      };

      buckets = mkOption {
        description = mdDoc "Buckets to provision in this organization.";
        default = {};
        type = types.attrsOf (types.submodule (bucketSubmod: let
          bucket = bucketSubmod.config._module.args.name;
        in {
          options = {
            present = mkOption {
              description = mdDoc "Whether to ensure that this bucket is present or absent.";
              type = types.bool;
              default = true;
            };

            description = mkOption {
              description = mdDoc "Optional description for the bucket.";
              default = null;
              type = types.nullOr types.str;
            };

            retention = mkOption {
              type = types.ints.unsigned;
              default = 0;
              description = mdDoc "The duration in seconds for which the bucket will retain data (0 is infinite).";
            };
          };
        }));
      };

      auths = mkOption {
        description = mdDoc "API tokens to provision for the user in this organization.";
        default = {};
        type = types.attrsOf (types.submodule (authSubmod: let
          auth = authSubmod.config._module.args.name;
        in {
          options = {
            id = mkOption {
              description = mdDoc "A unique identifier for this authentication token. Since influx doesn't store names for tokens, this will be hashed and appended to the description to identify the token.";
              readOnly = true;
              default = builtins.substring 0 32 (builtins.hashString "sha256" "${org}:${auth}");
              defaultText = "<a hash derived from org and name>";
              type = types.str;
            };

            present = mkOption {
              description = mdDoc "Whether to ensure that this user is present or absent.";
              type = types.bool;
              default = true;
            };

            description = mkOption {
              description = ''
                Optional description for the API token.
                Note that the actual token will always be created with a descriptionregardless
                of whether this is given or not. The name is always added plus a unique suffix
                to later identify the token to track whether it has already been created.
              '';
              default = null;
              type = types.nullOr types.str;
            };

            tokenFile = mkOption {
              type = types.nullOr types.path;
              default = null;
              description = mdDoc "The token value. If not given, influx will automatically generate one.";
            };

            operator = mkOption {
              description = mdDoc "Grants all permissions in all organizations.";
              default = false;
              type = types.bool;
            };

            allAccess = mkOption {
              description = mdDoc "Grants all permissions in the associated organization.";
              default = false;
              type = types.bool;
            };

            readPermissions = mkOption {
              description = mdDoc ''
                The read permissions to include for this token. Access is usually granted only
                for resources in the associated organization.

                Available permissions are `authorizations`, `buckets`, `dashboards`,
                `orgs`, `tasks`, `telegrafs`, `users`, `variables`, `secrets`, `labels`, `views`,
                `documents`, `notificationRules`, `notificationEndpoints`, `checks`, `dbrp`,
                `annotations`, `sources`, `scrapers`, `notebooks`, `remotes`, `replications`.

                Refer to `influx auth create --help` for a full list with descriptions.

                `buckets` grants read access to all associated buckets. Use `readBuckets` to define
                more granular access permissions.
              '';
              default = [];
              type = types.listOf (types.enum validPermissions);
            };

            writePermissions = mkOption {
              description = mdDoc ''
                The read permissions to include for this token. Access is usually granted only
                for resources in the associated organization.

                Available permissions are `authorizations`, `buckets`, `dashboards`,
                `orgs`, `tasks`, `telegrafs`, `users`, `variables`, `secrets`, `labels`, `views`,
                `documents`, `notificationRules`, `notificationEndpoints`, `checks`, `dbrp`,
                `annotations`, `sources`, `scrapers`, `notebooks`, `remotes`, `replications`.

                Refer to `influx auth create --help` for a full list with descriptions.

                `buckets` grants write access to all associated buckets. Use `writeBuckets` to define
                more granular access permissions.
              '';
              default = [];
              type = types.listOf (types.enum validPermissions);
            };

            readBuckets = mkOption {
              description = mdDoc "The organization's buckets which should be allowed to be read";
              default = [];
              type = types.listOf types.str;
            };

            writeBuckets = mkOption {
              description = mdDoc "The organization's buckets which should be allowed to be written";
              default = [];
              type = types.listOf types.str;
            };
          };
        }));
      };
    };
  });
in
{
  options = {
    services.influxdb2 = {
      enable = mkEnableOption (mdDoc "the influxdb2 server");

      package = mkPackageOption pkgs "influxdb2" { };

      settings = mkOption {
        default = { };
        description = mdDoc ''configuration options for influxdb2, see <https://docs.influxdata.com/influxdb/v2.0/reference/config-options> for details.'';
        type = format.type;
      };

      provision = {
        enable = mkEnableOption "initial database setup and provisioning";

        initialSetup = {
          organization = mkOption {
            type = types.str;
            example = "main";
            description = mdDoc "Primary organization name";
          };

          bucket = mkOption {
            type = types.str;
            example = "example";
            description = mdDoc "Primary bucket name";
          };

          username = mkOption {
            type = types.str;
            default = "admin";
            description = mdDoc "Primary username";
          };

          retention = mkOption {
            type = types.ints.unsigned;
            default = 0;
            description = mdDoc "The duration in seconds for which the bucket will retain data (0 is infinite).";
          };

          passwordFile = mkOption {
            type = types.path;
            description = mdDoc "Password for primary user. Don't use a file from the nix store!";
          };

          tokenFile = mkOption {
            type = types.path;
            description = mdDoc "API Token to set for the admin user. Don't use a file from the nix store!";
          };
        };

        organizations = mkOption {
          description = mdDoc "Organizations to provision.";
          example = literalExpression ''
            {
              myorg = {
                description = "My organization";
                buckets.mybucket = {
                  description = "My bucket";
                  retention = 31536000; # 1 year
                };
                auths.mytoken = {
                  readBuckets = ["mybucket"];
                  tokenFile = "/run/secrets/mytoken";
                };
              };
            }
          '';
          default = {};
          type = types.attrsOf organizationSubmodule;
        };

        users = mkOption {
          description = mdDoc "Users to provision.";
          default = {};
          example = literalExpression ''
            {
              # admin = {}; /* The initialSetup.username will automatically be added. */
              myuser.passwordFile = "/run/secrets/myuser_password";
            }
          '';
          type = types.attrsOf (types.submodule (userSubmod: let
            user = userSubmod.config._module.args.name;
            org = userSubmod.config.org;
          in {
            options = {
              present = mkOption {
                description = mdDoc "Whether to ensure that this user is present or absent.";
                type = types.bool;
                default = true;
              };

              passwordFile = mkOption {
                description = mdDoc "Password for the user. If unset, the user will not be able to log in until a password is set by an operator! Don't use a file from the nix store!";
                default = null;
                type = types.nullOr types.path;
              };
            };
          }));
        };
      };
    };
  };

  config = mkIf cfg.enable {
    assertions =
      [
        {
          assertion = !(hasAttr "bolt-path" cfg.settings) && !(hasAttr "engine-path" cfg.settings);
          message = "services.influxdb2.config: bolt-path and engine-path should not be set as they are managed by systemd";
        }
      ]
      ++ flatten (flip mapAttrsToList cfg.provision.organizations (orgName: org:
        flip mapAttrsToList org.auths (authName: auth:
          [
            {
              assertion = 1 == count (x: x) [
                auth.operator
                auth.allAccess
                (auth.readPermissions != []
                  || auth.writePermissions != []
                  || auth.readBuckets != []
                  || auth.writeBuckets != [])
              ];
              message = "influxdb2: provision.organizations.${orgName}.auths.${authName}: The `operator` and `allAccess` options are mutually exclusive with each other and the granular permission settings.";
            }
            (let unknownBuckets = subtractLists (attrNames org.buckets) auth.readBuckets; in {
              assertion = unknownBuckets == [];
              message = "influxdb2: provision.organizations.${orgName}.auths.${authName}: Refers to invalid buckets in readBuckets: ${toString unknownBuckets}";
            })
            (let unknownBuckets = subtractLists (attrNames org.buckets) auth.writeBuckets; in {
              assertion = unknownBuckets == [];
              message = "influxdb2: provision.organizations.${orgName}.auths.${authName}: Refers to invalid buckets in writeBuckets: ${toString unknownBuckets}";
            })
          ]
        )
      ));

    services.influxdb2.provision = mkIf cfg.provision.enable {
      organizations.${cfg.provision.initialSetup.organization} = {
        buckets.${cfg.provision.initialSetup.bucket} = {
          inherit (cfg.provision.initialSetup) retention;
        };
      };
      users.${cfg.provision.initialSetup.username} = {
        inherit (cfg.provision.initialSetup) passwordFile;
      };
    };

    systemd.services.influxdb2 = {
      description = "InfluxDB is an open-source, distributed, time series database";
      documentation = [ "https://docs.influxdata.com/influxdb/" ];
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      environment = {
        INFLUXD_CONFIG_PATH = configFile;
        ZONEINFO = "${pkgs.tzdata}/share/zoneinfo";
      };
      serviceConfig = {
        ExecStart = "${cfg.package}/bin/influxd --bolt-path \${STATE_DIRECTORY}/influxd.bolt --engine-path \${STATE_DIRECTORY}/engine";
        StateDirectory = "influxdb2";
        User = "influxdb2";
        Group = "influxdb2";
        CapabilityBoundingSet = "";
        SystemCallFilter = "@system-service";
        LimitNOFILE = 65536;
        KillMode = "control-group";
        Restart = "on-failure";
        LoadCredential = mkIf cfg.provision.enable [
          "admin-password:${cfg.provision.initialSetup.passwordFile}"
          "admin-token:${cfg.provision.initialSetup.tokenFile}"
        ];

        ExecStartPost = mkIf cfg.provision.enable (
          [provisioningScript] ++
          # Only the restarter runs with elevated privileges
          optional anyAuthDefined "+${restarterScript}"
        );
      };

      path = [
        pkgs.influxdb2-cli
        pkgs.jq
      ];

      # Mark if this is the first startup so postStart can do the initial setup.
      # Also extract any token secret mappings and apply them if this isn't the first start.
      preStart = let
        tokenPaths = listToAttrs (flatten
          # For all organizations
          (flip mapAttrsToList cfg.provision.organizations
            # For each contained token that has a token file
            (_: org: flip mapAttrsToList (filterAttrs (_: x: x.tokenFile != null) org.auths)
              # Collect id -> tokenFile for the mapping
              (_: auth: nameValuePair auth.id auth.tokenFile))));
        tokenMappings = pkgs.writeText "token_mappings.json" (builtins.toJSON tokenPaths);
      in mkIf cfg.provision.enable ''
        if ! test -e "$STATE_DIRECTORY/influxd.bolt"; then
          touch "$STATE_DIRECTORY/.first_startup"
        else
          # Manipulate provisioned api tokens if necessary
          ${getExe pkgs.influxdb2-token-manipulator} "$STATE_DIRECTORY/influxd.bolt" ${tokenMappings}
        fi
      '';
    };

    users.extraUsers.influxdb2 = {
      isSystemUser = true;
      group = "influxdb2";
    };

    users.extraGroups.influxdb2 = {};
  };

  meta.maintainers = with lib.maintainers; [ nickcao oddlama ];
}