@ -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 ) {
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 applyKMSFlags ( 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 ) {
config . BLSKeys . KMSConfigFile = cli . GetStringFlagValue ( cmd , kmsConfigFileFlag )
fileSpecified = true
}
if cli . HasFlagChanged ( cmd , kmsConfigSrcTypeFlag ) {
config . BLSKeys . KMSConfigSrcType = cli . GetStringFlagValue ( cmd , kmsConfigSrcTypeFlag )
} else if fileSpecified {
config . BLSKeys . KMSConfigSrcType = blsPassTypeFile
}
}
}
func applyLegacyBLSPassFlags ( cmd * cobra . Command , config * hmyConfig ) {
func applyLegacyBLSPassFlags ( cmd * cobra . Command , config * hmyConfig ) {
if cli . HasFlagChanged ( cmd , legacyBLSPassFlag ) {
}
val := cli . GetStringFlagValue ( cmd , legacyBLSPassFlag )
legacyApplyBLSPassVal ( val , config )
func applyLegacyKMSFlags ( cmd * cobra . Command , config * hmyConfig ) {
}
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 {
if cli . HasFlagChanged ( cmd , legacyBLSPersistPassFlag ) {
return nil , fmt . Errorf ( "0 bls keys loaded" )
config . BLSKeys . SavePassphrase = cli . GetBoolFlagValue ( cmd , legacyBLSPersistPassFlag )
}
}
if len ( keys ) > * maxBLSKeysPerNode {
return nil , fmt . Errorf ( "bls keys exceed maximum count %v" , * maxBLSKeysPerNode )
}
return keys , err
}
}
func parseBLSLoadingConfig ( ) ( blsgen . Config , error ) {
func applyLegacyKMSFlags ( cmd * cobra . Command , config * hmyConfig ) {
var (
if cli . HasFlagChanged ( cmd , legacyKMSConfigSourceFlag ) {
config blsgen . Config
val := cli . GetStringFlagValue ( cmd , legacyKMSConfigSourceFlag )
err error
legacyApplyKMSSourceVal ( val , config )
)
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
}
}
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 . PassSrc Auto
config . BLSKeys . PassSrcType = blsPassTypeAuto
case "file" :
case legacyBLSPassTypeStatic :
config . PassSrcType = blsgen . PassSrc File
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
//}