Certbot+DNS+flake upadte
This commit is contained in:
1 parent
b23ab9be23
commit
acafdbbdd2
20 files changed
+393
-180
No files matched your search
@@ -0,0 +1,74 @@
|
||||
{ config, lib, pkgs, ... }:
|
||||
|
||||
with lib;
|
||||
|
||||
let
|
||||
cfg = config.skyg.server.dns.certbot;
|
||||
in
|
||||
{
|
||||
options = {
|
||||
skyg.server.dns.certbot = {
|
||||
enable = mkEnableOption "Let's Encrypt certificate management with certbot for DNS domains";
|
||||
|
||||
publicDomains = mkOption {
|
||||
description = "List of public domains for which to obtain Let's Encrypt certificates";
|
||||
type = types.listOf types.str;
|
||||
default = [ ];
|
||||
};
|
||||
|
||||
email = mkOption {
|
||||
description = "Email address for Let's Encrypt account registration";
|
||||
type = types.str;
|
||||
example = "admin@example.com";
|
||||
};
|
||||
|
||||
testMode = mkOption {
|
||||
description = "Use certbot in test mode (dry-run) for development/testing";
|
||||
type = types.bool;
|
||||
default = false;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
config = mkIf cfg.enable {
|
||||
environment.systemPackages = [ pkgs.certbot pkgs.certbot-nginx ];
|
||||
|
||||
# Set up systemd service for certificate renewal
|
||||
systemd.services.certbot-renew = {
|
||||
description = "Renew Let's Encrypt certificates";
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
User = "root";
|
||||
ExecStart =
|
||||
if cfg.testMode then
|
||||
"${pkgs.certbot}/bin/certbot renew --dry-run --nginx"
|
||||
else
|
||||
"${pkgs.certbot}/bin/certbot renew --nginx";
|
||||
};
|
||||
};
|
||||
|
||||
# Set up systemd timer for certificate renewal
|
||||
systemd.timers.certbot-renew = {
|
||||
description = "Daily renewal of Let's Encrypt certificates";
|
||||
wantedBy = [ "timers.target" ];
|
||||
timerConfig = {
|
||||
OnCalendar = "daily";
|
||||
Persistent = true;
|
||||
};
|
||||
};
|
||||
|
||||
# Enable the renewal timer
|
||||
systemd.timers.certbot-renew.enable = true;
|
||||
|
||||
# Configure nginx with virtual hosts for public domains
|
||||
services.nginx = {
|
||||
enable = true;
|
||||
};
|
||||
|
||||
# Create a simple certbot command that can be run manually
|
||||
environment.shellAliases = {
|
||||
"certbot-get-certs" =
|
||||
"${pkgs.certbot}/bin/certbot certonly --nginx --non-interactive --agree-tos --email ${cfg.email} --expand ${concatMapStrings (domain: " --domains ${domain}") cfg.publicDomains}";
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -1,88 +1,8 @@
|
||||
{ config, lib, ... }:
|
||||
|
||||
with lib;
|
||||
|
||||
let
|
||||
cfg = config.skyg.server.dns;
|
||||
|
||||
# Each { domain, ip } generates both exact and wildcard dnsmasq address entries
|
||||
addressEntries = concatMap
|
||||
({ domain, ip }: [
|
||||
"/${domain}/${ip}"
|
||||
"/.${domain}/${ip}"
|
||||
])
|
||||
cfg.addresses;
|
||||
in
|
||||
{ ... }:
|
||||
{
|
||||
options = {
|
||||
skyg.server.dns = {
|
||||
enable = mkEnableOption "local DNS resolver via dnsmasq";
|
||||
|
||||
addresses = mkOption {
|
||||
description = "Static domain-to-IP mappings. Each entry generates both exact and wildcard dnsmasq address= entries.";
|
||||
type = types.listOf (types.submodule {
|
||||
options = {
|
||||
domain = mkOption { type = types.str; };
|
||||
ip = mkOption { type = types.str; };
|
||||
};
|
||||
});
|
||||
default = [ ];
|
||||
};
|
||||
|
||||
addressesSecretName = mkOption {
|
||||
description = ''
|
||||
Name of an agenix secret whose decrypted content is a dnsmasq config
|
||||
fragment (address= lines). Included via dnsmasq conf-file. Set null to skip.
|
||||
'';
|
||||
type = types.nullOr types.str;
|
||||
default = null;
|
||||
example = "dns-addresses.conf";
|
||||
};
|
||||
|
||||
upstreamServers = mkOption {
|
||||
description = "Upstream DNS servers to forward unresolved queries to.";
|
||||
type = types.listOf types.str;
|
||||
default = [ "1.1.1.1" "1.0.0.1" ];
|
||||
};
|
||||
|
||||
listenAddresses = mkOption {
|
||||
description = "IPs to bind. Empty list means listen on all interfaces (dnsmasq default).";
|
||||
type = types.listOf types.str;
|
||||
default = [ ];
|
||||
};
|
||||
|
||||
openFirewall = mkOption {
|
||||
description = "Open port 53 UDP/TCP in the firewall.";
|
||||
type = types.bool;
|
||||
default = false;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
config = mkIf cfg.enable {
|
||||
age.secrets = mkIf (cfg.addressesSecretName != null) {
|
||||
${cfg.addressesSecretName} = {
|
||||
file = ../../../../secrets + "/${cfg.addressesSecretName}.age";
|
||||
};
|
||||
};
|
||||
|
||||
services.dnsmasq = {
|
||||
enable = true;
|
||||
settings = {
|
||||
address = addressEntries;
|
||||
server = cfg.upstreamServers;
|
||||
}
|
||||
// optionalAttrs (cfg.listenAddresses != [ ]) {
|
||||
listen-address = cfg.listenAddresses;
|
||||
}
|
||||
// optionalAttrs (cfg.addressesSecretName != null) {
|
||||
conf-file = config.age.secrets.${cfg.addressesSecretName}.path;
|
||||
};
|
||||
};
|
||||
|
||||
networking.firewall = mkIf cfg.openFirewall {
|
||||
allowedUDPPorts = [ 53 ];
|
||||
allowedTCPPorts = [ 53 ];
|
||||
};
|
||||
};
|
||||
imports = [
|
||||
./certbot
|
||||
./routing
|
||||
./local-ca
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
{ config, lib, pkgs, ... }:
|
||||
|
||||
with lib;
|
||||
|
||||
let
|
||||
cfg = config.skyg.server.dns.local-ca;
|
||||
in
|
||||
{
|
||||
options = {
|
||||
skyg.server.dns.local-ca = {
|
||||
enable = mkEnableOption "Local Certificate Authority for DNS domains";
|
||||
|
||||
caCert = mkOption {
|
||||
description = "Path to the local CA certificate";
|
||||
type = types.str;
|
||||
default = "/etc/ssl/local-ca/cert.pem";
|
||||
};
|
||||
|
||||
caKey = mkOption {
|
||||
description = "Path to the local CA private key";
|
||||
type = types.str;
|
||||
default = "/etc/ssl/local-ca/key.pem";
|
||||
};
|
||||
|
||||
domain = mkOption {
|
||||
description = "Domain name for the local CA";
|
||||
type = types.str;
|
||||
default = "local-ca.skyg.internal";
|
||||
};
|
||||
|
||||
organizationalUnit = mkOption {
|
||||
description = "Organizational unit for the local CA certificate";
|
||||
type = types.str;
|
||||
default = "Skyg Internal CA";
|
||||
};
|
||||
|
||||
organization = mkOption {
|
||||
description = "Organization for the local CA certificate";
|
||||
type = types.str;
|
||||
default = "Skyg";
|
||||
};
|
||||
|
||||
country = mkOption {
|
||||
description = "Country code for the local CA certificate";
|
||||
type = types.str;
|
||||
default = "CA";
|
||||
};
|
||||
|
||||
email = mkOption {
|
||||
description = "Email address for the local CA";
|
||||
type = types.str;
|
||||
default = "admin@skyg.ca";
|
||||
};
|
||||
|
||||
certificateLifetime = mkOption {
|
||||
description = "Certificate lifetime in days";
|
||||
type = types.int;
|
||||
default = 365;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
config = mkIf cfg.enable {
|
||||
environment.systemPackages = [ pkgs.openssl ];
|
||||
|
||||
# Create directory for local CA
|
||||
environment.etc."ssl/local-ca" = {
|
||||
recursive = true;
|
||||
owner = "root";
|
||||
group = "root";
|
||||
mode = "0755";
|
||||
};
|
||||
|
||||
# Create local CA certificate (this would typically be generated by a script)
|
||||
# For now, we'll just set up the structure and configuration
|
||||
systemd.services.local-ca-setup = {
|
||||
description = "Setup local Certificate Authority";
|
||||
wantedBy = [ "multi-user.target" ];
|
||||
after = [ "network.target" ];
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
ExecStart = ''
|
||||
mkdir -p /etc/ssl/local-ca
|
||||
# In a real implementation, you would generate the CA certificate here
|
||||
# openssl req -new -x509 -nodes -days ${toString cfg.certificateLifetime} \
|
||||
# -subj "/C=${cfg.country}/O=${cfg.organization}/OU=${cfg.organizationalUnit}/CN=${cfg.domain}/emailAddress=${cfg.email}" \
|
||||
# -keyout ${cfg.caKey} -out ${cfg.caCert}
|
||||
'';
|
||||
User = "root";
|
||||
};
|
||||
};
|
||||
|
||||
# Configuration for services to trust local CA
|
||||
environment.etc."ssl/certs/ca-certificates.crt".text = ''
|
||||
# Local CA certificates would be added here
|
||||
# This is a placeholder for where local CA certs would be included
|
||||
'';
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
{ config, lib, ... }:
|
||||
|
||||
with lib;
|
||||
|
||||
let
|
||||
cfg = config.skyg.server.dns.routing;
|
||||
|
||||
# Each { domain, ip } generates both exact and wildcard dnsmasq address entries
|
||||
addressEntries = concatMap
|
||||
({ domain, ip }: [
|
||||
"/${domain}/${ip}"
|
||||
"/.${domain}/${ip}"
|
||||
])
|
||||
cfg.addresses;
|
||||
in
|
||||
{
|
||||
options = {
|
||||
skyg.server.dns.routing = {
|
||||
enable = mkEnableOption "local DNS resolver via dnsmasq";
|
||||
|
||||
addresses = mkOption {
|
||||
description = "Static domain-to-IP mappings. Each entry generates both exact and wildcard dnsmasq address= entries.";
|
||||
type = types.listOf (types.submodule {
|
||||
options = {
|
||||
domain = mkOption { type = types.str; };
|
||||
ip = mkOption { type = types.str; };
|
||||
};
|
||||
});
|
||||
default = [ ];
|
||||
};
|
||||
|
||||
addressesSecretName = mkOption {
|
||||
description = ''
|
||||
Name of an agenix secret whose decrypted content is a dnsmasq config
|
||||
fragment (address= lines). Included via dnsmasq conf-file. Set null to skip.
|
||||
'';
|
||||
type = types.nullOr types.str;
|
||||
default = null;
|
||||
example = "dns-addresses.conf";
|
||||
};
|
||||
|
||||
upstreamServers = mkOption {
|
||||
description = "Upstream DNS servers to forward unresolved queries to.";
|
||||
type = types.listOf types.str;
|
||||
default = [ "1.1.1.1" "1.0.0.1" ];
|
||||
};
|
||||
|
||||
listenAddresses = mkOption {
|
||||
description = "IPs to bind. Empty list means listen on all interfaces (dnsmasq default).";
|
||||
type = types.listOf types.str;
|
||||
default = [ ];
|
||||
};
|
||||
|
||||
openFirewall = mkOption {
|
||||
description = "Open port 53 UDP/TCP in the firewall.";
|
||||
type = types.bool;
|
||||
default = false;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
config = mkIf cfg.enable {
|
||||
age.secrets = mkIf (cfg.addressesSecretName != null) {
|
||||
${cfg.addressesSecretName} = {
|
||||
file = ../../../../secrets + "/${cfg.addressesSecretName}.age";
|
||||
};
|
||||
};
|
||||
|
||||
services.dnsmasq = {
|
||||
enable = true;
|
||||
settings = {
|
||||
address = addressEntries;
|
||||
server = cfg.upstreamServers;
|
||||
}
|
||||
// optionalAttrs (cfg.listenAddresses != [ ]) {
|
||||
listen-address = cfg.listenAddresses;
|
||||
}
|
||||
// optionalAttrs (cfg.addressesSecretName != null) {
|
||||
conf-file = config.age.secrets.${cfg.addressesSecretName}.path;
|
||||
};
|
||||
};
|
||||
|
||||
networking.firewall = mkIf cfg.openFirewall {
|
||||
allowedUDPPorts = [ 53 ];
|
||||
allowedTCPPorts = [ 53 ];
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user