summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/monitoring/go-csp-collector.nix
blob: 6fafbb8407402346d5ae94255442a5e71943ed98 (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
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.services.go-csp-collector;

  inherit (lib)
    boolToString
    concatStringsSep
    getExe
    isBool
    literalExpression
    maintainers
    mapAttrsToList
    mkEnableOption
    mkIf
    mkOption
    mkPackageOption
    types
    ;

  settingsToArgs =
    settings:
    concatStringsSep " " (
      mapAttrsToList (
        name: value:
        let
          flag = "-${name}";
        in
        if isBool value then "${flag}=${boolToString value}" else "${flag} ${toString value}"
      ) settings
    );
in
{
  meta.maintainers = with maintainers; [ stepbrobd ];

  options.services.go-csp-collector = {
    enable = mkEnableOption "go-csp-collector, a content security policy violation collector";

    package = mkPackageOption pkgs "go-csp-collector" { };

    settings = mkOption {
      type = types.submodule {
        freeformType =
          with types;
          attrsOf (oneOf [
            bool
            path
            str
          ]);

        options = {
          port = mkOption {
            type = types.port;
            description = "The port to listen on.";
            default = 8080;
            example = 8080;
          };

          output-format = mkOption {
            type = types.enum [
              "text"
              "json"
            ];
            description = "Define how the violation reports are formatted for output.";
            default = "text";
            example = "text";
          };
        };
      };

      description = ''
        Settings for go-csp-collector. See
        <https://github.com/jacobbednarz/go-csp-collector> for supported options.
      '';

      default = { };

      example = literalExpression ''
        {
          debug = true;
          health-check-path = "/health";
        }
      '';
    };
  };

  config = mkIf cfg.enable {
    systemd.packages = [ cfg.package ];
    systemd.services.go-csp-collector = {
      description = "CSP violation collector";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];
      serviceConfig = {
        ReadOnlyPaths = cfg.settings.filter-file or "";
        ExecStart = [
          ""
          "${getExe cfg.package} ${settingsToArgs cfg.settings}"
        ];
      };
    };
  };
}