Search in sources :

Example 1 with Migrator

use of org.cryptomator.cryptofs.migration.api.Migrator in project cryptofs by cryptomator.

the class MigratorsTest method testMigrateUnsupportedVaultFormat.

@Test(expected = IllegalStateException.class)
@SuppressWarnings("deprecation")
public void testMigrateUnsupportedVaultFormat() throws NoApplicableMigratorException, InvalidPassphraseException, IOException {
    Migrator migrator = Mockito.mock(Migrator.class);
    Migrators migrators = new Migrators(new HashMap<Migration, Migrator>() {

        {
            put(Migration.ZERO_TO_ONE, migrator);
        }
    });
    Mockito.doThrow(new UnsupportedVaultFormatException(Integer.MAX_VALUE, 1)).when(migrator).migrate(pathToVault, "masterkey.cryptomator", "secret");
    migrators.migrate(pathToVault, "masterkey.cryptomator", "secret");
}
Also used : Migrator(org.cryptomator.cryptofs.migration.api.Migrator) UnsupportedVaultFormatException(org.cryptomator.cryptolib.api.UnsupportedVaultFormatException) Test(org.junit.Test)

Example 2 with Migrator

use of org.cryptomator.cryptofs.migration.api.Migrator in project cryptofs by cryptomator.

the class Migrators method migrate.

/**
 * Performs the actual migration. This task may take a while and this method will block.
 *
 * @param pathToVault Path to the vault's root
 * @param masterkeyFilename Name of the masterkey file located in the vault
 * @param passphrase The passphrase needed to unlock the vault
 * @throws NoApplicableMigratorException If the vault can not be migrated, because no migrator could be found
 * @throws InvalidPassphraseException If the passphrase could not be used to unlock the vault
 * @throws IOException if an I/O error occurs migrating the vault
 */
public void migrate(Path pathToVault, String masterkeyFilename, CharSequence passphrase) throws NoApplicableMigratorException, InvalidPassphraseException, IOException {
    Path masterKeyPath = pathToVault.resolve(masterkeyFilename);
    byte[] keyFileContents = Files.readAllBytes(masterKeyPath);
    KeyFile keyFile = KeyFile.parse(keyFileContents);
    try {
        Migrator migrator = findApplicableMigrator(keyFile.getVersion()).orElseThrow(NoApplicableMigratorException::new);
        migrator.migrate(pathToVault, masterkeyFilename, passphrase);
    } catch (UnsupportedVaultFormatException e) {
        // might be a tampered masterkey file, as this exception is also thrown if the vault version MAC is not authentic.
        throw new IllegalStateException("Vault version checked beforehand but not supported by migrator.");
    }
}
Also used : Path(java.nio.file.Path) NoApplicableMigratorException(org.cryptomator.cryptofs.migration.api.NoApplicableMigratorException) KeyFile(org.cryptomator.cryptolib.api.KeyFile) Migrator(org.cryptomator.cryptofs.migration.api.Migrator) UnsupportedVaultFormatException(org.cryptomator.cryptolib.api.UnsupportedVaultFormatException)

Example 3 with Migrator

use of org.cryptomator.cryptofs.migration.api.Migrator in project cryptofs by cryptomator.

the class MigratorsTest method testMigrate.

@Test
@SuppressWarnings("deprecation")
public void testMigrate() throws NoApplicableMigratorException, InvalidPassphraseException, IOException {
    Migrator migrator = Mockito.mock(Migrator.class);
    Migrators migrators = new Migrators(new HashMap<Migration, Migrator>() {

        {
            put(Migration.ZERO_TO_ONE, migrator);
        }
    });
    migrators.migrate(pathToVault, "masterkey.cryptomator", "secret");
    Mockito.verify(migrator).migrate(pathToVault, "masterkey.cryptomator", "secret");
}
Also used : Migrator(org.cryptomator.cryptofs.migration.api.Migrator) Test(org.junit.Test)

Example 4 with Migrator

use of org.cryptomator.cryptofs.migration.api.Migrator in project cryptofs by cryptomator.

the class Version6MigratorTest method testMigrate.

@Test
public void testMigrate() throws IOException {
    String oldPassword = Normalizer.normalize("ä", Form.NFD);
    String newPassword = Normalizer.normalize("ä", Form.NFC);
    Assert.assertNotEquals(oldPassword, newPassword);
    KeyFile beforeMigration = cryptorProvider.createNew().writeKeysToMasterkeyFile(oldPassword, 5);
    Assert.assertEquals(5, beforeMigration.getVersion());
    Files.write(masterkeyFile, beforeMigration.serialize());
    Migrator migrator = new Version6Migrator(cryptorProvider);
    migrator.migrate(pathToVault, "masterkey.cryptomator", oldPassword);
    KeyFile afterMigration = KeyFile.parse(Files.readAllBytes(masterkeyFile));
    Assert.assertEquals(6, afterMigration.getVersion());
    try (Cryptor cryptor = cryptorProvider.createFromKeyFile(afterMigration, newPassword, 6)) {
        Assert.assertNotNull(cryptor);
    }
    Assert.assertTrue(Files.exists(masterkeyBackupFile));
    KeyFile backupKey = KeyFile.parse(Files.readAllBytes(masterkeyBackupFile));
    Assert.assertEquals(5, backupKey.getVersion());
}
Also used : Cryptor(org.cryptomator.cryptolib.api.Cryptor) KeyFile(org.cryptomator.cryptolib.api.KeyFile) Migrator(org.cryptomator.cryptofs.migration.api.Migrator) Test(org.junit.Test)

Aggregations

Migrator (org.cryptomator.cryptofs.migration.api.Migrator)4 Test (org.junit.Test)3 KeyFile (org.cryptomator.cryptolib.api.KeyFile)2 UnsupportedVaultFormatException (org.cryptomator.cryptolib.api.UnsupportedVaultFormatException)2 Path (java.nio.file.Path)1 NoApplicableMigratorException (org.cryptomator.cryptofs.migration.api.NoApplicableMigratorException)1 Cryptor (org.cryptomator.cryptolib.api.Cryptor)1