feat: Add Cloud Endpoint Sync Commands (#216)

* feat: Add cloud sync command

* feat: Add pmg cloud commands

* fix: Code review fixes

* fix: Code review fixes

* fix: Code review fixes

* fix: Code review fixes

* fix: Code review fixes

* fix: Code review fixes
This commit is contained in:
Abhisek Datta
2026-04-13 13:40:18 +05:30
committed by GitHub
parent e67735c1c3
commit 887984612c
14 changed files with 453 additions and 92 deletions
+18
View File
@@ -0,0 +1,18 @@
package cloud
import (
"github.com/spf13/cobra"
)
func NewCloudCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "cloud",
Short: "SafeDep Cloud operations",
}
cmd.AddCommand(newSyncCommand())
cmd.AddCommand(newLoginCommand())
cmd.AddCommand(newLogoutCommand())
return cmd
}
+72
View File
@@ -0,0 +1,72 @@
package cloud
import (
"github.com/safedep/dry/cloud"
"github.com/safedep/dry/log"
"github.com/safedep/pmg/internal/ui"
"github.com/safedep/pmg/usefulerror"
"github.com/spf13/cobra"
)
func newLoginCommand() *cobra.Command {
return &cobra.Command{
Use: "login",
Short: "Store SafeDep Cloud credentials securely",
RunE: runLogin,
}
}
func runLogin(cmd *cobra.Command, args []string) error {
tenantID, err := ui.PromptInput("Tenant ID: ")
if err != nil {
ui.ErrorExit(usefulerror.Useful().
Wrap(err).
WithCode(usefulerror.ErrCodeLifecycle).
WithHumanError("Failed to read Tenant ID"))
}
if tenantID == "" {
ui.ErrorExit(usefulerror.Useful().
WithCode(usefulerror.ErrCodeInvalidArgument).
WithHumanError("Tenant ID cannot be empty"))
}
apiKey, err := ui.PromptSecret("API Key: ")
if err != nil {
ui.ErrorExit(usefulerror.Useful().
Wrap(err).
WithCode(usefulerror.ErrCodeLifecycle).
WithHumanError("Failed to read API Key"))
}
if apiKey == "" {
ui.ErrorExit(usefulerror.Useful().
WithCode(usefulerror.ErrCodeInvalidArgument).
WithHumanError("API Key cannot be empty"))
}
store, err := cloud.NewKeychainCredentialStore()
if err != nil {
ui.ErrorExit(usefulerror.Useful().
Wrap(err).
WithCode(usefulerror.ErrCodeLifecycle).
WithHumanError("Failed to initialize credential store").
WithHelp("Your system may not support secure credential storage"))
}
defer func() {
if err := store.Close(); err != nil {
log.Warnf("failed to close credential store: %v", err)
}
}()
if err := store.SaveAPIKeyCredential(apiKey, tenantID); err != nil {
ui.ErrorExit(usefulerror.Useful().
Wrap(err).
WithCode(usefulerror.ErrCodeLifecycle).
WithHumanError("Failed to save credentials").
WithHelp("Your system may not support secure credential storage"))
}
ui.Successf("Credentials saved securely")
return nil
}
+43
View File
@@ -0,0 +1,43 @@
package cloud
import (
"github.com/safedep/dry/cloud"
"github.com/safedep/dry/log"
"github.com/safedep/pmg/internal/ui"
"github.com/safedep/pmg/usefulerror"
"github.com/spf13/cobra"
)
func newLogoutCommand() *cobra.Command {
return &cobra.Command{
Use: "logout",
Short: "Clear stored SafeDep Cloud credentials",
RunE: runLogout,
}
}
func runLogout(cmd *cobra.Command, args []string) error {
store, err := cloud.NewKeychainCredentialStore()
if err != nil {
ui.ErrorExit(usefulerror.Useful().
Wrap(err).
WithCode(usefulerror.ErrCodeLifecycle).
WithHumanError("Failed to initialize credential store").
WithHelp("Your system may not support secure credential storage"))
}
defer func() {
if err := store.Close(); err != nil {
log.Warnf("failed to close credential store: %v", err)
}
}()
if err := store.Clear(); err != nil {
ui.ErrorExit(usefulerror.Useful().
Wrap(err).
WithCode(usefulerror.ErrCodeLifecycle).
WithHumanError("Failed to clear credentials"))
}
ui.Successf("Credentials cleared from keychain")
return nil
}
+74
View File
@@ -0,0 +1,74 @@
package cloud
import (
"context"
"time"
"github.com/safedep/dry/log"
"github.com/safedep/pmg/config"
"github.com/safedep/pmg/internal/analytics"
"github.com/safedep/pmg/internal/audit"
"github.com/safedep/pmg/internal/ui"
"github.com/safedep/pmg/usefulerror"
"github.com/spf13/cobra"
)
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 analytics.IsDisabled() {
ui.Infof("Cloud sync is disabled because telemetry is disabled (PMG_DISABLE_TELEMETRY)")
return nil
}
if !cfg.Config.Cloud.Enabled {
ui.ErrorExit(usefulerror.Useful().
WithCode(usefulerror.ErrCodeLifecycle).
WithHumanError("Cloud sync is not enabled").
WithHelp("Set 'cloud.enabled: true' in PMG config to enable cloud sync"))
}
ctx, cancel := context.WithTimeout(cmd.Context(), syncTimeout)
defer cancel()
bundle, err := audit.NewSyncClientBundle(cfg)
if err != nil {
ui.ErrorExit(usefulerror.Useful().
Wrap(err).
WithCode(usefulerror.ErrCodeLifecycle).
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)
if err != nil {
ui.ErrorExit(usefulerror.Useful().
Wrap(err).
WithCode(usefulerror.ErrCodeNetwork).
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
}