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>
117 lines
2.7 KiB
Go
117 lines
2.7 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/swarm"
|
|
"github.com/docker/docker/client"
|
|
)
|
|
|
|
// DockerSecretsService manages Docker secrets via Docker API
|
|
type DockerSecretsService struct {
|
|
cli *client.Client
|
|
}
|
|
|
|
// NewDockerSecretsService creates a new Docker secrets service
|
|
func NewDockerSecretsService() (*DockerSecretsService, error) {
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create Docker client: %w", err)
|
|
}
|
|
|
|
// Test connection
|
|
ctx := context.Background()
|
|
if _, err := cli.Ping(ctx); err != nil {
|
|
return nil, fmt.Errorf("failed to connect to Docker daemon: %w", err)
|
|
}
|
|
|
|
return &DockerSecretsService{cli: cli}, nil
|
|
}
|
|
|
|
// CreateSecret creates a new Docker secret
|
|
func (s *DockerSecretsService) CreateSecret(name, value string) error {
|
|
ctx := context.Background()
|
|
|
|
// Check if secret already exists
|
|
secrets, err := s.cli.SecretList(ctx, types.SecretListOptions{})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to list secrets: %w", err)
|
|
}
|
|
|
|
for _, secret := range secrets {
|
|
if secret.Spec.Name == name {
|
|
return fmt.Errorf("secret %s already exists", name)
|
|
}
|
|
}
|
|
|
|
// Create the secret
|
|
secretSpec := swarm.SecretSpec{
|
|
Annotations: swarm.Annotations{
|
|
Name: name,
|
|
Labels: map[string]string{
|
|
"created-by": "redflag-setup",
|
|
"created-at": fmt.Sprintf("%d", 0), // Use current timestamp in real implementation
|
|
},
|
|
},
|
|
Data: []byte(value),
|
|
}
|
|
|
|
if _, err := s.cli.SecretCreate(ctx, secretSpec); err != nil {
|
|
return fmt.Errorf("failed to create secret %s: %w", name, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// DeleteSecret deletes a Docker secret
|
|
func (s *DockerSecretsService) DeleteSecret(name string) error {
|
|
ctx := context.Background()
|
|
|
|
// Find the secret
|
|
secrets, err := s.cli.SecretList(ctx, types.SecretListOptions{})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to list secrets: %w", err)
|
|
}
|
|
|
|
var secretID string
|
|
for _, secret := range secrets {
|
|
if secret.Spec.Name == name {
|
|
secretID = secret.ID
|
|
break
|
|
}
|
|
}
|
|
|
|
if secretID == "" {
|
|
return fmt.Errorf("secret %s not found", name)
|
|
}
|
|
|
|
if err := s.cli.SecretRemove(ctx, secretID); err != nil {
|
|
return fmt.Errorf("failed to remove secret %s: %w", name, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Close closes the Docker client
|
|
func (s *DockerSecretsService) Close() error {
|
|
if s.cli != nil {
|
|
return s.cli.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// IsDockerAvailable checks if Docker API is accessible
|
|
func IsDockerAvailable() bool {
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer cli.Close()
|
|
|
|
ctx := context.Background()
|
|
_, err = cli.Ping(ctx)
|
|
return err == nil
|
|
}
|