Search in sources :

Example 11 with StorageId

use of org.whispersystems.signalservice.api.storage.StorageId in project Signal-Android by signalapp.

the class ApplyUnknownFieldsToSelfMigrationJob method performMigration.

@Override
public void performMigration() {
    if (!SignalStore.account().isRegistered() || SignalStore.account().getAci() == null) {
        Log.w(TAG, "Not registered!");
        return;
    }
    Recipient self;
    RecipientRecord settings;
    try {
        self = Recipient.self();
        settings = SignalDatabase.recipients().getRecordForSync(self.getId());
    } catch (RecipientDatabase.MissingRecipientException e) {
        Log.w(TAG, "Unable to find self");
        return;
    }
    if (settings == null || settings.getSyncExtras().getStorageProto() == null) {
        Log.d(TAG, "No unknowns to apply");
        return;
    }
    try {
        StorageId storageId = StorageId.forAccount(self.getStorageServiceId());
        AccountRecord accountRecord = AccountRecord.parseFrom(settings.getSyncExtras().getStorageProto());
        SignalAccountRecord signalAccountRecord = new SignalAccountRecord(storageId, accountRecord);
        Log.d(TAG, "Applying potentially now known unknowns");
        StorageSyncHelper.applyAccountStorageSyncUpdates(context, self, signalAccountRecord, false);
    } catch (InvalidProtocolBufferException e) {
        Log.w(TAG, e);
    }
}
Also used : RecipientDatabase(org.thoughtcrime.securesms.database.RecipientDatabase) SignalAccountRecord(org.whispersystems.signalservice.api.storage.SignalAccountRecord) RecipientRecord(org.thoughtcrime.securesms.database.model.RecipientRecord) SignalAccountRecord(org.whispersystems.signalservice.api.storage.SignalAccountRecord) AccountRecord(org.whispersystems.signalservice.internal.storage.protos.AccountRecord) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Recipient(org.thoughtcrime.securesms.recipients.Recipient) StorageId(org.whispersystems.signalservice.api.storage.StorageId)

Example 12 with StorageId

use of org.whispersystems.signalservice.api.storage.StorageId in project Signal-Android by signalapp.

the class SignalServiceAccountManager method writeStorageRecords.

/**
 * @return If there was a conflict, the latest {@link SignalStorageManifest}. Otherwise absent.
 */
private Optional<SignalStorageManifest> writeStorageRecords(StorageKey storageKey, SignalStorageManifest manifest, List<SignalStorageRecord> inserts, List<byte[]> deletes, boolean clearAll) throws IOException, InvalidKeyException {
    ManifestRecord.Builder manifestRecordBuilder = ManifestRecord.newBuilder().setVersion(manifest.getVersion());
    for (StorageId id : manifest.getStorageIds()) {
        ManifestRecord.Identifier idProto = ManifestRecord.Identifier.newBuilder().setRaw(ByteString.copyFrom(id.getRaw())).setType(ManifestRecord.Identifier.Type.forNumber(id.getType())).build();
        manifestRecordBuilder.addIdentifiers(idProto);
    }
    String authToken = this.pushServiceSocket.getStorageAuth();
    StorageManifestKey manifestKey = storageKey.deriveManifestKey(manifest.getVersion());
    byte[] encryptedRecord = SignalStorageCipher.encrypt(manifestKey, manifestRecordBuilder.build().toByteArray());
    StorageManifest storageManifest = StorageManifest.newBuilder().setVersion(manifest.getVersion()).setValue(ByteString.copyFrom(encryptedRecord)).build();
    WriteOperation.Builder writeBuilder = WriteOperation.newBuilder().setManifest(storageManifest);
    for (SignalStorageRecord insert : inserts) {
        writeBuilder.addInsertItem(SignalStorageModels.localToRemoteStorageRecord(insert, storageKey));
    }
    if (clearAll) {
        writeBuilder.setClearAll(true);
    } else {
        for (byte[] delete : deletes) {
            writeBuilder.addDeleteKey(ByteString.copyFrom(delete));
        }
    }
    Optional<StorageManifest> conflict = this.pushServiceSocket.writeStorageContacts(authToken, writeBuilder.build());
    if (conflict.isPresent()) {
        StorageManifestKey conflictKey = storageKey.deriveManifestKey(conflict.get().getVersion());
        byte[] rawManifestRecord = SignalStorageCipher.decrypt(conflictKey, conflict.get().getValue().toByteArray());
        ManifestRecord record = ManifestRecord.parseFrom(rawManifestRecord);
        List<StorageId> ids = new ArrayList<>(record.getIdentifiersCount());
        for (ManifestRecord.Identifier id : record.getIdentifiersList()) {
            ids.add(StorageId.forType(id.getRaw().toByteArray(), id.getType().getNumber()));
        }
        SignalStorageManifest conflictManifest = new SignalStorageManifest(record.getVersion(), ids);
        return Optional.of(conflictManifest);
    } else {
        return Optional.absent();
    }
}
Also used : SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest) ManifestRecord(org.whispersystems.signalservice.internal.storage.protos.ManifestRecord) StorageManifestKey(org.whispersystems.signalservice.api.storage.StorageManifestKey) ArrayList(java.util.ArrayList) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) ByteString(com.google.protobuf.ByteString) StorageId(org.whispersystems.signalservice.api.storage.StorageId) StorageManifest(org.whispersystems.signalservice.internal.storage.protos.StorageManifest) SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest) WriteOperation(org.whispersystems.signalservice.internal.storage.protos.WriteOperation)

