Different blocking for different devices: steps and client groups
Client groups put devices (addresses, networks, or ACLs of them) at a blocking step: off (only your rules), normal (the default), or steps added above it, each including the lists of the steps below. Lists start at a step. In each view, a gate policy zone in front of a step's lists lets the devices below that step through (rpz-client-ip triggers with rpz-passthru), so every list is still loaded once. The IPv6 part of a gate leaves the IPv4-mapped range out: named matches IPv4 clients as ::ffff:a.b.c.d. Web UI: a Devices panel on the Blocking page and a step on each list. CLI: jarl block step, jarl block group, jarl blocklist --step. API: /blocking/steps, /blocking/groups. Database schema 8. Also: copying the state kept empty lists as empty (not nil), so reverting a changeset no longer adds restore operations for unchanged blocklists, rules and views. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5b8f681e2a
commit
76aaaf3ef5
33 files changed
+2096
-55
No files matched your search
@@ -335,7 +335,9 @@ func (l *limitedReader) Read(p []byte) (int, error) {
|
||||
|
||||
// CheckBlocking reports, for every view with blocking, whether name is
|
||||
// blocked or allowed there, and by which rule or list. It looks names up
|
||||
// the way named does: rules first, then the lists in order.
|
||||
// the way named does: rules first, then the lists in order. The answer is
|
||||
// for clients at the normal step; a list of a higher step that blocks the
|
||||
// name says so (Match.Step), and clients at step off skip every list.
|
||||
func (e *Engine) CheckBlocking(st *model.State, name string) ([]blocking.Match, error) {
|
||||
n, err := blocking.NormalizeName(name)
|
||||
if err != nil {
|
||||
@@ -349,8 +351,8 @@ func (e *Engine) CheckBlocking(st *model.State, name string) ([]blocking.Match,
|
||||
out = append(out, blocking.Match{View: z.View, Result: "none"})
|
||||
cur = &out[len(out)-1]
|
||||
}
|
||||
if cur.Result != "none" {
|
||||
continue // an earlier zone decided
|
||||
if cur.Result != "none" || z.IsGate() {
|
||||
continue // an earlier zone decided, or a gate (it never blocks)
|
||||
}
|
||||
if z.ListID == 0 {
|
||||
owner, act, ok := blocking.MatchRules(blocking.RuleEntries(render.ViewRules(st, z.ViewID)), n)
|
||||
@@ -372,7 +374,11 @@ func (e *Engine) CheckBlocking(st *model.State, name string) ([]blocking.Match,
|
||||
feeds[z.File] = entry
|
||||
}
|
||||
if entry != "" {
|
||||
cur.Result, cur.Entry, cur.List = "blocked", entry, st.Blocklists[z.ListID].Name
|
||||
b := st.Blocklists[z.ListID]
|
||||
cur.Result, cur.Entry, cur.List = "blocked", entry, b.Name
|
||||
if b.StepID != model.StepNormal {
|
||||
cur.Step = st.StepName(b.StepID)
|
||||
}
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
|
||||
@@ -117,6 +117,16 @@ func applyBlocklistPut(st *model.State, op model.Op, c *applyCtx) error {
|
||||
if in.Disabled != nil {
|
||||
b.Disabled = *in.Disabled
|
||||
}
|
||||
step, given, err := stepRef(st, in.Step, in.StepID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if given {
|
||||
if step == model.StepOff {
|
||||
return errf(KindBadRequest, "a blocklist starts at normal or a step above it, not off")
|
||||
}
|
||||
b.StepID = step
|
||||
}
|
||||
if in.Comment != nil {
|
||||
b.Comment = *in.Comment
|
||||
}
|
||||
@@ -226,6 +236,11 @@ func blockingUsers(st *model.State, viewID int64) []string {
|
||||
out = append(out, ruleLabel(r))
|
||||
}
|
||||
}
|
||||
for _, g := range st.SortedClientGroups() {
|
||||
if slices.Contains(g.ViewIDs, viewID) {
|
||||
out = append(out, "client group "+g.Name)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,263 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/netip"
|
||||
"strings"
|
||||
|
||||
"jarl/internal/model"
|
||||
)
|
||||
|
||||
// ---- blocking steps and client groups (docs/blocking.md)
|
||||
|
||||
// stepRef resolves a step given by name or ID. ok is false when neither
|
||||
// is given.
|
||||
func stepRef(st *model.State, name *string, id *int64) (int64, bool, error) {
|
||||
switch {
|
||||
case name != nil:
|
||||
s, found := st.StepByName(*name)
|
||||
if !found {
|
||||
return 0, true, errf(KindBadRequest, "blocking step %q does not exist (steps: %s)", *name, stepNames(st))
|
||||
}
|
||||
return s, true, nil
|
||||
case id != nil:
|
||||
if _, found := st.StepRank(*id); !found {
|
||||
return 0, true, errf(KindBadRequest, "blocking step %d does not exist", *id)
|
||||
}
|
||||
return *id, true, nil
|
||||
}
|
||||
return 0, false, nil
|
||||
}
|
||||
|
||||
// stepNames lists the steps for messages: "off, normal, strict".
|
||||
func stepNames(st *model.State) string {
|
||||
names := []string{"off", "normal"}
|
||||
for _, s := range st.OrderedBlockSteps() {
|
||||
names = append(names, s.Name)
|
||||
}
|
||||
return strings.Join(names, ", ")
|
||||
}
|
||||
|
||||
// NormalizeMember checks a client group member: an address, a prefix or
|
||||
// an ACL name (an ACL's own elements are checked by validation). It
|
||||
// returns the canonical form.
|
||||
func NormalizeMember(st *model.State, m string) (string, error) {
|
||||
m = strings.TrimSpace(m)
|
||||
if a, err := netip.ParseAddr(m); err == nil {
|
||||
if a.Zone() != "" {
|
||||
return "", errf(KindBadRequest, "%q: an address with a zone can't be matched", m)
|
||||
}
|
||||
return a.Unmap().String(), nil
|
||||
}
|
||||
if p, err := netip.ParsePrefix(m); err == nil {
|
||||
return p.Masked().String(), nil
|
||||
}
|
||||
if a := st.ACLByName(m); a != nil {
|
||||
return a.Name, nil
|
||||
}
|
||||
return "", errf(KindBadRequest, "%q is not an address, a prefix (10.10.0.0/24) or an ACL", m)
|
||||
}
|
||||
|
||||
func applyBlockStepPut(st *model.State, op model.Op, c *applyCtx) error {
|
||||
if err := c.require(blockingAction()); err != nil {
|
||||
return err
|
||||
}
|
||||
var in BlockStepInput
|
||||
if err := decode(op.Data, &in); err != nil {
|
||||
return err
|
||||
}
|
||||
var s *model.BlockStep
|
||||
if op.Kind == OpBlockStepCreate {
|
||||
if op.ID <= 0 || st.BlockSteps[op.ID] != nil {
|
||||
return errf(KindConflict, "step ID %d is already in use", op.ID)
|
||||
}
|
||||
if in.Name == nil {
|
||||
return errf(KindBadRequest, "a blocking step needs a name")
|
||||
}
|
||||
pos := 0
|
||||
for _, o := range st.BlockSteps {
|
||||
pos = max(pos, o.Position+1)
|
||||
}
|
||||
s = &model.BlockStep{ID: op.ID, Position: pos}
|
||||
} else {
|
||||
s = st.BlockSteps[op.ID]
|
||||
if s == nil {
|
||||
return errf(KindNotFound, "blocking step %d does not exist", op.ID)
|
||||
}
|
||||
if err := checkVersion(op, s.Version, "step "+s.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if in.Name != nil {
|
||||
name := strings.ToLower(strings.TrimSpace(*in.Name))
|
||||
if !blocklistNameRe.MatchString(name) || name == "off" || name == "normal" {
|
||||
return errf(KindBadRequest, "step name %q: use 1 to 40 lowercase letters, digits and hyphens, like strict (off and normal are built in)", *in.Name)
|
||||
}
|
||||
if id, found := st.StepByName(name); found && id != s.ID {
|
||||
return errf(KindConflict, "step %s already exists", name)
|
||||
}
|
||||
s.Name = name
|
||||
}
|
||||
if in.Position != nil {
|
||||
s.Position = *in.Position
|
||||
}
|
||||
if in.Comment != nil {
|
||||
s.Comment = *in.Comment
|
||||
}
|
||||
s.Touch(c.user(), c.now)
|
||||
st.BlockSteps[s.ID] = s
|
||||
return nil
|
||||
}
|
||||
|
||||
func applyBlockStepDelete(st *model.State, op model.Op, c *applyCtx) error {
|
||||
if err := c.require(blockingAction()); err != nil {
|
||||
return err
|
||||
}
|
||||
s := st.BlockSteps[op.ID]
|
||||
if s == nil {
|
||||
return errf(KindNotFound, "blocking step %d does not exist", op.ID)
|
||||
}
|
||||
if err := checkVersion(op, s.Version, "step "+s.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
if users := stepUsers(st, s.ID); len(users) > 0 {
|
||||
return errf(KindConflict, "step %s is used by %s; move them to another step first", s.Name, strings.Join(users, ", "))
|
||||
}
|
||||
delete(st.BlockSteps, s.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// stepUsers lists the blocklists and client groups at a step.
|
||||
func stepUsers(st *model.State, id int64) []string {
|
||||
var out []string
|
||||
for _, b := range st.SortedBlocklists() {
|
||||
if b.StepID == id {
|
||||
out = append(out, "blocklist "+b.Name)
|
||||
}
|
||||
}
|
||||
for _, g := range st.SortedClientGroups() {
|
||||
if g.StepID == id {
|
||||
out = append(out, "client group "+g.Name)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func applyClientGroupPut(st *model.State, op model.Op, c *applyCtx) error {
|
||||
if err := c.require(blockingAction()); err != nil {
|
||||
return err
|
||||
}
|
||||
var in ClientGroupInput
|
||||
if err := decode(op.Data, &in); err != nil {
|
||||
return err
|
||||
}
|
||||
var g *model.ClientGroup
|
||||
if op.Kind == OpClientGroupCreate {
|
||||
if op.ID <= 0 || st.ClientGroups[op.ID] != nil {
|
||||
return errf(KindConflict, "client group ID %d is already in use", op.ID)
|
||||
}
|
||||
if in.Name == nil {
|
||||
return errf(KindBadRequest, "a client group needs a name")
|
||||
}
|
||||
g = &model.ClientGroup{ID: op.ID, Members: []string{}, StepID: model.StepNormal, ViewIDs: []int64{}}
|
||||
} else {
|
||||
g = st.ClientGroups[op.ID]
|
||||
if g == nil {
|
||||
return errf(KindNotFound, "client group %d does not exist", op.ID)
|
||||
}
|
||||
if err := checkVersion(op, g.Version, "client group "+g.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if in.Name != nil {
|
||||
name := strings.ToLower(strings.TrimSpace(*in.Name))
|
||||
if !blocklistNameRe.MatchString(name) {
|
||||
return errf(KindBadRequest, "client group name %q: use 1 to 40 lowercase letters, digits and hyphens, like kids", *in.Name)
|
||||
}
|
||||
if other := st.ClientGroupByName(name); other != nil && other.ID != g.ID {
|
||||
return errf(KindConflict, "client group %s already exists", name)
|
||||
}
|
||||
g.Name = name
|
||||
}
|
||||
if in.Members != nil {
|
||||
members := []string{}
|
||||
seen := map[string]bool{}
|
||||
for _, m := range *in.Members {
|
||||
if strings.TrimSpace(m) == "" {
|
||||
continue
|
||||
}
|
||||
n, err := NormalizeMember(st, m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !seen[n] {
|
||||
seen[n] = true
|
||||
members = append(members, n)
|
||||
}
|
||||
}
|
||||
g.Members = members
|
||||
}
|
||||
step, given, err := stepRef(st, in.Step, in.StepID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if given {
|
||||
g.StepID = step
|
||||
}
|
||||
ids, err := viewIDs(st, in.Views, in.ViewIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ids != nil {
|
||||
g.ViewIDs = ids
|
||||
}
|
||||
if in.Comment != nil {
|
||||
g.Comment = *in.Comment
|
||||
}
|
||||
g.Touch(c.user(), c.now)
|
||||
st.ClientGroups[g.ID] = g
|
||||
return nil
|
||||
}
|
||||
|
||||
func applyClientGroupDelete(st *model.State, op model.Op, c *applyCtx) error {
|
||||
if err := c.require(blockingAction()); err != nil {
|
||||
return err
|
||||
}
|
||||
g := st.ClientGroups[op.ID]
|
||||
if g == nil {
|
||||
return errf(KindNotFound, "client group %d does not exist", op.ID)
|
||||
}
|
||||
if err := checkVersion(op, g.Version, "client group "+g.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
delete(st.ClientGroups, g.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ---- expansion
|
||||
|
||||
func (x *expander) blockStepCreate(req Request) ([]model.Op, error) {
|
||||
return x.simpleCreate(req, OpBlockStepCreate, func(data json.RawMessage) (string, error) {
|
||||
var in BlockStepInput
|
||||
if err := decode(data, &in); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if in.Name == nil {
|
||||
return "", errf(KindBadRequest, "a blocking step needs a name")
|
||||
}
|
||||
return "add blocking step " + strings.ToLower(*in.Name), nil
|
||||
})
|
||||
}
|
||||
|
||||
func (x *expander) clientGroupCreate(req Request) ([]model.Op, error) {
|
||||
return x.simpleCreate(req, OpClientGroupCreate, func(data json.RawMessage) (string, error) {
|
||||
var in ClientGroupInput
|
||||
if err := decode(data, &in); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if in.Name == nil {
|
||||
return "", errf(KindBadRequest, "a client group needs a name")
|
||||
}
|
||||
return "add client group " + strings.ToLower(*in.Name), nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"jarl/internal/model"
|
||||
)
|
||||
|
||||
func clientGroup(name, step string, members ...string) Request {
|
||||
return NewRequest(OpClientGroupCreate, 0, ClientGroupInput{Name: &name, Step: &step, Members: &members})
|
||||
}
|
||||
|
||||
func TestClientGroups(t *testing.T) {
|
||||
env := newEnv(t)
|
||||
setupZone(env)
|
||||
_, srv := newListServer(t, "||ads.example^\n")
|
||||
env.submit(env.admin,
|
||||
NewRequest(OpBlockStepCreate, 0, BlockStepInput{Name: model.Ptr("strict")}),
|
||||
NewRequest(OpACLCreate, 0, ACLInput{Name: model.Ptr("tablets"), Elements: &[]string{"10.0.0.50", "10.0.0.51"}}),
|
||||
)
|
||||
env.submit(env.admin,
|
||||
blocklist("ads", srv.URL+"/a.txt"),
|
||||
NewRequest(OpBlocklistCreate, 0, BlocklistInput{Name: model.Ptr("social"), URL: model.Ptr(srv.URL + "/s.txt"), Step: model.Ptr("strict")}),
|
||||
clientGroup("work", "off", "10.0.0.20"),
|
||||
clientGroup("kids", "strict", "tablets", "2001:db8:1::/64"),
|
||||
)
|
||||
conf, _ := os.ReadFile(filepath.Join(env.dir, "etc/named.conf"))
|
||||
c := string(conf)
|
||||
// Off clients pass before the normal lists, everyone below strict
|
||||
// before the strict ones; gates aren't logged.
|
||||
want := "response-policy {\n" +
|
||||
"\t\tzone \"off.gate.jarl.invalid\" min-update-interval 0 log no;\n" +
|
||||
"\t\tzone \"ads.blocklist.jarl.invalid\" min-update-interval 0 log no;\n" +
|
||||
"\t\tzone \"strict.gate.jarl.invalid\" min-update-interval 0 log no;\n" +
|
||||
"\t\tzone \"social.blocklist.jarl.invalid\" min-update-interval 0 log no;\n"
|
||||
if !strings.Contains(c, want) {
|
||||
t.Errorf("response-policy:\n%s", c)
|
||||
}
|
||||
gate := func(name string) string {
|
||||
b, err := os.ReadFile(filepath.Join(env.dir, "zones/_default", name+".gate.jarl.invalid.rpz"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
if g := gate("off"); !strings.Contains(g, "32.20.0.0.10.rpz-client-ip\tCNAME\trpz-passthru.\n") || strings.Count(g, "rpz-client-ip") != 1 {
|
||||
t.Errorf("off gate:\n%s", g)
|
||||
}
|
||||
// Everyone but the kids' tablets (via the ACL) and network passes.
|
||||
sg := gate("strict")
|
||||
for _, notIn := range []string{"32.50.0.0.10.rpz-client-ip", "32.51.0.0.10.rpz-client-ip", "64.zz.1.db8.2001.rpz-client-ip"} {
|
||||
if strings.Contains(sg, notIn) {
|
||||
t.Errorf("strict gate lets %s through:\n%s", notIn, sg)
|
||||
}
|
||||
}
|
||||
// 10.0.0.0/27 holds the work laptop, 10.0.0.52/30 the next addresses
|
||||
// after the tablets, 128.0.0.0/1 the upper half of IPv4.
|
||||
for _, in := range []string{"27.0.0.0.10.rpz-client-ip", "30.52.0.0.10.rpz-client-ip", "1.0.0.0.128.rpz-client-ip"} {
|
||||
if !strings.Contains(sg, in) {
|
||||
t.Errorf("strict gate lacks %s:\n%s", in, sg)
|
||||
}
|
||||
}
|
||||
|
||||
st := env.state()
|
||||
strict, _ := st.StepByName("strict")
|
||||
// A step that lists or groups use can't be deleted.
|
||||
if e := env.submitErr(env.admin, NewRequest(OpBlockStepDelete, strict, nil)); e.Kind != KindConflict || !strings.Contains(e.Message, "blocklist social") {
|
||||
t.Errorf("deleting a used step: %+v", e)
|
||||
}
|
||||
// One client, one group; ACLs must hold addresses only.
|
||||
if e := env.submitErr(env.admin, clientGroup("dup", "normal", "10.0.0.20")); e.Kind != KindValidation || !strings.Contains(e.Message, "10.0.0.20/32 is in client groups") {
|
||||
t.Errorf("an address in two groups: %+v", e)
|
||||
}
|
||||
env.submit(env.admin, NewRequest(OpACLCreate, 0, ACLInput{Name: model.Ptr("keyed"), Elements: &[]string{"localnets"}}))
|
||||
if e := env.submitErr(env.admin, clientGroup("bad", "normal", "keyed")); e.Kind != KindValidation || !strings.Contains(e.Message, "isn't an address") {
|
||||
t.Errorf("an ACL with a key: %+v", e)
|
||||
}
|
||||
for _, req := range []Request{clientGroup("x", "nope", "10.1.1.1"), clientGroup("y", "normal", "not-an-acl"), clientGroup("Bad Name", "normal")} {
|
||||
if e := env.submitErr(env.admin, req); e.Kind != KindBadRequest {
|
||||
t.Errorf("%s: %+v", req.Data, e)
|
||||
}
|
||||
}
|
||||
// A more specific address in another group is fine: it wins.
|
||||
env.submit(env.admin, clientGroup("guest", "normal", "2001:db8:1::9"))
|
||||
if sg := gate("strict"); !strings.Contains(sg, "128.9.zz.1.db8.2001.rpz-client-ip") {
|
||||
t.Errorf("the guest inside the kids' network passes the strict gate:\n%s", sg)
|
||||
}
|
||||
|
||||
// The kids' group gone: the strict list blocks nobody (a warning).
|
||||
kids := env.state().ClientGroupByName("kids")
|
||||
res := env.submit(env.admin, NewRequest(OpClientGroupDelete, kids.ID, nil))
|
||||
found := false
|
||||
for _, p := range res.Problems {
|
||||
found = found || p.Code == "blocklist_step_unused"
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("no warning for a step no group reaches: %+v", res.Problems)
|
||||
}
|
||||
// Reverting brings it back.
|
||||
rev, err := env.e.Revert(context.Background(), res.ChangesetID, env.admin)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(rev.Ops) != 1 {
|
||||
t.Errorf("revert ops: %+v (only the deleted group should come back)", rev.Ops)
|
||||
}
|
||||
if _, err := env.e.Apply(context.Background(), rev.ID, env.admin, ApplyOptions{}); err != nil || env.state().ClientGroupByName("kids") == nil {
|
||||
t.Fatalf("revert: %v", err)
|
||||
}
|
||||
// Renaming the ACL renames the member; deleting it is refused.
|
||||
tablets := env.state().ACLByName("tablets")
|
||||
env.submit(env.admin, NewRequest(OpACLUpdate, tablets.ID, ACLInput{Name: model.Ptr("pads")}))
|
||||
if g := env.state().ClientGroupByName("kids"); g.Members[0] != "pads" {
|
||||
t.Errorf("ACL rename: members %v", g.Members)
|
||||
}
|
||||
if e := env.submitErr(env.admin, NewRequest(OpACLDelete, tablets.ID, nil)); e.Kind != KindConflict || !strings.Contains(e.Message, "client group kids") {
|
||||
t.Errorf("deleting an ACL a group uses: %+v", e)
|
||||
}
|
||||
}
|
||||
@@ -104,9 +104,19 @@ func DiffOps(from, to *model.State) []model.Op {
|
||||
add(OpRecordUpdate, k, o.Version, in, fmt.Sprintf("restore %s %s %s", n.Name, n.Type, n.Data))
|
||||
}
|
||||
}
|
||||
// Steps before the lists and groups that use them.
|
||||
for _, k := range sortedKeys(to.BlockSteps) {
|
||||
n := to.BlockSteps[k]
|
||||
in := BlockStepInput{Name: &n.Name, Position: model.Ptr(n.Position), Comment: &n.Comment}
|
||||
if o, ok := from.BlockSteps[k]; !ok {
|
||||
add(OpBlockStepCreate, k, 0, in, "add blocking step "+n.Name)
|
||||
} else if !sameObject(o, n) {
|
||||
add(OpBlockStepUpdate, k, o.Version, in, "restore blocking step "+n.Name)
|
||||
}
|
||||
}
|
||||
for _, k := range sortedKeys(to.Blocklists) {
|
||||
n := to.Blocklists[k]
|
||||
in := BlocklistInput{Name: &n.Name, URL: &n.URL, ViewIDs: model.Ptr(append([]int64{}, n.ViewIDs...)), Disabled: &n.Disabled, Comment: &n.Comment}
|
||||
in := BlocklistInput{Name: &n.Name, URL: &n.URL, ViewIDs: model.Ptr(append([]int64{}, n.ViewIDs...)), Disabled: &n.Disabled, Comment: &n.Comment, StepID: model.Ptr(n.StepID)}
|
||||
if o, ok := from.Blocklists[k]; !ok {
|
||||
add(OpBlocklistCreate, k, 0, in, "add blocklist "+n.Name)
|
||||
} else if !sameObject(o, n) {
|
||||
@@ -122,7 +132,21 @@ func DiffOps(from, to *model.State) []model.Op {
|
||||
add(OpBlockRuleUpdate, k, o.Version, in, "restore rule: "+ruleLabel(n))
|
||||
}
|
||||
}
|
||||
for _, k := range sortedKeys(to.ClientGroups) {
|
||||
n := to.ClientGroups[k]
|
||||
in := ClientGroupInput{Name: &n.Name, Members: model.Ptr(append([]string{}, n.Members...)), StepID: model.Ptr(n.StepID), ViewIDs: model.Ptr(append([]int64{}, n.ViewIDs...)), Comment: &n.Comment}
|
||||
if o, ok := from.ClientGroups[k]; !ok {
|
||||
add(OpClientGroupCreate, k, 0, in, "add client group "+n.Name)
|
||||
} else if !sameObject(o, n) {
|
||||
add(OpClientGroupUpdate, k, o.Version, in, "restore client group "+n.Name)
|
||||
}
|
||||
}
|
||||
// Deletions, most dependent objects first.
|
||||
for _, k := range sortedKeys(from.ClientGroups) {
|
||||
if _, ok := to.ClientGroups[k]; !ok {
|
||||
add(OpClientGroupDelete, k, from.ClientGroups[k].Version, nil, "delete client group "+from.ClientGroups[k].Name)
|
||||
}
|
||||
}
|
||||
for _, k := range sortedKeys(from.BlockRules) {
|
||||
if _, ok := to.BlockRules[k]; !ok {
|
||||
add(OpBlockRuleDelete, k, from.BlockRules[k].Version, nil, "delete rule: "+ruleLabel(from.BlockRules[k]))
|
||||
@@ -133,6 +157,11 @@ func DiffOps(from, to *model.State) []model.Op {
|
||||
add(OpBlocklistDelete, k, from.Blocklists[k].Version, nil, "delete blocklist "+from.Blocklists[k].Name)
|
||||
}
|
||||
}
|
||||
for _, k := range sortedKeys(from.BlockSteps) {
|
||||
if _, ok := to.BlockSteps[k]; !ok {
|
||||
add(OpBlockStepDelete, k, from.BlockSteps[k].Version, nil, "delete blocking step "+from.BlockSteps[k].Name)
|
||||
}
|
||||
}
|
||||
for _, k := range sortedKeys(from.Records) {
|
||||
if _, ok := to.Records[k]; !ok {
|
||||
o := from.Records[k]
|
||||
|
||||
@@ -501,13 +501,15 @@ func (e *Engine) makePlan(ctx context.Context, base, work *model.State, user str
|
||||
if len(work.Zones) != len(base.Zones) {
|
||||
pl.configChanged = true
|
||||
}
|
||||
// Rules zones whose content changed. New ones come with a change to
|
||||
// named.conf, and reconfig loads them.
|
||||
// Rules zones and gates whose content changed. New ones come with a
|
||||
// change to named.conf, and reconfig loads them.
|
||||
for _, z := range pl.out.Policy {
|
||||
if z.ListID != 0 || !changedPaths[z.File] {
|
||||
continue
|
||||
}
|
||||
if slices.Contains(baseOut.Policy, z) {
|
||||
if slices.ContainsFunc(baseOut.Policy, func(o render.PolicyZone) bool {
|
||||
return o.Name == z.Name && o.ViewID == z.ViewID && o.File == z.File
|
||||
}) {
|
||||
pl.reloadPolicy = append(pl.reloadPolicy, z)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,6 +264,30 @@ func (x *expander) expand(req Request) ([]model.Op, error) {
|
||||
verb = "delete rule: "
|
||||
}
|
||||
return []model.Op{x.versioned(req, r.Version, verb+ruleLabel(r))}, nil
|
||||
case OpBlockStepCreate:
|
||||
return x.blockStepCreate(req)
|
||||
case OpBlockStepUpdate, OpBlockStepDelete:
|
||||
s := x.st.BlockSteps[req.ID]
|
||||
if s == nil {
|
||||
return nil, errf(KindNotFound, "blocking step %d does not exist", req.ID)
|
||||
}
|
||||
verb := "change blocking step "
|
||||
if req.Op == OpBlockStepDelete {
|
||||
verb = "delete blocking step "
|
||||
}
|
||||
return []model.Op{x.versioned(req, s.Version, verb+s.Name)}, nil
|
||||
case OpClientGroupCreate:
|
||||
return x.clientGroupCreate(req)
|
||||
case OpClientGroupUpdate, OpClientGroupDelete:
|
||||
g := x.st.ClientGroups[req.ID]
|
||||
if g == nil {
|
||||
return nil, errf(KindNotFound, "client group %d does not exist", req.ID)
|
||||
}
|
||||
verb := "change client group "
|
||||
if req.Op == OpClientGroupDelete {
|
||||
verb = "delete client group "
|
||||
}
|
||||
return []model.Op{x.versioned(req, g.Version, verb+g.Name)}, nil
|
||||
}
|
||||
return nil, errf(KindBadRequest, "unknown operation %q", req.Op)
|
||||
}
|
||||
|
||||
@@ -39,6 +39,13 @@ const (
|
||||
OpBlockRuleDelete = "blockrule.delete"
|
||||
OpBlockingPause = "blocking.pause"
|
||||
|
||||
OpBlockStepCreate = "blockstep.create"
|
||||
OpBlockStepUpdate = "blockstep.update"
|
||||
OpBlockStepDelete = "blockstep.delete"
|
||||
OpClientGroupCreate = "clientgroup.create"
|
||||
OpClientGroupUpdate = "clientgroup.update"
|
||||
OpClientGroupDelete = "clientgroup.delete"
|
||||
|
||||
// Requests only: disabling and enabling records, by ID or by match
|
||||
// (zone, name, type and optionally data), become record.update
|
||||
// operations.
|
||||
@@ -50,7 +57,8 @@ const (
|
||||
var OpKinds = []string{OpZoneCreate, OpZoneUpdate, OpZoneDelete, OpRecordCreate, OpRecordUpdate, OpRecordDelete, OpRRSetReplace,
|
||||
OpViewCreate, OpViewUpdate, OpViewDelete, OpViewReorder, OpACLCreate, OpACLUpdate, OpACLDelete,
|
||||
OpTSIGCreate, OpTSIGUpdate, OpTSIGDelete, OpOptionCreate, OpOptionUpdate, OpOptionDelete, OpSettingsUpdate,
|
||||
OpBlocklistCreate, OpBlocklistUpdate, OpBlocklistDelete, OpBlockRuleCreate, OpBlockRuleUpdate, OpBlockRuleDelete, OpBlockingPause}
|
||||
OpBlocklistCreate, OpBlocklistUpdate, OpBlocklistDelete, OpBlockRuleCreate, OpBlockRuleUpdate, OpBlockRuleDelete, OpBlockingPause,
|
||||
OpBlockStepCreate, OpBlockStepUpdate, OpBlockStepDelete, OpClientGroupCreate, OpClientGroupUpdate, OpClientGroupDelete}
|
||||
|
||||
// Request is an operation as submitted by a client. Requests are expanded
|
||||
// into one or more stored operations (for example an A record with "ptr"
|
||||
@@ -236,6 +244,31 @@ type BlocklistInput struct {
|
||||
ViewIDs *[]int64 `json:"view_ids,omitempty"`
|
||||
Disabled *bool `json:"disabled,omitempty"`
|
||||
Comment *string `json:"comment,omitempty"`
|
||||
// Step is the step the list starts at, by name ("normal" or a step
|
||||
// above it) or ID (StepID; 0 is normal).
|
||||
Step *string `json:"step,omitempty"`
|
||||
StepID *int64 `json:"step_id,omitempty"`
|
||||
}
|
||||
|
||||
// BlockStepInput creates or updates a blocking step above Normal.
|
||||
// Position orders the steps (new steps go on top).
|
||||
type BlockStepInput struct {
|
||||
Name *string `json:"name,omitempty"`
|
||||
Position *int `json:"position,omitempty"`
|
||||
Comment *string `json:"comment,omitempty"`
|
||||
}
|
||||
|
||||
// ClientGroupInput creates or updates a client group. Members are
|
||||
// addresses, prefixes or ACL names; the step is given by name ("off",
|
||||
// "normal" or a step) or ID; views as for BlocklistInput.
|
||||
type ClientGroupInput struct {
|
||||
Name *string `json:"name,omitempty"`
|
||||
Members *[]string `json:"members,omitempty"`
|
||||
Step *string `json:"step,omitempty"`
|
||||
StepID *int64 `json:"step_id,omitempty"`
|
||||
Views *[]string `json:"views,omitempty"`
|
||||
ViewIDs *[]int64 `json:"view_ids,omitempty"`
|
||||
Comment *string `json:"comment,omitempty"`
|
||||
}
|
||||
|
||||
// BlockRuleInput creates or updates a block or allow rule. Views as for
|
||||
|
||||
@@ -106,6 +106,14 @@ func applyOp(st *model.State, op model.Op, c *applyCtx) error {
|
||||
err = applyBlockRulePut(st, op, c)
|
||||
case OpBlockRuleDelete:
|
||||
err = applyBlockRuleDelete(st, op, c)
|
||||
case OpBlockStepCreate, OpBlockStepUpdate:
|
||||
err = applyBlockStepPut(st, op, c)
|
||||
case OpBlockStepDelete:
|
||||
err = applyBlockStepDelete(st, op, c)
|
||||
case OpClientGroupCreate, OpClientGroupUpdate:
|
||||
err = applyClientGroupPut(st, op, c)
|
||||
case OpClientGroupDelete:
|
||||
err = applyClientGroupDelete(st, op, c)
|
||||
case OpBlockingPause:
|
||||
err = applyBlockingPause(st, op, c)
|
||||
default:
|
||||
|
||||
@@ -152,6 +152,11 @@ func FindRefs(st *model.State, kind refKind, name string) []string {
|
||||
out = append(out, "view "+v.Name)
|
||||
}
|
||||
}
|
||||
for _, g := range st.SortedClientGroups() {
|
||||
if _, hit := rewriteElements(g.Members, kind, name, name); hit {
|
||||
out = append(out, "client group "+g.Name)
|
||||
}
|
||||
}
|
||||
var opts []string
|
||||
for _, o := range st.Options {
|
||||
stmt, err := namedconf.ParseOptionValue(o.Name, o.Value)
|
||||
@@ -188,6 +193,12 @@ func renameRefs(st *model.State, kind refKind, old, new string, c *applyCtx) {
|
||||
v.Touch(c.user(), c.now)
|
||||
}
|
||||
}
|
||||
for _, g := range st.ClientGroups {
|
||||
if l, hit := rewriteElements(g.Members, kind, old, new); hit {
|
||||
g.Members = l
|
||||
g.Touch(c.user(), c.now)
|
||||
}
|
||||
}
|
||||
for _, o := range st.Options {
|
||||
stmt, err := namedconf.ParseOptionValue(o.Name, o.Value)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user