You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/agent/internal/capability/provider.go

117 lines
3.7 KiB
Go

// Package capability describes host capabilities without coupling the HTTP
// contract to a particular libvirt binding.
package capability
import (
"context"
"time"
"g.luoli.live/root/virt/agent/internal/hypervisor"
)
type Snapshot struct {
AgentID string `json:"agent_id"`
ProtocolVersion string `json:"protocol_version"`
Features map[string]bool `json:"features"`
Checks map[string]string `json:"checks,omitempty"`
ObservedAt time.Time `json:"observed_at"`
}
type Provider interface {
Snapshot(context.Context) (Snapshot, error)
Probe(context.Context) error
}
// StaticProvider is intentionally conservative and development-only. It keeps
// the protocol testable on macOS; Linux production processes use
// BackendProvider backed by the local libvirt adapter and never treat this
// provider's false feature set as host capability evidence.
type StaticProvider struct {
AgentID string
ProtocolVersion string
}
func (p StaticProvider) Snapshot(context.Context) (Snapshot, error) {
return Snapshot{
AgentID: p.AgentID,
ProtocolVersion: p.ProtocolVersion,
Features: map[string]bool{
"libvirt": false,
"uefi_q35": false,
"secure_boot": false,
"cloud_init": false,
"startup_script": false,
},
Checks: map[string]string{"provider": "static-development-provider"},
ObservedAt: time.Now().UTC(),
}, nil
}
func (p StaticProvider) Probe(context.Context) error { return nil }
// BackendProvider adapts the host-facing hypervisor seam to the transport
// capability contract. It deliberately reports the product policy separately
// from the host's ability to enable Secure Boot: Secure Boot remains disabled
// even when the firmware stack advertises that feature.
type BackendProvider struct {
Backend hypervisor.Backend
AgentID string
ProtocolVersion string
}
func (p BackendProvider) Snapshot(ctx context.Context) (Snapshot, error) {
if p.Backend == nil {
return Snapshot{}, hypervisor.ErrReadbackMismatch
}
caps, err := p.Backend.Capabilities(ctx)
if err != nil {
return Snapshot{}, err
}
return snapshotFromCapabilities(p.AgentID, p.ProtocolVersion, caps), nil
}
func (p BackendProvider) Probe(ctx context.Context) error {
if p.Backend == nil {
return hypervisor.ErrReadbackMismatch
}
_, err := p.Backend.Probe(ctx)
return err
}
func snapshotFromCapabilities(agentID, protocol string, caps hypervisor.CapabilitySnapshot) Snapshot {
return Snapshot{
AgentID: agentID,
ProtocolVersion: protocol,
Features: map[string]bool{
"libvirt": caps.Backend != "" && caps.Backend != "fake",
"uefi_q35": caps.UEFI && caps.Q35,
"secure_boot": caps.SecureBootCapable,
"secure_boot_disabled": caps.SecureBootDisabled,
"qga": caps.QGA,
"snapshots": caps.Snapshots,
"cloud_init": caps.CloudInit,
// The NoCloud renderer and immutable artifact publisher are Agent
// capabilities, not facts that libvirt's domcapabilities XML can
// prove. Keep the raw host/guest observation separate so a host
// probe cannot accidentally hide a feature the Agent implements.
"startup_script": true,
"startup_script_agent": true,
"startup_script_host": caps.StartupScript,
"nocloud_seed_artifact": true,
},
Checks: map[string]string{
"backend": caps.Backend,
"architecture": caps.Architecture,
"policy": policyStatus(caps),
},
ObservedAt: caps.ObservedAt,
}
}
func policyStatus(caps hypervisor.CapabilitySnapshot) string {
if err := caps.ValidatePlatformPolicy(); err != nil {
return "rejected"
}
return "efi-q35-no-secure-boot"
}