Complete RedFlag codebase with two major security audit implementations.
== A-1: Ed25519 Key Rotation Support ==
Server:
- SignCommand sets SignedAt timestamp and KeyID on every signature
- signing_keys database table (migration 020) for multi-key rotation
- InitializePrimaryKey registers active key at startup
- /api/v1/public-keys endpoint for rotation-aware agents
- SigningKeyQueries for key lifecycle management
Agent:
- Key-ID-aware verification via CheckKeyRotation
- FetchAndCacheAllActiveKeys for rotation pre-caching
- Cache metadata with TTL and staleness fallback
- SecurityLogger events for key rotation and command signing
== A-2: Replay Attack Fixes (F-1 through F-7) ==
F-5 CRITICAL - RetryCommand now signs via signAndCreateCommand
F-1 HIGH - v3 format: "{agent_id}:{cmd_id}:{type}:{hash}:{ts}"
F-7 HIGH - Migration 026: expires_at column with partial index
F-6 HIGH - GetPendingCommands/GetStuckCommands filter by expires_at
F-2 HIGH - Agent-side executedIDs dedup map with cleanup
F-4 HIGH - commandMaxAge reduced from 24h to 4h
F-3 CRITICAL - Old-format commands rejected after 48h via CreatedAt
Verification fixes: migration idempotency (ETHOS #4), log format
compliance (ETHOS #1), stale comments updated.
All 24 tests passing. Docker --no-cache build verified.
See docs/ for full audit reports and deviation log (DEV-001 to DEV-019).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
129 lines
3.4 KiB
Go
129 lines
3.4 KiB
Go
package system
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/denisbrodbeck/machineid"
|
|
)
|
|
|
|
// GetMachineID generates a unique machine identifier that persists across reboots
|
|
func GetMachineID() (string, error) {
|
|
// Try machineid library first (cross-platform)
|
|
id, err := machineid.ID()
|
|
if err == nil && id != "" {
|
|
// Hash the machine ID for consistency and privacy
|
|
return hashMachineID(id), nil
|
|
}
|
|
|
|
// Fallback to OS-specific methods
|
|
switch runtime.GOOS {
|
|
case "linux":
|
|
return getLinuxMachineID()
|
|
case "windows":
|
|
return getWindowsMachineID()
|
|
case "darwin":
|
|
return getDarwinMachineID()
|
|
default:
|
|
return generateGenericMachineID()
|
|
}
|
|
}
|
|
|
|
// hashMachineID creates a consistent hash from machine ID
|
|
func hashMachineID(id string) string {
|
|
hash := sha256.Sum256([]byte(id))
|
|
return hex.EncodeToString(hash[:]) // Return full hash for uniqueness
|
|
}
|
|
|
|
// getLinuxMachineID tries multiple sources for Linux machine ID
|
|
func getLinuxMachineID() (string, error) {
|
|
// Try /etc/machine-id first (systemd)
|
|
if id, err := os.ReadFile("/etc/machine-id"); err == nil {
|
|
idStr := strings.TrimSpace(string(id))
|
|
if idStr != "" {
|
|
return hashMachineID(idStr), nil
|
|
}
|
|
}
|
|
|
|
// Try /var/lib/dbus/machine-id
|
|
if id, err := os.ReadFile("/var/lib/dbus/machine-id"); err == nil {
|
|
idStr := strings.TrimSpace(string(id))
|
|
if idStr != "" {
|
|
return hashMachineID(idStr), nil
|
|
}
|
|
}
|
|
|
|
// Try DMI product UUID
|
|
if id, err := os.ReadFile("/sys/class/dmi/id/product_uuid"); err == nil {
|
|
idStr := strings.TrimSpace(string(id))
|
|
if idStr != "" {
|
|
return hashMachineID(idStr), nil
|
|
}
|
|
}
|
|
|
|
// Try /etc/hostname as last resort
|
|
if hostname, err := os.ReadFile("/etc/hostname"); err == nil {
|
|
hostnameStr := strings.TrimSpace(string(hostname))
|
|
if hostnameStr != "" {
|
|
return hashMachineID(hostnameStr + "-linux-fallback"), nil
|
|
}
|
|
}
|
|
|
|
return generateGenericMachineID()
|
|
}
|
|
|
|
// getWindowsMachineID gets Windows machine ID
|
|
func getWindowsMachineID() (string, error) {
|
|
// Try machineid library Windows registry keys first
|
|
if id, err := machineid.ID(); err == nil && id != "" {
|
|
return hashMachineID(id), nil
|
|
}
|
|
|
|
// Fallback to generating generic ID
|
|
return generateGenericMachineID()
|
|
}
|
|
|
|
// getDarwinMachineID gets macOS machine ID
|
|
func getDarwinMachineID() (string, error) {
|
|
// Try machineid library platform-specific keys first
|
|
if id, err := machineid.ID(); err == nil && id != "" {
|
|
return hashMachineID(id), nil
|
|
}
|
|
|
|
// Fallback to generating generic ID
|
|
return generateGenericMachineID()
|
|
}
|
|
|
|
// generateGenericMachineID creates a fallback machine ID from available system info
|
|
func generateGenericMachineID() (string, error) {
|
|
// Combine hostname with other available info
|
|
hostname, _ := os.Hostname()
|
|
if hostname == "" {
|
|
hostname = "unknown"
|
|
}
|
|
|
|
// Create a reasonably unique ID from available system info
|
|
idSource := fmt.Sprintf("%s-%s-%s", hostname, runtime.GOOS, runtime.GOARCH)
|
|
return hashMachineID(idSource), nil
|
|
}
|
|
|
|
// GetEmbeddedPublicKey returns the embedded public key fingerprint
|
|
// This should be set at build time using ldflags
|
|
var EmbeddedPublicKey = "not-set-at-build-time"
|
|
|
|
// GetPublicKeyFingerprint returns the fingerprint of the embedded public key
|
|
func GetPublicKeyFingerprint() string {
|
|
if EmbeddedPublicKey == "not-set-at-build-time" {
|
|
return ""
|
|
}
|
|
|
|
// Return first 8 bytes as fingerprint
|
|
if len(EmbeddedPublicKey) >= 16 {
|
|
return EmbeddedPublicKey[:16]
|
|
}
|
|
return EmbeddedPublicKey
|
|
} |