realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Make it easier to change encryption key

Open cmelchior opened this issue 9 years ago • 11 comments

Changing encryption key is a use case that has been brought up a few times. Right now it is fairly cumbersome and require some knowledge on where files are actually stored:

RealmConfiguration config1 = new RealmConfiguration.Builder(context)
  .name("old-name")
  .encryptionKey(getOldKey())
  .build()

Realm realm = Realm.getInstance(config1);
realm.writeEncryptedCopyTo(new File(context.getFilesDir(), "new-name"), getNewKey());
realm.close();

RealmConfiguration config2 = new RealmConfiguration.Builder(context)
  .name("new-name")
  .encryptionKey(getNewKey())
  .build()

We could make this easier by adding a helper method for it (it would still use writeEncryptedCopy) under the hood

Possible idea:

// Encrypts the database with the new encryption key and returns the new configuration for it. 
RealmConfiguration newConfig = Realm.changeEncryptionKey(config, newKey);

cmelchior avatar Dec 09 '15 09:12 cmelchior

Does this mean that the user has to change the name of the file as well? Should we be creating an deleting files? What happens if a user runs of of space during this process? What is the safety fall back measure in case of an unrecoverable error?

donnfelker avatar Dec 10 '15 21:12 donnfelker

There is no way around needing the space to do the actual copy of the DB. That should be documented in the JavaDoc IMO. Regarding safety/fallback then I think the file system will handle that for us in most cases.

If we run out of space trying to write the copy we will get an exception (I assume), at that point we just abort the process. I would assume that the file never got actually written in that case.

Theoretically there are edge case scenarios, e.g what happens if we managed to do the copy, but crashed before replacing the old one. Then suddenly we have a "hidden" file on disk that could be potentially quite large.

If people used Context we have getCacheDir which would probably delete the file at some point, but people might save their files in other locations.....Hmmm.

cmelchior avatar Dec 10 '15 21:12 cmelchior

One workaround could be to have a special naming scheme for temporary Realm files, then we could delete them automatically the next time the Realm was accessed.

cmelchior avatar Dec 10 '15 21:12 cmelchior

It would make more sense to have a "cookbook" section with things like this

cmelchior avatar Apr 14 '16 09:04 cmelchior

In my case, the whole app sharing a global realm configuration instance. It was using by multiple activities.

Is it possible to change the encryption key for the global configuration during runtime while other activities / thread already holding a realm instance ?

kuno avatar May 03 '16 16:05 kuno

No, you cannot change encryption keys while the Realm file is open.

cmelchior avatar May 03 '16 16:05 cmelchior

so I need to make sure all realm instance were closed before change the key ?

kuno avatar May 03 '16 16:05 kuno

Yes

cmelchior avatar May 03 '16 16:05 cmelchior

@cmelchior

Thanks for reply

Where can I find the best best practices for update key during runtime ?

Is it a good idea keep only one realm instance globally, so it will easy to manage ?

kuno avatar May 04 '16 03:05 kuno

Any update on this?

VenomVendor avatar Nov 10 '17 04:11 VenomVendor

If an encryption key is set, then the specified Realm will be attempted to be accessed with that key

Zhuinden avatar Nov 10 '17 08:11 Zhuinden