diff --git a/cmd/ud.go b/cmd/ud.go index 439178602125d5ce2015c937aa20657f3dff6352..084b318b829e1971af60c5ea1ed28a8f8d9da68d 100644 --- a/cmd/ud.go +++ b/cmd/ud.go @@ -10,7 +10,6 @@ package cmd import ( "fmt" - "strings" "time" "gitlab.com/elixxir/client/single" @@ -65,18 +64,10 @@ var udCmd = &cobra.Command{ rng := messenger.GetRng() userToRegister := viper.GetString(udRegisterFlag) jww.TRACE.Printf("[UD] Registering user %v...", userToRegister) - userDiscoveryMgr, err := ud.NewManager(messenger, messenger.GetComms(), + userDiscoveryMgr, err := ud.LoadOrNewManager(messenger, messenger.GetComms(), messenger.NetworkFollowerStatus, userToRegister, nil) if err != nil { - if strings.Contains(err.Error(), ud.IsRegisteredErr) { - userDiscoveryMgr, err = ud.LoadManager(messenger, messenger.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) diff --git a/ud/manager.go b/ud/manager.go index c9d408f0656a850b864deb2b4eaed065984d0013..4a67c111650a4dc2ad2c24175cbb84d55208c55f 100644 --- a/ud/manager.go +++ b/ud/manager.go @@ -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,15 @@ 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. +// LoadOrNewManager 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(messenger udE2e, comms Comms, follower udNetworkStatus, - username string, registrationValidationSignature []byte) (*Manager, error) { - jww.INFO.Println("ud.NewManager()") +func LoadOrNewManager(messenger udE2e, comms Comms, follower udNetworkStatus, + username string, networkValidationSig []byte) (*Manager, error) { + jww.INFO.Println("ud.LoadOrNewManager()") if follower() != xxdk.Running { return nil, errors.New( @@ -69,13 +59,18 @@ func NewManager(messenger udE2e, comms Comms, follower udNetworkStatus, // Initialize manager m := &Manager{ - messenger: messenger, - comms: comms, - registrationValidationSignature: registrationValidationSignature, + messenger: messenger, + comms: comms, } 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 +90,7 @@ func NewManager(messenger 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 +180,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(messenger udE2e, comms Comms) (*Manager, error) { - m := &Manager{ - messenger: messenger, - 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 { diff --git a/ud/register.go b/ud/register.go index bac71c0ceb312f382ebedf53c2a3ac5909a9eb72..d1aae3abdaaec31875577f229a02ad6d05ddf387 100644 --- a/ud/register.go +++ b/ud/register.go @@ -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.messenger.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, diff --git a/ud/register_test.go b/ud/register_test.go index fc1d4be40a5c949384f43f2a212c30d6ccf4c19a..e19c05edbf8e210bc242e22b2ddcbdf8294cb797 100644 --- a/ud/register_test.go +++ b/ud/register_test.go @@ -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.messenger.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.messenger.GetReceptionIdentity()