Fix zero UUID and storage metrics issues\n\n- Add ID generation in signAndCreateCommand to prevent zero UUIDs\n- Fix storage metrics metadata field (remove pq.Array)\n- Add IdempotencyKey field to AgentCommand model\n\nThese issues were causing duplicate key violations and storage page errors.

This commit is contained in:
Fimeg
2025-12-20 14:26:55 -05:00
parent e7a8cc90dd
commit 642eec5d44
8 changed files with 15 additions and 289 deletions

View File

@@ -32,6 +32,19 @@ func NewSubsystemHandler(sq *queries.SubsystemQueries, cq *queries.CommandQuerie
// signAndCreateCommand signs a command if signing service is enabled, then stores it in the database
func (h *SubsystemHandler) signAndCreateCommand(cmd *models.AgentCommand) error {
// Generate ID if not set (prevents zero UUID issues)
if cmd.ID == uuid.Nil {
cmd.ID = uuid.New()
}
// Set timestamps if not set
if cmd.CreatedAt.IsZero() {
cmd.CreatedAt = time.Now()
}
if cmd.UpdatedAt.IsZero() {
cmd.UpdatedAt = time.Now()
}
// Sign the command before storing
if h.signingService != nil && h.signingService.IsEnabled() {
signature, err := h.signingService.SignCommand(cmd)

View File

@@ -34,7 +34,7 @@ func (q *StorageMetricsQueries) InsertStorageMetric(ctx context.Context, metric
metric.ID, metric.AgentID, metric.Mountpoint, metric.Device,
metric.DiskType, metric.Filesystem, metric.TotalBytes,
metric.UsedBytes, metric.AvailableBytes, metric.UsedPercent,
metric.Severity, pq.Array(metric.Metadata), metric.CreatedAt,
metric.Severity, metric.Metadata, metric.CreatedAt,
)
if err != nil {

View File

@@ -16,6 +16,7 @@ type AgentCommand struct {
Status string `json:"status" db:"status"`
Source string `json:"source" db:"source"`
Signature string `json:"signature,omitempty" db:"signature"`
IdempotencyKey *string `json:"idempotency_key,omitempty" db:"idempotency_key"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
SentAt *time.Time `json:"sent_at,omitempty" db:"sent_at"`