Skip to content
Snippets Groups Projects
Commit 62328514 authored by Koen's avatar Koen
Browse files

Moved to upgradeOldSecrets.

parent faaa7a6d
No related branches found
No related tags found
No related merge requests found
......@@ -19,44 +19,14 @@ class SqlLiteDbHelper : SQLiteOpenHelper {
Log.w(TAG, "This is version 1, no DB to update")
var currentVersion = oldVersion
if (oldVersion == 1 && newVersion == 2) {
if (oldVersion <= 1 && newVersion == 2) {
deleteTables(db)
createTables(db)
currentVersion = 2
}
if (currentVersion == 2 && newVersion == 3) {
val cursor = db.rawQuery("SELECT value FROM process_secrets", arrayOf())
val secrets = arrayListOf<ProcessSecret>()
cursor.use {
if (it.moveToFirst()) {
do {
val value = it.getBlob(0)
if (value != null) {
try {
val secret = ProcessSecret.fromProto(Protocol.StorageTypeProcessSecret.parseFrom(PEncryptionProviderV0.instance.decrypt(value)))
secrets.add(secret)
} catch (e: Throwable) {
Log.i(TAG, "Failed to convert secret, assuming it is V1 already and skipping", e)
}
}
} while (cursor.moveToNext())
}
}
db.beginTransaction();
try {
for (secret in secrets) {
db.execSQL("DELETE FROM process_secrets WHERE public_key = ?", arrayOf(secret.system.publicKey.key.toBase64()))
val value = PEncryptionProviderV1.instance.encrypt(secret.toProto().toByteArray())
db.execSQL("INSERT INTO process_secrets VALUES(?, ?)", arrayOf(secret.system.publicKey.key.toBase64(), value))
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
upgradeOldSecrets(db)
currentVersion = 3
}
......@@ -86,6 +56,40 @@ class SqlLiteDbHelper : SQLiteOpenHelper {
}
}
fun upgradeOldSecrets(db: SQLiteDatabase) {
val cursor = db.rawQuery("SELECT value FROM process_secrets", arrayOf())
val secrets = arrayListOf<ProcessSecret>()
cursor.use {
if (it.moveToFirst()) {
do {
val value = it.getBlob(0)
if (value != null) {
try {
val secret = ProcessSecret.fromProto(Protocol.StorageTypeProcessSecret.parseFrom(PEncryptionProviderV0.instance.decrypt(value)))
Log.i(TAG, "Upgrading old secret system = ${secret.system.publicKey.key.toBase64()}")
secrets.add(secret)
} catch (e: Throwable) {
Log.i(TAG, "Failed to convert secret, assuming it is V1 already and skipping", e)
}
}
} while (cursor.moveToNext())
}
}
db.beginTransaction();
try {
for (secret in secrets) {
db.execSQL("DELETE FROM process_secrets WHERE public_key = ?", arrayOf(secret.system.publicKey.key.toBase64()))
val value = PEncryptionProviderV1.instance.encrypt(secret.toProto().toByteArray())
db.execSQL("INSERT INTO process_secrets VALUES(?, ?)", arrayOf(secret.system.publicKey.key.toBase64(), value))
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
private fun createTables(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE system_states (public_key VARCHAR PRIMARY KEY, value BLOB)")
db.execSQL("CREATE TABLE process_states (public_key_process VARCHAR PRIMARY KEY, value BLOB)")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment