2026-04-13 13:40:18 +05:30
|
|
|
package cloud
|
|
|
|
|
|
|
|
|
|
import (
|
2026-06-26 11:19:28 +05:30
|
|
|
"errors"
|
2026-04-13 13:40:18 +05:30
|
|
|
"time"
|
|
|
|
|
|
2026-07-21 16:02:04 +05:30
|
|
|
"github.com/safedep/dry/cloud"
|
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-06-26 11:19:28 +05:30
|
|
|
synced, err := audit.DrainToCloud(cmd.Context(), cfg, manualSyncLockTimeout, syncTimeout)
|
2026-05-20 13:56:50 +05:30
|
|
|
if err != nil {
|
2026-07-10 15:19:09 +05:30
|
|
|
ui.ErrorExit(syncFailureError(err))
|
2026-04-13 13:40:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ui.Successf("Synced %d events to SafeDep Cloud", synced)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-07-10 15:19:09 +05:30
|
|
|
|
|
|
|
|
// syncFailureError maps a DrainToCloud failure to a user-facing error. Errors
|
2026-07-21 16:02:04 +05:30
|
|
|
// the backend already classified (entitlements, quota — via gRPC status and
|
|
|
|
|
// usefulerror converters) pass through so the real cause is shown, except
|
|
|
|
|
// auth failures which get credential setup guidance; the network-flavored
|
|
|
|
|
// message is only a fallback for errors nothing can classify.
|
2026-07-10 15:19:09 +05:30
|
|
|
func syncFailureError(err error) error {
|
|
|
|
|
if errors.Is(err, audit.ErrSyncInProgress) {
|
|
|
|
|
return usefulerror.NewUsefulError().
|
|
|
|
|
WithCode(errcodes.Lifecycle).
|
|
|
|
|
WithHumanError("Another cloud sync is already in progress").
|
|
|
|
|
WithHelp("Wait for the in-progress sync to finish, then try again")
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 16:02:04 +05:30
|
|
|
if errors.Is(err, cloud.ErrMissingCredentials) {
|
|
|
|
|
return usefulerror.NewUsefulError().
|
|
|
|
|
Wrap(err).
|
|
|
|
|
WithCode(errcodes.CloudCredentialsNotFound).
|
|
|
|
|
WithHumanError("SafeDep Cloud credentials are not configured").
|
|
|
|
|
WithHelp("Run 'pmg cloud login' to store credentials, or set the SAFEDEP_API_KEY and SAFEDEP_TENANT_ID environment variables")
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-10 15:19:09 +05:30
|
|
|
if usefulErr, ok := usefulerror.AsUsefulError(err); ok {
|
2026-07-21 16:02:04 +05:30
|
|
|
return withCredentialGuidance(usefulErr, err)
|
2026-07-10 15:19:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return usefulerror.NewUsefulError().
|
|
|
|
|
Wrap(err).
|
|
|
|
|
WithCode(errcodes.Network).
|
|
|
|
|
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")
|
|
|
|
|
}
|
2026-07-21 16:02:04 +05:30
|
|
|
|
|
|
|
|
// withCredentialGuidance rebrands generic auth failures (invalid API key,
|
|
|
|
|
// wrong tenant, gateway 401/403) with how to fix credentials in pmg. Richer
|
|
|
|
|
// classifications like missing entitlements keep their own code and help.
|
|
|
|
|
func withCredentialGuidance(usefulErr usefulerror.UsefulError, err error) error {
|
|
|
|
|
code := usefulErr.Code()
|
|
|
|
|
if code != usefulerror.ErrAuthenticationFailed && code != usefulerror.ErrAuthorizationFailed {
|
|
|
|
|
return usefulErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return usefulerror.NewUsefulError().
|
|
|
|
|
Wrap(err).
|
|
|
|
|
WithCode(code).
|
|
|
|
|
WithHumanError("SafeDep Cloud rejected your credentials").
|
|
|
|
|
WithHelp("Run 'pmg cloud login' to update credentials, or check the SAFEDEP_API_KEY and SAFEDEP_TENANT_ID environment variables").
|
|
|
|
|
WithAdditionalHelp(usefulErr.AdditionalHelp())
|
|
|
|
|
}
|