2022-01-25 23:39:26 +00:00
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
2022-08-30 10:51:31 +01:00
"context"
2022-01-25 23:39:26 +00:00
"fmt"
"strings"
"github.com/zeebo/clingy"
"github.com/zeebo/errs"
2022-01-06 19:55:46 +00:00
"storj.io/storj/cmd/uplink/ulext"
2022-01-25 23:39:26 +00:00
"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 ) {
2022-05-16 17:04:25 +01:00
c . authService = params . Flag ( "auth-service" , "If generating backwards-compatible S3 Gateway credentials, use this auth service" , "https://auth.storjshare.io" ) . ( string )
2022-08-09 12:03:36 +01:00
c . am . Setup ( params , c . ex )
2022-01-25 23:39:26 +00:00
}
2022-08-30 10:51:31 +01:00
func ( c * cmdAccessSetup ) Execute ( ctx context . Context ) ( err error ) {
2022-01-25 23:39:26 +00:00
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." )
}
2022-10-24 14:54:51 +01:00
unencryptedObjectKeys := false
answer , err := c . ex . PromptInput ( ctx , "Would you like to disable encryption for object keys (allows lexicographical sorting of objects in listings)? (y/N):" )
if err != nil {
return errs . Wrap ( err )
}
answer = strings . ToLower ( answer )
if answer == "y" || answer == "yes" {
unencryptedObjectKeys = true
}
access , err = c . ex . RequestAccess ( ctx , satelliteAddr , keyOrGrant , passphrase , unencryptedObjectKeys )
2022-01-25 23:39:26 +00:00
if err != nil {
return errs . Wrap ( err )
}
_ , err = c . am . Execute ( ctx , name , access )
if err != nil {
return errs . Wrap ( err )
}
}
2022-08-30 10:51:31 +01:00
fmt . Fprintf ( clingy . Stdout ( ctx ) , "Switched default access to %q\n" , name )
2022-01-25 23:39:26 +00:00
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
}
2022-01-27 15:57:46 +00:00
credentials , err := RegisterAccess ( ctx , access , c . authService , false , "" )
2022-01-25 23:39:26 +00:00
if err != nil {
return errs . Wrap ( err )
}
2022-01-27 15:57:46 +00:00
return errs . Wrap ( DisplayGatewayCredentials ( ctx , * credentials , "" , "" ) )
2022-01-25 23:39:26 +00:00
}