199 lines
5.3 KiB
Go
199 lines
5.3 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package probe
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// Diff is the change between two profiles of one host.
|
|
type Diff struct {
|
|
Rebuild bool `json:"rebuild"`
|
|
RebuildReasons []string `json:"rebuild_reasons"`
|
|
Drift []Change `json:"drift"`
|
|
}
|
|
|
|
// Change is one drift entry: Kind is added, removed or changed.
|
|
type Change struct {
|
|
Section string `json:"section"`
|
|
Kind string `json:"kind"`
|
|
Item string `json:"item"`
|
|
Detail string `json:"detail,omitempty"`
|
|
}
|
|
|
|
// Empty reports no rebuild and no drift.
|
|
func (d Diff) Empty() bool { return !d.Rebuild && len(d.Drift) == 0 }
|
|
|
|
// Compare diffs old against new. REBUILD when the machine-id prefix or the set of host key fingerprints changes.
|
|
func Compare(oldP, newP *Profile) Diff {
|
|
d := Diff{RebuildReasons: []string{}, Drift: []Change{}}
|
|
oi, ni := oldP.Sections.Identity, newP.Sections.Identity
|
|
if oi.MachineIDSHA256Prefix != ni.MachineIDSHA256Prefix {
|
|
d.Rebuild = true
|
|
d.RebuildReasons = append(d.RebuildReasons,
|
|
fmt.Sprintf("machine-id changed (%s -> %s)", orDash(oi.MachineIDSHA256Prefix), orDash(ni.MachineIDSHA256Prefix)))
|
|
}
|
|
oldKeys, newKeys := keySet(oi.SSHHostKeys), keySet(ni.SSHHostKeys)
|
|
if added, removed := setDiff(newKeys, oldKeys), setDiff(oldKeys, newKeys); len(added)+len(removed) > 0 {
|
|
d.Rebuild = true
|
|
d.RebuildReasons = append(d.RebuildReasons,
|
|
fmt.Sprintf("ssh host keys changed (added %s, removed %s)", listOrNone(added), listOrNone(removed)))
|
|
}
|
|
|
|
d.Drift = append(d.Drift, diffSets("containers", containerItems(oldP), containerItems(newP))...)
|
|
d.Drift = append(d.Drift, diffSets("listening", listenItems(oldP), listenItems(newP))...)
|
|
d.Drift = append(d.Drift, diffSets("services", serviceItems(oldP), serviceItems(newP))...)
|
|
d.Drift = append(d.Drift, diffSets("candidates", candidateItems(oldP), candidateItems(newP))...)
|
|
d.Drift = append(d.Drift, diffSets("access", accessItems(oldP), accessItems(newP))...)
|
|
return d
|
|
}
|
|
|
|
// Render prints the diff as text.
|
|
func (d Diff) Render(host, oldStamp, newStamp string) string {
|
|
var b strings.Builder
|
|
fmt.Fprintf(&b, "%s: %s -> %s\n", host, oldStamp, newStamp)
|
|
if d.Rebuild {
|
|
for _, r := range d.RebuildReasons {
|
|
fmt.Fprintf(&b, "REBUILD: %s\n", r)
|
|
}
|
|
b.WriteString(" re-verify the host key out of band, then redo sudo, sshd drop-ins and the minion key\n")
|
|
}
|
|
if len(d.Drift) == 0 {
|
|
b.WriteString("drift: none\n")
|
|
}
|
|
for _, c := range d.Drift {
|
|
sign := map[string]string{"added": "+", "removed": "-", "changed": "~"}[c.Kind]
|
|
if c.Detail != "" {
|
|
fmt.Fprintf(&b, "%s %s %s (%s)\n", sign, c.Section, c.Item, c.Detail)
|
|
} else {
|
|
fmt.Fprintf(&b, "%s %s %s\n", sign, c.Section, c.Item)
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func orDash(s string) string {
|
|
if s == "" {
|
|
return "-"
|
|
}
|
|
return s
|
|
}
|
|
|
|
func keySet(keys []HostKey) map[string]bool {
|
|
m := map[string]bool{}
|
|
for _, k := range keys {
|
|
m[k.Type+" "+k.Fingerprint] = true
|
|
}
|
|
return m
|
|
}
|
|
|
|
func setDiff(a, b map[string]bool) []string {
|
|
var out []string
|
|
for k := range a {
|
|
if !b[k] {
|
|
out = append(out, k)
|
|
}
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|
|
|
|
func listOrNone(v []string) string {
|
|
if len(v) == 0 {
|
|
return "none"
|
|
}
|
|
return strings.Join(v, ", ")
|
|
}
|
|
|
|
// diffSets compares key -> value maps: added, removed, or changed value.
|
|
func diffSets(section string, oldM, newM map[string]string) []Change {
|
|
var out []Change
|
|
keys := map[string]bool{}
|
|
for k := range oldM {
|
|
keys[k] = true
|
|
}
|
|
for k := range newM {
|
|
keys[k] = true
|
|
}
|
|
sorted := make([]string, 0, len(keys))
|
|
for k := range keys {
|
|
sorted = append(sorted, k)
|
|
}
|
|
sort.Strings(sorted)
|
|
for _, k := range sorted {
|
|
ov, inOld := oldM[k]
|
|
nv, inNew := newM[k]
|
|
switch {
|
|
case !inOld:
|
|
out = append(out, Change{Section: section, Kind: "added", Item: k, Detail: nv})
|
|
case !inNew:
|
|
out = append(out, Change{Section: section, Kind: "removed", Item: k, Detail: ov})
|
|
case ov != nv:
|
|
out = append(out, Change{Section: section, Kind: "changed", Item: k, Detail: ov + " -> " + nv})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func containerItems(p *Profile) map[string]string {
|
|
m := map[string]string{}
|
|
for _, c := range p.Sections.Containers {
|
|
m[c.Name] = fmt.Sprintf("%s project=%s user=%q", c.Image, c.Project, c.User)
|
|
}
|
|
return m
|
|
}
|
|
|
|
func listenItems(p *Profile) map[string]string {
|
|
m := map[string]string{}
|
|
for _, l := range p.Sections.Listening {
|
|
proc := ""
|
|
if l.Process != nil {
|
|
proc = *l.Process
|
|
}
|
|
m[fmt.Sprintf("%s/%s:%d", l.Proto, l.Address, l.Port)] = proc
|
|
}
|
|
return m
|
|
}
|
|
|
|
func serviceItems(p *Profile) map[string]string {
|
|
m := map[string]string{}
|
|
for _, s := range p.Sections.Services {
|
|
m[s.Name] = s.Enabled + "/" + s.Active
|
|
}
|
|
return m
|
|
}
|
|
|
|
func candidateItems(p *Profile) map[string]string {
|
|
m := map[string]string{}
|
|
for _, c := range p.Sections.Candidates {
|
|
sum := "null"
|
|
if c.SHA256 != nil {
|
|
sum = *c.SHA256
|
|
if len(sum) > 12 {
|
|
sum = sum[:12]
|
|
}
|
|
}
|
|
m[c.Path] = "sha256 " + sum
|
|
}
|
|
return m
|
|
}
|
|
|
|
func accessItems(p *Profile) map[string]string {
|
|
a := p.Sections.Access
|
|
return map[string]string{
|
|
"sshd allowusers": listState(a.Sshd.AllowUsers),
|
|
"sshd allowgroups": listState(a.Sshd.AllowGroups),
|
|
"sshd passwordauth": a.Sshd.PasswordAuthentication,
|
|
"sshd permitrootlogin": a.Sshd.PermitRootLogin,
|
|
"sudo_nopasswd": fmt.Sprint(a.SudoNopasswd),
|
|
}
|
|
}
|
|
|
|
func listState(v []string) string {
|
|
if v == nil {
|
|
return "unknown"
|
|
}
|
|
return "[" + strings.Join(v, ",") + "]"
|
|
}
|