Example 13 with StorageId

use of org.whispersystems.signalservice.api.storage.StorageId in project Signal-Android by signalapp.

the class SignalServiceAccountManager method readStorageRecords.

public List<SignalStorageRecord> readStorageRecords(StorageKey storageKey, List<StorageId> storageKeys) throws IOException, InvalidKeyException {
    if (storageKeys.isEmpty()) {
        return Collections.emptyList();
    }
    List<SignalStorageRecord> result = new ArrayList<>();
    Map<ByteString, Integer> typeMap = new HashMap<>();
    List<ReadOperation> readOperations = new LinkedList<>();
    ReadOperation.Builder currentOperation = ReadOperation.newBuilder();
    for (StorageId key : storageKeys) {
        typeMap.put(ByteString.copyFrom(key.getRaw()), key.getType());
        if (currentOperation.getReadKeyCount() >= STORAGE_READ_MAX_ITEMS) {
            Log.i(TAG, "Going over max read items. Starting a new read operation.");
            readOperations.add(currentOperation.build());
            currentOperation = ReadOperation.newBuilder();
        }
        if (StorageId.isKnownType(key.getType())) {
            currentOperation.addReadKey(ByteString.copyFrom(key.getRaw()));
        } else {
            result.add(SignalStorageRecord.forUnknown(key));
        }
    }
    if (currentOperation.getReadKeyCount() > 0) {
        readOperations.add(currentOperation.build());
    }
    Log.i(TAG, "Reading " + storageKeys.size() + " items split over " + readOperations.size() + " page(s).");
    String authToken = this.pushServiceSocket.getStorageAuth();
    for (ReadOperation readOperation : readOperations) {
        StorageItems items = this.pushServiceSocket.readStorageItems(authToken, readOperation);
        for (StorageItem item : items.getItemsList()) {
            Integer type = typeMap.get(item.getKey());
            if (type != null) {
                result.add(SignalStorageModels.remoteToLocalStorageRecord(item, type, storageKey));
            } else {
                Log.w(TAG, "No type found! Skipping.");
            }
        }
    }
    return result;
}
Also used : ReadOperation(org.whispersystems.signalservice.internal.storage.protos.ReadOperation) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) ByteString(com.google.protobuf.ByteString) StorageId(org.whispersystems.signalservice.api.storage.StorageId) LinkedList(java.util.LinkedList) StorageItems(org.whispersystems.signalservice.internal.storage.protos.StorageItems) StorageItem(org.whispersystems.signalservice.internal.storage.protos.StorageItem)

Example 14 with StorageId

use of org.whispersystems.signalservice.api.storage.StorageId in project Signal-Android by WhisperSystems.

the class StorageAccountRestoreJob method onRun.

