Files
housecarl/scripts/fakejarl/main.go
T
abrown38andClaude Opus 5.5 68ba2d06c5 Sets from Jarl: sets holding the addresses of names in Jarl
A set can take its addresses from Jarl: every A and AAAA record of its
names (NAME@VIEW), CNAMEs followed within Jarl's zones in the same view.
housecarld reads them every jarl.refresh and after each change, keeps
only the addresses within the zones and networks the set allows, and
keeps what it last read (saved in the database and the boot ruleset)
while Jarl can't be reached. A change of the addresses applies the
ruleset again without a history entry and is audited (set.jarl).

hc set add --jarl --within, hc set jarl; the kind "from Jarl" and
Addresses… on the Sets page; GET /sets/{name}/jarl and /jarl/refresh;
validation codes set_jarl_*, status warnings jarl_*; docs/jarl.md.
e2e phase with scripts/fakejarl.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:51:55 +01:00

64 lines
2.0 KiB
Go

// Command fakejarl answers the part of Jarl's API that Housecarl's Jarl
// sets read (GET /zones, GET /zones/{id}/records), from a JSON file read
// again on each request, so the e2e test can change records:
//
// {"zones": [{"id": 1, "name": "example.com.", "view": "internal", "type": "primary"}],
// "records": {"1": [{"name": "nas.example.com.", "type": "A", "data": "10.0.0.5"}]}}
//
// Usage: fakejarl -listen 127.0.0.1:8053 -data jarl.json -token SECRET
package main
import (
"encoding/json"
"flag"
"log"
"net/http"
"os"
"strings"
)
type data struct {
Zones []map[string]any `json:"zones"`
Records map[string][]map[string]any `json:"records"`
}
func main() {
listen := flag.String("listen", "127.0.0.1:8053", "address to listen on")
file := flag.String("data", "jarl.json", "the zones and records")
token := flag.String("token", "", "the API token Housecarl must send")
flag.Parse()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer "+*token {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"error": {"code": "unauthorized", "message": "bad token"}}`))
return
}
b, err := os.ReadFile(*file)
var d data
if err == nil {
err = json.Unmarshal(b, &d)
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
p := strings.TrimPrefix(r.URL.Path, "/api/v1")
switch {
case p == "/zones":
json.NewEncoder(w).Encode(d.Zones)
case strings.HasPrefix(p, "/zones/") && strings.HasSuffix(p, "/records"):
id := strings.TrimSuffix(strings.TrimPrefix(p, "/zones/"), "/records")
recs := d.Records[id]
if recs == nil {
recs = []map[string]any{}
}
json.NewEncoder(w).Encode(recs)
default:
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{"error": {"code": "not_found", "message": "not found"}}`))
}
})
log.Fatal(http.ListenAndServe(*listen, nil))
}