Skip to content

Commit

Permalink
refactor: add common caddy configuration profile
Browse files Browse the repository at this point in the history
Additionally, prevent the caddy admin endpoint from being exposed to
localhost.
  • Loading branch information
diogotcorreia committed Apr 30, 2024
1 parent f46cede commit 38a75c9
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 86 deletions.
42 changes: 2 additions & 40 deletions hosts/bro/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
(inputs.nixpkgs-unstable + "/nixos/modules/services/misc/cfdyndns.nix")
]
++ (with profiles; [
caddy.rproxy
services.caddy.common
services.caddy.rproxy
services.ssh
]);

Expand Down Expand Up @@ -58,45 +59,6 @@
# Specific packages for this host
hm.home.packages = with pkgs; [];

# Caddy (web server)
networking.firewall.allowedTCPPorts = [80 443];
services.caddy = {
enable = true;
extraConfig = ''
# Rules for services behind Cloudflare proxy
(CLOUDFLARE_PROXY) {
header_up X-Forwarded-For {http.request.header.CF-Connecting-IP}
}
# Rules for services behind Nebula VPN (192.168.100.1/24)
(NEBULA) {
# Nebula
@not-nebula not remote_ip 192.168.100.1/24
abort @not-nebula
}
# Rules for services behind Authelia
(AUTHELIA) {
@not_healthchecks {
not {
method GET
path /
remote_ip 192.168.100.7 # phobos
}
}
forward_auth @not_healthchecks 192.168.100.1:9091 {
uri /api/verify?rd=https://auth.diogotc.com/
copy_headers Remote-User Remote-Groups Remote-Name Remote-Email
}
}
'';
};
users.users.caddy.extraGroups = [config.security.acme.defaults.group];

# Ensure nginx isn't turned on by some services (e.g. services using PHP)
services.nginx.enable = lib.mkForce false;

# ACME certificates
security.acme = {
acceptTerms = true;
Expand Down
40 changes: 1 addition & 39 deletions hosts/hera/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
(inputs.nixpkgs-unstable + "/nixos/modules/virtualisation/oci-containers.nix")
]
++ (with profiles; [
services.caddy.common
services.ssh
]);

Expand Down Expand Up @@ -86,45 +87,6 @@
# Keep laptop on when lid is closed
services.logind.lidSwitch = "ignore";

# Caddy (web server)
networking.firewall.allowedTCPPorts = [80 443];
services.caddy = {
enable = true;
extraConfig = ''
# Rules for services behind Cloudflare proxy
(CLOUDFLARE_PROXY) {
header_up X-Forwarded-For {http.request.header.CF-Connecting-IP}
}
# Rules for services behind Nebula VPN (192.168.100.1/24)
(NEBULA) {
# Nebula + Docker
@not-nebula not remote_ip 192.168.100.1/24 172.16.0.0/12
abort @not-nebula
}
# Rules for services behind Authelia
(AUTHELIA) {
@not_healthchecks {
not {
method GET
path /
remote_ip 192.168.100.7 # phobos
}
}
forward_auth @not_healthchecks 192.168.100.1:9091 {
uri /api/verify?rd=https://auth.diogotc.com/
copy_headers Remote-User Remote-Groups Remote-Name Remote-Email
}
}
'';
};
users.users.caddy.extraGroups = [config.security.acme.defaults.group];

# Ensure nginx isn't turned on by some services (e.g. services using PHP)
services.nginx.enable = lib.mkForce false;

# ACME certificates
security.acme = {
acceptTerms = true;
Expand Down
8 changes: 1 addition & 7 deletions hosts/phobos/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
...
}: {
imports = with profiles; [
services.caddy.common
services.ssh
];

Expand Down Expand Up @@ -59,13 +60,6 @@
# Specific packages for this host
hm.home.packages = with pkgs; [];

# Caddy (web server)
networking.firewall.allowedTCPPorts = [80 443];
services.caddy = {
enable = true;
email = "phobos-lets-encrypt@diogotc.com";
};

# PostgreSQL
services.postgresql.enable = true;

Expand Down
53 changes: 53 additions & 0 deletions profiles/services/caddy/common.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Common configuration for caddy reverse proxy
{config, lib, ...}: {
# Open firewall ports
networking.firewall.allowedTCPPorts = [80 443];

services.caddy = {
enable = true;
# TODO remove email once all servers have been migrated to lego (security.acme)
email = "${config.networking.hostName}-lets-encrypt@diogotc.com";
extraConfig = ''
# Rules for services behind Cloudflare proxy
(CLOUDFLARE_PROXY) {
header_up X-Forwarded-For {http.request.header.CF-Connecting-IP}
}
# Rules for services behind Nebula VPN (192.168.100.1/24)
(NEBULA) {
# Nebula + Docker
@not-nebula not remote_ip 192.168.100.1/24 172.16.0.0/12
abort @not-nebula
}
# Rules for services behind Authelia
(AUTHELIA) {
@not_healthchecks {
not {
method GET
path /
remote_ip 192.168.100.7 # phobos
}
}
forward_auth @not_healthchecks 192.168.100.1:9091 {
uri /api/verify?rd=https://auth.diogotc.com/
copy_headers Remote-User Remote-Groups Remote-Name Remote-Email
}
}
'';
};
users.users.caddy.extraGroups = [config.security.acme.defaults.group];

# Restrict caddy admin endpoint to the caddy user
systemd.services.caddy = {
environment = {
CADDY_ADMIN = "unix///run/caddy/caddy.sock";
};
serviceConfig = {
RuntimeDirectory = "caddy";
};
};

# Ensure nginx isn't turned on by some services (e.g. services using PHP)
services.nginx.enable = lib.mkForce false;
}
File renamed without changes.

0 comments on commit 38a75c9

Please sign in to comment.