ea1408f7a8
As a reminder: latest clingy removed the requirement of having custom context (which made the usage of context.WithValue harder) and uses simple context instead. Clingy saves the stdin/stdout/stderr to the context (earlier to separated context type) to make it available for unit testing. Change-Id: I8896574f4670721de43a577cd4b35952e3b5d00e
110 lines
2.4 KiB
Go
110 lines
2.4 KiB
Go
// Copyright (C) 2022 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/zeebo/clingy"
|
|
"github.com/zeebo/errs"
|
|
|
|
"storj.io/storj/cmd/uplink/ulext"
|
|
"storj.io/uplink"
|
|
)
|
|
|
|
type cmdAccessSetup struct {
|
|
ex ulext.External
|
|
am accessMaker
|
|
|
|
authService string
|
|
}
|
|
|
|
func newCmdAccessSetup(ex ulext.External) *cmdAccessSetup {
|
|
return &cmdAccessSetup{
|
|
ex: ex,
|
|
am: accessMaker{
|
|
ex: ex,
|
|
use: true,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (c *cmdAccessSetup) Setup(params clingy.Parameters) {
|
|
c.authService = params.Flag("auth-service", "If generating backwards-compatible S3 Gateway credentials, use this auth service", "https://auth.storjshare.io").(string)
|
|
|
|
c.am.Setup(params, c.ex)
|
|
}
|
|
|
|
func (c *cmdAccessSetup) Execute(ctx context.Context) (err error) {
|
|
name, err := c.ex.PromptInput(ctx, "Enter name to import as [default: main]:")
|
|
if err != nil {
|
|
return errs.Wrap(err)
|
|
}
|
|
if name == "" {
|
|
name = "main"
|
|
}
|
|
|
|
keyOrGrant, err := c.ex.PromptInput(ctx, "Enter API key or Access grant:")
|
|
if err != nil {
|
|
return errs.Wrap(err)
|
|
}
|
|
if keyOrGrant == "" {
|
|
return errs.New("API key cannot be empty.")
|
|
}
|
|
|
|
access, err := uplink.ParseAccess(keyOrGrant)
|
|
if err == nil {
|
|
_, err := c.am.Execute(ctx, name, access)
|
|
if err != nil {
|
|
return errs.Wrap(err)
|
|
}
|
|
} else {
|
|
satelliteAddr, err := c.ex.PromptInput(ctx, "Satellite address:")
|
|
if err != nil {
|
|
return errs.Wrap(err)
|
|
}
|
|
if satelliteAddr == "" {
|
|
return errs.New("Satellite address cannot be empty.")
|
|
}
|
|
|
|
passphrase, err := c.ex.PromptSecret(ctx, "Passphrase:")
|
|
if err != nil {
|
|
return errs.Wrap(err)
|
|
}
|
|
if passphrase == "" {
|
|
return errs.New("Encryption passphrase cannot be empty.")
|
|
}
|
|
|
|
access, err = c.ex.RequestAccess(ctx, satelliteAddr, keyOrGrant, passphrase)
|
|
if err != nil {
|
|
return errs.Wrap(err)
|
|
}
|
|
|
|
_, err = c.am.Execute(ctx, name, access)
|
|
if err != nil {
|
|
return errs.Wrap(err)
|
|
}
|
|
}
|
|
|
|
fmt.Fprintf(clingy.Stdout(ctx), "Switched default access to %q\n", name)
|
|
|
|
answer, err := c.ex.PromptInput(ctx, "Would you like S3 backwards-compatible Gateway credentials? (y/N):")
|
|
if err != nil {
|
|
return errs.Wrap(err)
|
|
}
|
|
answer = strings.ToLower(answer)
|
|
|
|
if answer != "y" && answer != "yes" {
|
|
return nil
|
|
}
|
|
|
|
credentials, err := RegisterAccess(ctx, access, c.authService, false, "")
|
|
if err != nil {
|
|
return errs.Wrap(err)
|
|
}
|
|
return errs.Wrap(DisplayGatewayCredentials(ctx, *credentials, "", ""))
|
|
}
|