dns + keybinding consolidation

This commit is contained in:
ari committed 2026-09-21 10:52:32 -04:00
1 parent df46627173
commit f8444b1549
23 files changed
+612 -109

No files matched your search

+7
View File
@@ -24,6 +24,12 @@ exported env vars before inlining each file's content via `builtins.readFile`.
## How it works
1. The router config lives as an **agenix secret** (e.g. `secrets/glmain.json.age`).
1b. Before generating, the decrypted JSON's `generalMappings` are **merged** (via `jq`) with
`$NIX_DNS_RECORDS` — the DNS records declared across all hosts through `skyg.dns`
(see `modules/nixos/common/dns-records/`) and aggregated by `modules/dns-aggregate.nix`
into `packages.<sys>.skyg-dns-records`. The secret stays the source of truth for
devices/MACs; **Nix owns service names**, so a container's DNS name lives next to the
host config that owns its IP. Inspect the aggregate with `nix eval .#dnsRecords --json`.
2. `openwrt-deploy` reads the decrypted JSON from stdin, runs `openwrt-gen dnsmasq`,
`openwrt-gen ethers`, and `openwrt-gen firewall` to render the three target files.
3. It SSHes to the router, shows a **colorized unified diff** against the current files
@@ -55,6 +61,7 @@ is passed as a command-line argument (default `glmain`).
```bash
skyg openwrt --dry-run
# → .tmp/openwrt-glmain/dnsmasq.conf
# → .tmp/openwrt-glmain/dns-records.json # the Nix-declared skyg.dns fragment merged in
# → .tmp/openwrt-glmain/ethers
# → .tmp/openwrt-glmain/firewall.batch # the exact `uci -m import firewall` input
```
+13
View File
@@ -17,6 +17,15 @@ in
config:
let
# Nix-generated DNS records (skyg.dns, aggregated across all hosts). Merged
# into the decrypted secret's generalMappings by the scripts below, so
# service names stay declarative next to the host config that owns the IP.
# Empty fragment when the caller doesn't supply one.
dnsRecords =
if config ? dnsRecords && config.dnsRecords != null
then config.dnsRecords
else builtins.toFile "skyg-dns-records-empty.json" (builtins.toJSON { generalMappings = [ ]; });
# Config JSON is read from stdin at deploy time — caller decrypts the age secret.
# Usage: age -d secrets/glmain.json.age | openwrt-deploy
#
@@ -24,10 +33,12 @@ let
# Nix-computed values it needs (router address, generator/apply-script store
# paths) and inlines the file's content after them.
deployScript = pkgs.writeShellScriptBin "openwrt-deploy" ''
export PATH="${lib.makeBinPath [ pkgs.jq ]}:$PATH"
export ROUTER="${config.router.ip}"
export ROUTER_USER="${config.router.user}"
export GENERATOR="${generator}"
export APPLY_SCRIPT="${applyScript}"
export NIX_DNS_RECORDS="${dnsRecords}"
${builtins.readFile ./openwrt-deploy.sh}
'';
@@ -35,7 +46,9 @@ let
# for review. No SSH, no uci, no router changes. Run from the repo root.
# The router name is passed as $1 (defaults to glmain) — see ./openwrt-dry-run.sh.
dryRunScript = pkgs.writeShellScriptBin "openwrt-dry-run" ''
export PATH="${lib.makeBinPath [ pkgs.jq ]}:$PATH"
export GENERATOR="${generator}"
export NIX_DNS_RECORDS="${dnsRecords}"
${builtins.readFile ./openwrt-dry-run.sh}
'';
in
+8 -1
View File
@@ -32,7 +32,14 @@ openwrt-gen firewall < config.json # prints UCI firewall rules (uci batch form
## Input shape (`config.rs`)
- `generalMappings`: `[{ ip, domains[] }]` — shared domain→IP records.
- `generalMappings`: `[{ ip, domains[], wildcard? }]` — shared domain→IP records.
`wildcard` defaults to `true` (omitted in existing secrets) and renders
`address=/domain/ip` + `address=/.domain/ip`, matching every subdomain too.
Set `false` for a plain `host-record=domain,ip` (A record + reverse PTR) —
this is what Nix-declared `skyg.dns` records use by default.
At deploy time this list is the **union** of the secret's entries and the
records aggregated from every host's `skyg.dns` (see
`modules/nixos/common/dns-records/`), merged by `jq` in `openwrt-deploy.sh`.
- `networks`: `{ <network>: [ { mac, name, id?, domains?, justMac?, internetOnly? } ] }` —
per-network hosts. A device with `internetOnly: true` is restricted to internet-only
access (matched by MAC), on any network.
+19
View File
@@ -17,6 +17,16 @@ pub struct Config {
pub struct GeneralMapping {
pub ip: String,
pub domains: Vec<String>,
/// When true (the default, kept for backwards compatibility with existing
/// router secrets), emit a wildcard `address=/domain/ip` that also matches
/// every subdomain. When false, emit a plain `host-record=domain,ip`,
/// which is a normal A record and also provides reverse PTR lookups.
#[serde(default = "default_true")]
pub wildcard: bool,
}
fn default_true() -> bool {
true
}
#[derive(Deserialize)]
@@ -77,6 +87,15 @@ mod tests {
let config: Config = serde_json::from_str(json).unwrap();
assert_eq!(config.general_mappings[0].ip, "192.168.1.1");
assert_eq!(config.general_mappings[0].domains, vec!["test.local"]);
// Existing secrets omit the flag; wildcard behaviour must be preserved.
assert!(config.general_mappings[0].wildcard);
}
#[test]
fn test_general_mapping_wildcard_false() {
let json = r#"{"generalMappings": [{"ip": "10.0.101.2", "domains": ["drawdb.app.internal"], "wildcard": false}], "networks": {}}"#;
let config: Config = serde_json::from_str(json).unwrap();
assert!(!config.general_mappings[0].wildcard);
}
#[test]
@@ -37,8 +37,13 @@ pub fn generate(config: &Config) -> String {
for gm in &config.general_mappings {
for domain in &gm.domains {
out.push_str(&format!("address=/{}/{}\n", domain, gm.ip));
out.push_str(&format!("address=/.{}/{}\n", domain, gm.ip));
if gm.wildcard {
out.push_str(&format!("address=/{}/{}\n", domain, gm.ip));
out.push_str(&format!("address=/.{}/{}\n", domain, gm.ip));
} else {
// Plain A record (+ reverse PTR), no subdomain wildcard.
out.push_str(&format!("host-record={},{}\n", domain, gm.ip));
}
}
}
@@ -78,6 +83,7 @@ mod tests {
general_mappings: vec![GeneralMapping {
ip: "192.168.1.1".to_string(),
domains: vec!["gw.local".to_string()],
wildcard: true,
}],
networks: BTreeMap::new(),
dns_resolvers: vec![],
@@ -85,6 +91,24 @@ mod tests {
};
let result = generate(&config);
assert!(result.contains("address=/gw.local/192.168.1.1\n"));
assert!(result.contains("address=/.gw.local/192.168.1.1\n"));
}
#[test]
fn test_general_mappings_host_record() {
let config = Config {
general_mappings: vec![GeneralMapping {
ip: "10.0.101.2".to_string(),
domains: vec!["drawdb.app.internal".to_string()],
wildcard: false,
}],
networks: BTreeMap::new(),
dns_resolvers: vec![],
internet_only: vec![],
};
let result = generate(&config);
assert!(result.contains("host-record=drawdb.app.internal,10.0.101.2\n"));
assert!(!result.contains("address=/"));
}
#[test]
+9 -1
View File
@@ -7,11 +7,19 @@
# ROUTER_USER - SSH user
# GENERATOR - store path of the openwrt-gen package (bin/openwrt-gen inside)
# APPLY_SCRIPT - store path of skyg-apply.sh
# NIX_DNS_RECORDS - store path of the Nix-generated generalMappings fragment
set -euo pipefail
SERVER="$ROUTER_USER@$ROUTER"
CONFIG=$(cat)
# Merge the Nix-declared DNS records (skyg.dns, aggregated across all hosts)
# into the secret's generalMappings. The secret stays the source of truth for
# devices/MACs; Nix owns service names.
CONFIG=$(cat | jq --slurpfile nix "$NIX_DNS_RECORDS" \
'.generalMappings = ((.generalMappings // []) + $nix[0].generalMappings)')
NIX_COUNT=$(jq '.generalMappings | length' "$NIX_DNS_RECORDS")
echo "Merged $NIX_COUNT Nix-declared DNS mapping(s) from skyg.dns."
DNSMASQ=$(echo "$CONFIG" | "$GENERATOR"/bin/openwrt-gen dnsmasq)
ETHERS=$(echo "$CONFIG" | "$GENERATOR"/bin/openwrt-gen ethers)
+9 -3
View File
@@ -3,14 +3,19 @@
# - no SSH, no uci, no router changes. Run from the repo root. Router name is
# passed as $1 (defaults to glmain).
#
# Expects GENERATOR to be exported by the Nix wrapper before this file's
# content runs (store path of the openwrt-gen package, bin/openwrt-gen inside).
# Expects GENERATOR and NIX_DNS_RECORDS to be exported by the Nix wrapper before
# this file's content runs (store paths of the openwrt-gen package and of the
# Nix-generated generalMappings fragment).
set -euo pipefail
if [ $# -ge 1 ]; then ROUTER="$1"; else ROUTER=glmain; fi
OUTDIR=".tmp/openwrt-$ROUTER"
mkdir -p "$OUTDIR"
CONFIG=$(cat)
# Same merge as the real deploy: secret config + Nix-declared skyg.dns records.
CONFIG=$(cat | jq --slurpfile nix "$NIX_DNS_RECORDS" \
'.generalMappings = ((.generalMappings // []) + $nix[0].generalMappings)')
cp "$NIX_DNS_RECORDS" "$OUTDIR/dns-records.json"
echo "$CONFIG" | "$GENERATOR"/bin/openwrt-gen dnsmasq > "$OUTDIR/dnsmasq.conf"
echo "$CONFIG" | "$GENERATOR"/bin/openwrt-gen ethers > "$OUTDIR/ethers"
@@ -20,3 +25,4 @@ echo "Dry-run configs written to $OUTDIR (no router changes made):"
echo " $OUTDIR/dnsmasq.conf"
echo " $OUTDIR/ethers"
echo " $OUTDIR/firewall.batch"
echo " $OUTDIR/dns-records.json # Nix-declared skyg.dns records merged in"