Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SFTP transport to deposit services #71

Merged
merged 5 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pass-deposit-services/deposit-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<amazon.sqs.version>2.1.1</amazon.sqs.version>
<mets-api.version>1.3</mets-api.version>
<handlebars.version>4.1.0</handlebars.version>
<sshd.version>2.11.0</sshd.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -83,11 +84,27 @@
<artifactId>commons-codec</artifactId>
</dependency>

<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
</dependency>

<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-sftp</artifactId>
<version>${sshd.version}</version>
</dependency>

<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
</dependency>

<dependency>
<groupId>org.swordapp</groupId>
<artifactId>sword2-client</artifactId>
</dependency>

<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
property = "protocol")
@JsonSubTypes({
@JsonSubTypes.Type(value = FtpBinding.class, name = FtpBinding.PROTO),
@JsonSubTypes.Type(value = SftpBinding.class, name = SftpBinding.PROTO),
@JsonSubTypes.Type(value = SwordV2Binding.class, name = SwordV2Binding.PROTO),
@JsonSubTypes.Type(value = FilesystemBinding.class, name = FilesystemBinding.PROTO)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2023 Johns Hopkins University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.pass.deposit.config.repository;

import static org.eclipse.pass.deposit.transport.Transport.TRANSPORT_AUTHMODE;
import static org.eclipse.pass.deposit.transport.Transport.TRANSPORT_PASSWORD;
import static org.eclipse.pass.deposit.transport.Transport.TRANSPORT_PROTOCOL;
import static org.eclipse.pass.deposit.transport.Transport.TRANSPORT_SERVER_FQDN;
import static org.eclipse.pass.deposit.transport.Transport.TRANSPORT_SERVER_PORT;
import static org.eclipse.pass.deposit.transport.Transport.TRANSPORT_USERNAME;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.eclipse.pass.deposit.transport.Transport;
import org.eclipse.pass.deposit.transport.sftp.SftpTransport;

/**
* @author Russ Poetker ([email protected])
*/
public class SftpBinding extends ProtocolBinding {

static final String PROTO = "sftp";

private String username;

private String password;

@JsonProperty("default-directory")
private String defaultDirectory;

public SftpBinding() {
this.setProtocol(PROTO);
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getDefaultDirectory() {
return defaultDirectory;
}

public void setDefaultDirectory(String defaultDirectory) {
this.defaultDirectory = defaultDirectory;
}

@Override
public Map<String, String> asPropertiesMap() {
Map<String, String> transportProperties = new HashMap<>();

transportProperties.put(TRANSPORT_USERNAME, getUsername());
transportProperties.put(TRANSPORT_PASSWORD, getPassword());
transportProperties.put(TRANSPORT_AUTHMODE, Transport.AUTHMODE.userpass.name());
transportProperties.put(TRANSPORT_PROTOCOL, Transport.PROTOCOL.sftp.name());
transportProperties.put(TRANSPORT_SERVER_FQDN, getServerFqdn());
transportProperties.put(TRANSPORT_SERVER_PORT, getServerPort());
transportProperties.put(SftpTransport.SFTP_BASE_DIRECTORY, getDefaultDirectory());

return transportProperties;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
SftpBinding that = (SftpBinding) o;
return Objects.equals(username, that.username) &&
Objects.equals(password, that.password) &&
Objects.equals(defaultDirectory, that.defaultDirectory);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), username, password, defaultDirectory);
}

@Override
public String toString() {
return "SftpBinding{" + "username='" + username + '\'' + ", password='" +
((password != null) ? "xxxx" : "<null>") + '\'' +
", defaultDirectory='" + defaultDirectory + '\'' + "} " + super.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ enum PROTOCOL {
http,
https,
ftp,
sftp,
SWORDv2,
filesystem
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ public TransportSession open(Map<String, String> hints) {
* @throws RuntimeException if the session cannot be successfully opened
*/
FtpTransportSession open(FTPClient ftpClient, Map<String, String> hints) {
String serverName = hints.get(Transport.TRANSPORT_SERVER_FQDN);
String serverPort = hints.get(Transport.TRANSPORT_SERVER_PORT);
String serverName = hints.get(TRANSPORT_SERVER_FQDN);
String serverPort = hints.get(TRANSPORT_SERVER_PORT);
String transferMode = hints.get(FtpTransportHints.TRANSFER_MODE);
String baseDir = hints.get(FtpTransportHints.BASE_DIRECTORY);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2023 Johns Hopkins University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.pass.deposit.transport.sftp;

import java.util.Map;

import org.eclipse.pass.deposit.transport.Transport;
import org.eclipse.pass.deposit.transport.TransportSession;
import org.springframework.stereotype.Component;

/**
* @author Russ Poetker ([email protected])
*/
@Component
public class SftpTransport implements Transport {

public static final String SFTP_BASE_DIRECTORY = "deposit.transport.protocol.sftp.basedir";

@Override
public PROTOCOL protocol() {
return PROTOCOL.sftp;
}

@Override
public TransportSession open(Map<String, String> hints) {
return new SftpTransportSession(hints);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2023 Johns Hopkins University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.pass.deposit.transport.sftp;

import org.eclipse.pass.deposit.transport.TransportResponse;

/**
* @author Russ Poetker ([email protected])
*/
class SftpTransportResponse implements TransportResponse {
private final boolean success;
private final Throwable throwable;

SftpTransportResponse(boolean success) {
this(success, null);
}

SftpTransportResponse(boolean success, Throwable throwable) {
this.success = success;
this.throwable = throwable;
}

@Override
public boolean success() {
return success;
}

@Override
public Throwable error() {
return throwable;
}
}
Loading
Loading