From d800b51dd946eb4c885b7a7a856ca2a7d88b4d8c Mon Sep 17 00:00:00 2001 From: Clement Sam Date: Fri, 26 Aug 2022 01:18:17 +0000 Subject: [PATCH] cmd/multinode: generate identity when not provided Closes https://github.com/storj/storj/issues/4974 Change-Id: I7a07d846be7ac8f8f7d7e9ad61511ff84d2ab400 --- cmd/multinode/main.go | 32 ++++++++++++++++++++++++++++---- multinode/peer.go | 1 + 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/cmd/multinode/main.go b/cmd/multinode/main.go index cb79060cf..7cd30c224 100644 --- a/cmd/multinode/main.go +++ b/cmd/multinode/main.go @@ -5,6 +5,7 @@ package main import ( "bytes" + "context" "encoding/json" "fmt" "io" @@ -18,6 +19,7 @@ import ( "golang.org/x/text/transform" "storj.io/common/fpath" + "storj.io/common/identity" "storj.io/common/peertls/tlsopts" "storj.io/common/rpc" "storj.io/common/storj" @@ -90,11 +92,16 @@ func main() { func init() { defaultConfDir := fpath.ApplicationDir("storj", "multinode") - defaultIdentityDir := fpath.ApplicationDir("storj", "identity", "multinode") cfgstruct.SetupFlag(zap.L(), rootCmd, &confDir, "config-dir", defaultConfDir, "main directory for multinode configuration") - cfgstruct.SetupFlag(zap.L(), rootCmd, &identityDir, "identity-dir", defaultIdentityDir, "main directory for multinode identity credentials") + cfgstruct.SetupFlag(zap.L(), rootCmd, &identityDir, "identity-dir", "", "main directory for multinode identity credentials") defaults := cfgstruct.DefaultsFlag(rootCmd) + // Ignoring errors since MarkDeprecated only errors if the flag + // doesn't exist or no deprecated message is provided. + // and MarkHidden only errors if the flag doesn't exist. + _ = rootCmd.PersistentFlags().MarkDeprecated("identity-dir", "multinode no longer requires an identity key") + _ = rootCmd.PersistentFlags().MarkHidden("identity-dir") + rootCmd.AddCommand(setupCmd) rootCmd.AddCommand(runCmd) rootCmd.AddCommand(addCmd) @@ -129,7 +136,7 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) { runCfg.Debug.Address = *process.DebugAddrFlag - identity, err := runCfg.Identity.Load() + identity, err := getIdentity(ctx, &runCfg) if err != nil { log.Error("failed to load identity", zap.Error(err)) return errs.New("failed to load identity: %+v", err) @@ -160,7 +167,7 @@ func cmdAdd(cmd *cobra.Command, args []string) (err error) { ctx, _ := process.Ctx(cmd) log := zap.L() - identity, err := addCfg.Identity.Load() + identity, err := getIdentity(ctx, &addCfg.Config) if err != nil { return errs.New("failed to load identity: %+v", err) } @@ -282,3 +289,20 @@ func unmarshalJSONNodes(nodesJSONData []byte) ([]nodes.Node, error) { return nodesInfo, nil } + +func getIdentity(ctx context.Context, cfg *Config) (*identity.FullIdentity, error) { + // for backwards compatibility reasons, check if an identity was provided. + if cfgstruct.FindIdentityDirParam() != "" { + ident, err := cfg.Identity.Load() + if err == nil { + return ident, nil + } + zap.L().Error("failed to load identity.", zap.Error(err)) + zap.L().Info("generating new identity.") + } + // generate new identity + return identity.NewFullIdentity(ctx, identity.NewCAOptions{ + Difficulty: 0, + Concurrency: 1, + }) +} diff --git a/multinode/peer.go b/multinode/peer.go index 747221e03..f79655428 100644 --- a/multinode/peer.go +++ b/multinode/peer.go @@ -48,6 +48,7 @@ type DB interface { // Config is all the configuration parameters for a Multinode Dashboard. type Config struct { + // TODO: remove Identity flags since --identity-dir is deprecated Identity identity.Config Debug debug.Config