systemd-resolved: the guard applies the uplink's names every round (issue 960)
Each round it finds the link carrying the default route (the lowest-metric kernel default route that is neither one route routed nor the private network's interface), gives it the names from uplink-names's file plus its own search and routing domains, and takes them off the link they were on before when that differs. What it applied is kept in uplink-names.applied, read at its own start so a restart never infers which link to take them off, and a resolved refusal is journalled once per change, not every round. The guard's own names are left out of whether a link is judged `route` routed, so the fallback still gives a link holding only them its default route back while the mesh's resolvers are silent (ADR 0688 stands). Builds: design 08 sign-in-page §7, §8
This commit is contained in:
@@ -94,6 +94,11 @@ type Guard struct {
|
||||
// silent is whether none of the mesh's resolvers answered at the last look, so each turn is said once.
|
||||
// Only the routes' loop reads and writes it.
|
||||
silent bool
|
||||
|
||||
// applied is what the guard last gave the link carrying the default route (design 08 sign-in-page §8):
|
||||
// read at start from UplinkNamesAppliedFile, so a restart takes the names off the link it names,
|
||||
// never inferring which. Only the uplink names' round reads and writes it.
|
||||
applied UplinkApplied
|
||||
}
|
||||
|
||||
// NewGuard is the machine's.
|
||||
@@ -335,6 +340,7 @@ func (g *Guard) Run(ctx context.Context) {
|
||||
g.Tick()
|
||||
case <-routes.C:
|
||||
g.KeepRoutes(ctx)
|
||||
g.KeepUplinkNames(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -367,6 +373,108 @@ func (g *Guard) KeepRoutes(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// sameNames is whether two name lists are the same, in the same order (the file holds them one a line, in
|
||||
// the order given).
|
||||
func sameNames(a, b []string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// rawDomainsOf is one link's domains as resolved holds them now, `~` kept, so its routing domains and its
|
||||
// plain search domains are told apart.
|
||||
func (g *Guard) rawDomainsOf(ctx context.Context, link string) ([]string, error) {
|
||||
out, err := g.Resolver.Read(ctx, "resolvectl", "domain")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, links := perScope(out)
|
||||
return links[link], nil
|
||||
}
|
||||
|
||||
// setLinkUplinkNames sets one link's domains to its own, apart from the names the guard applied there
|
||||
// last, plus `~<name>` per name now given (design 08 sign-in-page §7): every search domain and routing
|
||||
// domain the link already has, apart from the guard's own, stays. An empty names takes them off.
|
||||
func (g *Guard) setLinkUplinkNames(ctx context.Context, link string, names []string) error {
|
||||
raw, err := g.rawDomainsOf(ctx, link)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
marked := map[string]bool{}
|
||||
for _, n := range g.applied.Names {
|
||||
marked["~"+n] = true
|
||||
}
|
||||
base := withoutMarked(raw, marked)
|
||||
full := append([]string{}, base...)
|
||||
for _, n := range names {
|
||||
full = append(full, "~"+n)
|
||||
}
|
||||
_, err = g.Resolver.Change(ctx, "resolvectl", append([]string{"domain", link}, full...)...)
|
||||
return err
|
||||
}
|
||||
|
||||
// KeepUplinkNames gives the names in the file to the link carrying the default route, every round (design
|
||||
// 08 sign-in-page §7, §8): it never sets a link's servers, its default route for names, its addresses or
|
||||
// its routes. A round in which they are already in place changes nothing.
|
||||
func (g *Guard) KeepUplinkNames(ctx context.Context) {
|
||||
given := ReadUplinkNames(g.Resolver.UplinkNamesFile)
|
||||
routes, err := g.Resolver.Routes(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
link, err := g.Resolver.DefaultRouteLink(ctx, routes)
|
||||
if err != nil {
|
||||
g.Log("the kernel's default routes: %v", err)
|
||||
return
|
||||
}
|
||||
if link == g.applied.Link && sameNames(given, g.applied.Names) && g.applied.Refused == "" {
|
||||
return
|
||||
}
|
||||
if g.applied.Link != "" && g.applied.Link != link {
|
||||
if err := g.setLinkUplinkNames(ctx, g.applied.Link, nil); err != nil {
|
||||
g.Log("taking the uplink's names off %s failed: %v", g.applied.Link, err)
|
||||
}
|
||||
}
|
||||
refused := ""
|
||||
if link != "" {
|
||||
if err := g.setLinkUplinkNames(ctx, link, given); err != nil {
|
||||
refused = err.Error()
|
||||
}
|
||||
}
|
||||
if link != g.applied.Link {
|
||||
g.Log("the uplink's names are now answered over %q, were %q", link, g.applied.Link)
|
||||
} else if !sameNames(given, g.applied.Names) {
|
||||
g.Log("the uplink's names change on %s", link)
|
||||
}
|
||||
if refused != g.applied.Refused {
|
||||
if refused != "" {
|
||||
g.Log("resolved refuses the uplink's names: %s", refused)
|
||||
} else {
|
||||
g.Log("resolved accepts the uplink's names again")
|
||||
}
|
||||
}
|
||||
g.applied = UplinkApplied{Names: given, Link: link, At: g.Now(), Refused: refused}
|
||||
g.writeApplied()
|
||||
}
|
||||
|
||||
// writeApplied writes what the guard applied last, whole (beside, renamed over), design 08 sign-in-page §8.
|
||||
func (g *Guard) writeApplied() {
|
||||
raw, err := json.MarshalIndent(g.applied, "", " ")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tmp := g.Resolver.UplinkNamesAppliedFile + ".tmp"
|
||||
if os.WriteFile(tmp, raw, 0o644) == nil {
|
||||
_ = os.Rename(tmp, g.Resolver.UplinkNamesAppliedFile)
|
||||
}
|
||||
}
|
||||
|
||||
// Request is one call over the socket: a verb and its arguments, one JSON line.
|
||||
type Request struct {
|
||||
Verb string `json:"verb"`
|
||||
|
||||
Reference in New Issue
Block a user