summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/monitoring/orbit.nix
blob: bbaa7f3a0627b28bae56f28d5cf6328eec446b65 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.services.orbit;
in
{
  options.services.orbit = {
    enable = lib.mkEnableOption "Fleet Orbit agent" // {
      description = "Enable the Fleet Orbit agent.";
      example = lib.literalExpression ''
        # Use an enrollment secret from a plaintext file managed outside the Nix store.
        {
          services.orbit = {
            enable = true;
            fleetUrl = "https://fleet.example.com";
            enrollSecretPath = "/etc/fleet/enroll-secret";

            desktop.enable = true;
          };
        }

        # Use an enrollment secret from sops-nix.
        { config, ... }:
        {
          sops.secrets.fleet-orbit-enroll-secret = { };

          services.orbit = {
            enable = true;
            fleetUrl = "https://fleet.example.com";
            enrollSecretPath = config.sops.secrets.fleet-orbit-enroll-secret.path;

            desktop.enable = true;
          };
        }
      '';
    };

    orbitPackage = lib.mkPackageOption pkgs "fleet-orbit" { };

    osqueryPackage = lib.mkPackageOption pkgs "osquery" { };

    desktop = {
      enable = lib.mkEnableOption "Fleet Desktop tray application";

      package = lib.mkPackageOption pkgs "fleet-desktop" { };

      alternativeBrowserHost = lib.mkOption {
        type = lib.types.nullOr lib.types.str;
        default = null;
        example = "fleet-browser.example.com";
        description = ''
          Alternative host to use for Fleet Desktop browser URLs. This can be
          required when Fleet uses TLS client authentication.
        '';
      };
    };

    fleetUrl = lib.mkOption {
      type = lib.types.str;
      example = "https://fleet.example.com";
      description = "The base URL of the Fleet server.";
    };

    enrollSecretPath = lib.mkOption {
      type = lib.types.path;
      example = "/run/secrets/fleet-enroll-secret";
      description = ''
        Path to a file containing the enroll secret for authenticating to the Fleet server.
        This should point to a secret outside the Nix store, for example a sops-nix or agenix
        secret path.
      '';
    };

    fleetCertificate = lib.mkOption {
      type = lib.types.path;
      default = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
      defaultText = lib.literalExpression "\"\${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt\"";
      description = "Path to the Fleet server certificate chain.";
    };

    debug = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = "Enable debug logging.";
    };

    devMode = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = "Run Orbit in development mode.";
    };

    enableScripts = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = "Enable Fleet script execution.";
    };

    endUserEmail = lib.mkOption {
      type = lib.types.nullOr lib.types.str;
      default = null;
      example = "user@example.com";
      description = "End-user email to pass to Orbit.";
    };

    fleetManagedHostIdentityCertificate = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = ''
        Configure Orbit to use Fleet-managed host identity certificates.
        This requires a Fleet Enterprise Edition subscription.
      '';
    };

    hostIdentifier = lib.mkOption {
      type = lib.types.nullOr (
        lib.types.enum [
          "uuid"
          "instance"
        ]
      );
      default = null;
      example = "uuid";
      description = "Host identifier mode to use when Orbit and osquery enroll to Fleet.";
    };

    insecure = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = "Disable TLS certificate verification.";
    };
  };

  config = lib.mkIf cfg.enable {
    systemd.services.orbit = {
      description = "Fleet Orbit agent";
      wantedBy = [ "multi-user.target" ];
      after = [ "network-online.target" ];
      wants = [ "network-online.target" ];

      environment = lib.filterAttrs (_: value: value != null) {
        ORBIT_FLEET_URL = cfg.fleetUrl;
        ORBIT_ENROLL_SECRET_PATH = "%d/enroll-secret";
        ORBIT_FLEET_CERTIFICATE = cfg.fleetCertificate;
        ORBIT_DEBUG = lib.boolToString cfg.debug;
        ORBIT_DEV_MODE = lib.boolToString cfg.devMode;
        ORBIT_ENABLE_SCRIPTS = lib.boolToString cfg.enableScripts;
        ORBIT_END_USER_EMAIL = cfg.endUserEmail;
        ORBIT_FLEET_MANAGED_HOST_IDENTITY_CERTIFICATE = lib.boolToString cfg.fleetManagedHostIdentityCertificate;
        ORBIT_HOST_IDENTIFIER = cfg.hostIdentifier;
        ORBIT_INSECURE = lib.boolToString cfg.insecure;
        ORBIT_FLEET_DESKTOP_ALTERNATIVE_BROWSER_HOST = cfg.desktop.alternativeBrowserHost;

        ORBIT_DISABLE_KEYSTORE = "true";
        ORBIT_DISABLE_UPDATES = "true";
        ORBIT_FLEET_DESKTOP = lib.boolToString cfg.desktop.enable;
        ORBIT_LOG_FILE = "/var/log/orbit/orbit.log";
        ORBIT_OSQUERY_DB = "/var/lib/orbit/osquery.db";
        ORBIT_ROOT_DIR = "/var/lib/orbit";
        NIX_ORBIT_OSQUERYD_PATH = lib.getExe' cfg.osqueryPackage "osqueryd";
        NIX_ORBIT_OSQUERY_LOG_PATH = "/var/log/orbit/osquery";
        NIX_ORBIT_DESKTOP_PATH = if cfg.desktop.enable then lib.getExe cfg.desktop.package else null;
      };

      serviceConfig = {
        ExecStart = lib.getExe cfg.orbitPackage;
        LoadCredential = [ "enroll-secret:${cfg.enrollSecretPath}" ];
        StateDirectory = "orbit";
        LogsDirectory = "orbit";
        TimeoutStartSec = 0;
        Restart = "always";
        RestartSec = 60;
        KillMode = "control-group";
        KillSignal = "SIGTERM";
      };
    };
  };
}