dotfiles/modules/nixos/services/cloudflare-tunnel.nix

88 lines
2.4 KiB
Nix
Raw Normal View History

2023-04-29 16:00:31 +00:00
{ config, lib, ... }:
# First time setup:
# nix-shell -p cloudflared
# cloudflared tunnel login
2023-06-19 12:30:30 +00:00
# cloudflared tunnel create <host>
# nix run github:nmasur/dotfiles#encrypt-secret > private/cloudflared-<host>.age
2023-04-29 16:00:31 +00:00
# Paste ~/.cloudflared/<id>.json
2023-06-19 12:30:30 +00:00
# Set tunnel.id = "<id>"
2023-04-29 16:00:31 +00:00
# Remove ~/.cloudflared/
2023-07-03 15:49:21 +00:00
# For SSH access:
# Cloudflare Zero Trust -> Access -> Applications -> Create Application
# Service Auth -> SSH -> Select Application -> Generate Certificate
# Set ca = "<public key>"
2023-06-19 12:30:30 +00:00
{
2023-04-29 16:00:31 +00:00
2023-06-19 12:30:30 +00:00
options.cloudflareTunnel = {
enable = lib.mkEnableOption "Use Cloudflare Tunnel";
id = lib.mkOption {
type = lib.types.str;
description = "Cloudflare tunnel ID";
};
credentialsFile = lib.mkOption {
type = lib.types.path;
description = "Cloudflare tunnel credentials file (age-encrypted)";
};
ca = lib.mkOption {
type = lib.types.str;
description = "Cloudflare tunnel CA public key";
};
};
2023-04-29 16:00:31 +00:00
2023-04-30 21:51:35 +00:00
config = lib.mkIf config.cloudflareTunnel.enable {
2023-04-29 16:00:31 +00:00
services.cloudflared = {
enable = true;
tunnels = {
2023-06-19 12:30:30 +00:00
"${config.cloudflareTunnel.id}" = {
2023-04-29 16:00:31 +00:00
credentialsFile = config.secrets.cloudflared.dest;
default = "http_status:404";
ingress = { "*.masu.rs" = "ssh://localhost:22"; };
};
};
};
environment.etc = {
"ssh/ca.pub".text = ''
2023-06-19 12:30:30 +00:00
${config.cloudflareTunnel.ca}
2023-04-29 16:00:31 +00:00
'';
# Must match the username of the email address in Cloudflare Access
"ssh/authorized_principals".text = ''
${config.user}
'';
};
services.openssh.extraConfig = ''
PubkeyAuthentication yes
TrustedUserCAKeys /etc/ssh/ca.pub
Match User '${config.user}'
AuthorizedPrincipalsFile /etc/ssh/authorized_principals
# if there is no existing AuthenticationMethods
AuthenticationMethods publickey
'';
services.openssh.settings.Macs =
[ "hmac-sha2-512" ]; # Fix for failure to find matching mac
2023-04-29 16:00:31 +00:00
# Create credentials file for Cloudflare
secrets.cloudflared = {
2023-06-19 12:30:30 +00:00
source = config.cloudflareTunnel.credentialsFile;
2023-04-29 16:00:31 +00:00
dest = "${config.secretsDirectory}/cloudflared";
owner = "cloudflared";
group = "cloudflared";
permissions = "0440";
};
systemd.services.cloudflared-secret = {
2023-06-19 12:30:30 +00:00
requiredBy =
[ "cloudflared-tunnel-${config.cloudflareTunnel.id}.service" ];
before = [ "cloudflared-tunnel-${config.cloudflareTunnel.id}.service" ];
2023-04-29 16:00:31 +00:00
};
};
}