Skip to content

Commit f8228bf

Browse files
committed
add nginx in front
1 parent e425721 commit f8228bf

File tree

5 files changed

+64
-19
lines changed

5 files changed

+64
-19
lines changed

nixos-modules/profiles/auth.nix

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ in
1313
};
1414
listen_http = mkOption {
1515
type = types.str;
16-
default = "0.0.0.0:9000";
16+
default = "127.0.0.1:9000";
1717
};
1818
listen_metrics = mkOption {
1919
type = types.str;
20-
default = "0.0.0.0:9300";
20+
default = "127.0.0.1:9300";
2121
};
2222
};
2323
};
@@ -33,6 +33,13 @@ in
3333
};
3434
};
3535

36+
services.nginx.virtualHosts.${cfg.domain} = {
37+
locations."/" = {
38+
proxyWebsockets = true;
39+
proxyPass = "http://" + cfg.listen_http;
40+
};
41+
};
42+
3643
services.prometheus.scrapeConfigs = [{ job_name = "authentik"; static_configs = [{ targets = [ cfg.listen_metrics ]; }]; }];
3744
};
3845
}

nixos-modules/profiles/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
imports = [
33
./auth.nix
4+
./ingress.nix
45
./monitoring.nix
56
];
67
}

nixos-modules/profiles/ingress.nix

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{ config, lib, ... }:
2+
3+
let
4+
cfg = config.profiles.ingress;
5+
inherit (lib) types mkOption mkEnableOption mkIf concatMapAttrs;
6+
in
7+
{
8+
options = {
9+
profiles.ingress = {
10+
enable = mkEnableOption "ingress";
11+
};
12+
};
13+
14+
config = mkIf cfg.enable {
15+
services = {
16+
nginx = {
17+
enable = true;
18+
19+
statusPage = true;
20+
21+
recommendedGzipSettings = true;
22+
recommendedOptimisation = true;
23+
recommendedTlsSettings = true;
24+
recommendedProxySettings = true;
25+
26+
virtualHosts._.default = true;
27+
};
28+
};
29+
};
30+
}

nixos-modules/profiles/monitoring.nix

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ in
4242
server = {
4343
inherit (cfg) domain root_url;
4444
http_port = 3000;
45-
http_addr = "0.0.0.0";
45+
http_addr = "127.0.0.1";
4646
};
4747
"auth.generic_oauth" = {
4848
enabled = true;
@@ -72,10 +72,10 @@ in
7272
};
7373
};
7474

75-
services.nginx.virtualHosts."grafana.localho.st" = {
75+
services.nginx.virtualHosts.${cfg.domain} = {
7676
locations."/" = {
7777
proxyWebsockets = true;
78-
proxyPass = "http://127.0.0.1:${toString config.services.grafana.settings.server.http_port}/";
78+
proxyPass = "http://${config.services.grafana.settings.server.http_addr}:${toString config.services.grafana.settings.server.http_port}/";
7979
};
8080
};
8181
};

tests/monitoring-auth.nix

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
2121

2222
profiles.auth = {
2323
enable = true;
24+
domain = "authentik.localho.st";
2425
};
26+
27+
profiles.ingress.enable = true;
28+
2529
services.authentik.environmentFile = builtins.toFile "authentik-env-file" ''
2630
AUTHENTIK_SECRET_KEY=qwerty123456
2731
AUTHENTIK_BOOTSTRAP_PASSWORD=password
2832
AUTHENTIK_BOOTSTRAP_TOKEN=token
2933
'';
34+
3035
services.authentik.blueprints = [{
3136
metadata.name = "grafana-oauth";
3237
entries = [
@@ -53,7 +58,7 @@
5358
sub_mode = "hashed_user_id";
5459
include_claims_in_id_token = true;
5560
issuer_mode = "per_provider";
56-
redirect_uris = "http://localhost:3000/login/generic_oauth";
61+
redirect_uris = "http://grafana.localho.st/login/generic_oauth";
5762
};
5863
}
5964
{
@@ -71,48 +76,50 @@
7176
}];
7277
profiles.monitoring = {
7378
enable = true;
74-
domain = "localhost";
79+
domain = "grafana.localho.st";
80+
root_url = "%(protocol)s://%(domain)s/";
7581
oauth = {
7682
name = "Authentik";
7783
client_id_file = builtins.toFile "grafana-client-id" "grafana";
7884
client_secret_file = builtins.toFile "grafana-client-secret" "secret";
79-
auth_url = "http://127.0.0.1:9000/application/o/authorize/";
80-
token_url = "http://127.0.0.1:9000/application/o/token/";
81-
api_url = "http://127.0.0.1:9000/application/o/userinfo/";
85+
auth_url = "http://authentik.localho.st/application/o/authorize/";
86+
token_url = "http://authentik.localho.st/application/o/token/";
87+
api_url = "http://authentik.localho.st/application/o/userinfo/";
8288
};
8389
};
8490
};
8591

8692
extraPythonPackages = p: [ p.playwright ];
8793

8894
testScript = ''
95+
import os
96+
from playwright.sync_api import sync_playwright, expect
97+
8998
start_all()
9099
100+
machine.forward_port(80, 80)
101+
91102
with subtest("Wait for authentik services to start"):
92103
machine.wait_for_unit("postgresql.service")
93104
machine.wait_for_unit("redis-authentik.service")
94105
machine.wait_for_unit("authentik-migrate.service")
95106
machine.wait_for_unit("authentik-worker.service")
96107
machine.wait_for_unit("authentik.service")
108+
machine.wait_for_unit("nginx.service")
97109
98110
with subtest("Wait for Authentik itself to initialize"):
99111
machine.wait_for_open_port(9000)
100-
machine.wait_until_succeeds("curl -fL http://localhost:9000/if/flow/initial-setup/ >&2")
112+
machine.wait_until_succeeds("curl -fL http://authentik.localho.st/if/flow/initial-setup/ >&2")
101113
102114
with subtest("Wait for Authentik blueprints to be applied"):
103-
machine.wait_until_succeeds("curl -f http://localhost:9000/application/o/grafana/.well-known/openid-configuration >&2")
104-
105-
machine.forward_port(3000, 3000)
106-
machine.forward_port(9000, 9000)
107-
108-
from playwright.sync_api import sync_playwright, expect
115+
machine.wait_until_succeeds("curl -f http://authentik.localho.st/application/o/grafana/.well-known/openid-configuration >&2")
109116
110117
with sync_playwright() as p:
111-
browser = p.chromium.launch()
118+
browser = p.chromium.launch(headless=os.environ.get("HEADLESS", "true") != "false")
112119
page = browser.new_page()
113120
114121
with subtest("Login page"):
115-
page.goto("http://localhost:3000/login")
122+
page.goto("http://grafana.localho.st/login")
116123
page.reload()
117124
page.get_by_role("link", name="Sign in with Authentik").click()
118125
with subtest("Enter username"):

0 commit comments

Comments
 (0)