From df22a8607b9ed36528db0453478c97fb9adeac9c Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Sat, 30 Dec 2023 12:28:11 -0700 Subject: [PATCH] provisioning/Logout: use structured logging Signed-off-by: Sumner Evans --- provisioning.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/provisioning.go b/provisioning.go index 2bf00063..689177dc 100644 --- a/provisioning.go +++ b/provisioning.go @@ -330,14 +330,15 @@ func (prov *ProvisioningAPI) existingSession(user *User) (handle *provisioningHa return nil } -func (prov *ProvisioningAPI) clearSession(user *User) { +func (prov *ProvisioningAPI) clearSession(ctx context.Context, user *User) { + log := zerolog.Ctx(ctx).With().Str("function", "clearSession").Logger() prov.mutexForUser(user).Lock() defer prov.mutexForUser(user).Unlock() if existingSessionID, ok := prov.provisioningUsers[user.MXID.String()]; ok { - prov.log.Debug().Msgf("clearSession called for %v, clearing session %v", user.MXID, existingSessionID) + log.Debug().Int("existing_session_id", existingSessionID).Msg("clearing existing session") if existingSessionID >= len(prov.provisioningHandles) { - prov.log.Warn().Msgf("clearSession called for %v, session %v does not exist", user.MXID, existingSessionID) + log.Warn().Msg("session does not exist") return } if prov.provisioningHandles[existingSessionID].cancel != nil { @@ -346,7 +347,7 @@ func (prov *ProvisioningAPI) clearSession(user *User) { prov.provisioningHandles[existingSessionID] = nil delete(prov.provisioningUsers, user.MXID.String()) } else { - prov.log.Debug().Msgf("clearSession called for %v, no session found", user.MXID) + prov.log.Debug().Msg("no session found") } } @@ -361,7 +362,7 @@ func (prov *ProvisioningAPI) loginOrSendError(ctx context.Context, w http.Respon zerolog.Ctx(ctx).Debug(). Int("existing_provisioning_handle", handle.id). Msg("user already has pending provisioning request, cancelling") - prov.clearSession(user) + prov.clearSession(ctx, user) newSessionLoggedIn, handle, err = prov.newOrExistingSession(user) if err != nil { return nil, fmt.Errorf("error logging in after cancelling existing session: %w", err) @@ -639,8 +640,14 @@ func (prov *ProvisioningAPI) LinkWaitForAccount(w http.ResponseWriter, r *http.R func (prov *ProvisioningAPI) Logout(w http.ResponseWriter, r *http.Request) { user := r.Context().Value("user").(*User) - prov.log.Debug().Msgf("Logout called from %v (but not logging out)", user.MXID) - prov.clearSession(user) + log := prov.log.With(). + Str("action", "logout"). + Str("user_id", user.MXID.String()). + Logger() + ctx := log.WithContext(r.Context()) + log.Debug().Msg("Logout called (but not logging out)") + + prov.clearSession(ctx, user) // For now do nothing - we need this API to return 200 to be compatible with // the old Signal bridge, which needed a call to Logout before allowing LinkNew