2022-01-04 12:28:58 +01:00
|
|
|
# Configuration for oauth2_proxy, which is used as a handler for nginx
|
|
|
|
# auth-request setups.
|
|
|
|
#
|
|
|
|
# This module exports a helper function at
|
|
|
|
# `config.services.depot.oauth2_proxy.withAuth` that can be wrapped
|
|
|
|
# around nginx server configuration blocks to configure their
|
|
|
|
# authentication setup.
|
|
|
|
{ config, depot, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
description = "OAuth2 proxy to authenticate TVL services";
|
|
|
|
cfg = config.services.depot.oauth2_proxy;
|
|
|
|
configFile = pkgs.writeText "oauth2_proxy.cfg" ''
|
|
|
|
email_domains = [ "*" ]
|
|
|
|
http_address = "127.0.0.1:${toString cfg.port}"
|
|
|
|
provider = "keycloak-oidc"
|
|
|
|
client_id = "oauth2-proxy"
|
|
|
|
oidc_issuer_url = "https://auth.tvl.fyi/auth/realms/TVL"
|
|
|
|
reverse_proxy = true
|
|
|
|
set_xauthrequest = true
|
|
|
|
'';
|
2022-03-26 18:04:52 +01:00
|
|
|
|
|
|
|
# Depend on the Keycloak service if it is running on the same
|
|
|
|
# machine.
|
|
|
|
depends_on = lib.optional config.services.keycloak.enable "keycloak.service";
|
2022-01-30 17:06:58 +01:00
|
|
|
in
|
|
|
|
{
|
2022-01-04 12:28:58 +01:00
|
|
|
options.services.depot.oauth2_proxy = {
|
|
|
|
enable = lib.mkEnableOption description;
|
|
|
|
|
|
|
|
port = lib.mkOption {
|
|
|
|
description = "Port to listen on";
|
|
|
|
type = lib.types.int;
|
|
|
|
default = 2884; # "auth"
|
|
|
|
};
|
|
|
|
|
|
|
|
secretsFile = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "EnvironmentFile from which to load secrets";
|
2022-05-22 23:51:49 +02:00
|
|
|
default = config.age.secretsDir + "/oauth2_proxy";
|
2022-01-04 12:28:58 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
2022-01-17 12:43:06 +01:00
|
|
|
systemd.services.oauth2_proxy = {
|
2022-01-04 12:28:58 +01:00
|
|
|
inherit description;
|
2022-03-26 18:04:52 +01:00
|
|
|
after = depends_on;
|
|
|
|
wants = depends_on;
|
2022-01-04 12:28:58 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Restart = "always";
|
2022-05-26 22:47:40 +02:00
|
|
|
RestartSec = "5s";
|
2022-01-04 12:28:58 +01:00
|
|
|
DynamicUser = true;
|
|
|
|
EnvironmentFile = cfg.secretsFile;
|
2022-09-26 19:33:05 +02:00
|
|
|
ExecStart = "${pkgs.oauth2-proxy}/bin/oauth2-proxy --config ${configFile}";
|
2022-01-04 12:28:58 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|