diff --git a/cmd/gateway/main.go b/cmd/gateway/main.go index 4e0871df3..f81d6775d 100644 --- a/cmd/gateway/main.go +++ b/cmd/gateway/main.go @@ -285,28 +285,52 @@ func (flags GatewayFlags) openProject(ctx context.Context) (*libuplink.Project, func (flags GatewayFlags) interactive( cmd *cobra.Command, setupDir string, encryptionKeyFilepath string, overrides map[string]interface{}, ) error { + ctx := process.Ctx(cmd) + satelliteAddress, err := wizard.PromptForSatellite(cmd) if err != nil { return Error.Wrap(err) } - apiKey, err := wizard.PromptForAPIKey() + apiKeyString, err := wizard.PromptForAPIKey() if err != nil { return Error.Wrap(err) } - humanReadableKey, err := wizard.PromptForEncryptionKey() + apiKey, err := libuplink.ParseAPIKey(apiKeyString) if err != nil { return Error.Wrap(err) } - err = setup.SaveEncryptionKey(humanReadableKey, encryptionKeyFilepath) + passphrase, err := wizard.PromptForEncryptionPassphrase() + if err != nil { + return Error.Wrap(err) + } + + uplk, err := libuplink.NewUplink(ctx, nil) + if err != nil { + return Error.Wrap(err) + } + defer func() { err = errs.Combine(err, uplk.Close()) }() + + project, err := uplk.OpenProject(ctx, satelliteAddress, apiKey) + if err != nil { + return Error.Wrap(err) + } + defer func() { err = errs.Combine(err, project.Close()) }() + + key, err := project.SaltedKeyFromPassphrase(ctx, passphrase) + if err != nil { + return Error.Wrap(err) + } + + err = setup.SaveEncryptionKey(string(key[:]), encryptionKeyFilepath) if err != nil { return Error.Wrap(err) } overrides["satellite-addr"] = satelliteAddress - overrides["api-key"] = apiKey + overrides["api-key"] = apiKeyString overrides["enc.key-filepath"] = encryptionKeyFilepath err = process.SaveConfigWithAllDefaults(cmd.Flags(), filepath.Join(setupDir, "config.yaml"), overrides) diff --git a/cmd/internal/wizard/wizard.go b/cmd/internal/wizard/wizard.go index a82c6d421..467c752db 100644 --- a/cmd/internal/wizard/wizard.go +++ b/cmd/internal/wizard/wizard.go @@ -122,8 +122,8 @@ func PromptForAPIKey() (string, error) { return apiKey, nil } -// PromptForEncryptionKey handles user input for an encryption key to be used with wizards -func PromptForEncryptionKey() (string, error) { +// PromptForEncryptionPassphrase handles user input for an encryption passphrase to be used with wizards +func PromptForEncryptionPassphrase() (string, error) { _, err := fmt.Print("Enter your encryption passphrase: ") if err != nil { return "", err diff --git a/cmd/uplink/cmd/setup.go b/cmd/uplink/cmd/setup.go index 170402902..e3b5bcacf 100644 --- a/cmd/uplink/cmd/setup.go +++ b/cmd/uplink/cmd/setup.go @@ -11,9 +11,11 @@ import ( "strings" "github.com/spf13/cobra" + "github.com/zeebo/errs" "storj.io/storj/cmd/internal/wizard" "storj.io/storj/internal/fpath" + libuplink "storj.io/storj/lib/uplink" "storj.io/storj/pkg/cfgstruct" "storj.io/storj/pkg/process" "storj.io/storj/uplink/setup" @@ -105,29 +107,52 @@ func cmdSetupNonInteractive(cmd *cobra.Command, setupDir string, encryptionKeyFi // encryptionKeyFilepath should be set to the filepath indicated by the user or // or to a default path whose directory tree exists. func cmdSetupInteractive(cmd *cobra.Command, setupDir string, encryptionKeyFilepath string) error { + ctx := process.Ctx(cmd) satelliteAddress, err := wizard.PromptForSatellite(cmd) if err != nil { return err } - apiKey, err := wizard.PromptForAPIKey() + apiKeyString, err := wizard.PromptForAPIKey() if err != nil { - return err + return Error.Wrap(err) } - humanReadableKey, err := wizard.PromptForEncryptionKey() + apiKey, err := libuplink.ParseAPIKey(apiKeyString) if err != nil { - return err + return Error.Wrap(err) } - err = setup.SaveEncryptionKey(humanReadableKey, encryptionKeyFilepath) + passphrase, err := wizard.PromptForEncryptionPassphrase() + if err != nil { + return Error.Wrap(err) + } + + uplk, err := libuplink.NewUplink(ctx, nil) + if err != nil { + return Error.Wrap(err) + } + defer func() { err = errs.Combine(err, uplk.Close()) }() + + project, err := uplk.OpenProject(ctx, satelliteAddress, apiKey) + if err != nil { + return Error.Wrap(err) + } + defer func() { err = errs.Combine(err, project.Close()) }() + + key, err := project.SaltedKeyFromPassphrase(ctx, passphrase) + if err != nil { + return Error.Wrap(err) + } + + err = setup.SaveEncryptionKey(string(key[:]), encryptionKeyFilepath) if err != nil { return err } var override = map[string]interface{}{ - "api-key": apiKey, + "api-key": apiKeyString, "satellite-addr": satelliteAddress, "enc.key-filepath": encryptionKeyFilepath, }