Skip to content

Commit

Permalink
Add support for loading a private key from the configuration (#138)
Browse files Browse the repository at this point in the history
* Add support for loading priv key from config

* Fix unmarshalling
  • Loading branch information
galt-tr authored Sep 24, 2024
1 parent 975bdc3 commit a4c51f4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type (
Port string `json:"port" mapstructure:"port"` // Port is the port for the P2P server
AllowPrivateIPs bool `json:"allow_private_ip_addresses" mapstructure:"allow_private_ip_addresses"` // AllowPrivateIPs will disable the default behavior of filtering out private IP addresses
PrivateKeyPath string `json:"private_key_path" mapstructure:"private_key_path"` // PrivateKeyPath is the path to the private key
PrivateKey string `json:"private_key" mapstructure:"private_key"` // PrivateKey is a hex encoded private key to use directly
TopicName string `json:"topic_name" mapstructure:"topic_name"` // TopicName is the name of the topic to subscribe to
PeerDiscoveryInterval time.Duration `json:"peer_discovery_interval" mapstructure:"peer_discovery_interval"` // PeerDiscoveryInterval is the interval in which we will refresh the peer table and check peers for missing messages
}
Expand Down
46 changes: 37 additions & 9 deletions app/p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package p2p
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -64,17 +65,26 @@ type Server struct {
// Instantiate a new server instance, optionally include a subscriber
// if `subscriber` is nil, we won't process the subscription events
func NewServer(o ServerOptions) (*Server, error) {

o.Config.Services.Log.Debug("creating P2P service")
var pk *crypto.PrivKey
var err error

// Attempt to read the private key from the file
pk, err := readPrivateKey(o.Config.P2P.PrivateKeyPath)
if err != nil {

// If the file doesn't exist, generate a new private key
if pk, err = generatePrivateKey(o.Config.P2P.PrivateKeyPath); err != nil {
// If privatekey is defined in config, skip reading from file
if o.Config.P2P.PrivateKey != "" {
pk, err = readPrivateKey(o.Config.P2P.PrivateKey)
if err != nil {
return nil, err
}
} else {
// Attempt to read the private key from the file
pk, err = readPrivateKey(o.Config.P2P.PrivateKeyPath)
if err != nil {

// If the file doesn't exist, generate a new private key
if pk, err = generatePrivateKey(o.Config.P2P.PrivateKeyPath); err != nil {
return nil, err
}
}
}

var extMultiAddr maddr.Multiaddr
Expand Down Expand Up @@ -479,8 +489,8 @@ func generatePrivateKey(filePath string) (*crypto.PrivKey, error) {
return &privateKey, nil
}

// readPrivateKey reads a private key from `private_key` file
func readPrivateKey(filePath string) (*crypto.PrivKey, error) {
// readPrivateKeyFromFile reads a private key from `private_key_path` file
func readPrivateKeyFromFile(filePath string) (*crypto.PrivKey, error) {
// Read private key from a file
privateBytes, err := os.ReadFile(filePath) //nolint:gosec // This is a local private key
if err != nil {
Expand All @@ -496,6 +506,24 @@ func readPrivateKey(filePath string) (*crypto.PrivKey, error) {
return &privateKey, nil
}

// readPrivateKey reads a private key from `private_key` hex encoded string
func readPrivateKey(privKeyHex string) (*crypto.PrivKey, error) {
// Read private key from a file
privateBytes, err := hex.DecodeString(privKeyHex) //nolint:gosec // This is a local private key
if err != nil {
return nil, err
}

var privateKey crypto.PrivKey
// Unmarshal the private key bytes into a key
privateKey, err = crypto.UnmarshalEd25519PrivateKey(privateBytes)
if err != nil {
return nil, err
}

return &privateKey, nil
}

// Subscriptions lists all current subscriptions
func (s *Server) Subscriptions() map[string]*pubsub.Subscription {
return s.subscriptions
Expand Down

0 comments on commit a4c51f4

Please sign in to comment.