Crypto store : avoid copying before mapping to other data

This commit is contained in:
ganfra 2020-04-28 16:26:04 +02:00
parent 4e8177f738
commit 43055964ba

View file

@ -201,9 +201,9 @@ internal class RealmCryptoStore @Inject constructor(
} }
override fun getDeviceId(): String { override fun getDeviceId(): String {
return doRealmQueryAndCopy(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<CryptoMetadataEntity>().findFirst() it.where<CryptoMetadataEntity>().findFirst()?.deviceId
}?.deviceId ?: "" } ?: ""
} }
override fun saveOlmAccount() { override fun saveOlmAccount() {
@ -257,23 +257,24 @@ internal class RealmCryptoStore @Inject constructor(
} }
override fun getUserDevice(userId: String, deviceId: String): CryptoDeviceInfo? { override fun getUserDevice(userId: String, deviceId: String): CryptoDeviceInfo? {
return doRealmQueryAndCopy(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<DeviceInfoEntity>() it.where<DeviceInfoEntity>()
.equalTo(DeviceInfoEntityFields.PRIMARY_KEY, DeviceInfoEntity.createPrimaryKey(userId, deviceId)) .equalTo(DeviceInfoEntityFields.PRIMARY_KEY, DeviceInfoEntity.createPrimaryKey(userId, deviceId))
.findFirst() .findFirst()
}?.let { ?.let { deviceInfo ->
CryptoMapper.mapToModel(it) CryptoMapper.mapToModel(deviceInfo)
}
} }
} }
override fun deviceWithIdentityKey(identityKey: String): CryptoDeviceInfo? { override fun deviceWithIdentityKey(identityKey: String): CryptoDeviceInfo? {
return doRealmQueryAndCopy(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<DeviceInfoEntity>() it.where<DeviceInfoEntity>()
.equalTo(DeviceInfoEntityFields.IDENTITY_KEY, identityKey) .equalTo(DeviceInfoEntityFields.IDENTITY_KEY, identityKey)
.findFirst() .findFirst()
?.let { deviceInfo ->
CryptoMapper.mapToModel(deviceInfo)
} }
?.let {
CryptoMapper.mapToModel(it)
} }
} }
@ -344,9 +345,10 @@ internal class RealmCryptoStore @Inject constructor(
} }
override fun getCrossSigningPrivateKeys(): PrivateKeysInfo? { override fun getCrossSigningPrivateKeys(): PrivateKeysInfo? {
return doRealmQueryAndCopy(realmConfiguration) { realm -> return doWithRealm(realmConfiguration) { realm ->
realm.where<CryptoMetadataEntity>().findFirst() realm.where<CryptoMetadataEntity>()
}?.let { .findFirst()
?.let {
PrivateKeysInfo( PrivateKeysInfo(
master = it.xSignMasterPrivateKey, master = it.xSignMasterPrivateKey,
selfSigned = it.xSignSelfSignedPrivateKey, selfSigned = it.xSignSelfSignedPrivateKey,
@ -354,6 +356,7 @@ internal class RealmCryptoStore @Inject constructor(
) )
} }
} }
}
override fun storePrivateKeysInfo(msk: String?, usk: String?, ssk: String?) { override fun storePrivateKeysInfo(msk: String?, usk: String?, ssk: String?) {
doRealmTransaction(realmConfiguration) { realm -> doRealmTransaction(realmConfiguration) { realm ->
@ -375,9 +378,10 @@ internal class RealmCryptoStore @Inject constructor(
} }
override fun getKeyBackupRecoveryKeyInfo(): SavedKeyBackupKeyInfo? { override fun getKeyBackupRecoveryKeyInfo(): SavedKeyBackupKeyInfo? {
return doRealmQueryAndCopy(realmConfiguration) { realm -> return doWithRealm(realmConfiguration) { realm ->
realm.where<CryptoMetadataEntity>().findFirst() realm.where<CryptoMetadataEntity>()
}?.let { .findFirst()
?.let {
val key = it.keyBackupRecoveryKey val key = it.keyBackupRecoveryKey
val version = it.keyBackupRecoveryKeyVersion val version = it.keyBackupRecoveryKeyVersion
if (!key.isNullOrBlank() && !version.isNullOrBlank()) { if (!key.isNullOrBlank() && !version.isNullOrBlank()) {
@ -387,6 +391,7 @@ internal class RealmCryptoStore @Inject constructor(
} }
} }
} }
}
override fun storeSSKPrivateKey(ssk: String?) { override fun storeSSKPrivateKey(ssk: String?) {
doRealmTransaction(realmConfiguration) { realm -> doRealmTransaction(realmConfiguration) { realm ->
@ -405,24 +410,30 @@ internal class RealmCryptoStore @Inject constructor(
} }
override fun getUserDevices(userId: String): Map<String, CryptoDeviceInfo>? { override fun getUserDevices(userId: String): Map<String, CryptoDeviceInfo>? {
return doRealmQueryAndCopy(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<UserEntity>() it.where<UserEntity>()
.equalTo(UserEntityFields.USER_ID, userId) .equalTo(UserEntityFields.USER_ID, userId)
.findFirst() .findFirst()
}
?.devices ?.devices
?.map { CryptoMapper.mapToModel(it) } ?.map { deviceInfo ->
?.associateBy { it.deviceId } CryptoMapper.mapToModel(deviceInfo)
}
?.associateBy { cryptoDevice ->
cryptoDevice.deviceId
}
}
} }
override fun getUserDeviceList(userId: String): List<CryptoDeviceInfo>? { override fun getUserDeviceList(userId: String): List<CryptoDeviceInfo>? {
return doRealmQueryAndCopy(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<UserEntity>() it.where<UserEntity>()
.equalTo(UserEntityFields.USER_ID, userId) .equalTo(UserEntityFields.USER_ID, userId)
.findFirst() .findFirst()
}
?.devices ?.devices
?.map { CryptoMapper.mapToModel(it) } ?.map { deviceInfo ->
CryptoMapper.mapToModel(deviceInfo)
}
}
} }
override fun getLiveDeviceList(userId: String): LiveData<List<CryptoDeviceInfo>> { override fun getLiveDeviceList(userId: String): LiveData<List<CryptoDeviceInfo>> {
@ -478,17 +489,16 @@ internal class RealmCryptoStore @Inject constructor(
} }
override fun getRoomAlgorithm(roomId: String): String? { override fun getRoomAlgorithm(roomId: String): String? {
return doRealmQueryAndCopy(realmConfiguration) { return doWithRealm(realmConfiguration) {
CryptoRoomEntity.getById(it, roomId) CryptoRoomEntity.getById(it, roomId)?.algorithm
} }
?.algorithm
} }
override fun shouldEncryptForInvitedMembers(roomId: String): Boolean { override fun shouldEncryptForInvitedMembers(roomId: String): Boolean {
return doRealmQueryAndCopy(realmConfiguration) { return doWithRealm(realmConfiguration) {
CryptoRoomEntity.getById(it, roomId) CryptoRoomEntity.getById(it, roomId)?.shouldEncryptForInvitedMembers
} }
?.shouldEncryptForInvitedMembers ?: false ?: false
} }
override fun setShouldEncryptForInvitedMembers(roomId: String, shouldEncryptForInvitedMembers: Boolean) { override fun setShouldEncryptForInvitedMembers(roomId: String, shouldEncryptForInvitedMembers: Boolean) {
@ -552,23 +562,23 @@ internal class RealmCryptoStore @Inject constructor(
} }
override fun getLastUsedSessionId(deviceKey: String): String? { override fun getLastUsedSessionId(deviceKey: String): String? {
return doRealmQueryAndCopy(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<OlmSessionEntity>() it.where<OlmSessionEntity>()
.equalTo(OlmSessionEntityFields.DEVICE_KEY, deviceKey) .equalTo(OlmSessionEntityFields.DEVICE_KEY, deviceKey)
.sort(OlmSessionEntityFields.LAST_RECEIVED_MESSAGE_TS, Sort.DESCENDING) .sort(OlmSessionEntityFields.LAST_RECEIVED_MESSAGE_TS, Sort.DESCENDING)
.findFirst() .findFirst()
}
?.sessionId ?.sessionId
} }
}
override fun getDeviceSessionIds(deviceKey: String): MutableSet<String> { override fun getDeviceSessionIds(deviceKey: String): MutableSet<String> {
return doRealmQueryAndCopyList(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<OlmSessionEntity>() it.where<OlmSessionEntity>()
.equalTo(OlmSessionEntityFields.DEVICE_KEY, deviceKey) .equalTo(OlmSessionEntityFields.DEVICE_KEY, deviceKey)
.findAll() .findAll()
.mapNotNull { sessionEntity ->
sessionEntity.sessionId
} }
.mapNotNull {
it.sessionId
} }
.toMutableSet() .toMutableSet()
} }
@ -616,12 +626,12 @@ internal class RealmCryptoStore @Inject constructor(
// If not in cache (or not found), try to read it from realm // If not in cache (or not found), try to read it from realm
if (inboundGroupSessionToRelease[key] == null) { if (inboundGroupSessionToRelease[key] == null) {
doRealmQueryAndCopy(realmConfiguration) { doWithRealm(realmConfiguration) {
it.where<OlmInboundGroupSessionEntity>() it.where<OlmInboundGroupSessionEntity>()
.equalTo(OlmInboundGroupSessionEntityFields.PRIMARY_KEY, key) .equalTo(OlmInboundGroupSessionEntityFields.PRIMARY_KEY, key)
.findFirst() .findFirst()
}
?.getInboundGroupSession() ?.getInboundGroupSession()
}
?.let { ?.let {
inboundGroupSessionToRelease[key] = it inboundGroupSessionToRelease[key] = it
} }
@ -635,12 +645,12 @@ internal class RealmCryptoStore @Inject constructor(
* so there is no need to use or update `inboundGroupSessionToRelease` for native memory management * so there is no need to use or update `inboundGroupSessionToRelease` for native memory management
*/ */
override fun getInboundGroupSessions(): MutableList<OlmInboundGroupSessionWrapper> { override fun getInboundGroupSessions(): MutableList<OlmInboundGroupSessionWrapper> {
return doRealmQueryAndCopyList(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<OlmInboundGroupSessionEntity>() it.where<OlmInboundGroupSessionEntity>()
.findAll() .findAll()
.mapNotNull { inboundGroupSessionEntity ->
inboundGroupSessionEntity.getInboundGroupSession()
} }
.mapNotNull {
it.getInboundGroupSession()
} }
.toMutableList() .toMutableList()
} }
@ -730,15 +740,16 @@ internal class RealmCryptoStore @Inject constructor(
} }
override fun inboundGroupSessionsToBackup(limit: Int): List<OlmInboundGroupSessionWrapper> { override fun inboundGroupSessionsToBackup(limit: Int): List<OlmInboundGroupSessionWrapper> {
return doRealmQueryAndCopyList(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<OlmInboundGroupSessionEntity>() it.where<OlmInboundGroupSessionEntity>()
.equalTo(OlmInboundGroupSessionEntityFields.BACKED_UP, false) .equalTo(OlmInboundGroupSessionEntityFields.BACKED_UP, false)
.limit(limit.toLong()) .limit(limit.toLong())
.findAll() .findAll()
}.mapNotNull { inboundGroupSession -> .mapNotNull { inboundGroupSession ->
inboundGroupSession.getInboundGroupSession() inboundGroupSession.getInboundGroupSession()
} }
} }
}
override fun inboundGroupSessionsCount(onlyBackedUp: Boolean): Int { override fun inboundGroupSessionsCount(onlyBackedUp: Boolean): Int {
return doWithRealm(realmConfiguration) { return doWithRealm(realmConfiguration) {
@ -760,10 +771,9 @@ internal class RealmCryptoStore @Inject constructor(
} }
override fun getGlobalBlacklistUnverifiedDevices(): Boolean { override fun getGlobalBlacklistUnverifiedDevices(): Boolean {
return doRealmQueryAndCopy(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<CryptoMetadataEntity>().findFirst() it.where<CryptoMetadataEntity>().findFirst()?.globalBlacklistUnverifiedDevices
}?.globalBlacklistUnverifiedDevices } ?: false
?: false
} }
override fun setRoomsListBlacklistUnverifiedDevices(roomIds: List<String>) { override fun setRoomsListBlacklistUnverifiedDevices(roomIds: List<String>) {
@ -786,27 +796,27 @@ internal class RealmCryptoStore @Inject constructor(
} }
override fun getRoomsListBlacklistUnverifiedDevices(): MutableList<String> { override fun getRoomsListBlacklistUnverifiedDevices(): MutableList<String> {
return doRealmQueryAndCopyList(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<CryptoRoomEntity>() it.where<CryptoRoomEntity>()
.equalTo(CryptoRoomEntityFields.BLACKLIST_UNVERIFIED_DEVICES, true) .equalTo(CryptoRoomEntityFields.BLACKLIST_UNVERIFIED_DEVICES, true)
.findAll() .findAll()
.mapNotNull { cryptoRoom ->
cryptoRoom.roomId
} }
.mapNotNull {
it.roomId
} }
.toMutableList() .toMutableList()
} }
override fun getDeviceTrackingStatuses(): MutableMap<String, Int> { override fun getDeviceTrackingStatuses(): MutableMap<String, Int> {
return doRealmQueryAndCopyList(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<UserEntity>() it.where<UserEntity>()
.findAll() .findAll()
.associateBy { user ->
user.userId!!
} }
.associateBy { .mapValues { entry ->
it.userId!! entry.value.deviceTrackingStatus
} }
.mapValues {
it.value.deviceTrackingStatus
} }
.toMutableMap() .toMutableMap()
} }
@ -822,12 +832,12 @@ internal class RealmCryptoStore @Inject constructor(
} }
override fun getDeviceTrackingStatus(userId: String, defaultValue: Int): Int { override fun getDeviceTrackingStatus(userId: String, defaultValue: Int): Int {
return doRealmQueryAndCopy(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<UserEntity>() it.where<UserEntity>()
.equalTo(UserEntityFields.USER_ID, userId) .equalTo(UserEntityFields.USER_ID, userId)
.findFirst() .findFirst()
}
?.deviceTrackingStatus ?.deviceTrackingStatus
}
?: defaultValue ?: defaultValue
} }
@ -1064,24 +1074,25 @@ internal class RealmCryptoStore @Inject constructor(
} }
override fun getIncomingRoomKeyRequest(userId: String, deviceId: String, requestId: String): IncomingRoomKeyRequest? { override fun getIncomingRoomKeyRequest(userId: String, deviceId: String, requestId: String): IncomingRoomKeyRequest? {
return doRealmQueryAndCopyList(realmConfiguration) { realm -> return doWithRealm(realmConfiguration) { realm ->
realm.where<IncomingGossipingRequestEntity>() realm.where<IncomingGossipingRequestEntity>()
.equalTo(IncomingGossipingRequestEntityFields.TYPE_STR, GossipRequestType.KEY.name) .equalTo(IncomingGossipingRequestEntityFields.TYPE_STR, GossipRequestType.KEY.name)
.equalTo(IncomingGossipingRequestEntityFields.OTHER_DEVICE_ID, deviceId) .equalTo(IncomingGossipingRequestEntityFields.OTHER_DEVICE_ID, deviceId)
.equalTo(IncomingGossipingRequestEntityFields.OTHER_USER_ID, userId) .equalTo(IncomingGossipingRequestEntityFields.OTHER_USER_ID, userId)
.findAll() .findAll()
}.mapNotNull { entity -> .mapNotNull { entity ->
entity.toIncomingGossipingRequest() as? IncomingRoomKeyRequest entity.toIncomingGossipingRequest() as? IncomingRoomKeyRequest
}.firstOrNull() }
.firstOrNull()
}
} }
override fun getPendingIncomingRoomKeyRequests(): List<IncomingRoomKeyRequest> { override fun getPendingIncomingRoomKeyRequests(): List<IncomingRoomKeyRequest> {
return doRealmQueryAndCopyList(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<IncomingGossipingRequestEntity>() it.where<IncomingGossipingRequestEntity>()
.equalTo(IncomingGossipingRequestEntityFields.TYPE_STR, GossipRequestType.KEY.name) .equalTo(IncomingGossipingRequestEntityFields.TYPE_STR, GossipRequestType.KEY.name)
.equalTo(IncomingGossipingRequestEntityFields.REQUEST_STATE_STR, GossipingRequestState.PENDING.name) .equalTo(IncomingGossipingRequestEntityFields.REQUEST_STATE_STR, GossipingRequestState.PENDING.name)
.findAll() .findAll()
}
.map { entity -> .map { entity ->
IncomingRoomKeyRequest( IncomingRoomKeyRequest(
userId = entity.otherUserId, userId = entity.otherUserId,
@ -1092,13 +1103,13 @@ internal class RealmCryptoStore @Inject constructor(
) )
} }
} }
}
override fun getPendingIncomingGossipingRequests(): List<IncomingShareRequestCommon> { override fun getPendingIncomingGossipingRequests(): List<IncomingShareRequestCommon> {
return doRealmQueryAndCopyList(realmConfiguration) { return doWithRealm(realmConfiguration) {
it.where<IncomingGossipingRequestEntity>() it.where<IncomingGossipingRequestEntity>()
.equalTo(IncomingGossipingRequestEntityFields.REQUEST_STATE_STR, GossipingRequestState.PENDING.name) .equalTo(IncomingGossipingRequestEntityFields.REQUEST_STATE_STR, GossipingRequestState.PENDING.name)
.findAll() .findAll()
}
.mapNotNull { entity -> .mapNotNull { entity ->
when (entity.type) { when (entity.type) {
GossipRequestType.KEY -> { GossipRequestType.KEY -> {
@ -1122,6 +1133,7 @@ internal class RealmCryptoStore @Inject constructor(
} }
} }
} }
}
override fun storeIncomingGossipingRequest(request: IncomingShareRequestCommon, ageLocalTS: Long?) { override fun storeIncomingGossipingRequest(request: IncomingShareRequestCommon, ageLocalTS: Long?) {
doRealmTransactionAsync(realmConfiguration) { realm -> doRealmTransactionAsync(realmConfiguration) { realm ->