cmd/uplink/cmd: remove old libuplink from Uplink CLI code
Change-Id: I38444a67e3bdec5f63c56895b5fc362464e7dce1
This commit is contained in:
parent
bcb867f1de
commit
340700eda9
@ -4,13 +4,17 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/btcsuite/btcutil/base58"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
"storj.io/common/pb"
|
||||
"storj.io/private/cfgstruct"
|
||||
"storj.io/private/process"
|
||||
libuplink "storj.io/storj/lib/uplink"
|
||||
"storj.io/uplink"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -52,18 +56,18 @@ func accessList(cmd *cobra.Command, args []string) (err error) {
|
||||
accesses := listCfg.Accesses
|
||||
fmt.Println("=========== ACCESSES LIST: name / satellite ================================")
|
||||
for name, data := range accesses {
|
||||
access, err := libuplink.ParseScope(data)
|
||||
satelliteAddr, _, _, err := parseAccess(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(name, "/", access.SatelliteAddr)
|
||||
fmt.Println(name, "/", satelliteAddr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func accessInspect(cmd *cobra.Command, args []string) (err error) {
|
||||
var access *libuplink.Scope
|
||||
var access *uplink.Access
|
||||
if len(args) == 0 {
|
||||
access, err = inspectCfg.GetAccess()
|
||||
if err != nil {
|
||||
@ -78,21 +82,46 @@ func accessInspect(cmd *cobra.Command, args []string) (err error) {
|
||||
}
|
||||
|
||||
if access == nil {
|
||||
if access, err = libuplink.ParseScope(firstArg); err != nil {
|
||||
if access, err = uplink.ParseAccess(firstArg); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
serializedAPIKey := access.APIKey.Serialize()
|
||||
serializedEncAccess, err := access.EncryptionAccess.Serialize()
|
||||
serializedAccesss, err := access.Serialize()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
satAddr, apiKey, ea, err := parseAccess(serializedAccesss)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("=========== ACCESS INFO ==================================================================")
|
||||
fmt.Println("Satellite :", access.SatelliteAddr)
|
||||
fmt.Println("API Key :", serializedAPIKey)
|
||||
fmt.Println("Encryption Access:", serializedEncAccess)
|
||||
fmt.Println("Satellite :", satAddr)
|
||||
fmt.Println("API Key :", apiKey)
|
||||
fmt.Println("Encryption Access:", ea)
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseAccess(access string) (sa string, apiKey string, ea string, err error) {
|
||||
data, version, err := base58.CheckDecode(access)
|
||||
if err != nil || version != 0 {
|
||||
return "", "", "", errors.New("invalid access grant format")
|
||||
}
|
||||
|
||||
p := new(pb.Scope)
|
||||
if err := pb.Unmarshal(data, p); err != nil {
|
||||
return "", "", "", err
|
||||
}
|
||||
|
||||
eaData, err := pb.Marshal(p.EncryptionAccess)
|
||||
if err != nil {
|
||||
return "", "", "", errs.New("unable to marshal encryption access: %v", err)
|
||||
}
|
||||
|
||||
apiKey = base58.CheckEncode(p.ApiKey, 0)
|
||||
ea = base58.CheckEncode(eaData, 0)
|
||||
return p.SatelliteAddr, apiKey, ea, nil
|
||||
}
|
||||
|
@ -4,15 +4,10 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spacemonkeygo/monkit/v3"
|
||||
"github.com/zeebo/errs"
|
||||
|
||||
"storj.io/common/storj"
|
||||
libuplink "storj.io/storj/lib/uplink"
|
||||
"storj.io/uplink"
|
||||
)
|
||||
|
||||
@ -39,21 +34,6 @@ type AccessConfig struct {
|
||||
// used for backward compatibility
|
||||
Scopes map[string]string `internal:"true"` // deprecated
|
||||
Scope string `internal:"true"` // deprecated
|
||||
|
||||
Legacy // Holds on to legacy configuration values
|
||||
}
|
||||
|
||||
// Legacy holds deprecated configuration values
|
||||
type Legacy struct {
|
||||
Client struct {
|
||||
APIKey string `default:"" help:"the api key to use for the satellite (deprecated)" noprefix:"true" deprecated:"true"`
|
||||
SatelliteAddr string `releaseDefault:"127.0.0.1:7777" devDefault:"127.0.0.1:10000" help:"the address to use for the satellite (deprecated)" noprefix:"true"`
|
||||
}
|
||||
Enc struct {
|
||||
EncryptionKey string `help:"the root key for encrypting the data which will be stored in KeyFilePath (deprecated)" setup:"true" deprecated:"true"`
|
||||
KeyFilepath string `help:"the path to the file which contains the root key for encrypting the data (deprecated)" deprecated:"true"`
|
||||
EncAccessFilepath string `help:"the path to a file containing a serialized encryption access (deprecated)" deprecated:"true"`
|
||||
}
|
||||
}
|
||||
|
||||
// normalize looks for usage of deprecated config values and sets the respective
|
||||
@ -79,7 +59,7 @@ func (a AccessConfig) normalize() (_ AccessConfig) {
|
||||
}
|
||||
|
||||
// GetAccess returns the appropriate access for the config.
|
||||
func (a AccessConfig) GetAccess() (_ *libuplink.Scope, err error) {
|
||||
func (a AccessConfig) GetAccess() (_ *uplink.Access, err error) {
|
||||
defer mon.Task()(nil)(&err)
|
||||
|
||||
a = a.normalize()
|
||||
@ -93,84 +73,14 @@ func (a AccessConfig) GetAccess() (_ *libuplink.Scope, err error) {
|
||||
}
|
||||
|
||||
// Otherwise, try to load the access name as a serialized access.
|
||||
if access, err := libuplink.ParseScope(a.Access); err == nil {
|
||||
return access, nil
|
||||
}
|
||||
|
||||
if len(a.Legacy.Client.APIKey) == 0 {
|
||||
return nil, errs.New("unable to find access grant, run 'setup' command or provide '--access' parameter")
|
||||
}
|
||||
|
||||
// fall back to trying to load the legacy values.
|
||||
apiKey, err := libuplink.ParseAPIKey(a.Legacy.Client.APIKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
satelliteAddr := a.Legacy.Client.SatelliteAddr
|
||||
if satelliteAddr == "" {
|
||||
return nil, errs.New("must specify a satellite address")
|
||||
}
|
||||
|
||||
var encAccess *libuplink.EncryptionAccess
|
||||
if a.Legacy.Enc.EncAccessFilepath != "" {
|
||||
data, err := ioutil.ReadFile(a.Legacy.Enc.EncAccessFilepath)
|
||||
if err != nil {
|
||||
return nil, errs.Wrap(err)
|
||||
}
|
||||
encAccess, err = libuplink.ParseEncryptionAccess(strings.TrimSpace(string(data)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
data := []byte(a.Legacy.Enc.EncryptionKey)
|
||||
if a.Legacy.Enc.KeyFilepath != "" {
|
||||
data, err = ioutil.ReadFile(a.Legacy.Enc.KeyFilepath)
|
||||
if err != nil {
|
||||
return nil, errs.Wrap(err)
|
||||
}
|
||||
}
|
||||
key, err := storj.NewKey(data)
|
||||
if err != nil {
|
||||
return nil, errs.Wrap(err)
|
||||
}
|
||||
encAccess = libuplink.NewEncryptionAccessWithDefaultKey(*key)
|
||||
encAccess.SetDefaultPathCipher(storj.EncAESGCM)
|
||||
}
|
||||
|
||||
return &libuplink.Scope{
|
||||
APIKey: apiKey,
|
||||
SatelliteAddr: satelliteAddr,
|
||||
EncryptionAccess: encAccess,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetNewAccess returns the appropriate access for the config.
|
||||
func (a AccessConfig) GetNewAccess() (_ *uplink.Access, err error) {
|
||||
defer mon.Task()(nil)(&err)
|
||||
|
||||
oldAccess, err := a.GetAccess()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
serializedOldAccess, err := oldAccess.Serialize()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
access, err := uplink.ParseAccess(serializedOldAccess)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return access, nil
|
||||
return uplink.ParseAccess(a.Access)
|
||||
}
|
||||
|
||||
// GetNamedAccess returns named access if exists.
|
||||
func (a AccessConfig) GetNamedAccess(name string) (_ *libuplink.Scope, err error) {
|
||||
func (a AccessConfig) GetNamedAccess(name string) (_ *uplink.Access, err error) {
|
||||
// if an access exists for that name, try to load it.
|
||||
if data, ok := a.Accesses[name]; ok {
|
||||
return libuplink.ParseScope(data)
|
||||
return uplink.ParseAccess(data)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
@ -178,6 +88,6 @@ func (a AccessConfig) GetNamedAccess(name string) (_ *libuplink.Scope, err error
|
||||
// IsSerializedAccess returns whether the passed access is a serialized
|
||||
// access string or not.
|
||||
func IsSerializedAccess(access string) bool {
|
||||
_, err := libuplink.ParseScope(access)
|
||||
_, err := uplink.ParseAccess(access)
|
||||
return err == nil
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ func addCmd(cmd *cobra.Command, root *cobra.Command) *cobra.Command {
|
||||
}
|
||||
|
||||
func (cliCfg *UplinkFlags) getProject(ctx context.Context, encryptionBypass bool) (_ *uplink.Project, err error) {
|
||||
access, err := cfg.GetNewAccess()
|
||||
access, err := cfg.GetAccess()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -107,7 +107,7 @@ func shareMain(cmd *cobra.Command, args []string) (err error) {
|
||||
})
|
||||
}
|
||||
|
||||
access, err := shareCfg.GetNewAccess()
|
||||
access, err := shareCfg.GetAccess()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -130,9 +130,11 @@ func shareMain(cmd *cobra.Command, args []string) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO extend libuplink to give this value
|
||||
// fmt.Println("Sharing access to satellite", access.SatelliteAddr)
|
||||
|
||||
satelliteAddr, _, _, err := parseAccess(newAccessData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Sharing access to satellite", satelliteAddr)
|
||||
fmt.Println("=========== ACCESS RESTRICTIONS ==========================================================")
|
||||
fmt.Println("Download :", formatPermission(permission.AllowDownload))
|
||||
fmt.Println("Upload :", formatPermission(permission.AllowUpload))
|
||||
|
Loading…
Reference in New Issue
Block a user