[node.sh] refactored passDecrypter finished

pull/3219/head
Jacky Wang 4 years ago
parent 40721eafd0
commit 61577b803e
No known key found for this signature in database
GPG Key ID: 1085CE5F4FF5842C
  1. 2
      cmd/harmony/blsloader/decrypter.go
  2. 30
      cmd/harmony/blsloader/helper.go
  3. 119
      cmd/harmony/blsloader/passProvider.go
  4. 79
      cmd/harmony/blsloader/utils.go

@ -3,6 +3,6 @@ package blsloader
import bls_core "github.com/harmony-one/bls/ffi/go/bls" import bls_core "github.com/harmony-one/bls/ffi/go/bls"
type decrypter interface { type decrypter interface {
validate() error extension() string
decrypt(keyFile string) (*bls_core.SecretKey, error) decrypt(keyFile string) (*bls_core.SecretKey, error)
} }

@ -183,21 +183,21 @@ func (loader *blsDirLoader) skippingErrors() []error {
} }
} }
func (loader *blsDirLoader) loadKeyFromFile(path string) (*bls_core.SecretKey, error) { //func (loader *blsDirLoader) loadKeyFromFile(path string) (*bls_core.SecretKey, error) {
var ( // var (
key *bls_core.SecretKey // key *bls_core.SecretKey
err error // err error
) // )
switch { // switch {
case isBasicKeyFile(path): // case isBasicKeyFile(path):
key, err = loadBasicKey(path, loader.pps) // key, err = loadBasicKey(path, loader.pps)
case isKMSKeyFile(path): // case isKMSKeyFile(path):
key, err = loadKmsKeyFromFile(path, loader.kcp) // key, err = loadKmsKeyFromFile(path, loader.kcp)
default: // default:
err = errUnknownExtension // err = errUnknownExtension
} // }
return key, err // return key, err
} //}
// errIsErrors return whether the err is one of the errs // errIsErrors return whether the err is one of the errs
func errIsErrors(err error, errs []error) bool { func errIsErrors(err error, errs []error) bool {

@ -8,8 +8,6 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/harmony-one/harmony/crypto/bls"
bls_core "github.com/harmony-one/bls/ffi/go/bls" bls_core "github.com/harmony-one/bls/ffi/go/bls"
) )
@ -36,79 +34,84 @@ func (srcType PassSrcType) isValid() bool {
} }
} }
type passDecrypter struct { // passDecrypterConfig is the data structure of passProviders config
pps []passProvider type passDecrypterConfig struct {
} passSrcType PassSrcType
passFile *string
func newPassDecrypter(cfg passDecrypterConfig) *passDecrypter { persistPassphrase bool
pps := cfg.makePassProviders()
return &passDecrypter{pps}
} }
func (pd *passDecrypter) decrypt(keyFile string) (*bls_core.SecretKey, error) { // passDecrypter decrypt the .key bls files with passphrase from a series
for _, pp := range pd.pps { // of passProvider as passphrase source
type passDecrypter struct {
config passDecrypterConfig
} pps []passProvider
} }
func (pd *passDecrypter) checkDecryptResult(keyFile string, got *bls_core.SecretKey) error { func newPassDecrypter(cfg passDecrypterConfig) (*passDecrypter, error) {
expPubKey, err := getPubKeyFromFilePath(keyFile, passExt) pd := &passDecrypter{config: cfg}
if err != nil { if err := pd.validate(); err != nil {
if err == errUnableGetPubkey { return nil, err
// file name not bls pub key + .pass
return nil
}
return err
}
gotPubKey := *bls.FromLibBLSPublicKeyUnsafe(got.GetPublicKey())
if expPubKey != gotPubKey {
return errors.New("public key unexpected")
} }
return nil pd.makePassProviders()
return pd, nil
} }
// passDecrypterConfig is the data structure of passProviders config func (pd *passDecrypter) extension() string {
type passDecrypterConfig struct { return basicKeyExt
passSrcType PassSrcType
passFile *string
passDir *string
persistPassphrase bool
} }
func (config passDecrypterConfig) validate() error { func (pd *passDecrypter) validate() error {
config := pd.config
if !config.passSrcType.isValid() { if !config.passSrcType.isValid() {
return errors.New("unknown PassSrcType") return errors.New("unknown PassSrcType")
} }
if stringIsSet(config.passFile) {
if err := isPassFile(*config.passFile); err != nil {
return fmt.Errorf("%v not a passphrase file: %v", *config.passFile, err)
}
}
return nil return nil
} }
func (config passDecrypterConfig) makePassProviders() []passProvider { func (pd *passDecrypter) makePassProviders() {
switch config.passSrcType { switch pd.config.passSrcType {
case PassSrcFile: case PassSrcFile:
return []passProvider{config.getFilePassProvider()} pd.pps = []passProvider{pd.getFilePassProvider()}
case PassSrcPrompt: case PassSrcPrompt:
return []passProvider{config.getPromptPassProvider()} pd.pps = []passProvider{pd.getPromptPassProvider()}
case PassSrcAuto: case PassSrcAuto:
return []passProvider{ pd.pps = []passProvider{
config.getFilePassProvider(), pd.getFilePassProvider(),
config.getPromptPassProvider(), pd.getPromptPassProvider(),
} }
} }
} }
func (config passDecrypterConfig) getFilePassProvider() passProvider { func (pd *passDecrypter) getPromptPassProvider() passProvider {
return newPromptPassProvider(pd.config.persistPassphrase)
}
func (pd *passDecrypter) getFilePassProvider() passProvider {
switch { switch {
case stringIsSet(config.passFile): case stringIsSet(pd.config.passFile):
return newStaticPassProvider(*config.passFile) return newStaticPassProvider(*pd.config.passFile)
case stringIsSet(config.passDir):
return newDirPassProvider(*config.passDir)
default: default:
return newDynamicPassProvider() return newDynamicPassProvider()
} }
} }
func (config passDecrypterConfig) getPromptPassProvider() passProvider { func (pd *passDecrypter) decrypt(keyFile string) (*bls_core.SecretKey, error) {
return newPromptPassProvider(config.persistPassphrase) for _, pp := range pd.pps {
secretKey, err := loadBasicKeyWithProvider(keyFile, pp)
if err != nil {
console.println(err)
continue
}
return secretKey, nil
}
return nil, fmt.Errorf("failed to load bls key %v", keyFile)
} }
// passProvider is the interface to provide the passphrase of a bls keys. // passProvider is the interface to provide the passphrase of a bls keys.
@ -200,30 +203,6 @@ func newDynamicPassProvider() passProvider {
func (provider *dynamicPassProvider) getPassphrase(keyFile string) (string, error) { func (provider *dynamicPassProvider) getPassphrase(keyFile string) (string, error) {
passFile := keyFileToPassFileFull(keyFile) passFile := keyFileToPassFileFull(keyFile)
if !isPassFile(passFile) {
return "", fmt.Errorf("pass file %v not exist", passFile)
}
return readPassFromFile(passFile)
}
// dirPassProvider provide the all bls password available in the directory.
type dirPassProvider struct {
dirPath string
}
func (provider *dirPassProvider) toStr() string {
return "directory " + provider.dirPath
}
func newDirPassProvider(dirPath string) *dirPassProvider {
return &dirPassProvider{dirPath: dirPath}
}
func (provider *dirPassProvider) getPassphrase(keyFile string) (string, error) {
passFile := keyFileToPassFileFull(keyFile)
if !isPassFile(passFile) {
return "", fmt.Errorf("pass file %v not exist", passFile)
}
return readPassFromFile(passFile) return readPassFromFile(passFile)
} }

@ -4,13 +4,8 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/pkg/errors" "github.com/pkg/errors"
bls_core "github.com/harmony-one/bls/ffi/go/bls" bls_core "github.com/harmony-one/bls/ffi/go/bls"
@ -70,67 +65,59 @@ func loadKmsKeyFromFile(blsKeyFile string, kcp kmsProvider) (*bls_core.SecretKey
return secretKey, nil return secretKey, nil
} }
func isFile(path string) bool { func isFile(path string) error {
info, err := os.Stat(path) info, err := os.Stat(path)
if err != nil { if err != nil {
return false return err
} }
return !info.IsDir() if info.IsDir() {
return errors.New("is directory")
}
return nil
} }
func isDir(path string) bool { func isDir(path string) error {
info, err := os.Stat(path) info, err := os.Stat(path)
if err != nil { if err != nil {
return false return err
} }
return info.IsDir() if info.IsDir() {
} return errors.New("is a file")
func isBasicKeyFile(path string) bool {
exist := isFile(path)
if !exist {
return false
} }
return filepath.Ext(path) == basicKeyExt return nil
} }
func isPassFile(path string) bool { func isBasicKeyFile(path string) error {
exist := isFile(path) err := isFile(path)
if !exist { if err != nil {
return false return err
} }
return filepath.Ext(path) == passExt if filepath.Ext(path) != basicKeyExt {
} return errors.New("should have extension .key")
func isKMSKeyFile(path string) bool {
exist := isFile(path)
if !exist {
return false
} }
return filepath.Ext(path) == kmsKeyExt return nil
} }
var regexFmt = `^[\da-f]{96}%s$` func isPassFile(path string) error {
err := isFile(path)
func getPubKeyFromFilePath(path string, ext string) (bls.SerializedPublicKey, error) {
baseName := filepath.Base(path)
re, err := regexp.Compile(fmt.Sprintf(regexFmt, ext))
if err != nil { if err != nil {
return bls.SerializedPublicKey{}, err return err
} }
res := re.FindAllStringSubmatch(baseName, 1) if filepath.Ext(path) != passExt {
if len(res) == 0 { return errors.New("should have extension .pass")
return bls.SerializedPublicKey{}, errUnableGetPubkey
} }
return nil
b := common.Hex2Bytes(res[0][1])
var pubKey bls.SerializedPublicKey
copy(pubKey[:], b)
return pubKey, nil
} }
func keyFileToPassFileBase(keyFileBase string) string { func isKMSKeyFile(path string) error {
return strings.Trim(keyFileBase, basicKeyExt) + passExt err := isFile(path)
if err != nil {
return err
}
if filepath.Ext(path) != kmsKeyExt {
return errors.New("should have extension .bls")
}
return nil
} }
func keyFileToPassFileFull(keyFile string) string { func keyFileToPassFileFull(keyFile string) string {

Loading…
Cancel
Save