extract UpdatedKeys access from KeychainProvider into KeyMetadataDao

This commit is contained in:
Vincent Breitmoser
2018-06-18 11:40:39 +02:00
parent d133b732e5
commit bae90f1b23
24 changed files with 245 additions and 414 deletions

View File

@@ -37,7 +37,6 @@ import org.sufficientlysecure.keychain.model.ApiApp;
public class ApiDataAccessObject {
private final SupportSQLiteDatabase db;
public ApiDataAccessObject(Context context) {

View File

@@ -35,7 +35,7 @@ public class DatabaseNotifyManager {
contentResolver.notifyChange(uri, null);
}
public void notifyKeyserverStatusChange(long masterKeyId) {
public void notifyKeyMetadataChange(long masterKeyId) {
Uri uri = KeyRings.buildGenericKeyRingUri(masterKeyId);
contentResolver.notifyChange(uri, null);
}

View File

@@ -0,0 +1,69 @@
package org.sufficientlysecure.keychain.provider;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import android.arch.persistence.db.SupportSQLiteDatabase;
import android.content.Context;
import android.database.Cursor;
import com.squareup.sqldelight.SqlDelightQuery;
import org.sufficientlysecure.keychain.KeyMetadataModel.ReplaceKeyMetadata;
import org.sufficientlysecure.keychain.model.KeyMetadata;
public class KeyMetadataDao {
private final SupportSQLiteDatabase db;
private DatabaseNotifyManager databaseNotifyManager;
public static KeyMetadataDao create(Context context) {
SupportSQLiteDatabase supportSQLiteDatabase = new KeychainDatabase(context).getWritableDatabase();
DatabaseNotifyManager databaseNotifyManager = DatabaseNotifyManager.create(context);
return new KeyMetadataDao(supportSQLiteDatabase, databaseNotifyManager);
}
private KeyMetadataDao(SupportSQLiteDatabase supportSQLiteDatabase, DatabaseNotifyManager databaseNotifyManager) {
this.db = supportSQLiteDatabase;
this.databaseNotifyManager = databaseNotifyManager;
}
public KeyMetadata getKeyMetadata(long masterKeyId) {
SqlDelightQuery query = KeyMetadata.FACTORY.selectByMasterKeyId(masterKeyId);
try (Cursor cursor = db.query(query)) {
if (cursor.moveToFirst()) {
return KeyMetadata.FACTORY.selectByMasterKeyIdMapper().map(cursor);
}
}
return null;
}
public void resetAllLastUpdatedTimes() {
new KeyMetadata.DeleteAllLastUpdatedTimes(db).execute();
}
public void renewKeyLastUpdatedTime(long masterKeyId, boolean seenOnKeyservers) {
ReplaceKeyMetadata replaceStatement = new ReplaceKeyMetadata(db, KeyMetadata.FACTORY);
replaceStatement.bind(masterKeyId, new Date(), seenOnKeyservers);
replaceStatement.executeInsert();
databaseNotifyManager.notifyKeyMetadataChange(masterKeyId);
}
public List<byte[]> getFingerprintsForKeysOlderThan(long olderThan, TimeUnit timeUnit) {
SqlDelightQuery query = KeyMetadata.FACTORY.selectFingerprintsForKeysOlderThan(new Date(timeUnit.toMillis(olderThan)));
List<byte[]> fingerprintList = new ArrayList<>();
try (Cursor cursor = db.query(query)) {
while (cursor.moveToNext()) {
byte[] fingerprint = KeyMetadata.FACTORY.selectFingerprintsForKeysOlderThanMapper().map(cursor);
fingerprintList.add(fingerprint);
}
}
return fingerprintList;
}
}

View File

@@ -27,7 +27,6 @@ import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.Nullable;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
@@ -39,9 +38,7 @@ import org.sufficientlysecure.keychain.pgp.exception.PgpKeyNotFoundException;
import org.sufficientlysecure.keychain.provider.KeychainContract.Certs;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRingData;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.KeychainContract.UpdatedKeys;
import org.sufficientlysecure.keychain.provider.KeychainContract.UserPackets;
import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables;
import timber.log.Timber;
@@ -284,31 +281,6 @@ public class KeyRepository {
return contentResolver;
}
@Nullable
Long getLastUpdateTime(long masterKeyId) {
Cursor lastUpdatedCursor = contentResolver.query(
UpdatedKeys.CONTENT_URI,
new String[] { UpdatedKeys.LAST_UPDATED },
Tables.UPDATED_KEYS + "." + UpdatedKeys.MASTER_KEY_ID + " = ?",
new String[] { "" + masterKeyId },
null
);
if (lastUpdatedCursor == null) {
return null;
}
Long lastUpdateTime;
try {
if (!lastUpdatedCursor.moveToNext()) {
return null;
}
lastUpdateTime = lastUpdatedCursor.getLong(0);
} finally {
lastUpdatedCursor.close();
}
return lastUpdateTime;
}
public final byte[] loadPublicKeyRingData(long masterKeyId) throws NotFoundException {
byte[] data = (byte[]) getGenericDataOrNull(KeyRingData.buildPublicKeyRingUri(masterKeyId),
KeyRingData.KEY_RING_DATA, FIELD_TYPE_BLOB);

View File

@@ -61,7 +61,6 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRingData;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeySignatures;
import org.sufficientlysecure.keychain.provider.KeychainContract.Keys;
import org.sufficientlysecure.keychain.provider.KeychainContract.UpdatedKeys;
import org.sufficientlysecure.keychain.provider.KeychainContract.UserPackets;
import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
import org.sufficientlysecure.keychain.util.IterableIterator;
@@ -84,36 +83,30 @@ public class KeyWritableRepository extends KeyRepository {
private static final int MAX_CACHED_KEY_SIZE = 1024 * 50;
private final Context context;
private final LastUpdateInteractor lastUpdateInteractor;
private final DatabaseNotifyManager databaseNotifyManager;
public static KeyWritableRepository create(Context context) {
LocalPublicKeyStorage localPublicKeyStorage = LocalPublicKeyStorage.getInstance(context);
LocalSecretKeyStorage localSecretKeyStorage = LocalSecretKeyStorage.getInstance(context);
DatabaseNotifyManager databaseNotifyManager = DatabaseNotifyManager.create(context);
LastUpdateInteractor lastUpdateInteractor = LastUpdateInteractor.create(context);
return new KeyWritableRepository(context, localPublicKeyStorage, localSecretKeyStorage, databaseNotifyManager,
lastUpdateInteractor);
return new KeyWritableRepository(context, localPublicKeyStorage, localSecretKeyStorage, databaseNotifyManager);
}
@VisibleForTesting
KeyWritableRepository(Context context,
LocalPublicKeyStorage localPublicKeyStorage,
LocalSecretKeyStorage localSecretKeyStorage,
DatabaseNotifyManager databaseNotifyManager,
LastUpdateInteractor lastUpdateInteractor) {
this(context, localPublicKeyStorage, localSecretKeyStorage, databaseNotifyManager, lastUpdateInteractor,
new OperationLog(), 0);
DatabaseNotifyManager databaseNotifyManager) {
this(context, localPublicKeyStorage, localSecretKeyStorage, databaseNotifyManager, new OperationLog(), 0);
}
private KeyWritableRepository(Context context, LocalPublicKeyStorage localPublicKeyStorage,
LocalSecretKeyStorage localSecretKeyStorage, DatabaseNotifyManager databaseNotifyManager,
LastUpdateInteractor lastUpdateInteractor, OperationLog log, int indent) {
OperationLog log, int indent) {
super(context.getContentResolver(), localPublicKeyStorage, localSecretKeyStorage, log, indent);
this.context = context;
this.databaseNotifyManager = databaseNotifyManager;
this.lastUpdateInteractor = lastUpdateInteractor;
}
private LongSparseArray<CanonicalizedPublicKey> getTrustedMasterKeys() {
@@ -532,11 +525,6 @@ public class KeyWritableRepository extends KeyRepository {
mIndent -= 1;
}
ContentProviderOperation lastUpdateReinsertOp = getLastUpdatedReinsertOperationByMasterKeyId(masterKeyId);
if (lastUpdateReinsertOp != null) {
operations.add(lastUpdateReinsertOp);
}
try {
// delete old version of this keyRing (from database only!), which also deletes all keys and userIds on cascade
int deleted = contentResolver.delete(
@@ -567,26 +555,6 @@ public class KeyWritableRepository extends KeyRepository {
}
private ContentProviderOperation getLastUpdatedReinsertOperationByMasterKeyId(long masterKeyId) {
Long lastUpdateTime = getLastUpdateTime(masterKeyId);
if (lastUpdateTime == null) {
return null;
}
Boolean seenOnKeyservers = lastUpdateInteractor.getSeenOnKeyservers(masterKeyId);
ContentValues lastUpdatedEntry = new ContentValues(2);
lastUpdatedEntry.put(UpdatedKeys.MASTER_KEY_ID, masterKeyId);
lastUpdatedEntry.put(UpdatedKeys.LAST_UPDATED, lastUpdateTime);
if (seenOnKeyservers != null){
lastUpdatedEntry.put(UpdatedKeys.SEEN_ON_KEYSERVERS, seenOnKeyservers);
}
return ContentProviderOperation
.newInsert(UpdatedKeys.CONTENT_URI)
.withValues(lastUpdatedEntry)
.build();
}
private void writePublicKeyRing(CanonicalizedPublicKeyRing keyRing, long masterKeyId,
ArrayList<ContentProviderOperation> operations) throws IOException {
byte[] encodedKey = keyRing.getEncoded();

View File

@@ -263,16 +263,6 @@ public class KeychainContract {
}
public static class UpdatedKeys implements UpdatedKeysColumns, BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI_INTERNAL.buildUpon()
.appendPath(BASE_UPDATED_KEYS).build();
public static final String CONTENT_TYPE
= "vnd.android.cursor.dir/vnd.org.sufficientlysecure.keychain.provider.updated_keys";
public static final String CONTENT_ITEM_TYPE
= "vnd.android.cursor.item/vnd.org.sufficientlysecure.keychain.provider.updated_keys";
}
public static class KeySignatures implements KeySignaturesColumns, BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI_INTERNAL.buildUpon()
.appendPath(BASE_KEY_SIGNATURES).build();

View File

@@ -34,6 +34,9 @@ import android.database.sqlite.SQLiteException;
import android.provider.BaseColumns;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.KeyMetadataModel;
import org.sufficientlysecure.keychain.KeyRingsPublicModel;
import org.sufficientlysecure.keychain.model.ApiApp;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiAppsAllowedKeysColumns;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiAutocryptPeerColumns;
import org.sufficientlysecure.keychain.provider.KeychainContract.CertsColumns;
@@ -57,7 +60,7 @@ import timber.log.Timber;
*/
public class KeychainDatabase {
private static final String DATABASE_NAME = "openkeychain.db";
private static final int DATABASE_VERSION = 26;
private static final int DATABASE_VERSION = 27;
private final SupportSQLiteOpenHelper supportSQLiteOpenHelper;
private Context context;
@@ -226,15 +229,16 @@ public class KeychainDatabase {
private void onCreate(SupportSQLiteDatabase db) {
Timber.w("Creating database...");
db.execSQL(CREATE_KEYRINGS_PUBLIC);
db.execSQL(KeyRingsPublicModel.CREATE_TABLE);
db.execSQL(CREATE_KEYS);
db.execSQL(CREATE_USER_PACKETS);
db.execSQL(CREATE_CERTS);
db.execSQL(CREATE_UPDATE_KEYS);
db.execSQL(KeyMetadataModel.CREATE_TABLE);
db.execSQL(CREATE_KEY_SIGNATURES);
db.execSQL(CREATE_API_APPS_ALLOWED_KEYS);
db.execSQL(CREATE_OVERRIDDEN_WARNINGS);
db.execSQL(CREATE_API_AUTOCRYPT_PEERS);
db.execSQL(ApiApp.CREATE_TABLE);
db.execSQL("CREATE INDEX keys_by_rank ON keys (" + KeysColumns.RANK + ", " + KeysColumns.MASTER_KEY_ID + ");");
db.execSQL("CREATE INDEX uids_by_rank ON user_packets (" + UserPacketsColumns.RANK + ", "
@@ -459,6 +463,9 @@ public class KeychainDatabase {
}
}
case 26: {
migrateUpdatedKeysToKeyMetadataTable(db);
}
}
}
@@ -476,6 +483,11 @@ public class KeychainDatabase {
// db.execSQL("DROP TABLE keyrings_secret");
}
private void migrateUpdatedKeysToKeyMetadataTable(SupportSQLiteDatabase db) {
db.execSQL("ALTER TABLE updated_keys RENAME TO key_metadata;");
db.execSQL("UPDATE key_metadata SET last_updated = last_updated * 1000;");
}
public void onDowngrade(SupportSQLiteDatabase db, int oldVersion, int newVersion) {
// Downgrade is ok for the debug version, makes it easier to work with branches
if (Constants.DEBUG) {

View File

@@ -45,7 +45,6 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRingData;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeySignatures;
import org.sufficientlysecure.keychain.provider.KeychainContract.Keys;
import org.sufficientlysecure.keychain.provider.KeychainContract.UpdatedKeys;
import org.sufficientlysecure.keychain.provider.KeychainContract.UserPackets;
import org.sufficientlysecure.keychain.provider.KeychainContract.UserPacketsColumns;
import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables;
@@ -75,9 +74,6 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
private static final int KEY_RINGS_FIND_BY_USER_ID = 402;
private static final int KEY_RINGS_FILTER_BY_SIGNER = 403;
private static final int UPDATED_KEYS = 500;
private static final int UPDATED_KEYS_SPECIFIC = 501;
private static final int AUTOCRYPT_PEERS_BY_MASTER_KEY_ID = 601;
private static final int AUTOCRYPT_PEERS_BY_PACKAGE_NAME = 602;
private static final int AUTOCRYPT_PEERS_BY_PACKAGE_NAME_AND_TRUST_ID = 603;
@@ -192,14 +188,6 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
matcher.addURI(authority, KeychainContract.BASE_AUTOCRYPT_PEERS + "/" +
KeychainContract.PATH_BY_PACKAGE_NAME + "/*/*", AUTOCRYPT_PEERS_BY_PACKAGE_NAME_AND_TRUST_ID);
/*
* to access table containing last updated dates of keys
*/
matcher.addURI(authority, KeychainContract.BASE_UPDATED_KEYS, UPDATED_KEYS);
matcher.addURI(authority, KeychainContract.BASE_UPDATED_KEYS + "/*", UPDATED_KEYS_SPECIFIC);
matcher.addURI(authority, KeychainContract.BASE_KEY_SIGNATURES, KEY_SIGNATURES);
@@ -239,12 +227,6 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
case KEY_RING_USER_IDS:
return UserPackets.CONTENT_TYPE;
case UPDATED_KEYS:
return UpdatedKeys.CONTENT_TYPE;
case UPDATED_KEYS_SPECIFIC:
return UpdatedKeys.CONTENT_ITEM_TYPE;
case KEY_SIGNATURES:
return KeySignatures.CONTENT_TYPE;
@@ -727,30 +709,6 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
break;
}
case UPDATED_KEYS:
case UPDATED_KEYS_SPECIFIC: {
HashMap<String, String> projectionMap = new HashMap<>();
projectionMap.put(UpdatedKeys.MASTER_KEY_ID,
Tables.UPDATED_KEYS + "." + UpdatedKeys.MASTER_KEY_ID + " AS " + UpdatedKeys.MASTER_KEY_ID);
projectionMap.put(UpdatedKeys.LAST_UPDATED,
Tables.UPDATED_KEYS + "." + UpdatedKeys.LAST_UPDATED + " AS " + UpdatedKeys.LAST_UPDATED);
projectionMap.put(UpdatedKeys.SEEN_ON_KEYSERVERS,
Tables.UPDATED_KEYS + "." + UpdatedKeys.SEEN_ON_KEYSERVERS + " AS " + UpdatedKeys.SEEN_ON_KEYSERVERS);
projectionMap.put(UpdatedKeys.FINGERPRINT,
Tables.KEYS + "." + Keys.FINGERPRINT + " AS " + UpdatedKeys.FINGERPRINT);
qb.setProjectionMap(projectionMap);
qb.setTables(Tables.UPDATED_KEYS +
" LEFT JOIN " + Tables.KEYS +
" ON (" + Tables.KEYS + "." + Keys.KEY_ID + " = " + Tables.UPDATED_KEYS + "." + UpdatedKeys.MASTER_KEY_ID + ")"
);
if (match == UPDATED_KEYS_SPECIFIC) {
qb.appendWhere(Tables.UPDATED_KEYS + "." + UpdatedKeys.MASTER_KEY_ID + " = ");
qb.appendWhereEscapeString(uri.getPathSegments().get(1));
}
break;
}
default: {
throw new IllegalArgumentException("Unknown URI " + uri + " (" + match + ")");
}
@@ -835,17 +793,6 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
keyId = values.getAsLong(Certs.MASTER_KEY_ID);
break;
}
case UPDATED_KEYS: {
keyId = values.getAsLong(UpdatedKeys.MASTER_KEY_ID);
try {
db.insert(Tables.UPDATED_KEYS, SQLiteDatabase.CONFLICT_FAIL, values);
} catch (SQLiteConstraintException e) {
db.update(Tables.UPDATED_KEYS, SQLiteDatabase.CONFLICT_IGNORE, values,
UpdatedKeys.MASTER_KEY_ID + " = ?", new String[] { Long.toString(keyId) });
}
rowUri = UpdatedKeys.CONTENT_URI;
break;
}
case KEY_SIGNATURES: {
db.insert(Tables.KEY_SIGNATURES, SQLiteDatabase.CONFLICT_FAIL, values);
rowUri = KeySignatures.CONTENT_URI;
@@ -953,19 +900,6 @@ public class KeychainProvider extends ContentProvider implements SimpleContentRe
count = db.update(Tables.KEYS, SQLiteDatabase.CONFLICT_FAIL, values, actualSelection, selectionArgs);
break;
}
case UPDATED_KEYS: {
if (values.size() != 2 ||
!values.containsKey(UpdatedKeys.SEEN_ON_KEYSERVERS) ||
!values.containsKey(UpdatedKeys.LAST_UPDATED) ||
values.get(UpdatedKeys.LAST_UPDATED) != null ||
values.get(UpdatedKeys.SEEN_ON_KEYSERVERS) != null ||
selection != null || selectionArgs != null) {
throw new UnsupportedOperationException("can only reset all keys");
}
db.update(Tables.UPDATED_KEYS, SQLiteDatabase.CONFLICT_FAIL, values, null, null);
break;
}
case AUTOCRYPT_PEERS_BY_PACKAGE_NAME_AND_TRUST_ID: {
String packageName = uri.getPathSegments().get(2);
String identifier = uri.getLastPathSegment();

View File

@@ -1,105 +0,0 @@
package org.sufficientlysecure.keychain.provider;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.concurrent.TimeUnit;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.Nullable;
import org.sufficientlysecure.keychain.provider.KeychainContract.UpdatedKeys;
import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables;
public class LastUpdateInteractor {
private final ContentResolver contentResolver;
private final DatabaseNotifyManager databaseNotifyManager;
public static LastUpdateInteractor create(Context context) {
return new LastUpdateInteractor(context.getContentResolver(), DatabaseNotifyManager.create(context));
}
private LastUpdateInteractor(ContentResolver contentResolver, DatabaseNotifyManager databaseNotifyManager) {
this.contentResolver = contentResolver;
this.databaseNotifyManager = databaseNotifyManager;
}
@Nullable
public Boolean getSeenOnKeyservers(long masterKeyId) {
Cursor cursor = contentResolver.query(
UpdatedKeys.CONTENT_URI,
new String[] { UpdatedKeys.SEEN_ON_KEYSERVERS },
Tables.UPDATED_KEYS + "." + UpdatedKeys.MASTER_KEY_ID + " = ?",
new String[] { "" + masterKeyId },
null
);
if (cursor == null) {
return null;
}
Boolean seenOnKeyservers;
try {
if (!cursor.moveToNext()) {
return null;
}
seenOnKeyservers = cursor.isNull(0) ? null : cursor.getInt(0) != 0;
} finally {
cursor.close();
}
return seenOnKeyservers;
}
public void resetAllLastUpdatedTimes() {
ContentValues values = new ContentValues();
values.putNull(UpdatedKeys.LAST_UPDATED);
values.putNull(UpdatedKeys.SEEN_ON_KEYSERVERS);
contentResolver.update(UpdatedKeys.CONTENT_URI, values, null, null);
}
public Uri renewKeyLastUpdatedTime(long masterKeyId, boolean seenOnKeyservers) {
boolean isFirstKeyserverStatusCheck = getSeenOnKeyservers(masterKeyId) == null;
ContentValues values = new ContentValues();
values.put(UpdatedKeys.MASTER_KEY_ID, masterKeyId);
values.put(UpdatedKeys.LAST_UPDATED, GregorianCalendar.getInstance().getTimeInMillis() / 1000);
if (seenOnKeyservers || isFirstKeyserverStatusCheck) {
values.put(UpdatedKeys.SEEN_ON_KEYSERVERS, seenOnKeyservers);
}
// this will actually update/replace, doing the right thing™ for seenOnKeyservers value
// see `KeychainProvider.insert()`
Uri insert = contentResolver.insert(UpdatedKeys.CONTENT_URI, values);
databaseNotifyManager.notifyKeyserverStatusChange(masterKeyId);
return insert;
}
public List<byte[]> getFingerprintsForKeysOlderThan(long olderThan, TimeUnit timeUnit) {
Cursor outdatedKeysCursor = contentResolver.query(
KeychainContract.UpdatedKeys.CONTENT_URI,
new String[] { KeychainContract.UpdatedKeys.FINGERPRINT, },
KeychainContract.UpdatedKeys.LAST_UPDATED + " < ?",
new String[] { Long.toString(timeUnit.toSeconds(olderThan)) },
null
);
List<byte[]> fingerprintList = new ArrayList<>();
if (outdatedKeysCursor == null) {
return fingerprintList;
}
while (outdatedKeysCursor.moveToNext()) {
byte[] fingerprint = outdatedKeysCursor.getBlob(0);
fingerprintList.add(fingerprint);
}
outdatedKeysCursor.close();
return fingerprintList;
}
}