Search in sources :

Example 16 with ConfigTO

use of org.syncany.config.to.ConfigTO in project syncany by syncany.

the class AbstractInitCommand method createConfigTO.

protected ConfigTO createConfigTO(TransferSettings transferSettings) throws Exception {
    ConfigTO configTO = new ConfigTO();
    configTO.setDisplayName(getDefaultDisplayName());
    configTO.setMachineName(getRandomMachineName());
    configTO.setMasterKey(null);
    // can be null
    configTO.setTransferSettings(transferSettings);
    return configTO;
}
Also used : ConfigTO(org.syncany.config.to.ConfigTO)

Example 17 with ConfigTO

use of org.syncany.config.to.ConfigTO in project syncany by syncany.

the class ConnectOperation method execute.

@Override
public ConnectOperationResult execute() throws Exception {
    logger.log(Level.INFO, "");
    logger.log(Level.INFO, "Running 'Connect'");
    logger.log(Level.INFO, "--------------------------------------------");
    // Decrypt and init configTO
    ConfigTO configTO = null;
    try {
        configTO = createConfigTO();
    } catch (CipherException e) {
        logger.log(Level.FINE, "Could not create config", e);
        return new ConnectOperationResult(ConnectResultCode.NOK_DECRYPT_ERROR);
    }
    // Init plugin and transfer manager
    transferManager = createTransferManagerFromNullConfig(options.getConfigTO());
    // Test the repo
    if (!performRepoTest(transferManager)) {
        logger.log(Level.INFO, "- Connecting to the repo failed, repo already exists or cannot be created: " + result.getResultCode());
        return result;
    }
    logger.log(Level.INFO, "- Connecting to the repo was successful; now downloading repo file ...");
    // Create local .syncany directory
    File tmpRepoFile = downloadFile(transferManager, new SyncanyRemoteFile());
    if (CipherUtil.isEncrypted(tmpRepoFile)) {
        logger.log(Level.INFO, "- Repo is ENCRYPTED. Decryption necessary.");
        if (configTO.getMasterKey() == null) {
            logger.log(Level.INFO, "- No master key present; Asking for password ...");
            boolean retryPassword = true;
            while (retryPassword) {
                SaltedSecretKey possibleMasterKey = askPasswordAndCreateMasterKey();
                logger.log(Level.INFO, "- Master key created. Now verifying by decrypting repo file...");
                if (decryptAndVerifyRepoFile(tmpRepoFile, possibleMasterKey)) {
                    logger.log(Level.INFO, "- SUCCESS: Repo file decrypted successfully.");
                    configTO.setMasterKey(possibleMasterKey);
                    retryPassword = false;
                } else {
                    logger.log(Level.INFO, "- FAILURE: Repo file decryption failed. Asking for retry.");
                    retryPassword = askRetryPassword();
                    if (!retryPassword) {
                        logger.log(Level.INFO, "- No retry possible/desired. Returning NOK_DECRYPT_ERROR.");
                        return new ConnectOperationResult(ConnectResultCode.NOK_DECRYPT_ERROR);
                    }
                }
            }
        } else {
            logger.log(Level.INFO, "- Master key present; Now verifying by decrypting repo file...");
            if (!decryptAndVerifyRepoFile(tmpRepoFile, configTO.getMasterKey())) {
                logger.log(Level.INFO, "- FAILURE: Repo file decryption failed. Returning NOK_DECRYPT_ERROR.");
                return new ConnectOperationResult(ConnectResultCode.NOK_DECRYPT_ERROR);
            }
        }
    } else {
        String repoFileStr = FileUtils.readFileToString(tmpRepoFile);
        verifyRepoFile(repoFileStr);
    }
    // Success, now do the work!
    File appDir = createAppDirs(options.getLocalDir());
    // Write file 'config.xml'
    File configFile = new File(appDir, Config.FILE_CONFIG);
    configTO.save(configFile);
    // Write file 'syncany'
    File repoFile = new File(appDir, Config.FILE_REPO);
    FileUtils.copyFile(tmpRepoFile, repoFile);
    tmpRepoFile.delete();
    // Write file 'master'
    if (configTO.getMasterKey() != null) {
        File masterFile = new File(appDir, Config.FILE_MASTER);
        new MasterTO(configTO.getMasterKey().getSalt()).save(masterFile);
    }
    // Shutdown plugin
    transferManager.disconnect();
    // Add to daemon (if requested)
    if (options.isDaemon()) {
        try {
            boolean addedToDaemonConfig = DaemonConfigHelper.addFolder(options.getLocalDir());
            result.setAddedToDaemon(addedToDaemonConfig);
        } catch (Exception e) {
            logger.log(Level.WARNING, "Cannot add folder to daemon config.", e);
            result.setAddedToDaemon(false);
        }
    }
    result.setResultCode(ConnectResultCode.OK);
    return result;
}
Also used : SaltedSecretKey(org.syncany.crypto.SaltedSecretKey) SyncanyRemoteFile(org.syncany.plugins.transfer.files.SyncanyRemoteFile) CipherException(org.syncany.crypto.CipherException) ConfigTO(org.syncany.config.to.ConfigTO) MasterTO(org.syncany.config.to.MasterTO) RemoteFile(org.syncany.plugins.transfer.files.RemoteFile) MasterRemoteFile(org.syncany.plugins.transfer.files.MasterRemoteFile) SyncanyRemoteFile(org.syncany.plugins.transfer.files.SyncanyRemoteFile) File(java.io.File) StorageException(org.syncany.plugins.transfer.StorageException) CipherException(org.syncany.crypto.CipherException)

