2026-04-13 13:40:18 +05:30
|
|
|
package cloud
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/safedep/dry/log"
|
2026-05-24 12:46:06 +05:30
|
|
|
"github.com/safedep/dry/usefulerror"
|
2026-04-13 13:40:18 +05:30
|
|
|
"github.com/safedep/pmg/config"
|
2026-05-24 12:46:06 +05:30
|
|
|
"github.com/safedep/pmg/errcodes"
|
2026-04-13 13:40:18 +05:30
|
|
|
"github.com/safedep/pmg/internal/audit"
|
|
|
|
|
"github.com/safedep/pmg/internal/ui"
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-20 13:56:50 +05:30
|
|
|
// manualSyncLockTimeout caps how long `pmg cloud sync` waits to acquire the
|
|
|
|
|
// shared sync lock when an auto-sync child is already running. Long enough to
|
|
|
|
|
// let a normal background drain complete, short enough that a stuck process
|
|
|
|
|
// surfaces as a usefulerror rather than an indefinite hang.
|
|
|
|
|
const manualSyncLockTimeout = 30 * time.Second
|
|
|
|
|
|
2026-04-13 13:40:18 +05:30
|
|
|
var syncTimeout time.Duration
|
|
|
|
|
|
|
|
|
|
func newSyncCommand() *cobra.Command {
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
|
Use: "sync",
|
|
|
|
|
Short: "Sync pending audit events to SafeDep Cloud",
|
|
|
|
|
RunE: runSync,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd.Flags().DurationVar(&syncTimeout, "timeout", 15*time.Minute, "Maximum time to spend syncing events")
|
|
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func runSync(cmd *cobra.Command, args []string) error {
|
|
|
|
|
cfg := config.Get()
|
|
|
|
|
|
|
|
|
|
if !cfg.Config.Cloud.Enabled {
|
2026-05-24 12:22:19 +05:30
|
|
|
ui.ErrorExit(usefulerror.NewUsefulError().
|
|
|
|
|
WithCode(errcodes.Lifecycle).
|
2026-04-13 13:40:18 +05:30
|
|
|
WithHumanError("Cloud sync is not enabled").
|
|
|
|
|
WithHelp("Set 'cloud.enabled: true' in PMG config to enable cloud sync"))
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-20 13:56:50 +05:30
|
|
|
lock := audit.NewSyncLock(cfg.CloudSyncLockPath())
|
|
|
|
|
lockCtx, lockCancel := context.WithTimeout(cmd.Context(), manualSyncLockTimeout)
|
|
|
|
|
defer lockCancel()
|
|
|
|
|
|
|
|
|
|
locked, err := lock.TryLockContext(lockCtx, 250*time.Millisecond)
|
|
|
|
|
if err != nil {
|
2026-05-24 12:22:19 +05:30
|
|
|
ui.ErrorExit(usefulerror.NewUsefulError().
|
2026-05-20 13:56:50 +05:30
|
|
|
Wrap(err).
|
2026-05-24 12:22:19 +05:30
|
|
|
WithCode(errcodes.Lifecycle).
|
2026-05-20 13:56:50 +05:30
|
|
|
WithHumanError("Failed to acquire cloud sync lock").
|
|
|
|
|
WithHelp("Another sync may be in progress; try again shortly"))
|
|
|
|
|
}
|
|
|
|
|
if !locked {
|
2026-05-24 12:22:19 +05:30
|
|
|
ui.ErrorExit(usefulerror.NewUsefulError().
|
|
|
|
|
WithCode(errcodes.Lifecycle).
|
2026-05-20 13:56:50 +05:30
|
|
|
WithHumanError("Another cloud sync is already in progress").
|
|
|
|
|
WithHelp("Wait for the in-progress sync to finish, then try again"))
|
|
|
|
|
}
|
|
|
|
|
defer func() {
|
|
|
|
|
if err := lock.Unlock(); err != nil {
|
|
|
|
|
log.Warnf("failed to release cloud sync lock: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2026-04-13 13:40:18 +05:30
|
|
|
ctx, cancel := context.WithTimeout(cmd.Context(), syncTimeout)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
bundle, err := audit.NewSyncClientBundle(cfg)
|
|
|
|
|
if err != nil {
|
2026-05-24 12:22:19 +05:30
|
|
|
ui.ErrorExit(usefulerror.NewUsefulError().
|
2026-04-13 13:40:18 +05:30
|
|
|
Wrap(err).
|
2026-05-24 12:22:19 +05:30
|
|
|
WithCode(errcodes.Lifecycle).
|
2026-04-13 13:40:18 +05:30
|
|
|
WithHumanError("Failed to initialize cloud sync client").
|
|
|
|
|
WithHelp("Run 'pmg cloud login' to store credentials, or set SAFEDEP_API_KEY and SAFEDEP_TENANT_ID environment variables"))
|
|
|
|
|
}
|
|
|
|
|
defer func() {
|
|
|
|
|
if err := bundle.Close(); err != nil {
|
|
|
|
|
log.Warnf("failed to close sync client: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
synced, err := bundle.Sync(ctx)
|
2026-05-20 13:56:50 +05:30
|
|
|
recordLastSyncAttempt(cfg)
|
2026-04-13 13:40:18 +05:30
|
|
|
if err != nil {
|
2026-05-24 12:22:19 +05:30
|
|
|
ui.ErrorExit(usefulerror.NewUsefulError().
|
2026-04-13 13:40:18 +05:30
|
|
|
Wrap(err).
|
2026-05-24 12:22:19 +05:30
|
|
|
WithCode(errcodes.Network).
|
2026-04-13 13:40:18 +05:30
|
|
|
WithHumanError("Failed to sync events to SafeDep Cloud").
|
|
|
|
|
WithHelp("Check your network connectivity and ensure SafeDep Cloud is reachable").
|
|
|
|
|
WithAdditionalHelp("Override the cloud endpoint with SAFEDEP_CLOUD_DATA_ADDR if needed"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ui.Successf("Synced %d events to SafeDep Cloud", synced)
|
|
|
|
|
return nil
|
|
|
|
|
}
|