@Override
protected void onRun() throws Exception {
    SignalServiceAccountManager accountManager = ApplicationDependencies.getSignalServiceAccountManager();
    StorageKey storageServiceKey = SignalStore.storageService().getOrCreateStorageKey();
    Log.i(TAG, "Retrieving manifest...");
    Optional<SignalStorageManifest> manifest = accountManager.getStorageManifest(storageServiceKey);
    if (!manifest.isPresent()) {
        Log.w(TAG, "Manifest did not exist or was undecryptable (bad key). Not restoring. Force-pushing.");
        ApplicationDependencies.getJobManager().add(new StorageForcePushJob());
        return;
    }
    Log.i(TAG, "Resetting the local manifest to an empty state so that it will sync later.");
    SignalStore.storageService().setManifest(SignalStorageManifest.EMPTY);
    Optional<StorageId> accountId = manifest.get().getAccountStorageId();
    if (!accountId.isPresent()) {
        Log.w(TAG, "Manifest had no account record! Not restoring.");
        return;
    }
    Log.i(TAG, "Retrieving account record...");
    List<SignalStorageRecord> records = accountManager.readStorageRecords(storageServiceKey, Collections.singletonList(accountId.get()));
    SignalStorageRecord record = records.size() > 0 ? records.get(0) : null;
    if (record == null) {
        Log.w(TAG, "Could not find account record, even though we had an ID! Not restoring.");
        return;
    }
    SignalAccountRecord accountRecord = record.getAccount().orNull();
    if (accountRecord == null) {
        Log.w(TAG, "The storage record didn't actually have an account on it! Not restoring.");
        return;
    }
    Log.i(TAG, "Applying changes locally...");
    SignalDatabase.getRawDatabase().beginTransaction();
    try {
        StorageSyncHelper.applyAccountStorageSyncUpdates(context, Recipient.self(), accountRecord, false);
        SignalDatabase.getRawDatabase().setTransactionSuccessful();
    } finally {
        SignalDatabase.getRawDatabase().endTransaction();
    }
    JobManager jobManager = ApplicationDependencies.getJobManager();
    if (accountRecord.getAvatarUrlPath().isPresent()) {
        Log.i(TAG, "Fetching avatar...");
        Optional<JobTracker.JobState> state = jobManager.runSynchronously(new RetrieveProfileAvatarJob(Recipient.self(), accountRecord.getAvatarUrlPath().get()), LIFESPAN / 2);
        if (state.isPresent()) {
            Log.i(TAG, "Avatar retrieved successfully. " + state.get());
        } else {
            Log.w(TAG, "Avatar retrieval did not complete in time (or otherwise failed).");
        }
    } else {
        Log.i(TAG, "No avatar present. Not fetching.");
    }
    Log.i(TAG, "Refreshing attributes...");
    Optional<JobTracker.JobState> state = jobManager.runSynchronously(new RefreshAttributesJob(), LIFESPAN / 2);
    if (state.isPresent()) {
        Log.i(TAG, "Attributes refreshed successfully. " + state.get());
    } else {
        Log.w(TAG, "Attribute refresh did not complete in time (or otherwise failed).");
    }
}
Also used : SignalAccountRecord(org.whispersystems.signalservice.api.storage.SignalAccountRecord) SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest) SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) JobManager(org.thoughtcrime.securesms.jobmanager.JobManager) StorageId(org.whispersystems.signalservice.api.storage.StorageId) StorageKey(org.whispersystems.signalservice.api.storage.StorageKey)

Example 15 with StorageId

use of org.whispersystems.signalservice.api.storage.StorageId in project Signal-Android by WhisperSystems.

the class StorageForcePushJob method onRun.

