Skip to content
Snippets Groups Projects
Commit 7907c0dd authored by Jake Taylor's avatar Jake Taylor :lips:
Browse files

Merge branch 'Josh/UdManagerRefactor' into 'release'

Remove ud.LoadManager for LoadOrNewManager

See merge request !292
parents 4decb591 87303791
No related branches found
No related tags found
2 merge requests!510Release,!292Remove ud.LoadManager for LoadOrNewManager
...@@ -10,7 +10,6 @@ package cmd ...@@ -10,7 +10,6 @@ package cmd
import ( import (
"fmt" "fmt"
"strings"
"time" "time"
"gitlab.com/elixxir/client/single" "gitlab.com/elixxir/client/single"
...@@ -62,21 +61,12 @@ var udCmd = &cobra.Command{ ...@@ -62,21 +61,12 @@ var udCmd = &cobra.Command{
jww.TRACE.Printf("[UD] Connected!") jww.TRACE.Printf("[UD] Connected!")
// Make user discovery manager // Make user discovery manager
rng := user.GetRng()
userToRegister := viper.GetString(udRegisterFlag) userToRegister := viper.GetString(udRegisterFlag)
jww.TRACE.Printf("[UD] Registering identity %v...", userToRegister) jww.TRACE.Printf("[UD] Registering identity %v...", userToRegister)
userDiscoveryMgr, err := ud.NewManager(user, user.GetComms(), userDiscoveryMgr, err := ud.LoadOrNewManager(user, user.GetComms(),
user.NetworkFollowerStatus, userToRegister, nil) user.NetworkFollowerStatus, userToRegister, nil)
if err != nil { if err != nil {
if strings.Contains(err.Error(), ud.IsRegisteredErr) { jww.FATAL.Panicf("Failed to load or create new UD manager: %+v", err)
userDiscoveryMgr, err = ud.LoadManager(user, user.GetComms())
if err != nil {
jww.FATAL.Panicf("Failed to load UD manager: %+v", err)
}
} else {
jww.FATAL.Panicf("Failed to create new UD manager: %+v", err)
}
} }
jww.INFO.Printf("[UD] Registered user %v", userToRegister) jww.INFO.Printf("[UD] Registered user %v", userToRegister)
...@@ -145,13 +135,11 @@ var udCmd = &cobra.Command{ ...@@ -145,13 +135,11 @@ var udCmd = &cobra.Command{
printContact(newContact) printContact(newContact)
} }
stream := rng.GetStream()
_, _, err = ud.Lookup(user, _, _, err = ud.Lookup(user,
udContact, cb, lookupID, single.GetDefaultRequestParams()) udContact, cb, lookupID, single.GetDefaultRequestParams())
if err != nil { if err != nil {
jww.WARN.Printf("Failed UD lookup: %+v", err) jww.WARN.Printf("Failed UD lookup: %+v", err)
} }
stream.Close()
time.Sleep(31 * time.Second) time.Sleep(31 * time.Second)
} }
......
...@@ -18,12 +18,6 @@ import ( ...@@ -18,12 +18,6 @@ import (
"gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/id"
) )
const (
IsRegisteredErr = "NewManager is already registered. " +
"NewManager is meant for the first instantiation. Use LoadManager " +
"for all other calls"
)
// Manager is the control structure for the contacting the user discovery service. // Manager is the control structure for the contacting the user discovery service.
type Manager struct { type Manager struct {
...@@ -48,19 +42,22 @@ type Manager struct { ...@@ -48,19 +42,22 @@ type Manager struct {
// alternativeUd is an alternate User discovery service to circumvent // alternativeUd is an alternate User discovery service to circumvent
// production. This is for testing with a separately deployed UD service. // production. This is for testing with a separately deployed UD service.
alternativeUd *alternateUd alternativeUd *alternateUd
// registrationValidationSignature for the ReceptionID
// Optional, depending on UD configuration
registrationValidationSignature []byte
} }
// NewManager builds a new user discovery manager. // LoadOrNewManager loads an existing Manager from storage or creates a
// It requires that an updated // new one if there is no extant storage information.
// NDF is available and will error if one is not. //
// registrationValidationSignature may be set to nil // Params
func NewManager(user udE2e, comms Comms, follower udNetworkStatus, // - user is an interface that adheres to the xxdk.E2e object.
username string, registrationValidationSignature []byte) (*Manager, error) { // - comms is an interface that adheres to client.Comms object.
jww.INFO.Println("ud.NewManager()") // - follower is a method off of xxdk.Cmix which returns the network follower's status.
// - username is the name of the user as it is registered with UD. This will be what the end user
// provides if through the bindings.
// - networkValidationSig is a signature provided by the network (i.e. the client registrar). This may
// be nil, however UD may return an error in some cases (e.g. in a production level environment).
func LoadOrNewManager(user udE2e, comms Comms, follower udNetworkStatus,
username string, networkValidationSig []byte) (*Manager, error) {
jww.INFO.Println("ud.LoadOrNewManager()")
if follower() != xxdk.Running { if follower() != xxdk.Running {
return nil, errors.New( return nil, errors.New(
...@@ -69,13 +66,18 @@ func NewManager(user udE2e, comms Comms, follower udNetworkStatus, ...@@ -69,13 +66,18 @@ func NewManager(user udE2e, comms Comms, follower udNetworkStatus,
// Initialize manager // Initialize manager
m := &Manager{ m := &Manager{
user: user, user: user,
comms: comms, comms: comms,
registrationValidationSignature: registrationValidationSignature,
} }
if m.isRegistered() { if m.isRegistered() {
return nil, errors.Errorf(IsRegisteredErr) // Load manager if already registered
var err error
m.store, err = store.NewOrLoadStore(m.getKv())
if err != nil {
return nil, errors.Errorf("Failed to initialize store: %v", err)
}
return m, nil
} }
// Initialize store // Initialize store
...@@ -95,7 +97,7 @@ func NewManager(user udE2e, comms Comms, follower udNetworkStatus, ...@@ -95,7 +97,7 @@ func NewManager(user udE2e, comms Comms, follower udNetworkStatus,
// Register with user discovery // Register with user discovery
stream := m.getRng().GetStream() stream := m.getRng().GetStream()
defer stream.Close() defer stream.Close()
err = m.register(username, stream, m.comms, udHost) err = m.register(username, networkValidationSig, stream, m.comms, udHost)
if err != nil { if err != nil {
return nil, errors.Errorf("Failed to register: %v", err) return nil, errors.Errorf("Failed to register: %v", err)
} }
...@@ -185,29 +187,6 @@ func InitStoreFromBackup(kv *versioned.KV, ...@@ -185,29 +187,6 @@ func InitStoreFromBackup(kv *versioned.KV,
return nil return nil
} }
// LoadManager loads the state of the Manager
// from disk. This is meant to be called after any the first
// instantiation of the manager by NewUserDiscovery.
func LoadManager(user udE2e, comms Comms) (*Manager, error) {
m := &Manager{
user: user,
comms: comms,
}
if !m.isRegistered() {
return nil, errors.Errorf("LoadManager could not detect that " +
"the user has been registered. Has a manager been initiated before?")
}
var err error
m.store, err = store.NewOrLoadStore(m.getKv())
if err != nil {
return nil, errors.Errorf("Failed to initialize store: %v", err)
}
return m, err
}
// GetFacts returns a list of fact.Fact objects that exist within the // GetFacts returns a list of fact.Fact objects that exist within the
// Store's registeredFacts map. // Store's registeredFacts map.
func (m *Manager) GetFacts() []fact.Fact { func (m *Manager) GetFacts() []fact.Fact {
......
...@@ -14,8 +14,8 @@ import ( ...@@ -14,8 +14,8 @@ import (
// register initiates registration with user discovery given a specified // register initiates registration with user discovery given a specified
// username. Provided a comms sub-interface to facilitate testing. // username. Provided a comms sub-interface to facilitate testing.
func (m *Manager) register(username string, rng csprng.Source, func (m *Manager) register(username string, networkSignature []byte,
comm registerUserComms, udHost *connect.Host) error { rng csprng.Source, comm registerUserComms, udHost *connect.Host) error {
var err error var err error
identity := m.user.GetReceptionIdentity() identity := m.user.GetReceptionIdentity()
...@@ -35,7 +35,7 @@ func (m *Manager) register(username string, rng csprng.Source, ...@@ -35,7 +35,7 @@ func (m *Manager) register(username string, rng csprng.Source,
// Construct the user registration message // Construct the user registration message
msg := &pb.UDBUserRegistration{ msg := &pb.UDBUserRegistration{
PermissioningSignature: m.registrationValidationSignature, PermissioningSignature: networkSignature,
RSAPublicPem: string(rsa.CreatePublicKeyPem(privKey.GetPublic())), RSAPublicPem: string(rsa.CreatePublicKeyPem(privKey.GetPublic())),
IdentityRegistration: &pb.Identity{ IdentityRegistration: &pb.Identity{
Username: username, Username: username,
......
...@@ -34,13 +34,15 @@ func TestManager_register(t *testing.T) { ...@@ -34,13 +34,15 @@ func TestManager_register(t *testing.T) {
c := &testRegisterComm{} c := &testRegisterComm{}
prng := NewPrng(42) prng := NewPrng(42)
err = m.register("testUser", prng, c, udHost) mockSig := []byte("mock")
err = m.register("testUser", mockSig, prng, c, udHost)
if err != nil { if err != nil {
t.Errorf("register() returned an error: %+v", err) t.Errorf("register() returned an error: %+v", err)
} }
// Check if the UDBUserRegistration contents are correct // Check if the UDBUserRegistration contents are correct
isCorrect("testUser", c.msg, m, t) isCorrect("testUser", mockSig, c.msg, m, t)
// Verify the signed identity data // Verify the signed identity data
pubKeyPem := m.user.GetReceptionIdentity().RSAPrivatePem pubKeyPem := m.user.GetReceptionIdentity().RSAPrivatePem
...@@ -66,10 +68,10 @@ func TestManager_register(t *testing.T) { ...@@ -66,10 +68,10 @@ func TestManager_register(t *testing.T) {
// isCorrect checks if the UDBUserRegistration has all the expected fields minus // isCorrect checks if the UDBUserRegistration has all the expected fields minus
// any signatures. // any signatures.
func isCorrect(username string, msg *pb.UDBUserRegistration, m *Manager, t *testing.T) { func isCorrect(username string, mockSig []byte, msg *pb.UDBUserRegistration, m *Manager, t *testing.T) {
if !bytes.Equal(m.registrationValidationSignature, msg.PermissioningSignature) { if !bytes.Equal(mockSig, msg.PermissioningSignature) {
t.Errorf("PermissioningSignature incorrect.\n\texpected: %v\n\treceived: %v", t.Errorf("PermissioningSignature incorrect.\n\texpected: %v\n\treceived: %v",
m.registrationValidationSignature, msg.PermissioningSignature) mockSig, msg.PermissioningSignature)
} }
identity := m.user.GetReceptionIdentity() identity := m.user.GetReceptionIdentity()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment