Merge pull request #2264 from vector-im/feature/ons/fix_riotx_crypto_migration

Fix crpyto store migration from RiotX.
This commit is contained in:
Benoit Marty 2020-10-20 22:15:21 +02:00 committed by GitHub
commit f1e545ad49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 21 deletions

View file

@ -59,6 +59,7 @@ Bugfix 🐛:
- Don't set presence when handling a push notification or polling (#2156) - Don't set presence when handling a push notification or polling (#2156)
- Be robust against `StrandHogg` task injection - Be robust against `StrandHogg` task injection
- Clear alerts if user sign out - Clear alerts if user sign out
- Messages encrypted with no way to decrypt after SDK update from 0.18 to 1.0.0 (#2252)
- Fix rows are hidden in Textinput (#2234) - Fix rows are hidden in Textinput (#2234)
- Uploading a file to a room caused it to have a info.size of -1 (#2141) - Uploading a file to a room caused it to have a info.size of -1 (#2141)

View file

@ -43,6 +43,7 @@ import org.matrix.android.sdk.internal.crypto.store.db.model.WithHeldSessionEnti
import org.matrix.android.sdk.internal.di.SerializeNulls import org.matrix.android.sdk.internal.di.SerializeNulls
import io.realm.DynamicRealm import io.realm.DynamicRealm
import io.realm.RealmMigration import io.realm.RealmMigration
import io.realm.RealmObjectSchema
import org.matrix.androidsdk.crypto.data.MXOlmInboundGroupSession2 import org.matrix.androidsdk.crypto.data.MXOlmInboundGroupSession2
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
@ -57,6 +58,27 @@ internal class RealmCryptoStoreMigration @Inject constructor(private val crossSi
const val CRYPTO_STORE_SCHEMA_VERSION = 11L const val CRYPTO_STORE_SCHEMA_VERSION = 11L
} }
private fun RealmObjectSchema.addFieldIfNotExists(fieldName: String, fieldType: Class<*>): RealmObjectSchema {
if (!hasField(fieldName)) {
addField(fieldName, fieldType)
}
return this
}
private fun RealmObjectSchema.removeFieldIfExists(fieldName: String): RealmObjectSchema {
if (hasField(fieldName)) {
removeField(fieldName)
}
return this
}
private fun RealmObjectSchema.setRequiredIfNotAlready(fieldName: String, isRequired: Boolean): RealmObjectSchema {
if (isRequired != isRequired(fieldName)) {
setRequired(fieldName, isRequired)
}
return this
}
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) { override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
Timber.v("Migrating Realm Crypto from $oldVersion to $newVersion") Timber.v("Migrating Realm Crypto from $oldVersion to $newVersion")
@ -89,13 +111,13 @@ internal class RealmCryptoStoreMigration @Inject constructor(private val crossSi
Timber.d("Update IncomingRoomKeyRequestEntity format: requestBodyString field is exploded into several fields") Timber.d("Update IncomingRoomKeyRequestEntity format: requestBodyString field is exploded into several fields")
realm.schema.get("IncomingRoomKeyRequestEntity") realm.schema.get("IncomingRoomKeyRequestEntity")
?.addField("requestBodyAlgorithm", String::class.java) ?.addFieldIfNotExists("requestBodyAlgorithm", String::class.java)
?.addField("requestBodyRoomId", String::class.java) ?.addFieldIfNotExists("requestBodyRoomId", String::class.java)
?.addField("requestBodySenderKey", String::class.java) ?.addFieldIfNotExists("requestBodySenderKey", String::class.java)
?.addField("requestBodySessionId", String::class.java) ?.addFieldIfNotExists("requestBodySessionId", String::class.java)
?.transform { dynamicObject -> ?.transform { dynamicObject ->
val requestBodyString = dynamicObject.getString("requestBodyString")
try { try {
val requestBodyString = dynamicObject.getString("requestBodyString")
// It was a map before // It was a map before
val map: Map<String, String>? = deserializeFromRealm(requestBodyString) val map: Map<String, String>? = deserializeFromRealm(requestBodyString)
@ -109,18 +131,18 @@ internal class RealmCryptoStoreMigration @Inject constructor(private val crossSi
Timber.e(e, "Error") Timber.e(e, "Error")
} }
} }
?.removeField("requestBodyString") ?.removeFieldIfExists("requestBodyString")
Timber.d("Update IncomingRoomKeyRequestEntity format: requestBodyString field is exploded into several fields") Timber.d("Update IncomingRoomKeyRequestEntity format: requestBodyString field is exploded into several fields")
realm.schema.get("OutgoingRoomKeyRequestEntity") realm.schema.get("OutgoingRoomKeyRequestEntity")
?.addField("requestBodyAlgorithm", String::class.java) ?.addFieldIfNotExists("requestBodyAlgorithm", String::class.java)
?.addField("requestBodyRoomId", String::class.java) ?.addFieldIfNotExists("requestBodyRoomId", String::class.java)
?.addField("requestBodySenderKey", String::class.java) ?.addFieldIfNotExists("requestBodySenderKey", String::class.java)
?.addField("requestBodySessionId", String::class.java) ?.addFieldIfNotExists("requestBodySessionId", String::class.java)
?.transform { dynamicObject -> ?.transform { dynamicObject ->
val requestBodyString = dynamicObject.getString("requestBodyString")
try { try {
val requestBodyString = dynamicObject.getString("requestBodyString")
// It was a map before // It was a map before
val map: Map<String, String>? = deserializeFromRealm(requestBodyString) val map: Map<String, String>? = deserializeFromRealm(requestBodyString)
@ -134,10 +156,11 @@ internal class RealmCryptoStoreMigration @Inject constructor(private val crossSi
Timber.e(e, "Error") Timber.e(e, "Error")
} }
} }
?.removeField("requestBodyString") ?.removeFieldIfExists("requestBodyString")
Timber.d("Create KeysBackupDataEntity") Timber.d("Create KeysBackupDataEntity")
if (!realm.schema.contains("KeysBackupDataEntity")) {
realm.schema.create("KeysBackupDataEntity") realm.schema.create("KeysBackupDataEntity")
.addField(KeysBackupDataEntityFields.PRIMARY_KEY, Integer::class.java) .addField(KeysBackupDataEntityFields.PRIMARY_KEY, Integer::class.java)
.addPrimaryKey(KeysBackupDataEntityFields.PRIMARY_KEY) .addPrimaryKey(KeysBackupDataEntityFields.PRIMARY_KEY)
@ -145,14 +168,15 @@ internal class RealmCryptoStoreMigration @Inject constructor(private val crossSi
.addField(KeysBackupDataEntityFields.BACKUP_LAST_SERVER_HASH, String::class.java) .addField(KeysBackupDataEntityFields.BACKUP_LAST_SERVER_HASH, String::class.java)
.addField(KeysBackupDataEntityFields.BACKUP_LAST_SERVER_NUMBER_OF_KEYS, Integer::class.java) .addField(KeysBackupDataEntityFields.BACKUP_LAST_SERVER_NUMBER_OF_KEYS, Integer::class.java)
} }
}
private fun migrateTo3RiotX(realm: DynamicRealm) { private fun migrateTo3RiotX(realm: DynamicRealm) {
Timber.d("Step 2 -> 3") Timber.d("Step 2 -> 3")
Timber.d("Migrate to RiotX model") Timber.d("Migrate to RiotX model")
realm.schema.get("CryptoRoomEntity") realm.schema.get("CryptoRoomEntity")
?.addField(CryptoRoomEntityFields.SHOULD_ENCRYPT_FOR_INVITED_MEMBERS, Boolean::class.java) ?.addFieldIfNotExists(CryptoRoomEntityFields.SHOULD_ENCRYPT_FOR_INVITED_MEMBERS, Boolean::class.java)
?.setRequired(CryptoRoomEntityFields.SHOULD_ENCRYPT_FOR_INVITED_MEMBERS, false) ?.setRequiredIfNotAlready(CryptoRoomEntityFields.SHOULD_ENCRYPT_FOR_INVITED_MEMBERS, false)
// Convert format of MXDeviceInfo, package has to be the same. // Convert format of MXDeviceInfo, package has to be the same.
realm.schema.get("DeviceInfoEntity") realm.schema.get("DeviceInfoEntity")
@ -204,8 +228,13 @@ internal class RealmCryptoStoreMigration @Inject constructor(private val crossSi
// Version 4L added Cross Signing info persistence // Version 4L added Cross Signing info persistence
private fun migrateTo4(realm: DynamicRealm) { private fun migrateTo4(realm: DynamicRealm) {
Timber.d("Step 3 -> 4") Timber.d("Step 3 -> 4")
Timber.d("Create KeyInfoEntity")
if (realm.schema.contains("TrustLevelEntity")) {
Timber.d("Skipping Step 3 -> 4 because entities already exist")
return
}
Timber.d("Create KeyInfoEntity")
val trustLevelEntityEntitySchema = realm.schema.create("TrustLevelEntity") val trustLevelEntityEntitySchema = realm.schema.create("TrustLevelEntity")
.addField(TrustLevelEntityFields.CROSS_SIGNED_VERIFIED, Boolean::class.java) .addField(TrustLevelEntityFields.CROSS_SIGNED_VERIFIED, Boolean::class.java)
.setNullable(TrustLevelEntityFields.CROSS_SIGNED_VERIFIED, true) .setNullable(TrustLevelEntityFields.CROSS_SIGNED_VERIFIED, true)