-
-
Notifications
You must be signed in to change notification settings - Fork 103
verification: Clean up logging when checking transaction IDs #440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bradtgmurray
wants to merge
3
commits into
main
Choose a base branch
from
verification-logging-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+71
−63
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,6 +141,70 @@ func (vh *VerificationHelper) getLog(ctx context.Context) *zerolog.Logger { | |
| return &logger | ||
| } | ||
|
|
||
| func (vh *VerificationHelper) checkTransactionID(ctx context.Context, evt *event.Event) *VerificationTransaction { | ||
| log := vh.getLog(ctx).With(). | ||
| Str("verification_action", "check transaction ID"). | ||
| Stringer("sender", evt.Sender). | ||
| Stringer("room_id", evt.RoomID). | ||
| Stringer("event_id", evt.ID). | ||
| Stringer("event_type", evt.Type). | ||
| Logger() | ||
| ctx = log.WithContext(ctx) | ||
|
|
||
| var transactionID id.VerificationTransactionID | ||
| if evt.ID != "" { | ||
| transactionID = id.VerificationTransactionID(evt.ID) | ||
| } else { | ||
| if txnID, ok := evt.Content.Parsed.(event.VerificationTransactionable); !ok { | ||
| log.Warn().Msg("Ignoring verification event without a transaction ID") | ||
| return nil | ||
| } else { | ||
| transactionID = txnID.GetTransactionID() | ||
| } | ||
| } | ||
| log = log.With().Stringer("transaction_id", transactionID).Logger() | ||
|
|
||
| vh.activeTransactionsLock.Lock() | ||
| defer vh.activeTransactionsLock.Unlock() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing to use defer and changing this to return a *VerificationTransaction should be the only changes to this code after factoring it out. |
||
|
|
||
| txn, err := vh.store.GetVerificationTransaction(ctx, transactionID) | ||
| if err != nil && errors.Is(err, ErrUnknownVerificationTransaction) { | ||
| log.Err(err).Msg("failed to get verification transaction") | ||
| return nil | ||
| } else if errors.Is(err, ErrUnknownVerificationTransaction) { | ||
| // If it's a cancellation event for an unknown transaction, we | ||
| // can just ignore it. | ||
| if evt.Type == event.ToDeviceVerificationCancel || evt.Type == event.InRoomVerificationCancel { | ||
| log.Info().Msg("Ignoring verification cancellation event for an unknown transaction") | ||
| return nil | ||
| } | ||
|
|
||
| log.Warn().Msg("Sending cancellation event for unknown transaction ID") | ||
|
|
||
| // We have to create a fake transaction so that the call to | ||
| // cancelVerificationTxn works. | ||
| txn = VerificationTransaction{ | ||
| ExpirationTime: jsontime.UnixMilli{Time: time.Now().Add(time.Minute * 10)}, | ||
| RoomID: evt.RoomID, | ||
| TheirUserID: evt.Sender, | ||
| } | ||
| if transactionable, ok := evt.Content.Parsed.(event.VerificationTransactionable); ok { | ||
| txn.TransactionID = transactionable.GetTransactionID() | ||
| } else { | ||
| txn.TransactionID = id.VerificationTransactionID(evt.ID) | ||
| } | ||
| if fromDevice, ok := evt.Content.Raw["from_device"]; ok { | ||
| txn.TheirDeviceID = id.DeviceID(fromDevice.(string)) | ||
| } | ||
|
|
||
| // Send a cancellation event. | ||
| vh.cancelVerificationTxn(ctx, txn, event.VerificationCancelCodeUnknownTransaction, "The transaction ID was not recognized.") | ||
| return nil | ||
| } | ||
|
|
||
| return &txn | ||
| } | ||
|
|
||
| // Init initializes the verification helper by adding the necessary event | ||
| // handlers to the syncer. | ||
| func (vh *VerificationHelper) Init(ctx context.Context) error { | ||
|
|
@@ -165,78 +229,22 @@ func (vh *VerificationHelper) Init(ctx context.Context) error { | |
| // and ignore the event if it isn't. | ||
| wrapHandler := func(callback func(context.Context, VerificationTransaction, *event.Event)) func(context.Context, *event.Event) { | ||
| return func(ctx context.Context, evt *event.Event) { | ||
| log := vh.getLog(ctx).With(). | ||
| Str("verification_action", "check transaction ID"). | ||
| Stringer("sender", evt.Sender). | ||
| Stringer("room_id", evt.RoomID). | ||
| Stringer("event_id", evt.ID). | ||
| Stringer("event_type", evt.Type). | ||
| Logger() | ||
| ctx = log.WithContext(ctx) | ||
|
|
||
| var transactionID id.VerificationTransactionID | ||
| if evt.ID != "" { | ||
| transactionID = id.VerificationTransactionID(evt.ID) | ||
| } else { | ||
| if txnID, ok := evt.Content.Parsed.(event.VerificationTransactionable); !ok { | ||
| log.Warn().Msg("Ignoring verification event without a transaction ID") | ||
| return | ||
| } else { | ||
| transactionID = txnID.GetTransactionID() | ||
| } | ||
| } | ||
| log = log.With().Stringer("transaction_id", transactionID).Logger() | ||
|
|
||
| vh.activeTransactionsLock.Lock() | ||
| txn, err := vh.store.GetVerificationTransaction(ctx, transactionID) | ||
| if err != nil && errors.Is(err, ErrUnknownVerificationTransaction) { | ||
| log.Err(err).Msg("failed to get verification transaction") | ||
| vh.activeTransactionsLock.Unlock() | ||
| txn := vh.checkTransactionID(ctx, evt) | ||
| if txn == nil { | ||
| return | ||
| } else if errors.Is(err, ErrUnknownVerificationTransaction) { | ||
| // If it's a cancellation event for an unknown transaction, we | ||
| // can just ignore it. | ||
| if evt.Type == event.ToDeviceVerificationCancel || evt.Type == event.InRoomVerificationCancel { | ||
| log.Info().Msg("Ignoring verification cancellation event for an unknown transaction") | ||
| vh.activeTransactionsLock.Unlock() | ||
| return | ||
| } | ||
|
|
||
| log.Warn().Msg("Sending cancellation event for unknown transaction ID") | ||
|
|
||
| // We have to create a fake transaction so that the call to | ||
| // cancelVerificationTxn works. | ||
| txn = VerificationTransaction{ | ||
| ExpirationTime: jsontime.UnixMilli{Time: time.Now().Add(time.Minute * 10)}, | ||
| RoomID: evt.RoomID, | ||
| TheirUserID: evt.Sender, | ||
| } | ||
| if transactionable, ok := evt.Content.Parsed.(event.VerificationTransactionable); ok { | ||
| txn.TransactionID = transactionable.GetTransactionID() | ||
| } else { | ||
| txn.TransactionID = id.VerificationTransactionID(evt.ID) | ||
| } | ||
| if fromDevice, ok := evt.Content.Raw["from_device"]; ok { | ||
| txn.TheirDeviceID = id.DeviceID(fromDevice.(string)) | ||
| } | ||
|
|
||
| // Send a cancellation event. | ||
| vh.cancelVerificationTxn(ctx, txn, event.VerificationCancelCodeUnknownTransaction, "The transaction ID was not recognized.") | ||
| vh.activeTransactionsLock.Unlock() | ||
| return | ||
| } else { | ||
| vh.activeTransactionsLock.Unlock() | ||
| } | ||
|
|
||
| logCtx := log.With(). | ||
| logCtx := vh.getLog(ctx).With(). | ||
| Stringer("transaction_step", txn.VerificationState). | ||
| Stringer("sender", evt.Sender) | ||
| Stringer("sender", evt.Sender). | ||
| Stringer("event_type", evt.Type) | ||
| if evt.RoomID != "" { | ||
| logCtx = logCtx. | ||
| Stringer("room_id", evt.RoomID). | ||
| Stringer("event_id", evt.ID) | ||
| } | ||
| callback(logCtx.Logger().WithContext(ctx), txn, evt) | ||
|
|
||
| callback(logCtx.Logger().WithContext(ctx), *txn, evt) | ||
| } | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note, this change looses the
event_typekey from the callback's logger. I'm not sure how useful it is (and it might be added to the context somewhere else).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, fixed!