Use SaltedKeyFromPassphrase in Uplink CLI and Gateway (#2637)

Co-authored-by: Jeff Wendling <leterip@gmail.com>
This commit is contained in:
Kaloyan Raev 2019-07-29 10:17:49 +03:00 committed by GitHub
parent dd7c8610bb
commit 175c30048d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 12 deletions

View File

@ -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)

View File

@ -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

View File

@ -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,
}