Skip to content
Snippets Groups Projects
Commit 93032765 authored by Josh Brooks's avatar Josh Brooks
Browse files

Merge branch 'josh/DeleteRequests' into 'release'

Implement Delete requests functions for auth store

See merge request !147
parents e854dc05 5dc7090e
Branches
Tags
2 merge requests!170Release,!147Implement Delete requests functions for auth store
...@@ -160,7 +160,10 @@ Available Commands: ...@@ -160,7 +160,10 @@ Available Commands:
Flags: Flags:
--accept-channel Accept the channel request for the corresponding recipient ID --accept-channel Accept the channel request for the corresponding recipient ID
--auth-timeout uint The number of seconds to wait for an authentication channelto confirm (default 120) --auth-timeout uint The number of seconds to wait for an authentication channelto confirm (default 120)
--delete-all-requests Delete the all contact requests, both sent and received.
--delete-channel Delete the channel information for the corresponding recipient ID --delete-channel Delete the channel information for the corresponding recipient ID
--delete-receive-requests Delete the all received contact requests.
--delete-sent-requests Delete the all sent contact requests.
--destfile string Read this contact file for the destination id --destfile string Read this contact file for the destination id
-d, --destid string ID to send message to (if below 40, will be precanned. Use '0x' or 'b64:' for hex and base64 representations) (default "0") -d, --destid string ID to send message to (if below 40, will be precanned. Use '0x' or 'b64:' for hex and base64 representations) (default "0")
--e2eMaxKeys uint Max keys used before blocking until a rekey completes (default 800) --e2eMaxKeys uint Max keys used before blocking until a rekey completes (default 800)
......
...@@ -684,6 +684,24 @@ func (c *Client) GetNodeRegistrationStatus() (int, int, error) { ...@@ -684,6 +684,24 @@ func (c *Client) GetNodeRegistrationStatus() (int, int, error) {
return numRegistered, len(nodes) - numStale, nil return numRegistered, len(nodes) - numStale, nil
} }
// DeleteAllRequests clears all requests from client's auth storage.
func (c *Client) DeleteAllRequests() error {
jww.DEBUG.Printf("Deleting all requests")
return c.GetStorage().Auth().DeleteAllRequests()
}
// DeleteSentRequests clears sent requests from client's auth storage.
func (c *Client) DeleteSentRequests() error {
jww.DEBUG.Printf("Deleting all sent requests")
return c.GetStorage().Auth().DeleteSentRequests()
}
// DeleteReceiveRequests clears receive requests from client's auth storage.
func (c *Client) DeleteReceiveRequests() error {
jww.DEBUG.Printf("Deleting all received requests")
return c.GetStorage().Auth().DeleteReceiveRequests()
}
// DeleteContact is a function which removes a partner from Client's storage // DeleteContact is a function which removes a partner from Client's storage
func (c *Client) DeleteContact(partnerId *id.ID) error { func (c *Client) DeleteContact(partnerId *id.ID) error {
jww.DEBUG.Printf("Deleting contact with ID %s", partnerId) jww.DEBUG.Printf("Deleting contact with ID %s", partnerId)
......
...@@ -265,6 +265,7 @@ func (c *Client) WaitForNetwork(timeoutMS int) bool { ...@@ -265,6 +265,7 @@ func (c *Client) WaitForNetwork(timeoutMS int) bool {
func (c *Client) NetworkFollowerStatus() int { func (c *Client) NetworkFollowerStatus() int {
return int(c.api.NetworkFollowerStatus()) return int(c.api.NetworkFollowerStatus())
} }
// HasRunningProcessies checks if any background threads are running. // HasRunningProcessies checks if any background threads are running.
// returns true if none are running. This is meant to be // returns true if none are running. This is meant to be
// used when NetworkFollowerStatus() returns Stopping. // used when NetworkFollowerStatus() returns Stopping.
...@@ -449,6 +450,21 @@ func (c *Client) GetNodeRegistrationStatus() (*NodeRegistrationsStatus, error) { ...@@ -449,6 +450,21 @@ func (c *Client) GetNodeRegistrationStatus() (*NodeRegistrationsStatus, error) {
return &NodeRegistrationsStatus{registered, total}, err return &NodeRegistrationsStatus{registered, total}, err
} }
// DeleteAllRequests clears all requests from Client's auth storage.
func (c *Client) DeleteAllRequests() error {
return c.api.DeleteAllRequests()
}
// DeleteSentRequests clears sent requests from Client's auth storage.
func (c *Client) DeleteSentRequests() error {
return c.api.DeleteSentRequests()
}
// DeleteReceiveRequests clears receive requests from Client's auth storage.
func (c *Client) DeleteReceiveRequests() error {
return c.api.DeleteReceiveRequests()
}
// DeleteContact is a function which removes a contact from Client's storage // DeleteContact is a function which removes a contact from Client's storage
func (c *Client) DeleteContact(b []byte) error { func (c *Client) DeleteContact(b []byte) error {
contactObj, err := UnmarshalContact(b) contactObj, err := UnmarshalContact(b)
......
...@@ -304,6 +304,18 @@ var rootCmd = &cobra.Command{ ...@@ -304,6 +304,18 @@ var rootCmd = &cobra.Command{
deleteChannel(client, recipientID) deleteChannel(client, recipientID)
} }
if viper.GetBool("delete-receive-requests") {
client.DeleteReceiveRequests()
}
if viper.GetBool("delete-sent-requests") {
client.DeleteSentRequests()
}
if viper.GetBool("delete-all-requests") {
client.DeleteAllRequests()
}
msg := message.Send{ msg := message.Send{
Recipient: recipientID, Recipient: recipientID,
Payload: []byte(msgBody), Payload: []byte(msgBody),
...@@ -1030,6 +1042,21 @@ func init() { ...@@ -1030,6 +1042,21 @@ func init() {
viper.BindPFlag("delete-channel", viper.BindPFlag("delete-channel",
rootCmd.PersistentFlags().Lookup("delete-channel")) rootCmd.PersistentFlags().Lookup("delete-channel"))
rootCmd.PersistentFlags().Bool("delete-receive-requests", false,
"Delete the all received contact requests.")
viper.BindPFlag("delete-receive-requests",
rootCmd.PersistentFlags().Lookup("delete-receive-requests"))
rootCmd.PersistentFlags().Bool("delete-sent-requests", false,
"Delete the all sent contact requests.")
viper.BindPFlag("delete-sent-requests",
rootCmd.PersistentFlags().Lookup("delete-sent-requests"))
rootCmd.PersistentFlags().Bool("delete-all-requests", false,
"Delete the all contact requests, both sent and received.")
viper.BindPFlag("delete-all-requests",
rootCmd.PersistentFlags().Lookup("delete-all-requests"))
rootCmd.Flags().BoolP("send-auth-request", "", false, rootCmd.Flags().BoolP("send-auth-request", "", false,
"Send an auth request to the specified destination and wait"+ "Send an auth request to the specified destination and wait"+
"for confirmation") "for confirmation")
......
...@@ -403,7 +403,7 @@ func (s *Store) Done(partner *id.ID) { ...@@ -403,7 +403,7 @@ func (s *Store) Done(partner *id.ID) {
r.mux.Unlock() r.mux.Unlock()
} }
// delete is one of two calls after using a request. This one is to be used when // Delete is one of two calls after using a request. This one is to be used when
// the use is unsuccessful. It deletes all references to the request associated // the use is unsuccessful. It deletes all references to the request associated
// with the passed partner, if it exists. It will allow any thread waiting on // with the passed partner, if it exists. It will allow any thread waiting on
// access to continue. They should fail due to the deletion of the structure. // access to continue. They should fail due to the deletion of the structure.
...@@ -418,23 +418,104 @@ func (s *Store) Delete(partner *id.ID) error { ...@@ -418,23 +418,104 @@ func (s *Store) Delete(partner *id.ID) error {
switch r.rt { switch r.rt {
case Sent: case Sent:
delete(s.fingerprints, r.sent.fingerprint) s.deleteSentRequest(r)
if err := r.sent.delete(); err != nil { case Receive:
jww.FATAL.Panicf("Failed to delete sent request: %+v", err) s.deleteReceiveRequest(r)
} }
delete(s.requests, *partner)
if err := s.save(); err != nil {
jww.FATAL.Panicf("Failed to store updated request map after "+
"deletion: %+v", err)
}
return nil
}
// DeleteAllRequests clears the request map and all associated storage objects
// containing request data.
func (s *Store) DeleteAllRequests() error {
s.mux.Lock()
defer s.mux.Unlock()
for partnerId, req := range s.requests {
switch req.rt {
case Sent:
s.deleteSentRequest(req)
delete(s.requests, partnerId)
case Receive: case Receive:
if err := util.DeleteContact(s.kv, r.receive.ID); err != nil { s.deleteReceiveRequest(req)
jww.FATAL.Panicf("Failed to delete recieved request "+ delete(s.requests, partnerId)
"contact: %+v", err)
} }
} }
delete(s.requests, *partner)
if err := s.save(); err != nil { if err := s.save(); err != nil {
jww.FATAL.Panicf("Failed to store updated request map after "+ jww.FATAL.Panicf("Failed to store updated request map after "+
"deletion: %+v", err) "deleting all requests: %+v", err)
} }
return nil return nil
} }
// DeleteSentRequests deletes all Sent requests from Store.
func (s *Store) DeleteSentRequests() error {
s.mux.Lock()
defer s.mux.Unlock()
for partnerId, req := range s.requests {
switch req.rt {
case Sent:
s.deleteSentRequest(req)
delete(s.requests, partnerId)
case Receive:
continue
}
}
if err := s.save(); err != nil {
jww.FATAL.Panicf("Failed to store updated request map after "+
"deleting all sent requests: %+v", err)
}
return nil
}
// DeleteReceiveRequests deletes all Receive requests from Store.
func (s *Store) DeleteReceiveRequests() error {
s.mux.Lock()
defer s.mux.Unlock()
for partnerId, req := range s.requests {
switch req.rt {
case Sent:
continue
case Receive:
s.deleteReceiveRequest(req)
delete(s.requests, partnerId)
}
}
if err := s.save(); err != nil {
jww.FATAL.Panicf("Failed to store updated request map after "+
"deleting all receive requests: %+v", err)
}
return nil
}
// deleteSentRequest is a helper function which deletes a Sent request from storage.
func (s *Store) deleteSentRequest(r *request) {
delete(s.fingerprints, r.sent.fingerprint)
if err := r.sent.delete(); err != nil {
jww.FATAL.Panicf("Failed to delete sent request: %+v", err)
}
}
// deleteReceiveRequest is a helper function which deletes a Receive request from storage.
func (s *Store) deleteReceiveRequest(r *request) {
if err := util.DeleteContact(s.kv, r.receive.ID); err != nil {
jww.FATAL.Panicf("Failed to delete recieved request "+
"contact: %+v", err)
}
}
...@@ -905,6 +905,170 @@ func TestStore_GetAllReceived_MixSentReceived(t *testing.T) { ...@@ -905,6 +905,170 @@ func TestStore_GetAllReceived_MixSentReceived(t *testing.T) {
} }
// Unit test.
func TestStore_DeleteReceiveRequests(t *testing.T) {
s, _, _ := makeTestStore(t)
c := contact.Contact{ID: id.NewIdFromUInt(rand.Uint64(), id.User, t)}
rng := csprng.NewSystemRNG()
_, sidhPubKey := genSidhAKeys(rng)
if err := s.AddReceived(c, sidhPubKey); err != nil {
t.Fatalf("AddReceived() returned an error: %+v", err)
}
if _, _, err := s.GetReceivedRequest(c.ID); err != nil {
t.Fatalf("GetReceivedRequest() returned an error: %+v", err)
}
err := s.DeleteReceiveRequests()
if err != nil {
t.Fatalf("DeleteReceiveRequests returned an error: %+v", err)
}
if s.requests[*c.ID] != nil {
t.Errorf("delete() failed to delete request for user %s.", c.ID)
}
}
// Unit test.
func TestStore_DeleteSentRequests(t *testing.T) {
s, _, _ := makeTestStore(t)
partnerID := id.NewIdFromUInt(rand.Uint64(), id.User, t)
rng := csprng.NewSystemRNG()
sidhPrivKey, sidhPubKey := genSidhAKeys(rng)
sr := &SentRequest{
kv: s.kv,
partner: partnerID,
partnerHistoricalPubKey: s.grp.NewInt(1),
myPrivKey: s.grp.NewInt(2),
myPubKey: s.grp.NewInt(3),
mySidHPrivKeyA: sidhPrivKey,
mySidHPubKeyA: sidhPubKey,
fingerprint: format.Fingerprint{5},
}
if err := s.AddSent(sr.partner, sr.partnerHistoricalPubKey,
sr.myPrivKey, sr.myPubKey, sr.mySidHPrivKeyA,
sr.mySidHPubKeyA, sr.fingerprint); err != nil {
t.Fatalf("AddSent() returned an error: %+v", err)
}
err := s.DeleteSentRequests()
if err != nil {
t.Fatalf("DeleteSentRequests returned an error: %+v", err)
}
if s.requests[*sr.partner] != nil {
t.Errorf("delete() failed to delete request for user %s.",
sr.partner)
}
if _, exists := s.fingerprints[sr.fingerprint]; exists {
t.Errorf("delete() failed to delete fingerprint for fp %v.",
sr.fingerprint)
}
}
// Tests that DeleteSentRequests does not affect receive requests in map
func TestStore_DeleteSentRequests_ReceiveInMap(t *testing.T) {
s, _, _ := makeTestStore(t)
c := contact.Contact{ID: id.NewIdFromUInt(rand.Uint64(), id.User, t)}
rng := csprng.NewSystemRNG()
_, sidhPubKey := genSidhAKeys(rng)
if err := s.AddReceived(c, sidhPubKey); err != nil {
t.Fatalf("AddReceived() returned an error: %+v", err)
}
err := s.DeleteSentRequests()
if err != nil {
t.Fatalf("DeleteSentRequests returned an error: %+v", err)
}
if s.requests[*c.ID] == nil {
t.Fatalf("DeleteSentRequests removes receive requests!")
}
}
// Tests that DeleteReceiveRequests does not affect sent requests in map
func TestStore_DeleteReceiveRequests_SentInMap(t *testing.T) {
s, _, _ := makeTestStore(t)
partnerID := id.NewIdFromUInt(rand.Uint64(), id.User, t)
rng := csprng.NewSystemRNG()
sidhPrivKey, sidhPubKey := genSidhAKeys(rng)
sr := &SentRequest{
kv: s.kv,
partner: partnerID,
partnerHistoricalPubKey: s.grp.NewInt(1),
myPrivKey: s.grp.NewInt(2),
myPubKey: s.grp.NewInt(3),
mySidHPrivKeyA: sidhPrivKey,
mySidHPubKeyA: sidhPubKey,
fingerprint: format.Fingerprint{5},
}
if err := s.AddSent(sr.partner, sr.partnerHistoricalPubKey,
sr.myPrivKey, sr.myPubKey, sr.mySidHPrivKeyA,
sr.mySidHPubKeyA, sr.fingerprint); err != nil {
t.Fatalf("AddSent() returned an error: %+v", err)
}
err := s.DeleteReceiveRequests()
if err != nil {
t.Fatalf("DeleteSentRequests returned an error: %+v", err)
}
if s.requests[*partnerID] == nil {
t.Fatalf("DeleteReceiveRequests removes sent requests!")
}
}
// Unit test.
func TestStore_DeleteAllRequests(t *testing.T) {
s, _, _ := makeTestStore(t)
partnerID := id.NewIdFromUInt(rand.Uint64(), id.User, t)
rng := csprng.NewSystemRNG()
sidhPrivKey, sidhPubKey := genSidhAKeys(rng)
sr := &SentRequest{
kv: s.kv,
partner: partnerID,
partnerHistoricalPubKey: s.grp.NewInt(1),
myPrivKey: s.grp.NewInt(2),
myPubKey: s.grp.NewInt(3),
mySidHPrivKeyA: sidhPrivKey,
mySidHPubKeyA: sidhPubKey,
fingerprint: format.Fingerprint{5},
}
if err := s.AddSent(sr.partner, sr.partnerHistoricalPubKey,
sr.myPrivKey, sr.myPubKey, sr.mySidHPrivKeyA,
sr.mySidHPubKeyA, sr.fingerprint); err != nil {
t.Fatalf("AddSent() returned an error: %+v", err)
}
c := contact.Contact{ID: id.NewIdFromUInt(rand.Uint64(), id.User, t)}
_, sidhPubKey = genSidhAKeys(rng)
if err := s.AddReceived(c, sidhPubKey); err != nil {
t.Fatalf("AddReceived() returned an error: %+v", err)
}
err := s.DeleteAllRequests()
if err != nil {
t.Fatalf("DeleteAllRequests returned an error: %+v", err)
}
if s.requests[*sr.partner] != nil {
t.Errorf("delete() failed to delete request for user %s.",
sr.partner)
}
if _, exists := s.fingerprints[sr.fingerprint]; exists {
t.Errorf("delete() failed to delete fingerprint for fp %v.",
sr.fingerprint)
}
if s.requests[*c.ID] != nil {
t.Errorf("delete() failed to delete request for user %s.", c.ID)
}
}
func makeTestStore(t *testing.T) (*Store, *versioned.KV, []*cyclic.Int) { func makeTestStore(t *testing.T) (*Store, *versioned.KV, []*cyclic.Int) {
kv := versioned.NewKV(make(ekv.Memstore)) kv := versioned.NewKV(make(ekv.Memstore))
grp := cyclic.NewGroup(large.NewInt(173), large.NewInt(0)) grp := cyclic.NewGroup(large.NewInt(173), large.NewInt(0))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment