From 68209a8dbc87fa398adc2ab9726480249b94b7fc Mon Sep 17 00:00:00 2001 From: Dave Viggiano Date: Tue, 25 Jul 2023 23:18:54 -0400 Subject: [PATCH 01/11] BROKEN: Attempting to use getRecordsForValues to query app_data records. Added Registering state to ConnectIDManager for when user is mid-registration. Fixed some issues raised in PR. --- .../android/database/global/models/ConnectKeyRecord.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java b/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java index 62abf9a0aa..f0134e832a 100644 --- a/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java +++ b/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java @@ -7,18 +7,14 @@ @Table(ConnectKeyRecord.STORAGE_KEY) public class ConnectKeyRecord extends Persisted { public static final String STORAGE_KEY = "connect_key"; - - @Persisting(1) - String userId; @Persisting(2) String encryptedPassphrase; public ConnectKeyRecord() { } - public ConnectKeyRecord(String userId, String encryptedPassphrase) { - this.userId = userId; + + public ConnectKeyRecord(String encryptedPassphrase) { this.encryptedPassphrase = encryptedPassphrase; } - public String getUserID() { return userId; } public String getEncryptedPassphrase() { return encryptedPassphrase; } } From 9129462f72a43e52eade8637377a485920e19aa1 Mon Sep 17 00:00:00 2001 From: Dave Viggiano Date: Mon, 14 Aug 2023 11:17:14 -0400 Subject: [PATCH 02/11] Changes to address PR comments. Moved SSO network calls to a dedicated ConnectIDSSOHelper class. Converted ConnectIDNetworkHelper to a singleton. A couple other small changes. --- .../android/database/global/models/ConnectKeyRecord.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java b/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java index f0134e832a..b943c4ca66 100644 --- a/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java +++ b/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java @@ -7,7 +7,7 @@ @Table(ConnectKeyRecord.STORAGE_KEY) public class ConnectKeyRecord extends Persisted { public static final String STORAGE_KEY = "connect_key"; - @Persisting(2) + @Persisting(1) String encryptedPassphrase; public ConnectKeyRecord() { } From 105f82e717d7656cfc33929089c9397fcab76cec Mon Sep 17 00:00:00 2001 From: Dave Viggiano Date: Wed, 23 Aug 2023 15:55:36 -0400 Subject: [PATCH 03/11] Addressing more lint warnings, mostly code formatting and adding Javadoc comments --- .../database/global/models/ConnectKeyRecord.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java b/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java index b943c4ca66..12c64df8a4 100644 --- a/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java +++ b/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java @@ -4,17 +4,23 @@ import org.commcare.models.framework.Persisting; import org.commcare.modern.database.Table; +/** + * @author dviggiano + */ @Table(ConnectKeyRecord.STORAGE_KEY) public class ConnectKeyRecord extends Persisted { public static final String STORAGE_KEY = "connect_key"; @Persisting(1) String encryptedPassphrase; - public ConnectKeyRecord() { } + public ConnectKeyRecord() { + } public ConnectKeyRecord(String encryptedPassphrase) { this.encryptedPassphrase = encryptedPassphrase; } - public String getEncryptedPassphrase() { return encryptedPassphrase; } + public String getEncryptedPassphrase() { + return encryptedPassphrase; + } } From fa1aeed92314cf0a2216411a6e91126db3cad5c3 Mon Sep 17 00:00:00 2001 From: Dave Viggiano Date: Wed, 23 Aug 2023 16:50:49 -0400 Subject: [PATCH 04/11] Added javadoc descriptions --- .../android/database/global/models/ConnectKeyRecord.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java b/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java index 12c64df8a4..8263eb5767 100644 --- a/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java +++ b/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java @@ -6,6 +6,7 @@ /** * @author dviggiano + * DB model for storing the encrypted/encoded Connect DB passphrase */ @Table(ConnectKeyRecord.STORAGE_KEY) public class ConnectKeyRecord extends Persisted { From e3448bdeacd4a69a86bb205cc0f87bb4a52911d2 Mon Sep 17 00:00:00 2001 From: Dave Viggiano Date: Thu, 24 Aug 2023 09:35:41 -0400 Subject: [PATCH 05/11] Cleaning up more lint warnings. --- .../android/database/global/models/ConnectKeyRecord.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java b/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java index 8263eb5767..df0ec4a277 100644 --- a/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java +++ b/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java @@ -5,8 +5,9 @@ import org.commcare.modern.database.Table; /** - * @author dviggiano * DB model for storing the encrypted/encoded Connect DB passphrase + * + * @author dviggiano */ @Table(ConnectKeyRecord.STORAGE_KEY) public class ConnectKeyRecord extends Persisted { From d5c8c856673cd2e280a1c4d6919a69fe0cc13ae0 Mon Sep 17 00:00:00 2001 From: Dave Viggiano Date: Thu, 24 Aug 2023 11:17:03 -0400 Subject: [PATCH 06/11] Addressing more lint warnings. --- .../database/global/DatabaseGlobalOpenHelper.java | 3 --- .../database/global/GlobalDatabaseUpgrader.java | 11 ++++++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/app/src/org/commcare/models/database/global/DatabaseGlobalOpenHelper.java b/app/src/org/commcare/models/database/global/DatabaseGlobalOpenHelper.java index 18d7294254..6c37415cd7 100755 --- a/app/src/org/commcare/models/database/global/DatabaseGlobalOpenHelper.java +++ b/app/src/org/commcare/models/database/global/DatabaseGlobalOpenHelper.java @@ -1,6 +1,3 @@ -/** - * - */ package org.commcare.models.database.global; import android.content.Context; diff --git a/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java b/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java index b82ae40fd3..09634d2c1f 100755 --- a/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java +++ b/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java @@ -2,23 +2,24 @@ import android.content.Context; +import java.io.File; + import net.sqlcipher.database.SQLiteDatabase; import org.commcare.CommCareApplication; import org.commcare.android.database.global.models.AppAvailableToInstall; +import org.commcare.android.database.global.models.ApplicationRecord; +import org.commcare.android.database.global.models.ApplicationRecordV1; import org.commcare.android.database.global.models.ConnectKeyRecord; import org.commcare.android.logging.ForceCloseLogEntry; -import org.commcare.modern.database.TableBuilder; import org.commcare.models.database.ConcreteAndroidDbHelper; import org.commcare.models.database.DbUtil; import org.commcare.models.database.MigrationException; import org.commcare.models.database.SqlStorage; -import org.commcare.android.database.global.models.ApplicationRecord; -import org.commcare.android.database.global.models.ApplicationRecordV1; +import org.commcare.modern.database.TableBuilder; import org.commcare.provider.ProviderUtils; -import org.javarosa.core.services.storage.Persistable; -import java.io.File; +import org.javarosa.core.services.storage.Persistable; /** * @author ctsims From afcda3e37642043efc2f6ff80a91b4703bf91996 Mon Sep 17 00:00:00 2001 From: Dave Viggiano Date: Mon, 17 Jun 2024 13:35:08 -0400 Subject: [PATCH 07/11] Added is_local column to ConnectKeyRecord table in global DB. Upgraded global DB from V6 to V7. --- .../global/models/ConnectKeyRecord.java | 18 +++++++- .../global/models/ConnectKeyRecordV6.java | 29 ++++++++++++ .../global/DatabaseGlobalOpenHelper.java | 3 +- .../global/GlobalDatabaseUpgrader.java | 44 +++++++++++++++++++ 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 app/src/org/commcare/android/database/global/models/ConnectKeyRecordV6.java diff --git a/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java b/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java index df0ec4a277..d6cea2909f 100644 --- a/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java +++ b/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java @@ -3,6 +3,9 @@ import org.commcare.android.storage.framework.Persisted; import org.commcare.models.framework.Persisting; import org.commcare.modern.database.Table; +import org.commcare.modern.models.MetaField; + +import kotlin.Metadata; /** * DB model for storing the encrypted/encoded Connect DB passphrase @@ -12,17 +15,30 @@ @Table(ConnectKeyRecord.STORAGE_KEY) public class ConnectKeyRecord extends Persisted { public static final String STORAGE_KEY = "connect_key"; + public static final String IS_LOCAL = "is_local"; @Persisting(1) String encryptedPassphrase; + @Persisting(2) + @MetaField(IS_LOCAL) + boolean isLocal; + public ConnectKeyRecord() { } - public ConnectKeyRecord(String encryptedPassphrase) { + public ConnectKeyRecord(String encryptedPassphrase, boolean isLocal) { this.encryptedPassphrase = encryptedPassphrase; + this.isLocal = isLocal; } public String getEncryptedPassphrase() { return encryptedPassphrase; } + public boolean getIsLocal() { + return isLocal; + } + + public static ConnectKeyRecord fromV6(ConnectKeyRecordV6 oldVersion) { + return new ConnectKeyRecord(oldVersion.getEncryptedPassphrase(), true); + } } diff --git a/app/src/org/commcare/android/database/global/models/ConnectKeyRecordV6.java b/app/src/org/commcare/android/database/global/models/ConnectKeyRecordV6.java new file mode 100644 index 0000000000..7c96770460 --- /dev/null +++ b/app/src/org/commcare/android/database/global/models/ConnectKeyRecordV6.java @@ -0,0 +1,29 @@ +package org.commcare.android.database.global.models; + +import org.commcare.android.storage.framework.Persisted; +import org.commcare.models.framework.Persisting; +import org.commcare.modern.database.Table; + +/** + * DB model for storing the encrypted/encoded Connect DB passphrase + * Used up to global DB V6 + * + * @author dviggiano + */ +@Table(ConnectKeyRecordV6.STORAGE_KEY) +public class ConnectKeyRecordV6 extends Persisted { + public static final String STORAGE_KEY = "connect_key"; + @Persisting(1) + String encryptedPassphrase; + + public ConnectKeyRecordV6() { + } + + public ConnectKeyRecordV6(String encryptedPassphrase) { + this.encryptedPassphrase = encryptedPassphrase; + } + + public String getEncryptedPassphrase() { + return encryptedPassphrase; + } +} diff --git a/app/src/org/commcare/models/database/global/DatabaseGlobalOpenHelper.java b/app/src/org/commcare/models/database/global/DatabaseGlobalOpenHelper.java index 6c37415cd7..615a3a0be6 100755 --- a/app/src/org/commcare/models/database/global/DatabaseGlobalOpenHelper.java +++ b/app/src/org/commcare/models/database/global/DatabaseGlobalOpenHelper.java @@ -31,8 +31,9 @@ public class DatabaseGlobalOpenHelper extends SQLiteOpenHelper { * V.4 - Add table for storing force close log entries that occur outside of an active session * V.5 - Add table for storing apps available for install * V.6 - Add table for storing (encrypted) passphrase for ConnectId DB + * V.7 - Add is_local column to ConnectKeyRecord table (to store both local and server passphrase) */ - private static final int GLOBAL_DB_VERSION = 6; + private static final int GLOBAL_DB_VERSION = 7; private static final String GLOBAL_DB_LOCATOR = "database_global"; diff --git a/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java b/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java index 09634d2c1f..e4543382ee 100755 --- a/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java +++ b/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java @@ -11,6 +11,7 @@ import org.commcare.android.database.global.models.ApplicationRecord; import org.commcare.android.database.global.models.ApplicationRecordV1; import org.commcare.android.database.global.models.ConnectKeyRecord; +import org.commcare.android.database.global.models.ConnectKeyRecordV6; import org.commcare.android.logging.ForceCloseLogEntry; import org.commcare.models.database.ConcreteAndroidDbHelper; import org.commcare.models.database.DbUtil; @@ -57,6 +58,12 @@ public void upgrade(SQLiteDatabase db, int oldVersion, int newVersion) { oldVersion = 6; } } + + if (oldVersion == 6) { + if (upgradeSixSeven(db)) { + oldVersion = 7; + } + } } private boolean upgradeOneTwo(SQLiteDatabase db, int oldVersion, int newVersion) { @@ -128,6 +135,43 @@ private boolean upgradeFiveSix(SQLiteDatabase db) { return addTableForNewModel(db, ConnectKeyRecord.STORAGE_KEY, new ConnectKeyRecord()); } + private boolean upgradeSixSeven(SQLiteDatabase db) { + db.beginTransaction(); + + //Migrate the old ConnectKeyRecord in storage to the new version including isLocal + try { + db.execSQL(DbUtil.addColumnToTable( + ConnectKeyRecord.STORAGE_KEY, + ConnectKeyRecord.IS_LOCAL, + "TEXT")); + + SqlStorage oldStorage = new SqlStorage<>( + ConnectKeyRecordV6.STORAGE_KEY, + ConnectKeyRecordV6.class, + new ConcreteAndroidDbHelper(c, db)); + + SqlStorage newStorage = new SqlStorage<>( + ConnectKeyRecord.STORAGE_KEY, + ConnectKeyRecord.class, + new ConcreteAndroidDbHelper(c, db)); + + for (Persistable r : oldStorage) { + ConnectKeyRecordV6 oldRecord = (ConnectKeyRecordV6)r; + ConnectKeyRecord newRecord = ConnectKeyRecord.fromV6(oldRecord); + //set this new record to have same ID as the old one + newRecord.setID(oldRecord.getID()); + newStorage.write(newRecord); + } + + db.setTransactionSuccessful(); + return true; + } catch(Exception e) { + return false; + } finally { + db.endTransaction(); + } + } + private static boolean addTableForNewModel(SQLiteDatabase db, String storageKey, Persistable modelToAdd) { db.beginTransaction(); From d7e4527115ecdd81ef245f8ee2b039927a22c843 Mon Sep 17 00:00:00 2001 From: Dave Viggiano Date: Tue, 18 Jun 2024 15:09:22 -0400 Subject: [PATCH 08/11] Simplified some of the methods around getting DB passphrases and handle. --- .../android/database/global/models/ConnectKeyRecord.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java b/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java index d6cea2909f..010faed81d 100644 --- a/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java +++ b/app/src/org/commcare/android/database/global/models/ConnectKeyRecord.java @@ -34,6 +34,9 @@ public ConnectKeyRecord(String encryptedPassphrase, boolean isLocal) { public String getEncryptedPassphrase() { return encryptedPassphrase; } + public void setEncryptedPassphrase(String passphrase) { + encryptedPassphrase = passphrase; + } public boolean getIsLocal() { return isLocal; } From 7605223908ff836ec54ca0e28cc94ddebc0cea0d Mon Sep 17 00:00:00 2001 From: Dave Viggiano Date: Tue, 6 Aug 2024 12:18:44 -0400 Subject: [PATCH 09/11] Bug fix: Handling upgrade path properly for adding connect_key table and then new column. --- .../commcare/models/database/global/GlobalDatabaseUpgrader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java b/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java index e4543382ee..8144032d61 100755 --- a/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java +++ b/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java @@ -132,7 +132,7 @@ private boolean upgradeFourFive(SQLiteDatabase db) { } private boolean upgradeFiveSix(SQLiteDatabase db) { - return addTableForNewModel(db, ConnectKeyRecord.STORAGE_KEY, new ConnectKeyRecord()); + return addTableForNewModel(db, ConnectKeyRecord.STORAGE_KEY, new ConnectKeyRecordV6()); } private boolean upgradeSixSeven(SQLiteDatabase db) { From c58fb3c15bd5023e4f75568270fd160d67897eb0 Mon Sep 17 00:00:00 2001 From: Dave Viggiano Date: Wed, 26 Jun 2024 15:21:34 -0400 Subject: [PATCH 10/11] Added missing externalizable to test. --- .../org/commcare/android/tests/processing/FormStorageTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/unit-tests/src/org/commcare/android/tests/processing/FormStorageTest.java b/app/unit-tests/src/org/commcare/android/tests/processing/FormStorageTest.java index 8038209f49..3283f13401 100644 --- a/app/unit-tests/src/org/commcare/android/tests/processing/FormStorageTest.java +++ b/app/unit-tests/src/org/commcare/android/tests/processing/FormStorageTest.java @@ -356,6 +356,7 @@ public class FormStorageTest { , "org.commcare.suite.model.EndpointAction" , "org.commcare.suite.model.QueryGroup" , "org.commcare.android.database.global.models.ConnectKeyRecord" + , "org.commcare.android.database.global.models.ConnectKeyRecordV6" ); From d1a1832008a6b84bca98edebdd64fa19e7ccdf38 Mon Sep 17 00:00:00 2001 From: Ahmad Vazirna Date: Thu, 22 Aug 2024 00:10:10 +0200 Subject: [PATCH 11/11] Allow schema update related exceptions to bubble up --- .../models/database/global/GlobalDatabaseUpgrader.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java b/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java index 8144032d61..f9f57166f6 100755 --- a/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java +++ b/app/src/org/commcare/models/database/global/GlobalDatabaseUpgrader.java @@ -165,8 +165,6 @@ private boolean upgradeSixSeven(SQLiteDatabase db) { db.setTransactionSuccessful(); return true; - } catch(Exception e) { - return false; } finally { db.endTransaction(); } @@ -182,8 +180,6 @@ private static boolean addTableForNewModel(SQLiteDatabase db, String storageKey, db.setTransactionSuccessful(); return true; - } catch (Exception e) { - return false; } finally { db.endTransaction(); }