2021-03-31 16:56:34 +01:00
// Copyright (C) 2021 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"
2022-09-26 17:23:39 +01:00
"io"
2022-01-25 23:39:26 +00:00
"strconv"
"strings"
2021-03-31 16:56:34 +01:00
"github.com/zeebo/clingy"
2021-06-22 23:41:22 +01:00
"github.com/zeebo/errs"
2021-05-26 21:19:29 +01:00
2022-01-06 19:55:46 +00:00
"storj.io/storj/cmd/uplink/ulext"
2021-03-31 16:56:34 +01:00
)
type cmdAccessCreate struct {
2021-05-26 21:19:29 +01:00
ex ulext . External
2021-06-22 23:41:22 +01:00
am accessMaker
2021-03-31 16:56:34 +01:00
2022-01-25 23:39:26 +00:00
passphraseStdin bool
satelliteAddr string
apiKey string
importAs string
exportTo string
2022-10-24 14:54:51 +01:00
unencryptedObjectKeys * bool
2021-03-31 16:56:34 +01:00
}
2021-05-26 21:19:29 +01:00
func newCmdAccessCreate ( ex ulext . External ) * cmdAccessCreate {
return & cmdAccessCreate { ex : ex }
}
2021-05-25 00:11:50 +01:00
func ( c * cmdAccessCreate ) Setup ( params clingy . Parameters ) {
2022-01-25 23:39:26 +00:00
c . passphraseStdin = params . Flag ( "passphrase-stdin" , "If set, the passphrase is read from stdin, and all other values must be provided." , false ,
clingy . Transform ( strconv . ParseBool ) ,
clingy . Boolean ,
) . ( bool )
c . satelliteAddr = params . Flag ( "satellite-address" , "Satellite address from satellite UI (prompted if unspecified)" , "" ) . ( string )
c . apiKey = params . Flag ( "api-key" , "API key from satellite UI (prompted if unspecified)" , "" ) . ( string )
c . importAs = params . Flag ( "import-as" , "Import the access as this name" , "" ) . ( string )
c . exportTo = params . Flag ( "export-to" , "Export the access to this file path" , "" ) . ( string )
2021-03-31 16:56:34 +01:00
2022-10-24 14:54:51 +01:00
c . unencryptedObjectKeys = params . Flag ( "unencrypted-object-keys" , "If set, the created access grant won't encrypt object keys" , nil ,
clingy . Transform ( strconv . ParseBool ) , clingy . Boolean , clingy . Optional ,
) . ( * bool )
2021-06-22 23:41:22 +01:00
params . Break ( )
2022-01-25 23:39:26 +00:00
c . am . Setup ( params , c . ex )
2021-03-31 16:56:34 +01:00
}
2022-08-30 10:51:31 +01:00
func ( c * cmdAccessCreate ) Execute ( ctx context . Context ) ( err error ) {
2022-01-25 23:39:26 +00:00
if c . satelliteAddr == "" {
if c . passphraseStdin {
return errs . New ( "Must specify the satellite address as a flag when passphrase-stdin is set." )
}
c . satelliteAddr , err = c . ex . PromptInput ( ctx , "Satellite address:" )
2021-06-22 23:41:22 +01:00
if err != nil {
return errs . Wrap ( err )
}
}
2022-01-25 23:39:26 +00:00
if c . apiKey == "" {
if c . passphraseStdin {
return errs . New ( "Must specify the api key as a flag when passphrase-stdin is set." )
}
c . apiKey , err = c . ex . PromptInput ( ctx , "API key:" )
2021-06-22 23:41:22 +01:00
if err != nil {
return errs . Wrap ( err )
}
2022-01-25 23:39:26 +00:00
}
2022-10-24 14:54:51 +01:00
unencryptedObjectKeys := false
if c . unencryptedObjectKeys != nil {
unencryptedObjectKeys = * c . unencryptedObjectKeys
} else if ! c . passphraseStdin {
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
}
}
2022-01-25 23:39:26 +00:00
var passphrase string
if c . passphraseStdin {
2022-09-26 17:23:39 +01:00
stdinData , err := io . ReadAll ( clingy . Stdin ( ctx ) )
2022-01-25 23:39:26 +00:00
if err != nil {
return errs . Wrap ( err )
}
passphrase = strings . TrimRight ( string ( stdinData ) , "\r\n" )
} else {
passphrase , err = c . ex . PromptSecret ( ctx , "Passphrase:" )
if err != nil {
return errs . Wrap ( err )
2021-08-02 17:54:30 +01:00
}
2021-06-22 23:41:22 +01:00
}
2022-01-25 23:39:26 +00:00
if passphrase == "" {
return errs . New ( "Encryption passphrase must be non-empty" )
}
2021-06-22 23:41:22 +01:00
2022-10-24 14:54:51 +01:00
access , err := c . ex . RequestAccess ( ctx , c . satelliteAddr , c . apiKey , passphrase , unencryptedObjectKeys )
2021-06-22 23:41:22 +01:00
if err != nil {
return errs . Wrap ( err )
}
2022-01-25 23:39:26 +00:00
access , err = c . am . Execute ( ctx , c . importAs , access )
if err != nil {
return errs . Wrap ( err )
}
if c . exportTo != "" {
return c . ex . ExportAccess ( ctx , access , c . exportTo )
}
if c . importAs != "" {
return nil
}
serialized , err := access . Serialize ( )
if err != nil {
return errs . Wrap ( err )
}
2022-08-30 10:51:31 +01:00
fmt . Fprintln ( clingy . Stdout ( ctx ) , serialized )
2022-01-25 23:39:26 +00:00
return nil
2021-03-31 16:56:34 +01:00
}