Skip to content
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

Hashicorp Vault support #209

Merged
merged 20 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cleanup leftover comments, code blocks
  • Loading branch information
zivkovicmilos committed Nov 13, 2021
commit 28caebf3d60fbe3b1d7326cb8737b9e66eb624c6
1 change: 0 additions & 1 deletion consensus/ibft/ibft.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ func (i *Ibft) createKey() error {
return nil
}

// TODO remove this
const IbftKeyName = "validator.key"

// start starts the IBFT consensus state machine
Expand Down
75 changes: 4 additions & 71 deletions network/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,13 @@ package network

import (
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/0xPolygon/polygon-sdk/secrets"
"github.com/libp2p/go-libp2p-core/crypto"
)

var Libp2pKeyName = "libp2p.key"

// ReadLibp2pKey reads the libp2p private key from the passed in data directory.
//
// The key must be named 'libp2p.key'
//
// If no key is found, it is generated and returned
func ReadLibp2pKey(dataDir string) (crypto.PrivKey, error) {
if dataDir == "" {
// use an in-memory key
priv, _, err := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
if err != nil {
return nil, err
}

return priv, nil
}

path := filepath.Join(dataDir, Libp2pKeyName)
_, err := os.Stat(path)
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("failed to stat (%s): %v", path, err)
}

if os.IsNotExist(err) {
// The key doesn't exist, generate it
priv, _, err := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
if err != nil {
return nil, err
}

buf, err := crypto.MarshalPrivateKey(priv)
if err != nil {
return nil, err
}

if err := ioutil.WriteFile(
path,
[]byte(hex.EncodeToString(buf)),
0600,
); err != nil {
return nil, err
}

return priv, nil
}

// exists
raw, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}

buf, err := hex.DecodeString(string(raw))
if err != nil {
return nil, err
}

key, err := crypto.UnmarshalPrivateKey(buf)
if err != nil {
return nil, err
}

return key, nil
}

func ReadLibp2pKeyNEW(manager secrets.SecretsManager) (crypto.PrivKey, error) {
// ReadLibp2pKey reads the private networking key from the secrets manager
func ReadLibp2pKey(manager secrets.SecretsManager) (crypto.PrivKey, error) {
libp2pKey, err := manager.GetSecret(secrets.NetworkKey)
if err != nil {
return nil, err
Expand All @@ -86,6 +17,7 @@ func ReadLibp2pKeyNEW(manager secrets.SecretsManager) (crypto.PrivKey, error) {
return ParseLibp2pKey(libp2pKey)
}

// GenerateAndEncodeLibp2pKey generates a new networking private key, and encodes it into hex
func GenerateAndEncodeLibp2pKey() (crypto.PrivKey, []byte, error) {
priv, _, err := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
if err != nil {
Expand All @@ -100,6 +32,7 @@ func GenerateAndEncodeLibp2pKey() (crypto.PrivKey, []byte, error) {
return priv, []byte(hex.EncodeToString(buf)), nil
}

// ParseLibp2pKey converts a byte array to a private key
func ParseLibp2pKey(key []byte) (crypto.PrivKey, error) {
buf, err := hex.DecodeString(string(key))
if err != nil {
Expand Down
33 changes: 0 additions & 33 deletions network/keystore_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion network/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func setupLibp2pKey(secretsManager secrets.SecretsManager) (crypto.PrivKey, erro
var key crypto.PrivKey
if secretsManager.HasSecret(secrets.NetworkKey) {
// The key is present in the secrets manager, read it
networkingKey, readErr := ReadLibp2pKeyNEW(secretsManager)
networkingKey, readErr := ReadLibp2pKey(secretsManager)
if readErr != nil {
return nil, fmt.Errorf("unable to read networking private key from SM, %v", readErr)
lazartravica marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down