2018-07-26 15:21:35 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-08-08 23:22:59 +01:00
|
|
|
"fmt"
|
2018-07-26 15:21:35 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/cfgstruct"
|
|
|
|
"storj.io/storj/pkg/miniogw"
|
|
|
|
"storj.io/storj/pkg/process"
|
2018-08-13 09:39:45 +01:00
|
|
|
"storj.io/storj/pkg/provider"
|
2018-07-26 15:21:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "gw",
|
|
|
|
Short: "Gateway",
|
|
|
|
}
|
2018-07-30 08:38:31 +01:00
|
|
|
runCmd = &cobra.Command{
|
2018-07-26 15:21:35 +01:00
|
|
|
Use: "run",
|
|
|
|
Short: "Run the gateway",
|
|
|
|
RunE: cmdRun,
|
2018-07-30 08:38:31 +01:00
|
|
|
}
|
|
|
|
setupCmd = &cobra.Command{
|
2018-07-26 15:21:35 +01:00
|
|
|
Use: "setup",
|
|
|
|
Short: "Create config files",
|
|
|
|
RunE: cmdSetup,
|
2018-07-30 08:38:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
runCfg miniogw.Config
|
|
|
|
setupCfg struct {
|
2018-08-13 09:39:45 +01:00
|
|
|
CA provider.CASetupConfig
|
|
|
|
Identity provider.IdentitySetupConfig
|
|
|
|
BasePath string `default:"$CONFDIR" help:"base path for setup"`
|
|
|
|
Concurrency uint `default:"4" help:"number of concurrent workers for certificate authority generation"`
|
|
|
|
Overwrite bool `default:"false" help:"whether to overwrite pre-existing configuration files"`
|
2018-07-30 08:38:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defaultConfDir = "$HOME/.storj/gw"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(runCmd)
|
|
|
|
rootCmd.AddCommand(setupCmd)
|
|
|
|
cfgstruct.Bind(runCmd.Flags(), &runCfg, cfgstruct.ConfDir(defaultConfDir))
|
|
|
|
cfgstruct.Bind(setupCmd.Flags(), &setupCfg, cfgstruct.ConfDir(defaultConfDir))
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdRun(cmd *cobra.Command, args []string) (err error) {
|
2018-07-30 08:38:31 +01:00
|
|
|
return runCfg.Run(process.Ctx(cmd))
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdSetup(cmd *cobra.Command, args []string) (err error) {
|
2018-08-08 23:22:59 +01:00
|
|
|
_, err = os.Stat(setupCfg.BasePath)
|
|
|
|
if !setupCfg.Overwrite && err == nil {
|
|
|
|
fmt.Println("A gw configuration already exists. Rerun with --overwrite")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:38:31 +01:00
|
|
|
err = os.MkdirAll(setupCfg.BasePath, 0700)
|
2018-07-26 15:21:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-13 09:39:45 +01:00
|
|
|
// TODO: handle setting base path *and* identity file paths via args
|
|
|
|
// NB: if base path is set this overrides identity and CA path options
|
|
|
|
if setupCfg.BasePath != defaultConfDir {
|
|
|
|
setupCfg.CA.CertPath = filepath.Join(setupCfg.BasePath, "ca.cert")
|
|
|
|
setupCfg.CA.KeyPath = filepath.Join(setupCfg.BasePath, "ca.key")
|
|
|
|
setupCfg.Identity.CertPath = filepath.Join(setupCfg.BasePath, "identity.cert")
|
|
|
|
setupCfg.Identity.KeyPath = filepath.Join(setupCfg.BasePath, "identity.key")
|
|
|
|
}
|
|
|
|
err = provider.SetupIdentity(process.Ctx(cmd), setupCfg.CA, setupCfg.Identity)
|
2018-07-26 15:21:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-13 09:39:45 +01:00
|
|
|
o := map[string]interface{}{
|
|
|
|
"identity.cert-path": setupCfg.CA.CertPath,
|
|
|
|
"identity.key-path": setupCfg.CA.CertPath,
|
|
|
|
}
|
|
|
|
|
2018-07-30 08:38:31 +01:00
|
|
|
return process.SaveConfig(runCmd.Flags(),
|
2018-08-13 09:39:45 +01:00
|
|
|
filepath.Join(setupCfg.BasePath, "config.yaml"), o)
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2018-07-30 08:38:31 +01:00
|
|
|
runCmd.Flags().String("config",
|
|
|
|
filepath.Join(defaultConfDir, "config.yaml"), "path to configuration")
|
|
|
|
process.Exec(rootCmd)
|
2018-07-26 15:21:35 +01:00
|
|
|
}
|