about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/web-servers/rustus.nix
blob: 95c9a6455579393b40a7ec010baa38e8fc8c71e1 (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
{ lib, pkgs, config, ... }:
with lib;
let
  cfg = config.services.rustus;
in
{
  meta.maintainers = with maintainers; [ happysalada ];

  options.services.rustus = {

    enable = mkEnableOption (lib.mdDoc "TUS protocol implementation in Rust.");

    host = mkOption {
      type = types.str;
      description = lib.mdDoc ''
        The host that rustus will connect to.
      '';
      default = "127.0.0.1";
      example = "127.0.0.1";
    };

    port = mkOption {
      type = types.port;
      description = lib.mdDoc ''
        The port that rustus will connect to.
      '';
      default = 1081;
      example = 1081;
    };

    log_level = mkOption {
      type = types.enum [ "DEBUG" "INFO" "ERROR" ];
      description = lib.mdDoc ''
        Desired log level
      '';
      default = "INFO";
      example = "ERROR";
    };

    max_body_size = mkOption {
      type = types.str;
      description = lib.mdDoc ''
        Maximum body size in bytes
      '';
      default = "10000000"; # 10 mb
      example = "100000000";
    };

    url = mkOption {
      type = types.str;
      description = lib.mdDoc ''
        url path for uploads
      '';
      default = "/files";
    };

    disable_health_access_logs = mkOption {
      type = types.bool;
      description = lib.mdDoc ''
        disable access log for /health endpoint
      '';
      default = false;
    };

    cors = mkOption {
      type = types.listOf types.str;
      description = lib.mdDoc ''
        list of origins allowed to upload
      '';
      default = ["*"];
      example = ["*.staging.domain" "*.prod.domain"];
    };

    tus_extensions = mkOption {
      type = types.listOf (types.enum [
        "getting"
        "creation"
        "termination"
        "creation-with-upload"
        "creation-defer-length"
        "concatenation"
        "checksum"
      ]);
      description = lib.mdDoc ''
        Since TUS protocol offers extensibility you can turn off some protocol extensions.
      '';
      default = [
        "getting"
        "creation"
        "termination"
        "creation-with-upload"
        "creation-defer-length"
        "concatenation"
        "checksum"
      ];
    };

    remove_parts = mkOption {
      type = types.bool;
      description = lib.mdDoc ''
        remove parts files after successful concatenation
      '';
      default = true;
      example = false;
    };

    storage = lib.mkOption {
      description = lib.mdDoc ''
        Storages are used to actually store your files. You can configure where you want to store files.
      '';
      default = {};
      example = lib.literalExpression ''
        {
          type = "hybrid-s3"
          s3_access_key_file = konfig.age.secrets.R2_ACCESS_KEY.path;
          s3_secret_key_file = konfig.age.secrets.R2_SECRET_KEY.path;
          s3_bucket = "my_bucket";
          s3_url = "https://s3.example.com";
        }
      '';
      type = lib.types.submodule {
        options = {
          type = lib.mkOption {
            type = lib.types.enum ["file-storage" "hybrid-s3"];
            description = lib.mdDoc "Type of storage to use";
          };
          s3_access_key_file = lib.mkOption {
            type = lib.types.str;
            description = lib.mdDoc "File path that contains the S3 access key.";
          };
          s3_secret_key_file = lib.mkOption {
            type = lib.types.path;
            description = lib.mdDoc "File path that contains the S3 secret key.";
          };
          s3_region = lib.mkOption {
            type = lib.types.str;
            default = "us-east-1";
            description = lib.mdDoc "S3 region name.";
          };
          s3_bucket = lib.mkOption {
            type = lib.types.str;
            description = lib.mdDoc "S3 bucket.";
          };
          s3_url = lib.mkOption {
            type = lib.types.str;
            description = lib.mdDoc "S3 url.";
          };

          force_sync = lib.mkOption {
            type = lib.types.bool;
            description = lib.mdDoc "calls fsync system call after every write to disk in local storage";
            default = true;
          };
          data_dir = lib.mkOption {
            type = lib.types.str;
            description = lib.mdDoc "path to the local directory where all files are stored";
            default = "/var/lib/rustus";
          };
          dir_structure = lib.mkOption {
            type = lib.types.str;
            description = lib.mdDoc "pattern of a directory structure locally and on s3";
            default = "{year}/{month}/{day}";
          };
        };
      };
    };

    info_storage = lib.mkOption {
      description = lib.mdDoc ''
        Info storages are used to store information about file uploads. These storages must be persistent, because every time chunk is uploaded rustus updates information about upload. And when someone wants to download file, information about it requested from storage to get actual path of an upload.
      '';
      default = {};
      type = lib.types.submodule {
        options = {
          type = lib.mkOption {
            type = lib.types.enum ["file-info-storage"];
            description = lib.mdDoc "Type of info storage to use";
            default = "file-info-storage";
          };
          dir = lib.mkOption {
            type = lib.types.str;
            description = lib.mdDoc "directory to store info about uploads";
            default = "/var/lib/rustus";
          };
        };
      };
    };
  };

  config = lib.mkIf cfg.enable {

    systemd.services.rustus =
      let
        isHybridS3 = cfg.storage.type == "hybrid-s3";
      in
    {
      description = "Rustus server";
      documentation = [ "https://s3rius.github.io/rustus/" ];

      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      environment = {
        RUSTUS_SERVER_HOST = cfg.host;
        RUSTUS_SERVER_PORT = toString cfg.port;
        RUSTUS_LOG_LEVEL = cfg.log_level;
        RUSTUS_MAX_BODY_SIZE = cfg.max_body_size;
        RUSTUS_URL = cfg.url;
        RUSTUS_DISABLE_HEALTH_ACCESS_LOG = lib.mkIf cfg.disable_health_access_logs "true";
        RUSTUS_CORS = lib.concatStringsSep "," cfg.cors;
        RUSTUS_TUS_EXTENSIONS = lib.concatStringsSep "," cfg.tus_extensions;
        RUSTUS_REMOVE_PARTS= if cfg.remove_parts then "true" else "false";
        RUSTUS_STORAGE = cfg.storage.type;
        RUSTUS_DATA_DIR = cfg.storage.data_dir;
        RUSTUS_DIR_STRUCTURE = cfg.storage.dir_structure;
        RUSTUS_FORCE_FSYNC = if cfg.storage.force_sync then "true" else "false";
        RUSTUS_S3_URL = mkIf isHybridS3 cfg.storage.s3_url;
        RUSTUS_S3_BUCKET = mkIf isHybridS3 cfg.storage.s3_bucket;
        RUSTUS_S3_REGION = mkIf isHybridS3 cfg.storage.s3_region;
        RUSTUS_S3_ACCESS_KEY_PATH = mkIf isHybridS3 "%d/S3_ACCESS_KEY_PATH";
        RUSTUS_S3_SECRET_KEY_PATH = mkIf isHybridS3 "%d/S3_SECRET_KEY_PATH";
        RUSTUS_INFO_STORAGE = cfg.info_storage.type;
        RUSTUS_INFO_DIR = cfg.info_storage.dir;
      };

      serviceConfig = {
        ExecStart = "${pkgs.rustus}/bin/rustus";
        StateDirectory = "rustus";
        DynamicUser = true;
        LoadCredential = lib.optionals isHybridS3 [
          "S3_ACCESS_KEY_PATH:${cfg.storage.s3_access_key_file}"
          "S3_SECRET_KEY_PATH:${cfg.storage.s3_secret_key_file}"
        ];
        # hardening
        RestrictRealtime=true;
        RestrictNamespaces=true;
        LockPersonality=true;
        ProtectKernelModules=true;
        ProtectKernelTunables=true;
        ProtectKernelLogs=true;
        ProtectControlGroups=true;
        ProtectHostUserNamespaces=true;
        ProtectClock=true;
        RestrictSUIDSGID=true;
        SystemCallArchitectures="native";
        CapabilityBoundingSet="";
        ProtectProc = "invisible";
        # TODO consider SystemCallFilter LimitAS ProcSubset
      };
    };
  };
}