Example 18 with ConfigTO

use of org.syncany.config.to.ConfigTO in project syncany by syncany.

the class ConnectOperationTest method testConnectOperationSuccess.

@Test
public void testConnectOperationSuccess() throws Exception {
    // A.init()
    InitOperationOptions initOperationOptionsA = TestConfigUtil.createTestInitOperationOptions("A");
    InitOperation initOperationA = new InitOperation(initOperationOptionsA, null);
    InitOperationResult initOperationResultA = initOperationA.execute();
    String connectLinkA = initOperationResultA.getGenLinkResult().getShareLink();
    assertNotNull(connectLinkA);
    // B.connect()
    File localDirB = TestFileUtil.createTempDirectoryInSystemTemp(TestConfigUtil.createUniqueName("clientB", initOperationOptionsA));
    File localConnectDirB = new File(localDirB, Config.DIR_APPLICATION);
    ConfigTO connectionConfigToB = initOperationOptionsA.getConfigTO();
    connectionConfigToB.setMachineName("clientB" + Math.abs(new Random().nextInt()));
    connectionConfigToB.setMasterKey(null);
    ConnectOperationOptions connectOperationOptionsB = new ConnectOperationOptions();
    connectOperationOptionsB.setStrategy(ConnectOptionsStrategy.CONNECTION_TO);
    connectOperationOptionsB.setConfigTO(connectionConfigToB);
    connectOperationOptionsB.setPassword(initOperationOptionsA.getPassword());
    connectOperationOptionsB.setLocalDir(localDirB);
    ConnectOperation connectOperationB = new ConnectOperation(connectOperationOptionsB, null);
    ConnectOperationResult connectOperationResultB = connectOperationB.execute();
    assertEquals(ConnectResultCode.OK, connectOperationResultB.getResultCode());
    assertTrue(new File(localConnectDirB, Config.DIR_DATABASE).exists());
    assertTrue(new File(localConnectDirB, Config.DIR_CACHE).exists());
    assertTrue(new File(localConnectDirB, Config.FILE_CONFIG).exists());
    assertTrue(new File(localConnectDirB, Config.DIR_LOG).exists());
    assertTrue(new File(localConnectDirB, Config.FILE_REPO).exists());
    assertEquals(new File(localConnectDirB, Config.FILE_MASTER).exists(), TestConfigUtil.getCrypto());
    File repoDir = ((LocalTransferSettings) initOperationOptionsA.getConfigTO().getTransferSettings()).getPath();
    // Tear down
    TestFileUtil.deleteDirectory(repoDir);
    TestFileUtil.deleteDirectory(localConnectDirB);
    TestFileUtil.deleteDirectory(initOperationOptionsA.getLocalDir());
    TestFileUtil.deleteDirectory(connectOperationOptionsB.getLocalDir());
}
Also used : ConnectOperationResult(org.syncany.operations.init.ConnectOperationResult) InitOperationResult(org.syncany.operations.init.InitOperationResult) InitOperation(org.syncany.operations.init.InitOperation) Random(java.util.Random) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) InitOperationOptions(org.syncany.operations.init.InitOperationOptions) ConnectOperation(org.syncany.operations.init.ConnectOperation) ConfigTO(org.syncany.config.to.ConfigTO) File(java.io.File) ConnectOperationOptions(org.syncany.operations.init.ConnectOperationOptions) Test(org.junit.Test)

