You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
3.7 KiB
118 lines
3.7 KiB
4 years ago
|
package blsgen
|
||
4 years ago
|
|
||
4 years ago
|
import (
|
||
|
"errors"
|
||
4 years ago
|
"fmt"
|
||
4 years ago
|
|
||
4 years ago
|
bls_core "github.com/harmony-one/bls/ffi/go/bls"
|
||
4 years ago
|
"github.com/harmony-one/harmony/multibls"
|
||
|
)
|
||
|
|
||
4 years ago
|
func LoadKeys(cfg Config) (multibls.PrivateKeys, error) {
|
||
4 years ago
|
decrypters, err := getKeyDecrypters(cfg)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
4 years ago
|
}
|
||
4 years ago
|
helper, err := getHelper(cfg, decrypters)
|
||
4 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
4 years ago
|
return helper.loadKeys()
|
||
|
}
|
||
|
|
||
4 years ago
|
// Loader is the structure to load bls keys.
|
||
|
type Config struct {
|
||
4 years ago
|
// source for bls key loading. At least one of the multiBlsKeys and BlsDir
|
||
4 years ago
|
// need to be provided.
|
||
|
//
|
||
4 years ago
|
// multiBlsKeys defines a slice of key files to load from.
|
||
|
multiBlsKeys []string
|
||
4 years ago
|
// BlsDir defines a file directory to load keys from.
|
||
|
BlsDir *string
|
||
|
|
||
|
// Passphrase related settings. Used for passphrase encrypted key files.
|
||
|
//
|
||
|
// PassSrcType defines the source to get passphrase. Three source types are available
|
||
4 years ago
|
// PassSrcNil - do not use passphrase decryption
|
||
4 years ago
|
// PassSrcFile - get passphrase from a .pass file
|
||
|
// PassSrcPrompt - get passphrase from prompt
|
||
|
// PassSrcAuto - try to unlock with .pass file. If not success, ask user with prompt
|
||
|
PassSrcType PassSrcType
|
||
|
// PassFile specifies the .pass file to be used when loading passphrase from file.
|
||
|
// If not set, default to the .pass file in the same directory as the key file.
|
||
|
PassFile *string
|
||
|
// PersistPassphrase set whether to persist the passphrase to a .pass file when
|
||
4 years ago
|
// prompt the user for passphrase. Persisted pass file is a file with .pass extension
|
||
4 years ago
|
// under the same directory as the key file.
|
||
|
PersistPassphrase bool
|
||
4 years ago
|
|
||
4 years ago
|
// KMS related settings, including AWS credentials and region info.
|
||
4 years ago
|
// Used for KMS encrypted passphrase files.
|
||
|
//
|
||
|
// AwsCfgSrcType defines the source to get aws config. Three types available:
|
||
4 years ago
|
// AwsCfgSrcNil - do not use Aws KMS decryption service.
|
||
4 years ago
|
// AwsCfgSrcFile - get AWS config through a json file. See AwsConfig for content fields.
|
||
|
// AwsCfgSrcPrompt - get AWS config through prompt.
|
||
|
// AwsCfgSrcShared - Use the default AWS config settings (from env and $HOME/.aws/config)
|
||
|
AwsCfgSrcType AwsCfgSrcType
|
||
|
// AwsConfigFile set the json file to load aws config.
|
||
|
AwsConfigFile *string
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
func (cfg *Config) getPassProviderConfig() passDecrypterConfig {
|
||
|
return passDecrypterConfig{
|
||
4 years ago
|
passSrcType: cfg.PassSrcType,
|
||
|
passFile: cfg.PassFile,
|
||
|
persistPassphrase: cfg.PersistPassphrase,
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
func (cfg *Config) getKmsProviderConfig() kmsDecrypterConfig {
|
||
|
return kmsDecrypterConfig{
|
||
4 years ago
|
awsCfgSrcType: cfg.AwsCfgSrcType,
|
||
|
awsConfigFile: cfg.AwsConfigFile,
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
// keyDecrypter is the interface to decrypt the bls key file. Currently, two
|
||
|
// implementations are supported:
|
||
|
// passDecrypter - decrypt with passphrase
|
||
|
// kmsDecrypter - decrypt with aws kms service
|
||
|
type keyDecrypter interface {
|
||
|
extension() string
|
||
|
decryptFile(keyFile string) (*bls_core.SecretKey, error)
|
||
|
}
|
||
|
|
||
|
func getKeyDecrypters(cfg Config) ([]keyDecrypter, error) {
|
||
|
var decrypters []keyDecrypter
|
||
|
if cfg.PassSrcType != PassSrcNil {
|
||
|
pd, err := newPassDecrypter(cfg.getPassProviderConfig())
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
decrypters = append(decrypters, pd)
|
||
|
}
|
||
|
if cfg.AwsCfgSrcType != AwsCfgSrcNil {
|
||
|
kd, err := newKmsDecrypter(cfg.getKmsProviderConfig())
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
decrypters = append(decrypters, kd)
|
||
|
}
|
||
|
if len(decrypters) == 0 {
|
||
|
return nil, fmt.Errorf("must provide at least one bls key decryption")
|
||
|
}
|
||
|
return decrypters, nil
|
||
|
}
|
||
|
|
||
|
func getHelper(cfg Config, decrypters []keyDecrypter) (loadHelper, error) {
|
||
|
switch {
|
||
|
case len(cfg.multiBlsKeys) != 0:
|
||
|
return newMultiKeyLoader(cfg.multiBlsKeys, decrypters)
|
||
|
case stringIsSet(cfg.BlsDir):
|
||
|
return newBlsDirLoader(*cfg.BlsDir, decrypters)
|
||
|
default:
|
||
|
return nil, errors.New("either multiBlsKeys or BlsDir must be set")
|
||
|
}
|
||
|
}
|