@Override
protected void onRun() throws IOException, RetryLaterException {
    if (SignalStore.account().isLinkedDevice()) {
        Log.i(TAG, "Only the primary device can force push");
        return;
    }
    StorageKey storageServiceKey = SignalStore.storageService().getOrCreateStorageKey();
    SignalServiceAccountManager accountManager = ApplicationDependencies.getSignalServiceAccountManager();
    RecipientDatabase recipientDatabase = SignalDatabase.recipients();
    UnknownStorageIdDatabase storageIdDatabase = SignalDatabase.unknownStorageIds();
    long currentVersion = accountManager.getStorageManifestVersion();
    Map<RecipientId, StorageId> oldContactStorageIds = recipientDatabase.getContactStorageSyncIdsMap();
    long newVersion = currentVersion + 1;
    Map<RecipientId, StorageId> newContactStorageIds = generateContactStorageIds(oldContactStorageIds);
    List<SignalStorageRecord> inserts = Stream.of(oldContactStorageIds.keySet()).map(recipientDatabase::getRecordForSync).withoutNulls().map(s -> StorageSyncModels.localToRemoteRecord(s, Objects.requireNonNull(newContactStorageIds.get(s.getId())).getRaw())).toList();
    SignalStorageRecord accountRecord = StorageSyncHelper.buildAccountRecord(context, Recipient.self().fresh());
    List<StorageId> allNewStorageIds = new ArrayList<>(newContactStorageIds.values());
    inserts.add(accountRecord);
    allNewStorageIds.add(accountRecord.getId());
    SignalStorageManifest manifest = new SignalStorageManifest(newVersion, allNewStorageIds);
    StorageSyncValidations.validateForcePush(manifest, inserts, Recipient.self().fresh());
    try {
        if (newVersion > 1) {
            Log.i(TAG, String.format(Locale.ENGLISH, "Force-pushing data. Inserting %d IDs.", inserts.size()));
            if (accountManager.resetStorageRecords(storageServiceKey, manifest, inserts).isPresent()) {
                Log.w(TAG, "Hit a conflict. Trying again.");
                throw new RetryLaterException();
            }
        } else {
            Log.i(TAG, String.format(Locale.ENGLISH, "First version, normal push. Inserting %d IDs.", inserts.size()));
            if (accountManager.writeStorageRecords(storageServiceKey, manifest, inserts, Collections.emptyList()).isPresent()) {
                Log.w(TAG, "Hit a conflict. Trying again.");
                throw new RetryLaterException();
            }
        }
    } catch (InvalidKeyException e) {
        Log.w(TAG, "Hit an invalid key exception, which likely indicates a conflict.");
        throw new RetryLaterException(e);
    }
    Log.i(TAG, "Force push succeeded. Updating local manifest version to: " + newVersion);
    SignalStore.storageService().setManifest(manifest);
    recipientDatabase.applyStorageIdUpdates(newContactStorageIds);
    recipientDatabase.applyStorageIdUpdates(Collections.singletonMap(Recipient.self().getId(), accountRecord.getId()));
    storageIdDatabase.deleteAll();
}
Also used : SignalStore(org.thoughtcrime.securesms.keyvalue.SignalStore) SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) StorageSyncValidations(org.thoughtcrime.securesms.storage.StorageSyncValidations) SignalDatabase(org.thoughtcrime.securesms.database.SignalDatabase) RetryLaterException(org.thoughtcrime.securesms.transport.RetryLaterException) Stream(com.annimon.stream.Stream) NonNull(androidx.annotation.NonNull) Data(org.thoughtcrime.securesms.jobmanager.Data) StorageSyncModels(org.thoughtcrime.securesms.storage.StorageSyncModels) HashMap(java.util.HashMap) RecipientDatabase(org.thoughtcrime.securesms.database.RecipientDatabase) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) ArrayList(java.util.ArrayList) StorageKey(org.whispersystems.signalservice.api.storage.StorageKey) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) StorageSyncHelper(org.thoughtcrime.securesms.storage.StorageSyncHelper) Locale(java.util.Locale) Map(java.util.Map) Recipient(org.thoughtcrime.securesms.recipients.Recipient) SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest) StorageId(org.whispersystems.signalservice.api.storage.StorageId) PushNetworkException(org.whispersystems.signalservice.api.push.exceptions.PushNetworkException) ApplicationDependencies(org.thoughtcrime.securesms.dependencies.ApplicationDependencies) UnknownStorageIdDatabase(org.thoughtcrime.securesms.database.UnknownStorageIdDatabase) NetworkConstraint(org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint) IOException(java.io.IOException) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Log(org.signal.core.util.logging.Log) List(java.util.List) Job(org.thoughtcrime.securesms.jobmanager.Job) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) Collections(java.util.Collections) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) SignalStorageManifest(org.whispersystems.signalservice.api.storage.SignalStorageManifest) SignalServiceAccountManager(org.whispersystems.signalservice.api.SignalServiceAccountManager) ArrayList(java.util.ArrayList) SignalStorageRecord(org.whispersystems.signalservice.api.storage.SignalStorageRecord) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) StorageId(org.whispersystems.signalservice.api.storage.StorageId) RecipientDatabase(org.thoughtcrime.securesms.database.RecipientDatabase) UnknownStorageIdDatabase(org.thoughtcrime.securesms.database.UnknownStorageIdDatabase) RetryLaterException(org.thoughtcrime.securesms.transport.RetryLaterException) StorageKey(org.whispersystems.signalservice.api.storage.StorageKey)

Aggregations

StorageId (org.whispersystems.signalservice.api.storage.StorageId)22 SignalStorageRecord (org.whispersystems.signalservice.api.storage.SignalStorageRecord)16 SignalStorageManifest (org.whispersystems.signalservice.api.storage.SignalStorageManifest)12 ArrayList (java.util.ArrayList)10 Recipient (org.thoughtcrime.securesms.recipients.Recipient)10 NonNull (androidx.annotation.NonNull)8 RecipientDatabase (org.thoughtcrime.securesms.database.RecipientDatabase)8 SignalAccountRecord (org.whispersystems.signalservice.api.storage.SignalAccountRecord)8 Stream (com.annimon.stream.Stream)6 List (java.util.List)6 Log (org.signal.core.util.logging.Log)6 UnknownStorageIdDatabase (org.thoughtcrime.securesms.database.UnknownStorageIdDatabase)6 RecipientRecord (org.thoughtcrime.securesms.database.model.RecipientRecord)6 SignalServiceAccountManager (org.whispersystems.signalservice.api.SignalServiceAccountManager)6 StorageKey (org.whispersystems.signalservice.api.storage.StorageKey)6 Collectors (com.annimon.stream.Collectors)4 ByteString (com.google.protobuf.ByteString)4 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 LinkedList (java.util.LinkedList)4