Example 19 with ConfigTO

use of org.syncany.config.to.ConfigTO in project syncany by syncany.

the class ConnectOperationTest method testConnectOperationWithLink.

@Test
public void testConnectOperationWithLink() throws Exception {
    // A.init()
    InitOperationOptions initOperationOptionsA = TestConfigUtil.createTestInitOperationOptions("A");
    InitOperation initOperationA = new InitOperation(initOperationOptionsA, null);
    InitOperationResult initOperationResultA = initOperationA.execute();
    String connectLinkA = initOperationResultA.getGenLinkResult().getShareLink();
    assertNotNull(connectLinkA);
    // B.connect()
    File localDirB = TestFileUtil.createTempDirectoryInSystemTemp(TestConfigUtil.createUniqueName("clientB", initOperationOptionsA));
    File localConnectDirB = new File(localDirB, Config.DIR_APPLICATION);
    ConfigTO connectionConfigToB = initOperationOptionsA.getConfigTO();
    connectionConfigToB.setMachineName("clientB" + Math.abs(new Random().nextInt()));
    connectionConfigToB.setMasterKey(null);
    ConnectOperationOptions connectOperationOptionsB = new ConnectOperationOptions();
    connectOperationOptionsB.setStrategy(ConnectOptionsStrategy.CONNECTION_LINK);
    connectOperationOptionsB.setConnectLink(connectLinkA);
    connectOperationOptionsB.setConfigTO(connectionConfigToB);
    connectOperationOptionsB.setPassword(initOperationOptionsA.getPassword());
    connectOperationOptionsB.setLocalDir(localDirB);
    ConnectOperation connectOperationB = new ConnectOperation(connectOperationOptionsB, null);
    ConnectOperationResult connectOperationResultB = connectOperationB.execute();
    assertEquals(ConnectResultCode.OK, connectOperationResultB.getResultCode());
    assertTrue(new File(localConnectDirB, Config.DIR_DATABASE).exists());
    assertTrue(new File(localConnectDirB, Config.DIR_CACHE).exists());
    assertTrue(new File(localConnectDirB, Config.FILE_CONFIG).exists());
    assertTrue(new File(localConnectDirB, Config.DIR_LOG).exists());
    assertTrue(new File(localConnectDirB, Config.FILE_REPO).exists());
    assertEquals(new File(localConnectDirB, Config.FILE_MASTER).exists(), TestConfigUtil.getCrypto());
    File repoDir = ((LocalTransferSettings) initOperationOptionsA.getConfigTO().getTransferSettings()).getPath();
    // Tear down
    TestFileUtil.deleteDirectory(repoDir);
    TestFileUtil.deleteDirectory(localConnectDirB);
    TestFileUtil.deleteDirectory(initOperationOptionsA.getLocalDir());
    TestFileUtil.deleteDirectory(connectOperationOptionsB.getLocalDir());
}
Also used : ConnectOperationResult(org.syncany.operations.init.ConnectOperationResult) InitOperationResult(org.syncany.operations.init.InitOperationResult) InitOperation(org.syncany.operations.init.InitOperation) Random(java.util.Random) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) InitOperationOptions(org.syncany.operations.init.InitOperationOptions) ConnectOperation(org.syncany.operations.init.ConnectOperation) ConfigTO(org.syncany.config.to.ConfigTO) File(java.io.File) ConnectOperationOptions(org.syncany.operations.init.ConnectOperationOptions) Test(org.junit.Test)

