Skip to content

Commit

Permalink
Allow for different implementations.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher authored and chenkins committed Jun 20, 2023
1 parent 13fc7d1 commit a7abd5b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cli/src/main/java/ch/cyberduck/cli/Terminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public void uncaughtException(final Thread t, final Throwable e) {
source = SessionPoolFactory.create(connect, transcript, host,
new CertificateStoreX509TrustManager(new DisabledCertificateTrustCallback(), new DefaultTrustManagerHostnameCallback(host), new TerminalCertificateStore(reader)),
new PreferencesX509KeyManager(host, new TerminalCertificateStore(reader)),
VaultRegistryFactory.create(login));
VaultRegistryFactory.get(login));
final Path remote;
if(StringUtils.startsWith(new CommandLinePathParser(input, protocols).parse(uri).getAbsolute(), TildePathExpander.PREFIX)) {
final Path home = this.execute(new TerminalBackgroundAction<>(controller, source, new HomeFinderWorker()));
Expand Down Expand Up @@ -368,7 +368,7 @@ public void uncaughtException(final Thread t, final Throwable e) {
destination = SessionPoolFactory.create(connect, transcript, target,
new CertificateStoreX509TrustManager(new DisabledCertificateTrustCallback(), new DefaultTrustManagerHostnameCallback(target), new TerminalCertificateStore(reader)),
new PreferencesX509KeyManager(target, new TerminalCertificateStore(reader)),
VaultRegistryFactory.create(new TerminalPasswordCallback()));
VaultRegistryFactory.get(new TerminalPasswordCallback()));
return this.transfer(login, new CopyTransfer(
host, target, Collections.singletonMap(remote, new CommandLinePathParser(input, protocols).parse(input.getOptionValues(action.name())[1]))).withCache(cache),
source, destination);
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/ch/cyberduck/core/SessionPoolFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public static SessionPool create(final Controller controller, final Host bookmar
final LoginConnectionService connect = new LoginConnectionService(login, key, keychain, listener);
final CertificateStore certificates = CertificateStoreFactory.get();
return create(connect, transcript, bookmark,
new KeychainX509TrustManager(CertificateTrustCallbackFactory.get(controller), new DefaultTrustManagerHostnameCallback(bookmark), certificates),
new KeychainX509KeyManager(CertificateIdentityCallbackFactory.get(controller), bookmark, certificates),
VaultRegistryFactory.create(keychain, login), usage);
new KeychainX509TrustManager(CertificateTrustCallbackFactory.get(controller), new DefaultTrustManagerHostnameCallback(bookmark), certificates),
new KeychainX509KeyManager(CertificateIdentityCallbackFactory.get(controller), bookmark, certificates),
VaultRegistryFactory.get(keychain, login), usage);
}

public static SessionPool create(final ConnectionService connect, final TranscriptListener transcript,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import ch.cyberduck.core.updater.DisabledPeriodicUpdater;
import ch.cyberduck.core.updater.DisabledUpdateCheckerArguments;
import ch.cyberduck.core.urlhandler.DisabledSchemeHandler;
import ch.cyberduck.core.vault.DefaultVaultRegistry;
import ch.cyberduck.core.vault.DisabledVault;
import ch.cyberduck.core.webloc.InternetShortcutFileWriter;
import ch.cyberduck.ui.quicklook.ApplicationLauncherQuicklook;
Expand Down Expand Up @@ -511,6 +512,7 @@ protected void setFactories() {
this.setDefault("factory.threadpool.class", DefaultThreadPool.class.getName());
this.setDefault("factory.urlfilewriter.class", InternetShortcutFileWriter.class.getName());
this.setDefault("factory.vault.class", DisabledVault.class.getName());
this.setDefault("factory.vaultregistry.class", DefaultVaultRegistry.class.getName());
this.setDefault("factory.securerandom.class", DefaultSecureRandomProvider.class.getName());
this.setDefault("factory.providerhelpservice.class", DefaultProviderHelpService.class.getName());
this.setDefault("factory.quicklook.class", ApplicationLauncherQuicklook.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,54 @@
* GNU General Public License for more details.
*/

import ch.cyberduck.core.Factory;
import ch.cyberduck.core.FactoryException;
import ch.cyberduck.core.HostPasswordStore;
import ch.cyberduck.core.PasswordCallback;
import ch.cyberduck.core.PasswordStoreFactory;
import ch.cyberduck.core.preferences.PreferencesFactory;

public class VaultRegistryFactory {
import org.apache.commons.lang3.reflect.ConstructorUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public static VaultRegistry create(final PasswordCallback callback) {
return create(PasswordStoreFactory.get(), callback);
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class VaultRegistryFactory extends Factory<VaultRegistry> {
private static final Logger log = LogManager.getLogger(VaultRegistryFactory.class);

public VaultRegistryFactory() {
super("factory.vaultregistry.class");
}

public static VaultRegistry get(final PasswordCallback callback) {
return get(PasswordStoreFactory.get(), callback);
}

public static VaultRegistry create(final HostPasswordStore keychain, final PasswordCallback callback) {
public static VaultRegistry get(final HostPasswordStore keychain, final PasswordCallback callback) {
return PreferencesFactory.get().getBoolean("cryptomator.enable") ?
new DefaultVaultRegistry(keychain, callback) : VaultRegistry.DISABLED;
new VaultRegistryFactory().create(keychain, callback) : VaultRegistry.DISABLED;
}

public VaultRegistry create(final HostPasswordStore keychain, final PasswordCallback callback) {
if(null == clazz) {
throw new FactoryException(String.format("No implementation given for factory %s", this.getClass().getSimpleName()));
}
try {
final Constructor<? extends VaultRegistry> constructor = ConstructorUtils
.getMatchingAccessibleConstructor(clazz, keychain.getClass(), callback.getClass());
if(null == constructor) {
log.warn(String.format("No matching constructor for parameters %s,%s", keychain.getClass(), callback.getClass()));
// Call default constructor for disabled implementations
return clazz.getDeclaredConstructor().newInstance();
}
return constructor.newInstance(keychain, callback);
}
catch(InstantiationException | InvocationTargetException | IllegalAccessException |
NoSuchMethodException e) {
log.error(String.format("Failure loading callback class %s. %s", clazz, e.getMessage()));
return VaultRegistry.DISABLED;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ch.cyberduck.core.vault;

/*
* Copyright (c) 2002-2023 iterate GmbH. All rights reserved.
* https://cyberduck.io/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/

import ch.cyberduck.core.DisabledPasswordCallback;

import org.junit.Test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;

public class VaultRegistryFactoryTest {

@Test
public void testCreate() {
assertNotNull(VaultRegistryFactory.get(new DisabledPasswordCallback()));
assertNotSame(VaultRegistry.DISABLED, VaultRegistryFactory.get(new DisabledPasswordCallback()));
}
}

0 comments on commit a7abd5b

Please sign in to comment.