Enterprise release: add self-updates, command signing, and telemetry enhancements
This commit is contained in:
+208
-26
@@ -1,7 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"strings"
|
||||
@@ -15,8 +18,49 @@ type WSMessage struct {
|
||||
Payload map[string]interface{} `json:"payload"`
|
||||
}
|
||||
|
||||
func connectWebSocket(cfg *Config) {
|
||||
type MessageWriter interface {
|
||||
WriteMessage(messageType int, data []byte) error
|
||||
}
|
||||
|
||||
func verifySignature(cfg *Config, msg WSMessage) bool {
|
||||
if cfg.PublicKey == "" {
|
||||
log.Println("No public key configured; skipping signature verification")
|
||||
return true
|
||||
}
|
||||
|
||||
sigB64, ok := msg.Payload["signature"].(string)
|
||||
if !ok {
|
||||
log.Println("High-risk command missing signature")
|
||||
return false
|
||||
}
|
||||
|
||||
sig, err := base64.StdEncoding.DecodeString(sigB64)
|
||||
if err != nil {
|
||||
log.Printf("Invalid base64 signature: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
// Create canonical payload for verification (remove signature key)
|
||||
payloadCopy := make(map[string]interface{})
|
||||
for k, v := range msg.Payload {
|
||||
if k != "signature" {
|
||||
payloadCopy[k] = v
|
||||
}
|
||||
}
|
||||
canonicalPayload, _ := json.Marshal(payloadCopy)
|
||||
|
||||
pubKeyBytes, err := base64.StdEncoding.DecodeString(cfg.PublicKey)
|
||||
if err != nil || len(pubKeyBytes) != ed25519.PublicKeySize {
|
||||
log.Printf("Invalid public key in config: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
return ed25519.Verify(pubKeyBytes, canonicalPayload, sig)
|
||||
}
|
||||
|
||||
func connectWebSocket(cm *ConfigManager, exec Executor) {
|
||||
for {
|
||||
cfg := cm.Get()
|
||||
// Ensure protocol is ws/wss
|
||||
serverURL := strings.Replace(cfg.ServerURL, "http://", "ws://", 1)
|
||||
serverURL = strings.Replace(serverURL, "https://", "wss://", 1)
|
||||
@@ -39,12 +83,20 @@ func connectWebSocket(cfg *Config) {
|
||||
|
||||
log.Println("Successfully connected to SSO Manager.")
|
||||
|
||||
// Start telemetry if enabled
|
||||
var telemetryTicker *time.Ticker
|
||||
var telemetryDone chan bool
|
||||
if cfg.Capabilities.Telemetry {
|
||||
telemetryTicker, telemetryDone = startTelemetry(c, cfg)
|
||||
}
|
||||
// Start telemetry and discovery
|
||||
StartTelemetryLoop(c, cfg, exec)
|
||||
|
||||
// Heartbeat loop
|
||||
go func() {
|
||||
ticker := time.NewTicker(60 * time.Second)
|
||||
for range ticker.C {
|
||||
hb := WSMessage{Type: "heartbeat", Payload: map[string]interface{}{"timestamp": time.Now().Format(time.RFC3339)}}
|
||||
payload, _ := json.Marshal(hb)
|
||||
if err := c.WriteMessage(websocket.TextMessage, payload); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Read loop
|
||||
for {
|
||||
@@ -60,48 +112,178 @@ func connectWebSocket(cfg *Config) {
|
||||
continue
|
||||
}
|
||||
|
||||
handleCommand(cfg, msg, c)
|
||||
handleCommand(cm, msg, c, exec)
|
||||
}
|
||||
|
||||
// Cleanup on disconnect
|
||||
c.Close()
|
||||
if telemetryTicker != nil {
|
||||
telemetryTicker.Stop()
|
||||
telemetryDone <- true
|
||||
}
|
||||
|
||||
log.Println("WebSocket disconnected. Reconnecting in 5 seconds...")
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func handleCommand(cfg *Config, msg WSMessage, c *websocket.Conn) {
|
||||
func handleCommand(cm *ConfigManager, msg WSMessage, c MessageWriter, exec Executor) {
|
||||
cfg := cm.Get()
|
||||
log.Printf("Received command: %s", msg.Type)
|
||||
|
||||
sendResponse := func(status string, message string) {
|
||||
resp, _ := json.Marshal(map[string]string{"status": status, "message": message})
|
||||
c.WriteMessage(websocket.TextMessage, resp)
|
||||
}
|
||||
|
||||
switch msg.Type {
|
||||
case "config":
|
||||
log.Printf("Received config payload: %v", msg.Payload)
|
||||
case "reboot":
|
||||
if !cfg.Capabilities.Reboot {
|
||||
log.Println("Reboot rejected: capability disabled in agent.yml")
|
||||
case "reload_config":
|
||||
if err := cm.Reload(); err != nil {
|
||||
log.Printf("Reload failed: %v", err)
|
||||
sendResponse("error", "failed to reload config")
|
||||
} else {
|
||||
log.Println("Configuration reloaded successfully.")
|
||||
sendResponse("ok", "configuration reloaded")
|
||||
}
|
||||
case "fetch_logs":
|
||||
out, err := exec.Execute("journalctl", "-u", "theta-agent", "-n", "100")
|
||||
if err != nil {
|
||||
log.Printf("Log fetch failed: %v", err)
|
||||
sendResponse("error", "failed to fetch logs")
|
||||
return
|
||||
}
|
||||
log.Println("Reboot capability enabled. (Simulation: rebooting system...)")
|
||||
resp := map[string]string{
|
||||
"status": "ok",
|
||||
"logs": string(out),
|
||||
}
|
||||
respPayload, _ := json.Marshal(resp)
|
||||
c.WriteMessage(websocket.TextMessage, respPayload)
|
||||
return
|
||||
case "update_binary":
|
||||
if !verifySignature(cfg, msg) {
|
||||
sendResponse("error", "signature verification failed")
|
||||
return
|
||||
}
|
||||
if !cfg.Capabilities.ArbitraryBash { // Use Bash as a proxy for "dangerous update" capability
|
||||
sendResponse("error", "update capability disabled")
|
||||
return
|
||||
}
|
||||
|
||||
url, _ := msg.Payload["url"].(string)
|
||||
checksum, _ := msg.Payload["sha256"].(string)
|
||||
if url == "" || checksum == "" {
|
||||
sendResponse("error", "missing url or checksum")
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Updating binary from %s...", url)
|
||||
// implementation of download and replace
|
||||
// ... (simplified for now, using a shell command via executor for brevity in this turn)
|
||||
script := fmt.Sprintf("curl -fsSL %s -o /tmp/theta-agent.new && sha256sum -c <(echo '%s /tmp/theta-agent.new') && mv /tmp/theta-agent.new $(readlink -f /proc/self/exe)", url, checksum)
|
||||
if _, err := exec.Execute("bash", "-c", script); err != nil {
|
||||
log.Printf("Update failed: %v", err)
|
||||
sendResponse("error", "update failed")
|
||||
return
|
||||
}
|
||||
sendResponse("ok", "update applied. restarting agent...")
|
||||
os.Exit(0)
|
||||
case "config":
|
||||
log.Printf("Received config payload: %v", msg.Payload)
|
||||
sendResponse("ok", "Configuration received")
|
||||
case "reboot":
|
||||
if !verifySignature(cfg, msg) {
|
||||
sendResponse("error", "signature verification failed")
|
||||
return
|
||||
}
|
||||
if !cfg.Capabilities.Reboot {
|
||||
log.Println("Reboot rejected: capability disabled in agent.yml")
|
||||
sendResponse("error", "reboot capability disabled")
|
||||
return
|
||||
}
|
||||
log.Printf("Executing reboot...")
|
||||
if _, err := exec.Execute("reboot"); err != nil {
|
||||
log.Printf("Reboot failed: %v", err)
|
||||
sendResponse("error", "reboot failed")
|
||||
return
|
||||
}
|
||||
sendResponse("ok", "system rebooting")
|
||||
case "service_restart":
|
||||
serviceName, ok := msg.Payload["service"].(string)
|
||||
if !ok || !cfg.Capabilities.CanManageService(serviceName) {
|
||||
log.Printf("Service restart rejected for '%s': not in allowed service list", serviceName)
|
||||
sendResponse("error", "service restart rejected")
|
||||
return
|
||||
}
|
||||
log.Printf("Restarting service %s...", serviceName)
|
||||
if _, err := exec.Execute("systemctl", "restart", serviceName); err != nil {
|
||||
log.Printf("Service restart failed: %v", err)
|
||||
sendResponse("error", "restart failed")
|
||||
return
|
||||
}
|
||||
sendResponse("ok", "service restarted")
|
||||
case "configure_ldap":
|
||||
if !verifySignature(cfg, msg) {
|
||||
sendResponse("error", "signature verification failed")
|
||||
return
|
||||
}
|
||||
if !cfg.Capabilities.ConfigureLDAP {
|
||||
log.Println("LDAP config rejected: capability disabled in agent.yml")
|
||||
sendResponse("error", "LDAP config disabled")
|
||||
return
|
||||
}
|
||||
|
||||
configData, ok := msg.Payload["config"].(string)
|
||||
if !ok {
|
||||
log.Println("LDAP config payload missing or not a string")
|
||||
sendResponse("error", "invalid config payload")
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("Pushing updated SSSD configuration...")
|
||||
if err := exec.WriteFile("/etc/sssd/sssd.conf", []byte(configData), 0600); err != nil {
|
||||
log.Printf("Failed to write SSSD config: %v", err)
|
||||
sendResponse("error", "failed to write config")
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("Restarting SSSD service...")
|
||||
if _, err := exec.Execute("systemctl", "restart", "sssd"); err != nil {
|
||||
log.Printf("SSSD restart failed: %v", err)
|
||||
sendResponse("error", "failed to restart sssd")
|
||||
return
|
||||
}
|
||||
sendResponse("ok", "LDAP configuration updated")
|
||||
case "arbitrary_bash":
|
||||
if !verifySignature(cfg, msg) {
|
||||
sendResponse("error", "signature verification failed")
|
||||
return
|
||||
}
|
||||
if !cfg.Capabilities.ArbitraryBash {
|
||||
log.Println("Bash execution rejected: capability disabled in agent.yml")
|
||||
sendResponse("error", "bash execution disabled")
|
||||
return
|
||||
}
|
||||
|
||||
script, ok := msg.Payload["script"].(string)
|
||||
if !ok {
|
||||
log.Println("Bash payload missing or not a string")
|
||||
sendResponse("error", "invalid script payload")
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Executing remote script: %s", script)
|
||||
out, err := exec.Execute("bash", "-c", script)
|
||||
if err != nil {
|
||||
log.Printf("Script execution failed: %v", err)
|
||||
sendResponse("error", fmt.Sprintf("execution failed: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
resp := map[string]string{
|
||||
"status": "ok",
|
||||
"output": string(out),
|
||||
}
|
||||
respPayload, _ := json.Marshal(resp)
|
||||
c.WriteMessage(websocket.TextMessage, respPayload)
|
||||
return
|
||||
default:
|
||||
log.Printf("Unknown command type: %s", msg.Type)
|
||||
sendResponse("error", "unknown command type")
|
||||
}
|
||||
}
|
||||
|
||||
func startTelemetry(c *websocket.Conn, cfg *Config) (*time.Ticker, chan bool) {
|
||||
ticker := time.Ticker{C: nil} // placeholder logic
|
||||
done := make(chan bool)
|
||||
log.Println("Telemetry loop started (placeholder).")
|
||||
return &ticker, done
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user