From a328347829d74c687684e94c9e5d1a00fbe83a00 Mon Sep 17 00:00:00 2001 From: Andy Uhnak Date: Thu, 23 Feb 2023 17:12:25 +0000 Subject: [PATCH] Add more crypto logs --- .../RoomEvent/MXRoomEventEncryption.swift | 10 +++++++--- .../Crypto/CryptoMachine/MXCryptoMachine.swift | 15 ++++++++++----- MatrixSDK/Crypto/MXCryptoV2.swift | 3 --- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/MatrixSDK/Crypto/Algorithms/RoomEvent/MXRoomEventEncryption.swift b/MatrixSDK/Crypto/Algorithms/RoomEvent/MXRoomEventEncryption.swift index c98aaf82e..8475e44e7 100644 --- a/MatrixSDK/Crypto/Algorithms/RoomEvent/MXRoomEventEncryption.swift +++ b/MatrixSDK/Crypto/Algorithms/RoomEvent/MXRoomEventEncryption.swift @@ -140,13 +140,17 @@ struct MXRoomEventEncryption: MXRoomEventEncrypting { private func ensureRoomEncryption(roomId: String, algorithm: String?) throws { let existingAlgorithm = legacyStore.algorithm(forRoom: roomId) if existingAlgorithm != nil && existingAlgorithm == algorithm { - log.debug("Encryption in room is already set to the correct algorithm") + // Encryption in room is already set to the correct algorithm return } guard let algorithm = algorithm else { - log.error("Resetting encryption is not allowed") - throw Error.invalidEncryptionAlgorithm + if existingAlgorithm != nil { + log.error("Resetting encryption is not allowed") + return + } else { + throw Error.invalidEncryptionAlgorithm + } } let supportedAlgorithms = Set([kMXCryptoMegolmAlgorithm]) diff --git a/MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift b/MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift index 8a91dfe1d..38563d7cf 100644 --- a/MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift +++ b/MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift @@ -220,7 +220,7 @@ extension MXCryptoMachine: MXCryptoSyncing { // MARK: - Private private func handleRequest(_ request: Request) async throws { - log.debug("Handling request `\(request.type)`") + log.debug("Handling `\(request.type)` request") switch request { case .toDevice(let requestId, let eventType, let body): @@ -240,9 +240,14 @@ extension MXCryptoMachine: MXCryptoSyncing { try markRequestAsSent(requestId: requestId, requestType: .keysQuery, response: response.jsonString()) case .keysClaim(let requestId, let oneTimeKeys): + log.debug("Claiming keys \(oneTimeKeys)") + let response = try await requests.claimKeys( request: .init(oneTimeKeys: oneTimeKeys) ) + + let dictionary = response.jsonDictionary() as? [String: Any] ?? [:] + log.debug("Keys claimed\n\(dictionary)") try markRequestAsSent(requestId: requestId, requestType: .keysClaim, response: response.jsonString()) case .keysBackup(let requestId, let version, let rooms): @@ -387,11 +392,9 @@ extension MXCryptoMachine: MXCryptoRoomEventEncrypting { try await self?.getMissingSessions(users: users) } - log.debug("Acquiring lock for room \(roomId)") let roomQueue = await roomQueues.getQueue(for: roomId) try await roomQueue.sync { [weak self] in - self?.log.debug("Sharing room keys") try await self?.shareRoomKey(roomId: roomId, users: users, settings: settings) } } @@ -420,13 +423,13 @@ extension MXCryptoMachine: MXCryptoRoomEventEncrypting { // MARK: - Private private func getMissingSessions(users: [String]) async throws { - log.debug("Getting missing sessions for \(users.count) user(s)") + log.debug("Checking missing olm sessions for \(users.count) user(s): \(users)") guard let request = try machine.getMissingSessions(users: users), case .keysClaim = request else { - log.debug("No sessions are missing") + log.debug("No olm sessions are missing") return } @@ -435,6 +438,8 @@ extension MXCryptoMachine: MXCryptoRoomEventEncrypting { } private func shareRoomKey(roomId: String, users: [String], settings: EncryptionSettings) async throws { + log.debug("Checking unshared room keys") + let requests = try machine.shareRoomKey(roomId: roomId, users: users, settings: settings) guard !requests.isEmpty else { log.debug("There are no new keys to share") diff --git a/MatrixSDK/Crypto/MXCryptoV2.swift b/MatrixSDK/Crypto/MXCryptoV2.swift index 7e559c41d..0d2e57442 100644 --- a/MatrixSDK/Crypto/MXCryptoV2.swift +++ b/MatrixSDK/Crypto/MXCryptoV2.swift @@ -286,12 +286,9 @@ class MXCryptoV2: NSObject, MXCrypto { success: (() -> Void)?, failure: ((Swift.Error) -> Void)? ) -> MXHTTPOperation? { - log.debug("->") - Task { do { try await encryptor.ensureRoomKeysShared(roomId: roomId) - log.debug("Room keys shared when necessary") await MainActor.run { success?() }