103 lines
3.3 KiB
Go
103 lines
3.3 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// Package pillar renders adoption suggestions as reviewable pillar YAML. It only renders text: nothing here
|
|
// edits a pillar file, and nothing is written anywhere.
|
|
package pillar
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// ComposeProject is one security_hub:audit:hosts:<host>:compose_projects entry.
|
|
type ComposeProject struct {
|
|
Name string `json:"name"`
|
|
Services []string `json:"services"`
|
|
// AllowedPorts nil = no port rule (key omitted); empty = nothing may be published off loopback.
|
|
AllowedPorts []int `json:"allowed_ports"`
|
|
// PortsNote explains an omitted AllowedPorts (rendered as a comment).
|
|
PortsNote string `json:"ports_note,omitempty"`
|
|
ForbidRoot bool `json:"forbid_root"`
|
|
}
|
|
|
|
var plainRe = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9._-]*$`)
|
|
|
|
// scalar quotes a YAML scalar only when needed (also quotes words YAML would read as booleans or null).
|
|
func scalar(s string) string {
|
|
switch strings.ToLower(s) {
|
|
case "y", "n", "yes", "no", "on", "off", "true", "false", "null", "~":
|
|
return "'" + s + "'"
|
|
}
|
|
if plainRe.MatchString(s) {
|
|
return s
|
|
}
|
|
return "'" + strings.ReplaceAll(s, "'", "''") + "'"
|
|
}
|
|
|
|
func flowList(items []string) string {
|
|
q := make([]string, len(items))
|
|
for i, s := range items {
|
|
q[i] = scalar(s)
|
|
}
|
|
return "[" + strings.Join(q, ", ") + "]"
|
|
}
|
|
|
|
func portList(ports []int) string {
|
|
s := make([]string, len(ports))
|
|
for i, p := range ports {
|
|
s[i] = strconv.Itoa(p)
|
|
}
|
|
return "[" + strings.Join(s, ", ") + "]"
|
|
}
|
|
|
|
// OrderServices puts the service named like the project first (the primary one), then the rest alphabetically.
|
|
func OrderServices(project string, services []string) []string {
|
|
out := append([]string(nil), services...)
|
|
sort.Strings(out)
|
|
for i, s := range out {
|
|
if s == project && i > 0 {
|
|
copy(out[1:i+1], out[:i])
|
|
out[0] = s
|
|
break
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Render returns the snippet for pillar/security_hub/init.sls (hosts overlay) and, when nopasswd is non-empty,
|
|
// the per-host pillar file pillar/security_hub/<host>.sls. Both are comments-plus-YAML, ready to review and paste.
|
|
func Render(host string, projects []ComposeProject, nopasswd []string) string {
|
|
var b strings.Builder
|
|
fmt.Fprintf(&b, "# pillar/security_hub/init.sls (security_hub:audit:hosts:%s)\n", host)
|
|
if len(projects) == 0 {
|
|
b.WriteString("# no compose projects found: no overlay needed\n")
|
|
} else {
|
|
b.WriteString("security_hub:\n audit:\n hosts:\n")
|
|
fmt.Fprintf(&b, " %s:\n compose_projects:\n", scalar(host))
|
|
for _, p := range projects {
|
|
fmt.Fprintf(&b, " - name: %s\n", scalar(p.Name))
|
|
fmt.Fprintf(&b, " services: %s\n", flowList(p.Services))
|
|
switch {
|
|
case p.AllowedPorts != nil:
|
|
fmt.Fprintf(&b, " allowed_ports: %s\n", portList(p.AllowedPorts))
|
|
case p.PortsNote != "":
|
|
fmt.Fprintf(&b, " # allowed_ports omitted: %s\n", p.PortsNote)
|
|
}
|
|
if p.ForbidRoot {
|
|
b.WriteString(" forbid_root: true\n")
|
|
}
|
|
}
|
|
}
|
|
if len(nopasswd) > 0 {
|
|
fmt.Fprintf(&b, "# pillar/security_hub/%s.sls (expected_nopasswd_users is a per-host pillar key, not part of the hosts overlay)\n",
|
|
strings.ReplaceAll(host, "-", "_"))
|
|
b.WriteString("security_hub:\n audit:\n expected_nopasswd_users:\n")
|
|
for _, u := range nopasswd {
|
|
fmt.Fprintf(&b, " - %s\n", scalar(u))
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|