|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "github.com/stellar/go/support/config" |
| 10 | + "github.com/stellar/go/support/log" |
| 11 | + "github.com/stellar/wallet-backend/cmd/utils" |
| 12 | + "github.com/stellar/wallet-backend/internal/db" |
| 13 | + "github.com/stellar/wallet-backend/internal/services" |
| 14 | + "github.com/stellar/wallet-backend/internal/signing/awskms" |
| 15 | + "github.com/stellar/wallet-backend/internal/signing/store" |
| 16 | +) |
| 17 | + |
| 18 | +type kmsCommandConfig struct { |
| 19 | + databaseURL string |
| 20 | + kmsKeyARN string |
| 21 | + awsRegion string |
| 22 | + distributionAccountPublicKey string |
| 23 | +} |
| 24 | + |
| 25 | +type distributionAccountCmd struct{} |
| 26 | + |
| 27 | +func (c *distributionAccountCmd) Command() *cobra.Command { |
| 28 | + cmd := cobra.Command{ |
| 29 | + Use: "distribution-account", |
| 30 | + Short: "Distribution Account Private Key management.", |
| 31 | + } |
| 32 | + |
| 33 | + cmd.AddCommand(kmsCommand()) |
| 34 | + |
| 35 | + return &cmd |
| 36 | +} |
| 37 | + |
| 38 | +func kmsCommand() *cobra.Command { |
| 39 | + cfg := kmsCommandConfig{} |
| 40 | + cfgOpts := config.ConfigOptions{ |
| 41 | + utils.DatabaseURLOption(&cfg.databaseURL), |
| 42 | + utils.DistributionAccountPublicKeyOption(&cfg.distributionAccountPublicKey), |
| 43 | + } |
| 44 | + cfgOpts = append(cfgOpts, utils.AWSOptions(&cfg.awsRegion, &cfg.kmsKeyARN, true)...) |
| 45 | + |
| 46 | + cmd := &cobra.Command{ |
| 47 | + Use: "kms", |
| 48 | + Short: "Manage the Distribution Account private key using KMS.", |
| 49 | + } |
| 50 | + |
| 51 | + var kmsImportService services.KMSImportService |
| 52 | + importCmd := &cobra.Command{ |
| 53 | + Use: "import", |
| 54 | + Short: "Import your Distribution Account Private Key. This command encrypts and stores the encrypted private key.", |
| 55 | + Args: cobra.NoArgs, |
| 56 | + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
| 57 | + if err := cfgOpts.RequireE(); err != nil { |
| 58 | + return fmt.Errorf("requiring values of config options: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + if err := cfgOpts.SetValues(); err != nil { |
| 62 | + return fmt.Errorf("setting values of config options: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + dbConnectionPool, err := db.OpenDBConnectionPool(cfg.databaseURL) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("opening connection pool: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + kmsClient, err := awskms.GetKMSClient(cfg.awsRegion) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("getting kms client: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + kmsImportService, err = services.NewKMSImportService(kmsClient, cfg.kmsKeyARN, store.NewKeypairModel(dbConnectionPool), cfg.distributionAccountPublicKey) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("instantiating kms import service: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + return nil |
| 81 | + }, |
| 82 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 83 | + ctx := cmd.Context() |
| 84 | + |
| 85 | + passwordPrompter, err := utils.NewDefaultPasswordPrompter( |
| 86 | + "🔑 Input your Distribution Account Private Key (key will be hidden):", os.Stdin, os.Stdout) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("instantiating password prompter: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + distributionAccountSeed, err := passwordPrompter.Run() |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("getting distribution account seed input: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + err = kmsImportService.ImportDistributionAccountKey(ctx, distributionAccountSeed) |
| 97 | + if err != nil { |
| 98 | + if errors.Is(err, services.ErrMismatchDistributionAccount) { |
| 99 | + return fmt.Errorf("the private key provided doesn't belong to the configured distribution account public key") |
| 100 | + } |
| 101 | + return fmt.Errorf("importing distribution account seed: %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + log.Ctx(ctx).Info("Successfully imported and encrypted the Distribution Account Private Key") |
| 105 | + return nil |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + cmd.AddCommand(importCmd) |
| 110 | + |
| 111 | + if err := cfgOpts.Init(cmd); err != nil { |
| 112 | + log.Fatalf("Error initializing a config option: %s", err.Error()) |
| 113 | + } |
| 114 | + |
| 115 | + return cmd |
| 116 | +} |
0 commit comments