Skip to content

Commit

Permalink
Separate Identity and Configuaration
Browse files Browse the repository at this point in the history
  • Loading branch information
kishansagathiya committed Apr 30, 2019
1 parent 543656a commit 51214da
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 106 deletions.
72 changes: 3 additions & 69 deletions cluster_config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ipfscluster

import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
Expand All @@ -15,8 +14,6 @@ import (

"github.com/ipfs/ipfs-cluster/config"

crypto "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
pnet "github.com/libp2p/go-libp2p-pnet"
ma "github.com/multiformats/go-multiaddr"
)
Expand All @@ -25,8 +22,6 @@ const configKey = "cluster"

// Configuration defaults
const (
DefaultConfigCrypto = crypto.RSA
DefaultConfigKeyLength = 2048
DefaultListenAddr = "/ip4/0.0.0.0/tcp/9096"
DefaultStateSyncInterval = 600 * time.Second
DefaultIPFSSyncInterval = 130 * time.Second
Expand All @@ -46,11 +41,6 @@ type Config struct {
lock sync.Mutex
peerstoreLock sync.Mutex

// Libp2p ID and private key for Cluster communication (including)
// the Consensus component.
ID peer.ID
PrivateKey crypto.PrivKey

// User-defined peername for use as human-readable identifier.
Peername string

Expand Down Expand Up @@ -129,9 +119,9 @@ type Config struct {
// saved using JSON. Most configuration keys are converted into simple types
// like strings, and key names aim to be self-explanatory for the user.
type configJSON struct {
ID string `json:"id"`
ID string `json:"id,omitempty"`
Peername string `json:"peername"`
PrivateKey string `json:"private_key"`
PrivateKey string `json:"private_key,omitempty"`
Secret string `json:"secret"`
LeaveOnShutdown bool `json:"leave_on_shutdown"`
ListenMultiaddress string `json:"listen_multiaddress"`
Expand All @@ -153,26 +143,10 @@ func (cfg *Config) ConfigKey() string {

// Default fills in all the Config fields with
// default working values. This means, it will
// generate a valid random ID, PrivateKey and
// Secret.
// generate a Secret.
func (cfg *Config) Default() error {
cfg.setDefaults()

// pid and private key generation --
priv, pub, err := crypto.GenerateKeyPair(
DefaultConfigCrypto,
DefaultConfigKeyLength)
if err != nil {
return err
}
pid, err := peer.IDFromPublicKey(pub)
if err != nil {
return err
}
cfg.ID = pid
cfg.PrivateKey = priv
// --

// cluster secret
clusterSecret, err := pnet.GenerateV1Bytes()
if err != nil {
Expand Down Expand Up @@ -202,18 +176,6 @@ func (cfg *Config) ApplyEnvVars() error {
// Validate will check that the values of this config
// seem to be working ones.
func (cfg *Config) Validate() error {
if cfg.ID == "" {
return errors.New("cluster.ID not set")
}

if cfg.PrivateKey == nil {
return errors.New("no cluster.private_key set")
}

if !cfg.ID.MatchesPrivateKey(cfg.PrivateKey) {
return errors.New("cluster.ID does not match the private_key")
}

if cfg.ListenAddr == nil {
return errors.New("cluster.listen_addr is indefined")
}
Expand Down Expand Up @@ -304,27 +266,8 @@ func (cfg *Config) LoadJSON(raw []byte) error {
func (cfg *Config) applyConfigJSON(jcfg *configJSON) error {
config.SetIfNotDefault(jcfg.PeerstoreFile, &cfg.PeerstoreFile)

id, err := peer.IDB58Decode(jcfg.ID)
if err != nil {
err = fmt.Errorf("error decoding cluster ID: %s", err)
return err
}
cfg.ID = id

config.SetIfNotDefault(jcfg.Peername, &cfg.Peername)

pkb, err := base64.StdEncoding.DecodeString(jcfg.PrivateKey)
if err != nil {
err = fmt.Errorf("error decoding private_key: %s", err)
return err
}
pKey, err := crypto.UnmarshalPrivateKey(pkb)
if err != nil {
err = fmt.Errorf("error parsing private_key ID: %s", err)
return err
}
cfg.PrivateKey = pKey

clusterSecret, err := DecodeClusterSecret(jcfg.Secret)
if err != nil {
err = fmt.Errorf("error loading cluster secret from config: %s", err)
Expand Down Expand Up @@ -381,17 +324,8 @@ func (cfg *Config) toConfigJSON() (jcfg *configJSON, err error) {

jcfg = &configJSON{}

// Private Key
pkeyBytes, err := cfg.PrivateKey.Bytes()
if err != nil {
return
}
pKey := base64.StdEncoding.EncodeToString(pkeyBytes)

// Set all configuration fields
jcfg.ID = cfg.ID.Pretty()
jcfg.Peername = cfg.Peername
jcfg.PrivateKey = pKey
jcfg.Secret = EncodeProtectorKey(cfg.Secret)
jcfg.ReplicationFactorMin = cfg.ReplicationFactorMin
jcfg.ReplicationFactorMax = cfg.ReplicationFactorMax
Expand Down
4 changes: 3 additions & 1 deletion clusterhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/hex"

"github.com/ipfs/ipfs-cluster/identity"
libp2p "github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-crypto"
host "github.com/libp2p/go-libp2p-host"
Expand All @@ -22,9 +23,10 @@ import (
func NewClusterHost(
ctx context.Context,
cfg *Config,
ident *identity.Identity,
) (host.Host, *pubsub.PubSub, *dht.IpfsDHT, error) {

h, err := newHost(ctx, cfg.Secret, cfg.PrivateKey, []ma.Multiaddr{cfg.ListenAddr})
h, err := newHost(ctx, cfg.Secret, ident.PrivateKey, []ma.Multiaddr{cfg.ListenAddr})
if err != nil {
return nil, nil, nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/ipfs-cluster-service/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ipfs/ipfs-cluster/consensus/crdt"
"github.com/ipfs/ipfs-cluster/consensus/raft"
"github.com/ipfs/ipfs-cluster/datastore/badger"
"github.com/ipfs/ipfs-cluster/identity"
"github.com/ipfs/ipfs-cluster/informer/disk"
"github.com/ipfs/ipfs-cluster/informer/numpin"
"github.com/ipfs/ipfs-cluster/ipfsconn/ipfshttp"
Expand Down Expand Up @@ -98,15 +99,15 @@ func saveConfig(cfg *config.Manager) {
out("%s configuration written to %s\n", programName, configPath)
}

func propagateTracingConfig(cfgs *cfgs, tracingFlag bool) *cfgs {
func propagateTracingConfig(cfgs *cfgs, ident *identity.Identity, tracingFlag bool) *cfgs {
// tracingFlag represents the cli flag passed to ipfs-cluster-service daemon.
// It takes priority. If false, fallback to config file value.
tracingValue := tracingFlag
if !tracingFlag {
tracingValue = cfgs.tracingCfg.EnableTracing
}
// propagate to any other interested configuration
cfgs.tracingCfg.ClusterID = cfgs.clusterCfg.ID.Pretty()
cfgs.tracingCfg.ClusterID = ident.ID.Pretty()
cfgs.tracingCfg.ClusterPeername = cfgs.clusterCfg.Peername
cfgs.tracingCfg.EnableTracing = tracingValue
cfgs.clusterCfg.Tracing = tracingValue
Expand Down
18 changes: 13 additions & 5 deletions cmd/ipfs-cluster-service/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"syscall"
"time"

"github.com/ipfs/ipfs-cluster/identity"

ipfscluster "github.com/ipfs/ipfs-cluster"
"github.com/ipfs/ipfs-cluster/allocator/ascendalloc"
"github.com/ipfs/ipfs-cluster/allocator/descendalloc"
Expand Down Expand Up @@ -56,6 +58,10 @@ func daemon(c *cli.Context) error {
locker.lock()
defer locker.tryUnlock()

ident, _ := extractIdentity()
// TODO: if identity did not exist remove identity from configuration.json
// by saving it again

// Load all the configurations
cfgMgr, cfgs := makeAndLoadConfigs()
defer cfgMgr.Shutdown()
Expand All @@ -64,7 +70,7 @@ func daemon(c *cli.Context) error {
cfgs.metricsCfg.EnableStats = true
}

cfgs = propagateTracingConfig(cfgs, c.Bool("tracing"))
cfgs = propagateTracingConfig(cfgs, ident, c.Bool("tracing"))

// Cleanup state if bootstrapping
raftStaging := false
Expand All @@ -77,7 +83,7 @@ func daemon(c *cli.Context) error {
cfgs.clusterCfg.LeaveOnShutdown = true
}

cluster, err := createCluster(ctx, c, cfgs, raftStaging)
cluster, err := createCluster(ctx, c, cfgs, ident, raftStaging)
checkErr("starting cluster", err)

// noop if no bootstraps
Expand All @@ -97,10 +103,11 @@ func createCluster(
ctx context.Context,
c *cli.Context,
cfgs *cfgs,
ident *identity.Identity,
raftStaging bool,
) (*ipfscluster.Cluster, error) {

host, pubsub, dht, err := ipfscluster.NewClusterHost(ctx, cfgs.clusterCfg)
host, pubsub, dht, err := ipfscluster.NewClusterHost(ctx, cfgs.clusterCfg, ident)
checkErr("creating libP2P Host", err)

peerstoreMgr := pstoremgr.New(host, cfgs.clusterCfg.GetPeerstorePath())
Expand Down Expand Up @@ -143,7 +150,7 @@ func createCluster(
tracer, err := observations.SetupTracing(cfgs.tracingCfg)
checkErr("setting up Tracing", err)

store := setupDatastore(c.String("consensus"), cfgs)
store := setupDatastore(c.String("consensus"), cfgs, ident)

cons, err := setupConsensus(
c.String("consensus"),
Expand Down Expand Up @@ -295,8 +302,9 @@ func setupPinTracker(
func setupDatastore(
consensus string,
cfgs *cfgs,
ident *identity.Identity,
) ds.Datastore {
stmgr := newStateManager(consensus, cfgs)
stmgr := newStateManager(consensus, cfgs, ident)
store, err := stmgr.GetStore()
checkErr("creating datastore", err)
return store
Expand Down
Loading

0 comments on commit 51214da

Please sign in to comment.