-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
change private key loading logic #3621
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ package blockchain | |
import ( | ||
"crypto/ecdsa" | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
"github.com/iotexproject/go-pkgs/crypto" | ||
|
@@ -25,6 +26,7 @@ import ( | |
type ( | ||
// Config is the config struct for blockchain package | ||
Config struct { | ||
producerPrivKeyLoader sync.Once | ||
ChainDBPath string `yaml:"chainDBPath"` | ||
TrieDBPatchFile string `yaml:"trieDBPatchFile"` | ||
TrieDBPath string `yaml:"trieDBPath"` | ||
|
@@ -36,7 +38,7 @@ type ( | |
EVMNetworkID uint32 `yaml:"evmNetworkID"` | ||
Address string `yaml:"address"` | ||
ProducerPrivKey string `yaml:"producerPrivKey"` | ||
PrivKeyConfigFile string `yaml:"privKeyConfigFile"` | ||
ProducerPrivKeySchema string `yaml:"producerPrivKeySchema"` | ||
SignatureScheme []string `yaml:"signatureScheme"` | ||
EmptyGenesis bool `yaml:"emptyGenesis"` | ||
GravityChainDB db.Config `yaml:"gravityChainDB"` | ||
|
@@ -137,39 +139,32 @@ func (cfg *Config) ProducerPrivateKey() crypto.PrivateKey { | |
|
||
// SetProducerPrivKey set producer privKey by PrivKeyConfigFile info | ||
func (cfg *Config) SetProducerPrivKey() error { | ||
if cfg.PrivKeyConfigFile == "" { | ||
return nil | ||
} | ||
|
||
yaml, err := config.NewYAML(config.Expand(os.LookupEnv), config.File(cfg.PrivKeyConfigFile)) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to init private key config") | ||
} | ||
pc := &privKeyConfig{} | ||
if err := yaml.Get(config.Root).Populate(pc); err != nil { | ||
return errors.Wrap(err, "failed to unmarshal YAML config to privKeyConfig struct") | ||
} | ||
|
||
var loader privKeyLoader | ||
switch pc.Method { | ||
switch cfg.ProducerPrivKeySchema { | ||
case "hex", "": | ||
// do nothing | ||
case "hashiCorpVault": | ||
cli, err := newVaultClient(&pc.VaultConfig) | ||
yaml, err := config.NewYAML(config.Expand(os.LookupEnv), config.File(cfg.ProducerPrivKey)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
if err != nil { | ||
return errors.Wrap(err, "failed to init private key config") | ||
} | ||
hcv := &hashiCorpVault{} | ||
if err := yaml.Get(config.Root).Populate(hcv); err != nil { | ||
return errors.Wrap(err, "failed to unmarshal YAML config to privKeyConfig struct") | ||
} | ||
|
||
loader, err := newVaultPrivKeyLoader(hcv) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to new vault client") | ||
} | ||
loader = &vaultPrivKeyLoader{ | ||
cfg: &pc.VaultConfig, | ||
vaultClient: cli, | ||
key, err := loader.load() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to load producer private key") | ||
} | ||
cfg.ProducerPrivKey = key | ||
default: | ||
return errors.Wrap(ErrConfig, "invalid private key method") | ||
return errors.Wrap(ErrConfig, "invalid private key schema") | ||
} | ||
|
||
key, err := loader.load() | ||
if err != nil { | ||
return errors.Wrap(err, "failed to load producer private key") | ||
} | ||
cfg.ProducerPrivKey = key | ||
return nil | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defined but not used?
also, this can be outside of the
Config
struct?