-
-
Notifications
You must be signed in to change notification settings - Fork 45
Cleaned up local DB passphrase code #3354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4486bd2
34bbc35
becb0fc
70c859c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,18 +7,14 @@ | |
| import org.commcare.util.Base64; | ||
| import org.commcare.util.Base64DecoderException; | ||
| import org.commcare.util.EncryptionUtils; | ||
| import org.commcare.utils.CrashUtil; | ||
| import org.commcare.utils.EncryptionKeyAndTransform; | ||
| import org.commcare.utils.EncryptionKeyProvider; | ||
| import org.javarosa.core.services.Logger; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.Vector; | ||
|
|
||
| public class ConnectDatabaseUtils { | ||
| // the value of the key should not be renamed due to backward compatibility | ||
| private static final String SECRET_NAME = "secret"; | ||
| public static void storeConnectDbPassphrase(@NotNull Context context, byte[] passphrase, boolean isLocal) { | ||
| public static void storeConnectDbPassphrase(@NotNull Context context, byte[] passphrase) { | ||
| try { | ||
| if (passphrase == null || passphrase.length == 0) { | ||
| throw new IllegalArgumentException("Passphrase must not be null or empty"); | ||
|
|
@@ -29,9 +25,9 @@ public static void storeConnectDbPassphrase(@NotNull Context context, byte[] pas | |
| String encoded = EncryptionUtils.encrypt(passphrase, keyAndTransform.getKey(), | ||
| keyAndTransform.getTransformation(), true); | ||
|
|
||
| ConnectKeyRecord record = getKeyRecord(isLocal); | ||
| ConnectKeyRecord record = getKeyRecord(); | ||
| if (record == null) { | ||
| record = new ConnectKeyRecord(encoded, isLocal); | ||
| record = new ConnectKeyRecord(encoded); | ||
| } else { | ||
| record.setEncryptedPassphrase(encoded); | ||
| } | ||
|
|
@@ -42,39 +38,28 @@ record = new ConnectKeyRecord(encoded, isLocal); | |
| } | ||
| } | ||
|
|
||
| public static ConnectKeyRecord getKeyRecord(boolean local) { | ||
| Vector<ConnectKeyRecord> records = CommCareApplication.instance() | ||
| .getGlobalStorage(ConnectKeyRecord.class) | ||
| .getRecordsForValue(ConnectKeyRecord.IS_LOCAL, local); | ||
| public static ConnectKeyRecord getKeyRecord() { | ||
| Iterable<ConnectKeyRecord> records = CommCareApplication.instance() | ||
| .getGlobalStorage(ConnectKeyRecord.class); | ||
|
|
||
| return records.size() > 0 ? records.firstElement() : null; | ||
| if (records.iterator().hasNext()) { | ||
| return records.iterator().next(); | ||
| } | ||
|
Comment on lines
+45
to
+47
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @OrangeAndGreen I was thinking that app should ask for local specifically to check that all users are migrated?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I worried the same but tested and the false case is working fine through an app upgrade |
||
| return null; | ||
| } | ||
|
|
||
| public static void storeConnectDbPassphrase(Context context, String base64EncodedPassphrase, boolean isLocal) { | ||
| public static void storeConnectDbPassphrase(Context context, String base64EncodedPassphrase) { | ||
| try { | ||
| byte[] bytes = Base64.decode(base64EncodedPassphrase); | ||
| storeConnectDbPassphrase(context, bytes, isLocal); | ||
| storeConnectDbPassphrase(context, bytes); | ||
| } catch (Base64DecoderException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| public static String getConnectDbEncodedPassphrase(Context context, boolean isLocal) { | ||
| try { | ||
| byte[] passBytes = getConnectDbPassphrase(context, isLocal); | ||
| if (passBytes != null) { | ||
| return Base64.encode(passBytes); | ||
| } | ||
| } catch (Exception e) { | ||
| Logger.exception("Getting DB passphrase", e); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| public static byte[] getConnectDbPassphrase(Context context, boolean isLocal) { | ||
| public static byte[] getConnectDbPassphrase(Context context) { | ||
| try { | ||
| ConnectKeyRecord record = ConnectDatabaseUtils.getKeyRecord(isLocal); | ||
| ConnectKeyRecord record = ConnectDatabaseUtils.getKeyRecord(); | ||
| if (record == null) { | ||
| return null; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we also clear out the
rekeyDBfromDatabaseConnectOpenHelperor is it still used elsewhere ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, missed that one! becb0fc