Example 20 with ConfigTO

use of org.syncany.config.to.ConfigTO in project syncany by syncany.

the class TransferSettingsTest method testRestore.

@Test
public void testRestore() throws Exception {
    final String fooTest = "foo-test";
    final String bazTest = "baz";
    final int numberTest = 1234;
    final DummyTransferSettings ts = new DummyTransferSettings();
    final LocalTransferSettings lts = new LocalTransferSettings();
    final InitOperationOptions initOperationOptions = TestConfigUtil.createTestInitOperationOptions("syncanytest");
    final ConfigTO conf = initOperationOptions.getConfigTO();
    File repoDir = ((LocalTransferSettings) initOperationOptions.getConfigTO().getTransferSettings()).getPath();
    File localDir = initOperationOptions.getLocalDir();
    conf.setTransferSettings(ts);
    ts.foo = fooTest;
    ts.baz = bazTest;
    ts.number = numberTest;
    lts.setPath(File.createTempFile("aaa", "bbb"));
    ts.subsettings = lts;
    assertTrue(ts.isValid());
    Serializer serializer = new Persister();
    serializer.write(conf, tmpFile);
    System.out.println(new String(Files.readAllBytes(Paths.get(tmpFile.toURI()))));
    ConfigTO confRestored = ConfigTO.load(tmpFile);
    TransferPlugin plugin = Plugins.get(confRestored.getTransferSettings().getType(), TransferPlugin.class);
    assertNotNull(plugin);
    TransferSettings tsRestored = confRestored.getTransferSettings();
    assertNotNull(tsRestored);
    DummyTransferManager transferManager = plugin.createTransferManager(tsRestored, config);
    assertNotNull(transferManager);
    // Tear down
    FileUtils.deleteDirectory(localDir);
    FileUtils.deleteDirectory(repoDir);
}
Also used : DummyTransferPlugin(org.syncany.plugins.dummy.DummyTransferPlugin) TransferPlugin(org.syncany.plugins.transfer.TransferPlugin) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) InitOperationOptions(org.syncany.operations.init.InitOperationOptions) DummyTransferSettings(org.syncany.plugins.dummy.DummyTransferSettings) DummyTransferManager(org.syncany.plugins.dummy.DummyTransferManager) ConfigTO(org.syncany.config.to.ConfigTO) Persister(org.simpleframework.xml.core.Persister) LocalTransferSettings(org.syncany.plugins.local.LocalTransferSettings) TransferSettings(org.syncany.plugins.transfer.TransferSettings) DummyTransferSettings(org.syncany.plugins.dummy.DummyTransferSettings) File(java.io.File) Serializer(org.simpleframework.xml.Serializer) Test(org.junit.Test)

Aggregations

ConfigTO (org.syncany.config.to.ConfigTO)27 File (java.io.File)21 Test (org.junit.Test)18 RepoTO (org.syncany.config.to.RepoTO)17 Config (org.syncany.config.Config)15 InitOperationOptions (org.syncany.operations.init.InitOperationOptions)7 ConfigException (org.syncany.config.ConfigException)6 LocalTransferSettings (org.syncany.plugins.local.LocalTransferSettings)6 Random (java.util.Random)4 Persister (org.simpleframework.xml.core.Persister)4 ConnectOperationOptions (org.syncany.operations.init.ConnectOperationOptions)4 ArrayList (java.util.ArrayList)3 Serializer (org.simpleframework.xml.Serializer)3 TransformerTO (org.syncany.config.to.RepoTO.TransformerTO)3 SaltedSecretKey (org.syncany.crypto.SaltedSecretKey)3 ConnectOperation (org.syncany.operations.init.ConnectOperation)3 ConnectOperationResult (org.syncany.operations.init.ConnectOperationResult)3 InitOperation (org.syncany.operations.init.InitOperation)3 InitOperationResult (org.syncany.operations.init.InitOperationResult)3 TransferPlugin (org.syncany.plugins.transfer.TransferPlugin)3