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
Branches
Tags
2 merge requests!510Release,!292Remove ud.LoadManager for LoadOrNewManager
......@@ -10,7 +10,6 @@ package cmd
import (
"fmt"
"strings"
"time"
"gitlab.com/elixxir/client/single"
......@@ -62,21 +61,12 @@ var udCmd = &cobra.Command{
jww.TRACE.Printf("[UD] Connected!")
// Make user discovery manager
rng := user.GetRng()
userToRegister := viper.GetString(udRegisterFlag)
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)
if err != nil {
if strings.Contains(err.Error(), ud.IsRegisteredErr) {
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.FATAL.Panicf("Failed to load or create new UD manager: %+v", err)
}
jww.INFO.Printf("[UD] Registered user %v", userToRegister)
......@@ -145,13 +135,11 @@ var udCmd = &cobra.Command{
printContact(newContact)
}
stream := rng.GetStream()
_, _, err = ud.Lookup(user,
udContact, cb, lookupID, single.GetDefaultRequestParams())
if err != nil {
jww.WARN.Printf("Failed UD lookup: %+v", err)
}
stream.Close()
time.Sleep(31 * time.Second)
}
......
......@@ -18,12 +18,6 @@ import (
"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.
type Manager struct {
......@@ -48,19 +42,22 @@ type Manager struct {
// alternativeUd is an alternate User discovery service to circumvent
// production. This is for testing with a separately deployed UD service.
alternativeUd *alternateUd
// registrationValidationSignature for the ReceptionID
// Optional, depending on UD configuration
registrationValidationSignature []byte
}
// NewManager builds a new user discovery manager.
// It requires that an updated
// NDF is available and will error if one is not.
// registrationValidationSignature may be set to nil
func NewManager(user udE2e, comms Comms, follower udNetworkStatus,
username string, registrationValidationSignature []byte) (*Manager, error) {
jww.INFO.Println("ud.NewManager()")
// LoadOrNewManager loads an existing Manager from storage or creates a
// new one if there is no extant storage information.
//
// Params
// - user is an interface that adheres to the xxdk.E2e object.
// - comms is an interface that adheres to client.Comms object.
// - 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 {
return nil, errors.New(
......@@ -71,11 +68,16 @@ func NewManager(user udE2e, comms Comms, follower udNetworkStatus,
m := &Manager{
user: user,
comms: comms,
registrationValidationSignature: registrationValidationSignature,
}
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
......@@ -95,7 +97,7 @@ func NewManager(user udE2e, comms Comms, follower udNetworkStatus,
// Register with user discovery
stream := m.getRng().GetStream()
defer stream.Close()
err = m.register(username, stream, m.comms, udHost)
err = m.register(username, networkValidationSig, stream, m.comms, udHost)
if err != nil {
return nil, errors.Errorf("Failed to register: %v", err)
}
......@@ -185,29 +187,6 @@ func InitStoreFromBackup(kv *versioned.KV,
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
// Store's registeredFacts map.
func (m *Manager) GetFacts() []fact.Fact {
......
......@@ -14,8 +14,8 @@ import (
// register initiates registration with user discovery given a specified
// username. Provided a comms sub-interface to facilitate testing.
func (m *Manager) register(username string, rng csprng.Source,
comm registerUserComms, udHost *connect.Host) error {
func (m *Manager) register(username string, networkSignature []byte,
rng csprng.Source, comm registerUserComms, udHost *connect.Host) error {
var err error
identity := m.user.GetReceptionIdentity()
......@@ -35,7 +35,7 @@ func (m *Manager) register(username string, rng csprng.Source,
// Construct the user registration message
msg := &pb.UDBUserRegistration{
PermissioningSignature: m.registrationValidationSignature,
PermissioningSignature: networkSignature,
RSAPublicPem: string(rsa.CreatePublicKeyPem(privKey.GetPublic())),
IdentityRegistration: &pb.Identity{
Username: username,
......
......@@ -34,13 +34,15 @@ func TestManager_register(t *testing.T) {
c := &testRegisterComm{}
prng := NewPrng(42)
err = m.register("testUser", prng, c, udHost)
mockSig := []byte("mock")
err = m.register("testUser", mockSig, prng, c, udHost)
if err != nil {
t.Errorf("register() returned an error: %+v", err)
}
// 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
pubKeyPem := m.user.GetReceptionIdentity().RSAPrivatePem
......@@ -66,10 +68,10 @@ func TestManager_register(t *testing.T) {
// isCorrect checks if the UDBUserRegistration has all the expected fields minus
// any signatures.
func isCorrect(username string, msg *pb.UDBUserRegistration, m *Manager, t *testing.T) {
if !bytes.Equal(m.registrationValidationSignature, msg.PermissioningSignature) {
func isCorrect(username string, mockSig []byte, msg *pb.UDBUserRegistration, m *Manager, t *testing.T) {
if !bytes.Equal(mockSig, msg.PermissioningSignature) {
t.Errorf("PermissioningSignature incorrect.\n\texpected: %v\n\treceived: %v",
m.registrationValidationSignature, msg.PermissioningSignature)
mockSig, msg.PermissioningSignature)
}
identity := m.user.GetReceptionIdentity()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment