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>
138 lines
4.6 KiB
Go
138 lines
4.6 KiB
Go
package services
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/Fimeg/RedFlag/aggregator-server/internal/database/queries"
|
|
"github.com/Fimeg/RedFlag/aggregator-server/internal/models"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// BuildOrchestratorService handles building and signing agent binaries
|
|
type BuildOrchestratorService struct {
|
|
signingService *SigningService
|
|
packageQueries *queries.PackageQueries
|
|
agentDir string // Directory containing pre-built binaries
|
|
}
|
|
|
|
// NewBuildOrchestratorService creates a new build orchestrator service
|
|
func NewBuildOrchestratorService(signingService *SigningService, packageQueries *queries.PackageQueries, agentDir string) *BuildOrchestratorService {
|
|
return &BuildOrchestratorService{
|
|
signingService: signingService,
|
|
packageQueries: packageQueries,
|
|
agentDir: agentDir,
|
|
}
|
|
}
|
|
|
|
// BuildAndSignAgent builds (or retrieves) and signs an agent binary
|
|
func (s *BuildOrchestratorService) BuildAndSignAgent(version, platform, architecture string) (*models.AgentUpdatePackage, error) {
|
|
// Determine binary name
|
|
binaryName := "redflag-agent"
|
|
if strings.HasPrefix(platform, "windows") {
|
|
binaryName += ".exe"
|
|
}
|
|
|
|
// Path to pre-built binary
|
|
binaryPath := filepath.Join(s.agentDir, "binaries", platform, binaryName)
|
|
|
|
// Check if binary exists
|
|
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
|
|
return nil, fmt.Errorf("binary not found for platform %s: %w", platform, err)
|
|
}
|
|
|
|
// Sign the binary if signing is enabled
|
|
if s.signingService.IsEnabled() {
|
|
signedPackage, err := s.signingService.SignFile(binaryPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to sign agent binary: %w", err)
|
|
}
|
|
|
|
// Set additional fields
|
|
signedPackage.Version = version
|
|
signedPackage.Platform = platform
|
|
signedPackage.Architecture = architecture
|
|
|
|
// Store signed package in database
|
|
err = s.packageQueries.StoreSignedPackage(signedPackage)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to store signed package: %w", err)
|
|
}
|
|
|
|
log.Printf("Successfully signed and stored agent binary: %s (%s/%s)", signedPackage.ID, platform, architecture)
|
|
return signedPackage, nil
|
|
} else {
|
|
log.Printf("Signing disabled, creating unsigned package entry")
|
|
// Create unsigned package entry for backward compatibility
|
|
unsignedPackage := &models.AgentUpdatePackage{
|
|
ID: uuid.New(),
|
|
Version: version,
|
|
Platform: platform,
|
|
Architecture: architecture,
|
|
BinaryPath: binaryPath,
|
|
Signature: "",
|
|
Checksum: "", // Would need to calculate if needed
|
|
FileSize: 0, // Would need to stat if needed
|
|
CreatedBy: "build-orchestrator",
|
|
IsActive: true,
|
|
}
|
|
|
|
// Get file info
|
|
if info, err := os.Stat(binaryPath); err == nil {
|
|
unsignedPackage.FileSize = info.Size()
|
|
}
|
|
|
|
// Store unsigned package
|
|
err := s.packageQueries.StoreSignedPackage(unsignedPackage)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to store unsigned package: %w", err)
|
|
}
|
|
|
|
return unsignedPackage, nil
|
|
}
|
|
}
|
|
|
|
// SignExistingBinary signs an existing binary file
|
|
func (s *BuildOrchestratorService) SignExistingBinary(binaryPath, version, platform, architecture string) (*models.AgentUpdatePackage, error) {
|
|
// Check if file exists
|
|
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
|
|
return nil, fmt.Errorf("binary not found: %s", binaryPath)
|
|
}
|
|
|
|
// Sign the binary if signing is enabled
|
|
if !s.signingService.IsEnabled() {
|
|
return nil, fmt.Errorf("signing service is disabled")
|
|
}
|
|
|
|
signedPackage, err := s.signingService.SignFile(binaryPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to sign agent binary: %w", err)
|
|
}
|
|
|
|
// Set additional fields
|
|
signedPackage.Version = version
|
|
signedPackage.Platform = platform
|
|
signedPackage.Architecture = architecture
|
|
|
|
// Store signed package in database
|
|
err = s.packageQueries.StoreSignedPackage(signedPackage)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to store signed package: %w", err)
|
|
}
|
|
|
|
log.Printf("Successfully signed and stored agent binary: %s (%s/%s)", signedPackage.ID, platform, architecture)
|
|
return signedPackage, nil
|
|
}
|
|
|
|
// GetSignedPackage retrieves a signed package by version and platform
|
|
func (s *BuildOrchestratorService) GetSignedPackage(version, platform, architecture string) (*models.AgentUpdatePackage, error) {
|
|
return s.packageQueries.GetSignedPackage(version, platform, architecture)
|
|
}
|
|
|
|
// ListSignedPackages lists all signed packages (with optional filters)
|
|
func (s *BuildOrchestratorService) ListSignedPackages(version, platform string, limit, offset int) ([]models.AgentUpdatePackage, error) {
|
|
return s.packageQueries.ListUpdatePackages(version, platform, limit, offset)
|
|
} |