Skip to content

Commit

Permalink
Add support to read S3 credentials from .aws/credentials and environm…
Browse files Browse the repository at this point in the history
…ent variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_KEY`.
  • Loading branch information
dkocher committed Nov 27, 2017
1 parent 138e09b commit 4670f30
Show file tree
Hide file tree
Showing 29 changed files with 310 additions and 81 deletions.
@@ -0,0 +1,56 @@
package ch.cyberduck.core.local;

/*
* Copyright (c) 2002-2017 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.Local;
import ch.cyberduck.core.transfer.TransferStatus;

public class FinderIconService implements IconService {

private final IconService[] delegates;

public FinderIconService() {
this(new WorkspaceIconService(), new FoundationProgressIconService());
}

public FinderIconService(final IconService... delegates) {
this.delegates = delegates;
}

@Override
public boolean set(final Local file, final String image) {
for(IconService delegate : delegates) {
delegate.set(file, image);
}
return true;
}

@Override
public boolean set(final Local file, final TransferStatus progress) {
for(IconService delegate : delegates) {
delegate.set(file, progress);
}
return true;
}

@Override
public boolean remove(final Local file) {
for(IconService delegate : delegates) {
delegate.remove(file);
}
return true;
}
}
10 changes: 10 additions & 0 deletions core/src/main/java/ch/cyberduck/core/AbstractProtocol.java
Expand Up @@ -264,4 +264,14 @@ public int hashCode() {
result = 31 * result + (this.getProvider() != null ? this.getProvider().hashCode() : 0);
return result;
}

@Override
public CredentialsConfigurator getCredentialsFinder() {
return new DisabledCredentialsConfigurator();
}

@Override
public HostnameConfigurator getHostnameFinder() {
return new DisabledHostnameConfigurator();
}
}
Expand Up @@ -18,46 +18,17 @@
* dkocher@cyberduck.ch
*/

import ch.cyberduck.core.sftp.openssh.OpenSSHCredentialsConfigurator;

public final class CredentialsConfiguratorFactory {

private CredentialsConfiguratorFactory() {
//
}

private static CredentialsConfigurator instance;

private static final Object lock = new Object();

/**
* @param protocol Protocol
* @return Configurator for default settings
*/
public static CredentialsConfigurator get(final Protocol protocol) {
if(protocol.getType() == Protocol.Type.sftp) {
synchronized(lock) {
if(null == instance) {
instance = new OpenSSHCredentialsConfigurator();
}
else {
instance.reload();
}
return instance;
}
}
return new NullCredentialsConfigurator();
}

private static final class NullCredentialsConfigurator implements CredentialsConfigurator {
@Override
public Credentials configure(final Host host) {
return host.getCredentials();
}

@Override
public void reload() {
//
}
return protocol.getCredentialsFinder();
}
}
}
@@ -0,0 +1,29 @@
package ch.cyberduck.core;

/*
* Copyright (c) 2002-2017 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.
*/

public final class DisabledCredentialsConfigurator implements CredentialsConfigurator {

@Override
public Credentials configure(final Host host) {
return host.getCredentials();
}

@Override
public void reload() {
//
}
}
@@ -0,0 +1,34 @@
package ch.cyberduck.core;

/*
* Copyright (c) 2002-2017 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.
*/

public final class DisabledHostnameConfigurator implements HostnameConfigurator {

@Override
public String getHostname(String alias) {
return alias;
}

@Override
public int getPort(final String alias) {
return -1;
}

@Override
public void reload() {
//
}
}
Expand Up @@ -18,51 +18,17 @@
* dkocher@cyberduck.ch
*/

import ch.cyberduck.core.sftp.openssh.OpenSSHHostnameConfigurator;

public final class HostnameConfiguratorFactory {

private HostnameConfiguratorFactory() {
//
}

private static HostnameConfigurator instance;

private static final Object lock = new Object();

/**
* @param protocol Protocol
* @return Configurator for default settings
*/
public static HostnameConfigurator get(final Protocol protocol) {
if(protocol.getType() == Protocol.Type.sftp) {
synchronized(lock) {
if(null == instance) {
instance = new OpenSSHHostnameConfigurator();
}
else {
instance.reload();
}
return instance;
}
}
return new NullHostnameConfigurator();
}

private static final class NullHostnameConfigurator implements HostnameConfigurator {
@Override
public String getHostname(String alias) {
return alias;
}

@Override
public int getPort(final String alias) {
return -1;
}

@Override
public void reload() {
//
}
return protocol.getHostnameFinder();
}
}
10 changes: 10 additions & 0 deletions core/src/main/java/ch/cyberduck/core/Profile.java
Expand Up @@ -245,6 +245,16 @@ public boolean validate(Credentials credentials, LoginOptions options) {
return parent.validate(credentials, options);
}

@Override
public CredentialsConfigurator getCredentialsFinder() {
return parent.getCredentialsFinder();
}

@Override
public HostnameConfigurator getHostnameFinder() {
return parent.getHostnameFinder();
}

@Override
public Scheme getScheme() {
final String v = this.value("Scheme");
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/ch/cyberduck/core/Protocol.java
Expand Up @@ -30,6 +30,10 @@ public interface Protocol extends Comparable<Protocol> {

boolean validate(Credentials credentials, LoginOptions options);

CredentialsConfigurator getCredentialsFinder();

HostnameConfigurator getHostnameFinder();

enum Type {
ftp,
sftp,
Expand Down
@@ -0,0 +1,64 @@
package ch.cyberduck.core.auth;

/*
* Copyright (c) 2002-2017 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.Credentials;
import ch.cyberduck.core.CredentialsConfigurator;
import ch.cyberduck.core.Host;
import ch.cyberduck.core.LoginOptions;

import org.apache.log4j.Logger;

import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;

public class AWSCredentialsConfigurator implements CredentialsConfigurator {
private static final Logger log = Logger.getLogger(AWSCredentialsConfigurator.class);

private final AWSCredentialsProvider[] providers;

public AWSCredentialsConfigurator(final AWSCredentialsProvider... providers) {
this.providers = providers;
}

@Override
public Credentials configure(final Host host) {
final Credentials credentials = host.getCredentials();
if(!credentials.validate(host.getProtocol(), new LoginOptions(host.getProtocol()))) {
for(AWSCredentialsProvider provider : providers) {
try {
final AWSCredentials c = provider.getCredentials();
credentials.setUsername(c.getAWSAccessKeyId());
credentials.setPassword(c.getAWSSecretKey());
break;
}
catch(SdkClientException e) {
log.debug(String.format("Ignore failure loading credentials from provider %s", provider));
// Continue searching with next provider
}
}
}
return credentials;
}

@Override
public void reload() {
for(AWSCredentialsProvider provider : providers) {
provider.refresh();
}
}
}
@@ -0,0 +1,25 @@
package ch.cyberduck.core.auth;

/*
* Copyright (c) 2002-2017 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 com.amazonaws.auth.EnvironmentVariableCredentialsProvider;

public class AWSEnviromentCredentialsConfigurator extends AWSCredentialsConfigurator {

public AWSEnviromentCredentialsConfigurator() {
super(new EnvironmentVariableCredentialsProvider());
}
}
@@ -0,0 +1,25 @@
package ch.cyberduck.core.auth;

/*
* Copyright (c) 2002-2017 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 com.amazonaws.auth.profile.ProfileCredentialsProvider;

public class AWSProfileCredentialsConfigurator extends AWSCredentialsConfigurator {

public AWSProfileCredentialsConfigurator() {
super(new ProfileCredentialsProvider());
}
}

0 comments on commit 4670f30

Please sign in to comment.