Search in sources :

Example 1 with ResolveFailedException

use of ch.cyberduck.core.exception.ResolveFailedException in project cyberduck by iterate-ch.

the class S3Session method login.

@Override
public void login(final Proxy proxy, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException {
    if (Scheme.isURL(host.getProtocol().getContext())) {
        try {
            final Credentials temporary = new AWSSessionCredentialsRetriever(trust, key, this, host.getProtocol().getContext()).get();
            client.setProviderCredentials(new AWSSessionCredentials(temporary.getUsername(), temporary.getPassword(), temporary.getToken()));
        } catch (ConnectionTimeoutException | ConnectionRefusedException | ResolveFailedException | NotfoundException | InteroperabilityException e) {
            log.warn(String.format("Failure to retrieve session credentials from . %s", e.getMessage()));
            throw new LoginFailureException(e.getDetail(false), e);
        }
    } else {
        final Credentials credentials;
        // Only for AWS
        if (isAwsHostname(host.getHostname())) {
            // Try auto-configure
            credentials = new STSCredentialsConfigurator(new ThreadLocalHostnameDelegatingTrustManager(trust, host.getHostname()), key, prompt).configure(host);
        } else {
            credentials = host.getCredentials();
        }
        if (StringUtils.isNotBlank(credentials.getToken())) {
            client.setProviderCredentials(credentials.isAnonymousLogin() ? null : new AWSSessionCredentials(credentials.getUsername(), credentials.getPassword(), credentials.getToken()));
        } else {
            client.setProviderCredentials(credentials.isAnonymousLogin() ? null : new AWSCredentials(credentials.getUsername(), credentials.getPassword()));
        }
    }
    if (host.getCredentials().isPassed()) {
        log.warn(String.format("Skip verifying credentials with previous successful authentication event for %s", this));
        return;
    }
    try {
        final Location.Name location = new S3PathStyleFallbackAdapter<>(this, new BackgroundExceptionCallable<Location.Name>() {

            @Override
            public Location.Name call() throws BackgroundException {
                return new S3LocationFeature(S3Session.this, client.getRegionEndpointCache()).getLocation(new DelegatingHomeFeature(new DefaultPathHomeFeature(host)).find());
            }
        }).call();
        if (log.isDebugEnabled()) {
            log.debug(String.format("Retrieved region %s", location));
        }
        if (!Location.unknown.equals(location)) {
            client.getConfiguration().setProperty("storage-service.default-region", location.getIdentifier());
        }
    } catch (AccessDeniedException | InteroperabilityException e) {
        log.warn(String.format("Failure %s querying region", e));
        final Path home = new DefaultHomeFinderService(this).find();
        if (log.isDebugEnabled()) {
            log.debug(String.format("Retrieved %s", home));
        }
    }
}
Also used : Path(ch.cyberduck.core.Path) NotfoundException(ch.cyberduck.core.exception.NotfoundException) AccessDeniedException(ch.cyberduck.core.exception.AccessDeniedException) InteroperabilityException(ch.cyberduck.core.exception.InteroperabilityException) DelegatingHomeFeature(ch.cyberduck.core.shared.DelegatingHomeFeature) DefaultPathHomeFeature(ch.cyberduck.core.shared.DefaultPathHomeFeature) DefaultHomeFinderService(ch.cyberduck.core.shared.DefaultHomeFinderService) ConnectionRefusedException(ch.cyberduck.core.exception.ConnectionRefusedException) ResolveFailedException(ch.cyberduck.core.exception.ResolveFailedException) AWSCredentials(org.jets3t.service.security.AWSCredentials) BackgroundExceptionCallable(ch.cyberduck.core.threading.BackgroundExceptionCallable) ConnectionTimeoutException(ch.cyberduck.core.exception.ConnectionTimeoutException) LoginFailureException(ch.cyberduck.core.exception.LoginFailureException) AWSSessionCredentials(org.jets3t.service.security.AWSSessionCredentials) ThreadLocalHostnameDelegatingTrustManager(ch.cyberduck.core.ssl.ThreadLocalHostnameDelegatingTrustManager) AWSSessionCredentialsRetriever(ch.cyberduck.core.auth.AWSSessionCredentialsRetriever) AWSCredentials(org.jets3t.service.security.AWSCredentials) Credentials(ch.cyberduck.core.Credentials) AWSSessionCredentials(org.jets3t.service.security.AWSSessionCredentials) STSCredentialsConfigurator(ch.cyberduck.core.sts.STSCredentialsConfigurator)

Example 2 with ResolveFailedException

use of ch.cyberduck.core.exception.ResolveFailedException in project cyberduck by iterate-ch.

the class Resolver method resolve.

/**
 * This method is blocking until the hostname has been resolved or the lookup has been canceled using #cancel
 *
 * @return The resolved IP address for this hostname
 * @throws ResolveFailedException   If the hostname cannot be resolved
 * @throws ResolveCanceledException If the lookup has been interrupted
 */
public InetAddress resolve(final String hostname, final CancelCallback callback) throws ResolveFailedException, ResolveCanceledException {
    final CountDownLatch signal = new CountDownLatch(1);
    final AtomicReference<InetAddress> resolved = new AtomicReference<>();
    final AtomicReference<UnknownHostException> failure = new AtomicReference<>();
    final Thread resolver = threadFactory.newThread(new Runnable() {

        @Override
        public void run() {
            try {
                final InetAddress[] allByName = InetAddress.getAllByName(hostname);
                Arrays.stream(allByName).findFirst().ifPresent(resolved::set);
                if (preferIPv6) {
                    Arrays.stream(allByName).filter(inetAddress -> inetAddress instanceof Inet6Address).findFirst().ifPresent(resolved::set);
                }
                if (log.isInfoEnabled()) {
                    log.info(String.format("Resolved %s to %s", hostname, resolved.get().getHostAddress()));
                }
            } catch (UnknownHostException e) {
                log.warn(String.format("Failed resolving %s", hostname));
                failure.set(e);
            } finally {
                signal.countDown();
            }
        }
    });
    resolver.start();
    log.debug(String.format("Waiting for resolving of %s", hostname));
    // Wait for #run to finish
    while (!Uninterruptibles.awaitUninterruptibly(signal, 500, TimeUnit.MILLISECONDS)) {
        try {
            callback.verify();
        } catch (ConnectionCanceledException c) {
            throw new ResolveCanceledException(MessageFormat.format(LocaleFactory.localizedString("DNS lookup for {0} failed", "Error"), hostname), c);
        }
    }
    try {
        callback.verify();
    } catch (ConnectionCanceledException c) {
        throw new ResolveCanceledException(MessageFormat.format(LocaleFactory.localizedString("DNS lookup for {0} failed", "Error"), hostname), c);
    }
    if (null == resolved.get()) {
        if (null == failure.get()) {
            log.warn(String.format("Canceled resolving %s", hostname));
            throw new ResolveCanceledException(MessageFormat.format(LocaleFactory.localizedString("DNS lookup for {0} failed", "Error"), hostname));
        }
        throw new ResolveFailedException(MessageFormat.format(LocaleFactory.localizedString("DNS lookup for {0} failed", "Error"), hostname), failure.get());
    }
    return resolved.get();
}
Also used : ResolveCanceledException(ch.cyberduck.core.exception.ResolveCanceledException) Arrays(java.util.Arrays) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) CancelCallback(ch.cyberduck.core.threading.CancelCallback) PreferencesFactory(ch.cyberduck.core.preferences.PreferencesFactory) ResolveFailedException(ch.cyberduck.core.exception.ResolveFailedException) UnknownHostException(java.net.UnknownHostException) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageFormat(java.text.MessageFormat) InetAddress(java.net.InetAddress) TimeUnit(java.util.concurrent.TimeUnit) Inet6Address(java.net.Inet6Address) CountDownLatch(java.util.concurrent.CountDownLatch) Logger(org.apache.logging.log4j.Logger) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) NamedThreadFactory(ch.cyberduck.core.threading.NamedThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) LogManager(org.apache.logging.log4j.LogManager) ResolveCanceledException(ch.cyberduck.core.exception.ResolveCanceledException) UnknownHostException(java.net.UnknownHostException) ConnectionCanceledException(ch.cyberduck.core.exception.ConnectionCanceledException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Inet6Address(java.net.Inet6Address) CountDownLatch(java.util.concurrent.CountDownLatch) ResolveFailedException(ch.cyberduck.core.exception.ResolveFailedException) InetAddress(java.net.InetAddress)

Example 3 with ResolveFailedException

use of ch.cyberduck.core.exception.ResolveFailedException in project cyberduck by iterate-ch.

the class LoginConnectionService method connect.

@Override
public void connect(final Session<?> session, final CancelCallback cancel) throws BackgroundException {
    if (session.isConnected()) {
        this.close(session);
    }
    final Host bookmark = session.getHost();
    // Try to resolve the hostname first
    final String hostname = HostnameConfiguratorFactory.get(bookmark.getProtocol()).getHostname(bookmark.getHostname());
    listener.message(MessageFormat.format(LocaleFactory.localizedString("Resolving {0}", "Status"), hostname));
    final Proxy proxy = this.proxy.find(new ProxyHostUrlProvider().get(bookmark));
    if (proxy == Proxy.DIRECT) {
        // Only try to resolve target hostname if direct connection
        if (null == JumpHostConfiguratorFactory.get(bookmark.getProtocol()).getJumphost(bookmark.getHostname())) {
            // Do not attempt to resolve hostname that may only be reachable in internal network from jump host
            try {
                resolver.resolve(hostname, cancel);
            } catch (ResolveFailedException e) {
                log.warn(String.format("DNS resolver failed for %s", hostname));
                throw e;
            }
        }
    }
    listener.message(MessageFormat.format(LocaleFactory.localizedString("Opening {0} connection to {1}", "Status"), bookmark.getProtocol().getName(), hostname));
    // The IP address could successfully be determined
    session.open(proxy, key, prompt, cancel);
    listener.message(MessageFormat.format(LocaleFactory.localizedString("{0} connection opened", "Status"), bookmark.getProtocol().getName()));
    // Update last accessed timestamp
    bookmark.setTimestamp(new Date());
    // Warning about insecure connection prior authenticating
    if (session.alert(prompt)) {
        // Warning if credentials are sent plaintext.
        prompt.warn(bookmark, MessageFormat.format(LocaleFactory.localizedString("Unsecured {0} connection", "Credentials"), bookmark.getProtocol().getName()), MessageFormat.format("{0} {1}.", MessageFormat.format(LocaleFactory.localizedString("{0} will be sent in plaintext.", "Credentials"), bookmark.getProtocol().getPasswordPlaceholder()), LocaleFactory.localizedString("Please contact your web hosting service provider for assistance", "Support")), LocaleFactory.localizedString("Continue", "Credentials"), LocaleFactory.localizedString("Disconnect", "Credentials"), String.format("connection.unsecure.%s", bookmark.getHostname()));
    }
    // Login
    try {
        this.authenticate(proxy, session, cancel);
    } catch (BackgroundException e) {
        this.close(session);
        throw e;
    }
}
Also used : Proxy(ch.cyberduck.core.proxy.Proxy) ProxyHostUrlProvider(ch.cyberduck.core.proxy.ProxyHostUrlProvider) ResolveFailedException(ch.cyberduck.core.exception.ResolveFailedException) Date(java.util.Date) BackgroundException(ch.cyberduck.core.exception.BackgroundException)

Aggregations

ResolveFailedException (ch.cyberduck.core.exception.ResolveFailedException)3 Credentials (ch.cyberduck.core.Credentials)1 Path (ch.cyberduck.core.Path)1 AWSSessionCredentialsRetriever (ch.cyberduck.core.auth.AWSSessionCredentialsRetriever)1 AccessDeniedException (ch.cyberduck.core.exception.AccessDeniedException)1 BackgroundException (ch.cyberduck.core.exception.BackgroundException)1 ConnectionCanceledException (ch.cyberduck.core.exception.ConnectionCanceledException)1 ConnectionRefusedException (ch.cyberduck.core.exception.ConnectionRefusedException)1 ConnectionTimeoutException (ch.cyberduck.core.exception.ConnectionTimeoutException)1 InteroperabilityException (ch.cyberduck.core.exception.InteroperabilityException)1 LoginFailureException (ch.cyberduck.core.exception.LoginFailureException)1 NotfoundException (ch.cyberduck.core.exception.NotfoundException)1 ResolveCanceledException (ch.cyberduck.core.exception.ResolveCanceledException)1 PreferencesFactory (ch.cyberduck.core.preferences.PreferencesFactory)1 Proxy (ch.cyberduck.core.proxy.Proxy)1 ProxyHostUrlProvider (ch.cyberduck.core.proxy.ProxyHostUrlProvider)1 DefaultHomeFinderService (ch.cyberduck.core.shared.DefaultHomeFinderService)1 DefaultPathHomeFeature (ch.cyberduck.core.shared.DefaultPathHomeFeature)1 DelegatingHomeFeature (ch.cyberduck.core.shared.DelegatingHomeFeature)1 ThreadLocalHostnameDelegatingTrustManager (ch.cyberduck.core.ssl.ThreadLocalHostnameDelegatingTrustManager)1