201 lines
6.3 KiB
Go
201 lines
6.3 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// Package probe holds the host profile contract (schema 1, scripts/hostprobe.py), the ssh runner that collects it,
|
|
// the profile store under local/profiles, and the profile diff.
|
|
package probe
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
// SchemaVersion is the only profile schema this hub understands.
|
|
const SchemaVersion = 1
|
|
|
|
// Profile mirrors the probe output. Nullable values use pointers or nil slices ("not determinable").
|
|
type Profile struct {
|
|
Schema int `json:"schema"`
|
|
ProbedAt string `json:"probed_at"`
|
|
Unreadable []Unreadable `json:"unreadable"`
|
|
Sections Sections `json:"sections"`
|
|
}
|
|
|
|
type Unreadable struct {
|
|
Section string `json:"section"`
|
|
Item string `json:"item"`
|
|
Reason string `json:"reason"`
|
|
}
|
|
|
|
type Sections struct {
|
|
Identity Identity `json:"identity"`
|
|
Access Access `json:"access"`
|
|
Software Software `json:"software"`
|
|
Containers []Container `json:"containers"`
|
|
Services []Service `json:"services"`
|
|
Candidates []Candidate `json:"candidates"`
|
|
Listening []Listener `json:"listening"`
|
|
Cron Cron `json:"cron"`
|
|
}
|
|
|
|
type Identity struct {
|
|
Hostname string `json:"hostname"`
|
|
OS OSInfo `json:"os"`
|
|
Kernel string `json:"kernel"`
|
|
Arch string `json:"arch"`
|
|
CPUs int `json:"cpus"`
|
|
MemTotalKB int64 `json:"mem_total_kb"`
|
|
Virtualization *string `json:"virtualization"`
|
|
UptimeS int64 `json:"uptime_s"`
|
|
MachineIDSHA256Prefix string `json:"machine_id_sha256_prefix"`
|
|
SSHHostKeys []HostKey `json:"ssh_host_keys"`
|
|
Tailscale0 []string `json:"tailscale0"`
|
|
}
|
|
|
|
type OSInfo struct {
|
|
ID string `json:"id"`
|
|
VersionID string `json:"version_id"`
|
|
VersionCodename string `json:"version_codename"`
|
|
PrettyName string `json:"pretty_name"`
|
|
}
|
|
|
|
type HostKey struct {
|
|
Type string `json:"type"`
|
|
Fingerprint string `json:"fingerprint"`
|
|
}
|
|
|
|
type Access struct {
|
|
User string `json:"user"`
|
|
Groups []string `json:"groups"`
|
|
SudoNopasswd bool `json:"sudo_nopasswd"`
|
|
Sshd Sshd `json:"sshd"`
|
|
Fail2ban Svc `json:"fail2ban"`
|
|
UFW Svc `json:"ufw"`
|
|
NFT Svc `json:"nft"`
|
|
}
|
|
|
|
// Sshd: AllowUsers/AllowGroups nil = unknown, empty = readable and unset.
|
|
type Sshd struct {
|
|
Source *string `json:"source"`
|
|
AllowUsers []string `json:"allowusers"`
|
|
AllowGroups []string `json:"allowgroups"`
|
|
PasswordAuthentication string `json:"passwordauthentication"`
|
|
PermitRootLogin string `json:"permitrootlogin"`
|
|
Port []string `json:"port"`
|
|
ConfigDFiles []string `json:"config_d_files"`
|
|
}
|
|
|
|
type Svc struct {
|
|
Installed bool `json:"installed"`
|
|
Active bool `json:"active"`
|
|
}
|
|
|
|
type Software struct {
|
|
PackageManager *string `json:"package_manager"`
|
|
UnattendedUpgrades any `json:"unattended_upgrades"`
|
|
UnattendedUpgradesEnable *bool `json:"unattended_upgrades_enabled"`
|
|
DockerServerVersion *string `json:"docker_server_version"`
|
|
SaltMinion SaltMinion `json:"salt_minion"`
|
|
Python3 string `json:"python3"`
|
|
}
|
|
|
|
type SaltMinion struct {
|
|
Installed bool `json:"installed"`
|
|
Version *string `json:"version"`
|
|
Active bool `json:"active"`
|
|
ID *string `json:"id"`
|
|
Master *string `json:"master"`
|
|
}
|
|
|
|
type Container struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Project string `json:"project"`
|
|
Service string `json:"service"`
|
|
WorkingDir string `json:"working_dir"`
|
|
Image string `json:"image"`
|
|
User string `json:"user"`
|
|
Privileged bool `json:"privileged"`
|
|
Status string `json:"status"`
|
|
Health string `json:"health"`
|
|
RestartPolicy string `json:"restart_policy"`
|
|
Ports []Port `json:"ports"`
|
|
}
|
|
|
|
type Port struct {
|
|
ContainerPort *int `json:"container_port"`
|
|
Proto string `json:"proto"`
|
|
HostIP string `json:"host_ip"`
|
|
HostPort int `json:"host_port"`
|
|
}
|
|
|
|
type Service struct {
|
|
Name string `json:"name"`
|
|
Kind string `json:"kind"`
|
|
Enabled string `json:"enabled"`
|
|
Active string `json:"active"`
|
|
ExecPaths []string `json:"exec_paths"`
|
|
User *string `json:"user"`
|
|
Group *string `json:"group"`
|
|
OnCalendar []string `json:"on_calendar"`
|
|
Unit *string `json:"unit"`
|
|
Description *string `json:"description"`
|
|
HubManaged bool `json:"hub_managed"`
|
|
}
|
|
|
|
type Candidate struct {
|
|
Path string `json:"path"`
|
|
Unit string `json:"unit"`
|
|
SHA256 *string `json:"sha256"`
|
|
Owner string `json:"owner"`
|
|
Group string `json:"group"`
|
|
Mode string `json:"mode"`
|
|
Size int64 `json:"size"`
|
|
Note string `json:"note"`
|
|
}
|
|
|
|
type Listener struct {
|
|
Proto string `json:"proto"`
|
|
Address string `json:"address"`
|
|
Port int `json:"port"`
|
|
Process *string `json:"process"`
|
|
}
|
|
|
|
type Cron struct {
|
|
CronD []string `json:"cron_d"`
|
|
UserCrontabs *bool `json:"user_crontabs"`
|
|
}
|
|
|
|
var requiredSections = []string{"identity", "access", "software", "containers", "services", "candidates", "listening", "cron"}
|
|
|
|
// Parse decodes and validates one profile (schema 1, every section present).
|
|
func Parse(raw []byte) (*Profile, error) {
|
|
var top map[string]json.RawMessage
|
|
if err := json.Unmarshal(raw, &top); err != nil {
|
|
return nil, fmt.Errorf("profile is not a JSON object")
|
|
}
|
|
var schema int
|
|
if v, ok := top["schema"]; !ok || json.Unmarshal(v, &schema) != nil {
|
|
return nil, fmt.Errorf("profile has no schema")
|
|
}
|
|
if schema != SchemaVersion {
|
|
return nil, fmt.Errorf("unsupported profile schema %d (want %d)", schema, SchemaVersion)
|
|
}
|
|
var sections map[string]json.RawMessage
|
|
if v, ok := top["sections"]; !ok || json.Unmarshal(v, §ions) != nil {
|
|
return nil, fmt.Errorf("profile has no sections")
|
|
}
|
|
for _, name := range requiredSections {
|
|
if _, ok := sections[name]; !ok {
|
|
return nil, fmt.Errorf("profile section %q missing", name)
|
|
}
|
|
}
|
|
var p Profile
|
|
if err := json.NewDecoder(bytes.NewReader(raw)).Decode(&p); err != nil {
|
|
return nil, fmt.Errorf("profile does not match schema 1: %v", err)
|
|
}
|
|
if p.ProbedAt == "" {
|
|
return nil, fmt.Errorf("profile has no probed_at")
|
|
}
|
|
return &p, nil
|
|
}
|