[cmd] refactored bls.go

pull/3278/head
Jacky Wang 4 years ago
parent 3528b72bc6
commit 928b7cc661
No known key found for this signature in database
GPG Key ID: 1085CE5F4FF5842C
  1. 186
      cmd/harmony/bls.go
  2. 28
      cmd/harmony/config.go

@ -1,27 +1,12 @@
package main package main
import ( import (
"errors"
"flag"
"fmt"
"strings" "strings"
"sync" "sync"
"github.com/spf13/cobra"
"github.com/harmony-one/harmony/internal/blsgen"
"github.com/harmony-one/harmony/internal/cli" "github.com/harmony-one/harmony/internal/cli"
"github.com/harmony-one/harmony/multibls" "github.com/harmony-one/harmony/multibls"
) "github.com/spf13/cobra"
var (
blsKeyFile = flag.String("blskey_file", "", "The encrypted file of bls serialized private key by passphrase.")
blsFolder = flag.String("blsfolder", ".hmy/blskeys", "The folder that stores the bls keys and corresponding passphrases; e.g. <blskey>.key and <blskey>.pass; all bls keys mapped to same shard")
maxBLSKeysPerNode = flag.Int("max_bls_keys_per_node", 10, "Maximum number of bls keys allowed per node (default 4)")
blsPass = flag.String("blspass", "default", "The source for bls passphrases. (default, no-prompt, prompt, file:$PASS_FILE, none)")
persistPass = flag.Bool("save-passphrase", false, "Whether the prompt passphrase is saved after prompt.")
awsConfigSource = flag.String("aws-config-source", "default", "The source for aws config. (default, prompt, file:$CONFIG_FILE, none)")
) )
var ( var (
@ -170,101 +155,130 @@ func applyBLSFlags(cmd *cobra.Command, config *hmyConfig) {
} }
func applyBLSPassFlags(cmd *cobra.Command, config *hmyConfig) { func applyBLSPassFlags(cmd *cobra.Command, config *hmyConfig) {
if cli.HasFlagChanged(cmd, passEnabledFlag) {
config.BLSKeys.PassEnabled = cli.GetBoolFlagValue(cmd, passEnabledFlag)
} }
if cli.HasFlagChanged(cmd, passSrcTypeFlag) {
func applyKMSFlags(cmd *cobra.Command, config *hmyConfig) { config.BLSKeys.PassSrcType = cli.GetStringFlagValue(cmd, passSrcTypeFlag)
}
if cli.HasFlagChanged(cmd, passSrcFileFlag) {
config.BLSKeys.PassFile = cli.GetStringFlagValue(cmd, passSrcFileFlag)
}
if cli.HasFlagChanged(cmd, passSaveFlag) {
config.BLSKeys.SavePassphrase = cli.GetBoolFlagValue(cmd, passSaveFlag)
} }
func applyLegacyBLSPassFlags(cmd *cobra.Command, config *hmyConfig) {
} }
func applyLegacyKMSFlags(cmd *cobra.Command, config *hmyConfig) { func applyKMSFlags(cmd *cobra.Command, config *hmyConfig) {
var fileSpecified bool
if cli.HasFlagChanged(cmd, kmsEnabledFlag) {
config.BLSKeys.KMSEnabled = cli.GetBoolFlagValue(cmd, kmsEnabledFlag)
} }
if cli.HasFlagChanged(cmd, kmsConfigFileFlag) {
func loadBLSKeys() (multibls.PrivateKeys, error) { config.BLSKeys.KMSConfigFile = cli.GetStringFlagValue(cmd, kmsConfigFileFlag)
config, err := parseBLSLoadingConfig() fileSpecified = true
if err != nil {
return nil, err
} }
keys, err := blsgen.LoadKeys(config) if cli.HasFlagChanged(cmd, kmsConfigSrcTypeFlag) {
if err != nil { config.BLSKeys.KMSConfigSrcType = cli.GetStringFlagValue(cmd, kmsConfigSrcTypeFlag)
return nil, err } else if fileSpecified {
config.BLSKeys.KMSConfigSrcType = blsPassTypeFile
} }
if len(keys) == 0 {
return nil, fmt.Errorf("0 bls keys loaded")
} }
if len(keys) > *maxBLSKeysPerNode {
return nil, fmt.Errorf("bls keys exceed maximum count %v", *maxBLSKeysPerNode) func applyLegacyBLSPassFlags(cmd *cobra.Command, config *hmyConfig) {
if cli.HasFlagChanged(cmd, legacyBLSPassFlag) {
val := cli.GetStringFlagValue(cmd, legacyBLSPassFlag)
legacyApplyBLSPassVal(val, config)
} }
return keys, err if cli.HasFlagChanged(cmd, legacyBLSPersistPassFlag) {
config.BLSKeys.SavePassphrase = cli.GetBoolFlagValue(cmd, legacyBLSPersistPassFlag)
} }
func parseBLSLoadingConfig() (blsgen.Config, error) {
var (
config blsgen.Config
err error
)
if len(*blsKeyFile) != 0 {
config.MultiBlsKeys = strings.Split(*blsKeyFile, ",")
} }
config.BlsDir = blsFolder
config, err = parseBLSPass(config, *blsPass) func applyLegacyKMSFlags(cmd *cobra.Command, config *hmyConfig) {
if err != nil { if cli.HasFlagChanged(cmd, legacyKMSConfigSourceFlag) {
return blsgen.Config{}, err val := cli.GetStringFlagValue(cmd, legacyKMSConfigSourceFlag)
} legacyApplyKMSSourceVal(val, config)
config, err = parseAwsConfigSrc(config, *awsConfigSource)
if err != nil {
return blsgen.Config{}, err
} }
return config, nil
} }
func parseBLSPass(config blsgen.Config, src string) (blsgen.Config, error) { func legacyApplyBLSPassVal(src string, config *hmyConfig) {
methodArgs := strings.SplitN(src, ":", 2) methodArgs := strings.SplitN(src, ":", 2)
method := methodArgs[0] method := methodArgs[0]
switch method { switch method {
case "default", "stdin": case legacyBLSPassTypeDefault, legacyBLSPassTypeStdin:
config.PassSrcType = blsgen.PassSrcAuto config.BLSKeys.PassSrcType = blsPassTypeAuto
case "file": case legacyBLSPassTypeStatic:
config.PassSrcType = blsgen.PassSrcFile config.BLSKeys.PassSrcType = blsPassTypeFile
if len(methodArgs) < 2 { if len(methodArgs) >= 2 {
return blsgen.Config{}, errors.New("must specify passphrase file") config.BLSKeys.PassFile = methodArgs[1]
} }
config.PassFile = &methodArgs[1] case legacyBLSPassTypeDynamic:
case "no-prompt": config.BLSKeys.PassSrcType = blsPassTypePrompt
config.PassSrcType = blsgen.PassSrcFile case legacyBLSPassTypePrompt:
case "prompt": config.BLSKeys.PassSrcType = blsPassTypePrompt
config.PassSrcType = blsgen.PassSrcPrompt case legacyBLSPassTypeNone:
config.PersistPassphrase = *persistPass config.BLSKeys.PassEnabled = false
case "none": }
config.PassSrcType = blsgen.PassSrcNil
}
config.PersistPassphrase = *persistPass
return config, nil
} }
func parseAwsConfigSrc(config blsgen.Config, src string) (blsgen.Config, error) { func legacyApplyKMSSourceVal(src string, config *hmyConfig) {
methodArgs := strings.SplitN(src, ":", 2) methodArgs := strings.SplitN(src, ":", 2)
method := methodArgs[0] method := methodArgs[0]
switch method { switch method {
case "default": case legacyBLSKmsTypeDefault:
config.AwsCfgSrcType = blsgen.AwsCfgSrcShared config.BLSKeys.KMSConfigSrcType = kmsConfigTypeShared
case "file": case legacyBLSKmsTypePrompt:
config.AwsCfgSrcType = blsgen.AwsCfgSrcFile config.BLSKeys.KMSConfigSrcType = kmsConfigTypePrompt
if len(methodArgs) < 2 { case legacyBLSKmsTypeFile:
return blsgen.Config{}, errors.New("must specify aws config file") config.BLSKeys.KMSConfigSrcType = kmsConfigTypeFile
if len(methodArgs) >= 2 {
config.BLSKeys.KMSConfigFile = methodArgs[1]
} }
config.AwsConfigFile = &methodArgs[1] case legacyBLSKmsTypeNone:
case "prompt": config.BLSKeys.KMSEnabled = false
config.AwsCfgSrcType = blsgen.AwsCfgSrcPrompt
case "none":
config.AwsCfgSrcType = blsgen.AwsCfgSrcNil
} }
return config, nil
} }
//// TODO: refactor this
//func loadBLSKeys() (multibls.PrivateKeys, error) {
// config, err := parseBLSLoadingConfig()
// if err != nil {
// return nil, err
// }
// keys, err := blsgen.LoadKeys(config)
// if err != nil {
// return nil, err
// }
// if len(keys) == 0 {
// return nil, fmt.Errorf("0 bls keys loaded")
// }
// if len(keys) >= *maxBLSKeysPerNode {
// return nil, fmt.Errorf("bls keys exceed maximum count %v", *maxBLSKeysPerNode)
// }
// return keys, err
//}
//
//func parseBLSLoadingConfig() (blsgen.Config, error) {
// var (
// config blsgen.Config
// err error
// )
// if len(*blsKeyFile) != 0 {
// config.MultiBlsKeys = strings.Split(*blsKeyFile, ",")
// }
// config.BlsDir = blsFolder
//
// config, err = parseBLSPass(config, *blsPass)
// if err != nil {
// return blsgen.Config{}, err
// }
// config, err = parseAwsConfigSrc(config, *awsConfigSource)
// if err != nil {
// return blsgen.Config{}, err
// }
// return config, nil
//}

@ -25,15 +25,37 @@ var defaultConfig = hmyConfig{
MaxKeys: 10, MaxKeys: 10,
PassEnabled: true, PassEnabled: true,
PassSrcType: "auto", PassSrcType: blsPassTypeAuto,
PassFile: "", PassFile: "",
SavePassphrase: false, SavePassphrase: false,
KMSEnabled: false, KMSEnabled: true,
KMSConfigSrcType: "shared", KMSConfigSrcType: kmsConfigTypeShared,
KMSConfigFile: "", KMSConfigFile: "",
}, },
} }
const (
blsPassTypeAuto = "auto"
blsPassTypeFile = "file"
blsPassTypePrompt = "prompt"
kmsConfigTypeShared = "shared"
kmsConfigTypePrompt = "prompt"
kmsConfigTypeFile = "file"
legacyBLSPassTypeDefault = "default"
legacyBLSPassTypeStdin = "stdin"
legacyBLSPassTypeDynamic = "no-prompt"
legacyBLSPassTypePrompt = "prompt"
legacyBLSPassTypeStatic = "file"
legacyBLSPassTypeNone = "none"
legacyBLSKmsTypeDefault = "default"
legacyBLSKmsTypePrompt = "prompt"
legacyBLSKmsTypeFile = "file"
legacyBLSKmsTypeNone = "none"
)
type hmyConfig struct { type hmyConfig struct {
General generalConfig General generalConfig
Network networkConfig Network networkConfig

Loading…
Cancel
Save