2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
2018-10-25 00:14:38 +01:00
// See LICENSE for copying information.
package main
import (
2019-02-04 15:50:06 +00:00
"context"
"crypto/rand"
"fmt"
"net"
"os"
"path/filepath"
2019-11-22 21:00:04 +00:00
"github.com/btcsuite/btcutil/base58"
2019-02-08 12:57:35 +00:00
"github.com/minio/cli"
minio "github.com/minio/minio/cmd"
2019-02-04 15:50:06 +00:00
"github.com/spf13/cobra"
2019-02-08 12:57:35 +00:00
"github.com/zeebo/errs"
2019-02-04 15:50:06 +00:00
"go.uber.org/zap"
2019-12-27 11:48:47 +00:00
"storj.io/common/fpath"
"storj.io/common/storj"
2019-06-25 19:25:31 +01:00
"storj.io/storj/cmd/internal/wizard"
2020-01-01 18:33:12 +00:00
"storj.io/storj/cmd/uplink/cmd"
2019-04-15 21:13:02 +01:00
libuplink "storj.io/storj/lib/uplink"
2019-02-04 15:50:06 +00:00
"storj.io/storj/pkg/cfgstruct"
"storj.io/storj/pkg/miniogw"
2018-10-25 00:14:38 +01:00
"storj.io/storj/pkg/process"
2019-11-14 19:46:15 +00:00
"storj.io/storj/private/version"
"storj.io/storj/private/version/checker"
2019-02-04 15:50:06 +00:00
)
// GatewayFlags configuration flags
type GatewayFlags struct {
2019-05-08 17:52:57 +01:00
NonInteractive bool ` help:"disable interactive mode" default:"false" setup:"true" `
2019-02-04 15:50:06 +00:00
2019-02-08 12:57:35 +00:00
Server miniogw . ServerConfig
Minio miniogw . MinioConfig
2020-01-01 18:33:12 +00:00
cmd . Config
2019-07-08 15:45:20 +01:00
2019-10-20 08:56:23 +01:00
Version checker . Config
2019-11-22 21:00:04 +00:00
2019-11-25 15:55:54 +00:00
PBKDFConcurrency int ` help:"Unfortunately, up until v0.26.2, keys generated from passphrases depended on the number of cores the local CPU had. If you entered a passphrase with v0.26.2 earlier, you'll want to set this number to the number of CPU cores your computer had at the time. This flag may go away in the future. For new installations the default value is highly recommended." default:"0" `
2019-02-04 15:50:06 +00:00
}
var (
2019-05-07 15:29:57 +01:00
// Error is the default gateway setup errs class
Error = errs . Class ( "gateway setup error" )
2019-02-04 15:50:06 +00:00
// rootCmd represents the base gateway command when called without any subcommands
rootCmd = & cobra . Command {
Use : "gateway" ,
Short : "The Storj client-side S3 gateway" ,
Args : cobra . OnlyValidArgs ,
}
setupCmd = & cobra . Command {
Use : "setup" ,
Short : "Create a gateway config file" ,
RunE : cmdSetup ,
Annotations : map [ string ] string { "type" : "setup" } ,
}
runCmd = & cobra . Command {
Use : "run" ,
Short : "Run the S3 gateway" ,
RunE : cmdRun ,
}
setupCfg GatewayFlags
runCfg GatewayFlags
confDir string
identityDir string
2018-10-25 00:14:38 +01:00
)
2019-02-04 15:50:06 +00:00
func init ( ) {
2019-03-12 12:51:06 +00:00
defaultConfDir := fpath . ApplicationDir ( "storj" , "gateway" )
defaultIdentityDir := fpath . ApplicationDir ( "storj" , "identity" , "gateway" )
cfgstruct . SetupFlag ( zap . L ( ) , rootCmd , & confDir , "config-dir" , defaultConfDir , "main directory for gateway configuration" )
cfgstruct . SetupFlag ( zap . L ( ) , rootCmd , & identityDir , "identity-dir" , defaultIdentityDir , "main directory for gateway identity credentials" )
2019-04-19 19:17:30 +01:00
defaults := cfgstruct . DefaultsFlag ( rootCmd )
2019-02-04 15:50:06 +00:00
rootCmd . AddCommand ( runCmd )
rootCmd . AddCommand ( setupCmd )
Command line flags features and cleanup (#2068)
* change BindSetup to be an option to Bind
* add process.Bind to allow composite structures
* hack fix for noprefix flags
* used tagged version of structs
Before this PR, some flags were created by calling `cfgstruct.Bind` and having their fields create a flag. Once the flags were parsed, `viper` was used to acquire all the values from them and config files, and the fields in the struct were set through the flag interface.
This doesn't work for slices of things on config structs very well, since it can only set strings, and for a string slice, it turns out that the implementation in `pflag` appends an entry rather than setting it.
This changes three things:
1. Only have a `Bind` call instead of `Bind` and `BindSetup`, and make `BindSetup` an option instead.
2. Add a `process.Bind` call that takes in a `*cobra.Cmd`, binds the struct to the command's flags, and keeps track of that struct in a global map keyed by the command.
3. Use `viper` to get the values and load them into the bound configuration structs instead of using the flags to propagate the changes.
In this way, we can support whatever rich configuration we want in the config yaml files, while still getting command like flags when important.
2019-05-29 18:56:22 +01:00
process . Bind ( runCmd , & runCfg , defaults , cfgstruct . ConfDir ( confDir ) , cfgstruct . IdentityDir ( identityDir ) )
process . Bind ( setupCmd , & setupCfg , defaults , cfgstruct . ConfDir ( confDir ) , cfgstruct . IdentityDir ( identityDir ) , cfgstruct . SetupMode ( ) )
2019-02-04 15:50:06 +00:00
}
func cmdSetup ( cmd * cobra . Command , args [ ] string ) ( err error ) {
setupDir , err := filepath . Abs ( confDir )
if err != nil {
2019-06-07 17:14:40 +01:00
return Error . Wrap ( err )
2019-02-04 15:50:06 +00:00
}
valid , _ := fpath . IsValidSetupDir ( setupDir )
if ! valid {
2019-06-07 17:14:40 +01:00
return Error . New ( "gateway configuration already exists (%v)" , setupDir )
2019-02-04 15:50:06 +00:00
}
err = os . MkdirAll ( setupDir , 0700 )
if err != nil {
2019-06-07 17:14:40 +01:00
return Error . Wrap ( err )
2019-02-04 15:50:06 +00:00
}
2019-03-22 09:01:49 +00:00
overrides := map [ string ] interface { } { }
2019-05-07 15:29:57 +01:00
2019-02-04 15:50:06 +00:00
accessKeyFlag := cmd . Flag ( "minio.access-key" )
if ! accessKeyFlag . Changed {
accessKey , err := generateKey ( )
if err != nil {
return err
}
overrides [ accessKeyFlag . Name ] = accessKey
}
2019-06-07 17:14:40 +01:00
2019-02-04 15:50:06 +00:00
secretKeyFlag := cmd . Flag ( "minio.secret-key" )
if ! secretKeyFlag . Changed {
secretKey , err := generateKey ( )
if err != nil {
return err
}
overrides [ secretKeyFlag . Name ] = secretKey
}
2019-06-07 17:14:40 +01:00
if setupCfg . NonInteractive {
2019-08-05 18:01:20 +01:00
return setupCfg . nonInteractive ( cmd , setupDir , overrides )
2019-06-07 17:14:40 +01:00
}
2019-08-05 18:01:20 +01:00
return setupCfg . interactive ( cmd , setupDir , overrides )
2019-02-04 15:50:06 +00:00
}
func cmdRun ( cmd * cobra . Command , args [ ] string ) ( err error ) {
address := runCfg . Server . Address
host , port , err := net . SplitHostPort ( address )
if err != nil {
return err
}
if host == "" {
2019-04-05 13:09:16 +01:00
address = net . JoinHostPort ( "127.0.0.1" , port )
2019-02-04 15:50:06 +00:00
}
2019-09-19 17:37:40 +01:00
ctx , _ := process . Ctx ( cmd )
2019-02-04 15:50:06 +00:00
2019-07-31 15:38:44 +01:00
if err := process . InitMetrics ( ctx , zap . L ( ) , nil , "" ) ; err != nil {
2019-08-27 10:24:47 +01:00
zap . S ( ) . Warn ( "Failed to initialize telemetry batcher: " , err )
2019-02-04 15:50:06 +00:00
}
2019-04-03 20:13:39 +01:00
2019-10-20 08:56:23 +01:00
err = checker . CheckProcessVersion ( ctx , zap . L ( ) , runCfg . Version , version . Build , "Gateway" )
2019-07-08 15:45:20 +01:00
if err != nil {
return err
}
zap . S ( ) . Infof ( "Starting Storj S3-compatible gateway!\n\n" )
zap . S ( ) . Infof ( "Endpoint: %s\n" , address )
zap . S ( ) . Infof ( "Access key: %s\n" , runCfg . Minio . AccessKey )
zap . S ( ) . Infof ( "Secret key: %s\n" , runCfg . Minio . SecretKey )
2019-05-10 12:17:58 +01:00
err = checkCfg ( ctx )
2019-02-04 15:50:06 +00:00
if err != nil {
2019-07-08 15:45:20 +01:00
zap . S ( ) . Warn ( "Failed to contact Satellite. Perhaps your configuration is invalid?" )
return err
2019-02-04 15:50:06 +00:00
}
2019-05-08 17:52:57 +01:00
return runCfg . Run ( ctx )
2019-02-04 15:50:06 +00:00
}
func generateKey ( ) ( key string , err error ) {
var buf [ 20 ] byte
_ , err = rand . Read ( buf [ : ] )
if err != nil {
2019-06-07 17:14:40 +01:00
return "" , Error . Wrap ( err )
2019-02-04 15:50:06 +00:00
}
return base58 . Encode ( buf [ : ] ) , nil
}
2019-05-10 12:17:58 +01:00
func checkCfg ( ctx context . Context ) ( err error ) {
proj , err := runCfg . openProject ( ctx )
if err != nil {
return err
}
defer func ( ) { err = errs . Combine ( err , proj . Close ( ) ) } ( )
2019-07-12 13:57:02 +01:00
_ , err = proj . ListBuckets ( ctx , & storj . BucketListOptions { Direction : storj . Forward } )
2019-05-10 12:17:58 +01:00
return err
}
2019-02-08 12:57:35 +00:00
// Run starts a Minio Gateway given proper config
2019-05-08 17:52:57 +01:00
func ( flags GatewayFlags ) Run ( ctx context . Context ) ( err error ) {
2019-02-08 12:57:35 +00:00
err = minio . RegisterGatewayCommand ( cli . Command {
Name : "storj" ,
Usage : "Storj" ,
Action : func ( cliCtx * cli . Context ) error {
2019-05-08 17:52:57 +01:00
return flags . action ( ctx , cliCtx )
2019-02-08 12:57:35 +00:00
} ,
HideHelpCommand : true ,
} )
if err != nil {
return err
}
// TODO(jt): Surely there is a better way. This is so upsetting
err = os . Setenv ( "MINIO_ACCESS_KEY" , flags . Minio . AccessKey )
if err != nil {
return err
}
err = os . Setenv ( "MINIO_SECRET_KEY" , flags . Minio . SecretKey )
if err != nil {
return err
}
minio . Main ( [ ] string { "storj" , "gateway" , "storj" ,
"--address" , flags . Server . Address , "--config-dir" , flags . Minio . Dir , "--quiet" } )
return errs . New ( "unexpected minio exit" )
}
2019-05-08 17:52:57 +01:00
func ( flags GatewayFlags ) action ( ctx context . Context , cliCtx * cli . Context ) ( err error ) {
gw , err := flags . NewGateway ( ctx )
2019-02-08 12:57:35 +00:00
if err != nil {
return err
}
minio . StartGateway ( cliCtx , miniogw . Logging ( gw , zap . L ( ) ) )
return errs . New ( "unexpected minio exit" )
}
// NewGateway creates a new minio Gateway
2019-05-08 17:52:57 +01:00
func ( flags GatewayFlags ) NewGateway ( ctx context . Context ) ( gw minio . Gateway , err error ) {
2020-01-09 12:24:49 +00:00
access , err := flags . GetAccess ( )
2019-05-22 14:57:12 +01:00
if err != nil {
return nil , err
}
2019-05-08 17:52:57 +01:00
project , err := flags . openProject ( ctx )
if err != nil {
return nil , err
}
return miniogw . NewStorjGateway (
project ,
2020-01-09 12:24:49 +00:00
access . EncryptionAccess ,
2019-07-03 19:07:44 +01:00
storj . CipherSuite ( flags . Enc . PathType ) ,
flags . GetEncryptionParameters ( ) ,
2019-05-08 17:52:57 +01:00
flags . GetRedundancyScheme ( ) ,
flags . Client . SegmentSize ,
) , nil
}
2019-07-30 18:49:01 +01:00
func ( flags * GatewayFlags ) newUplink ( ctx context . Context ) ( * libuplink . Uplink , error ) {
// Transform the gateway config flags to the libuplink config object
libuplinkCfg := & libuplink . Config { }
2019-08-01 12:14:09 +01:00
libuplinkCfg . Volatile . Log = zap . L ( )
2019-07-30 18:49:01 +01:00
libuplinkCfg . Volatile . MaxInlineSize = flags . Client . MaxInlineSize
libuplinkCfg . Volatile . MaxMemory = flags . RS . MaxBufferMem
libuplinkCfg . Volatile . PeerIDVersion = flags . TLS . PeerIDVersions
2019-08-01 12:14:09 +01:00
libuplinkCfg . Volatile . TLS . SkipPeerCAWhitelist = ! flags . TLS . UsePeerCAWhitelist
libuplinkCfg . Volatile . TLS . PeerCAWhitelistPath = flags . TLS . PeerCAWhitelistPath
2019-07-30 18:49:01 +01:00
libuplinkCfg . Volatile . DialTimeout = flags . Client . DialTimeout
2019-11-22 21:00:04 +00:00
libuplinkCfg . Volatile . PBKDFConcurrency = flags . PBKDFConcurrency
2019-07-30 18:49:01 +01:00
return libuplink . NewUplink ( ctx , libuplinkCfg )
}
func ( flags GatewayFlags ) openProject ( ctx context . Context ) ( * libuplink . Project , error ) {
2020-01-09 12:24:49 +00:00
access , err := flags . GetAccess ( )
2019-04-15 21:13:02 +01:00
if err != nil {
2019-08-05 18:01:20 +01:00
return nil , Error . Wrap ( err )
2019-04-15 21:13:02 +01:00
}
2019-08-05 18:01:20 +01:00
// TODO(jeff): this leaks the uplink and project :(
uplink , err := flags . newUplink ( ctx )
2019-05-22 14:57:12 +01:00
if err != nil {
2019-08-05 18:01:20 +01:00
return nil , Error . Wrap ( err )
2019-05-22 14:57:12 +01:00
}
2020-01-09 12:24:49 +00:00
project , err := uplink . OpenProject ( ctx , access . SatelliteAddr , access . APIKey )
2019-08-05 18:01:20 +01:00
if err != nil {
return nil , Error . Wrap ( err )
}
return project , nil
2019-02-04 15:50:06 +00:00
}
2019-06-07 17:14:40 +01:00
// interactive creates the configuration of the gateway interactively.
2019-08-05 18:01:20 +01:00
func ( flags GatewayFlags ) interactive ( cmd * cobra . Command , setupDir string , overrides map [ string ] interface { } ) error {
2019-09-19 17:37:40 +01:00
ctx , _ := process . Ctx ( cmd )
2019-07-29 08:17:49 +01:00
2019-06-25 19:25:31 +01:00
satelliteAddress , err := wizard . PromptForSatellite ( cmd )
2019-05-07 15:29:57 +01:00
if err != nil {
return Error . Wrap ( err )
}
2019-07-29 08:17:49 +01:00
apiKeyString , err := wizard . PromptForAPIKey ( )
if err != nil {
return Error . Wrap ( err )
}
apiKey , err := libuplink . ParseAPIKey ( apiKeyString )
if err != nil {
return Error . Wrap ( err )
}
passphrase , err := wizard . PromptForEncryptionPassphrase ( )
if err != nil {
return Error . Wrap ( err )
}
2019-08-05 18:01:20 +01:00
uplink , err := flags . newUplink ( ctx )
2019-07-29 08:17:49 +01:00
if err != nil {
return Error . Wrap ( err )
}
2019-08-05 18:01:20 +01:00
defer func ( ) { err = errs . Combine ( err , uplink . Close ( ) ) } ( )
2019-07-29 08:17:49 +01:00
2019-08-05 18:01:20 +01:00
project , err := uplink . OpenProject ( ctx , satelliteAddress , apiKey )
2019-05-07 15:29:57 +01:00
if err != nil {
return Error . Wrap ( err )
}
2019-07-29 08:17:49 +01:00
defer func ( ) { err = errs . Combine ( err , project . Close ( ) ) } ( )
2019-05-07 15:29:57 +01:00
2019-07-29 08:17:49 +01:00
key , err := project . SaltedKeyFromPassphrase ( ctx , passphrase )
2019-06-07 17:14:40 +01:00
if err != nil {
return Error . Wrap ( err )
}
2020-02-11 08:25:31 +00:00
encAccess := libuplink . NewEncryptionAccessWithDefaultKey ( * key )
encAccess . SetDefaultPathCipher ( storj . EncAESGCM )
2020-01-09 12:24:49 +00:00
accessData , err := ( & libuplink . Scope {
2019-08-05 18:01:20 +01:00
SatelliteAddr : satelliteAddress ,
APIKey : apiKey ,
2020-02-11 08:25:31 +00:00
EncryptionAccess : encAccess ,
2019-08-05 18:01:20 +01:00
} ) . Serialize ( )
2019-05-07 15:29:57 +01:00
if err != nil {
return Error . Wrap ( err )
}
2020-01-09 12:24:49 +00:00
overrides [ "access" ] = accessData
2019-05-07 15:29:57 +01:00
2019-08-05 18:01:20 +01:00
err = process . SaveConfig ( cmd , filepath . Join ( setupDir , "config.yaml" ) ,
process . SaveConfigWithOverrides ( overrides ) ,
process . SaveConfigRemovingDeprecated ( ) )
2019-05-07 15:29:57 +01:00
if err != nil {
2019-08-05 18:01:20 +01:00
return Error . Wrap ( err )
2019-05-07 15:29:57 +01:00
}
2019-08-05 18:01:20 +01:00
fmt . Println ( `
2019-05-07 15:29:57 +01:00
Your S3 Gateway is configured and ready to use !
Some things to try next :
2020-01-21 09:50:37 +00:00
* See https : //documentation.tardigrade.io/api-reference/s3-gateway for some example commands`)
2019-05-07 15:29:57 +01:00
return nil
}
2019-06-07 17:14:40 +01:00
// nonInteractive creates the configuration of the gateway non-interactively.
2019-08-05 18:01:20 +01:00
func ( flags GatewayFlags ) nonInteractive ( cmd * cobra . Command , setupDir string , overrides map [ string ] interface { } ) error {
2020-01-09 12:24:49 +00:00
// ensure we're using the access for the setup
access , err := setupCfg . GetAccess ( )
2019-06-07 17:14:40 +01:00
if err != nil {
2019-08-05 18:01:20 +01:00
return err
2019-06-07 17:14:40 +01:00
}
2020-01-09 12:24:49 +00:00
accessData , err := access . Serialize ( )
2019-08-05 18:01:20 +01:00
if err != nil {
return err
2019-06-07 17:14:40 +01:00
}
2020-01-09 12:24:49 +00:00
overrides [ "access" ] = accessData
2019-06-07 17:14:40 +01:00
2019-08-05 18:01:20 +01:00
return Error . Wrap ( process . SaveConfig ( cmd , filepath . Join ( setupDir , "config.yaml" ) ,
process . SaveConfigWithOverrides ( overrides ) ,
process . SaveConfigRemovingDeprecated ( ) ) )
2019-06-07 17:14:40 +01:00
}
2018-10-25 00:14:38 +01:00
func main ( ) {
2019-02-04 15:50:06 +00:00
process . Exec ( rootCmd )
2018-10-25 00:14:38 +01:00
}