Skip to content

Commit

Permalink
8.0.3
Browse files Browse the repository at this point in the history
- Added enforcer plugin to possibly fix transitive dependencies issues (mainly related to uploading backups)
- RSA Key Handling: The sftp method now expects the path to the RSA key file instead of a Base64 string. It uses jsch.addIdentity(rsaKeyPath) to load the private key.
  • Loading branch information
Osiris-Team committed May 15, 2024
1 parent ced3167 commit e6dc487
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 55 deletions.
17 changes: 13 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@ gradle-app.setting
# Cache of project
.gradletasknamecache

# Eclipse Gradle plugin generated files
# Eclipse Core
.project
# JDT-specific (Eclipse Java Development Tools)
# IDEA
**/.idea/
**/*.iml

# Visual Studio Code
**/.vscode/

# MacOS
.DS_Store

# Eclipse
.settings
.classpath
.project
45 changes: 24 additions & 21 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
// Important for AutoPlugs Self-Updater!
// Also take look at the generateAutoplugProperties task where these properties get turned into the actual autoplug.properties file.
group = 'com.osiris.autoplug.client'
version = '8.0.2'
version = '8.0.3'
description = 'Responsible for all the main actions.'

java {
Expand Down Expand Up @@ -63,8 +63,25 @@ repositories {

dependencies {
// To make sure transitive dependencies do not include an older version. This must stay at the top.

implementation 'net.java.dev.jna:jna:5.12.1'
// See settings.gradle for converged/enforced transitive dependencies
implementation 'net.java.dev.jna:jna:5.14.0' // Also used in jsch https://github.com/mwiede/jsch/blob/master/pom.xml
implementation 'net.java.dev.jna:jna-platform:5.14.0'
// Also used in jsch https://github.com/mwiede/jsch/blob/master/pom.xml
implementation 'commons-io:commons-io:2.16.1'
// Also used in jsch https://github.com/mwiede/jsch/blob/master/pom.xml
implementation 'com.github.Osiris-Team:jansi:2.4.2'
implementation 'org.jetbrains:annotations:23.0.0'
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.9.23' // Also used in okhttp
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common:1.9.23' // Also used in okhttp
// To remove this annoying warning, add the dependency below:
// SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
// SLF4J: Defaulting to no-operation (NOP) logger implementation
// SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details
// https://mvnrepository.com/artifact/org.slf4j/slf4j-simple
// https://stackoverflow.com/questions/7421612/slf4j-failed-to-load-class-org-slf4j-impl-staticloggerbinder
implementation 'org.slf4j:slf4j-api:2.0.13'
implementation 'org.slf4j:slf4j-nop:2.0.13'

// https://github.com/Osiris-Team/AutoPlug-Core
implementation 'com.github.Osiris-Team:jlib:18.0'

Expand All @@ -89,32 +106,18 @@ dependencies {

// Requests
// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.2'

// https://mvnrepository.com/artifact/org.apache.commons/commons-io
implementation 'org.apache.commons:commons-io:1.3.2'

implementation 'commons-io:commons-io:2.7'
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.14'

implementation 'commons-lang:commons-lang:2.6'

// https://mvnrepository.com/artifact/commons-net/commons-net
implementation 'commons-net:commons-net:3.9.0'

// https://mvnrepository.com/artifact/com.jcraft/jsch
implementation 'com.jcraft:jsch:0.1.55'
implementation 'com.github.mwiede:jsch:0.2.17'

// Quartz Core
implementation 'org.quartz-scheduler:quartz:2.3.2'


// To remove this annoying warning, add the dependency below:
// SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
// SLF4J: Defaulting to no-operation (NOP) logger implementation
// SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details
// https://mvnrepository.com/artifact/org.slf4j/slf4j-simple
// https://stackoverflow.com/questions/7421612/slf4j-failed-to-load-class-org-slf4j-impl-staticloggerbinder
implementation 'org.slf4j:slf4j-api:2.0.13'
implementation 'org.slf4j:slf4j-nop:2.0.13'

implementation 'org.tomlj:tomlj:1.0.0'

Expand Down
42 changes: 42 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
rootProject.name = 'AutoPlug-Client'

buildscript {
repositories {
mavenCentral()
gradlePluginPortal()
}
dependencies {
classpath 'org.kordamp.gradle:enforcer-gradle-plugin:0.13.0'
}
}
apply plugin: 'org.kordamp.gradle.enforcer'

/*
Fails when the same dependency exists more than once, with different versions or if it's a SNAPSHOT.
If that is the case specify/add that dependency in <dependenciesManagement> (transitive dependency) above,
with a version that is compatible with all other dependencies, or
try to find an alternative dependency with similar functionality.
Note that having this check is crucial to avoid runtime "method not found" errors that will lead to crashes
and/or unexpected behaviour.
*/
enforce {
rule(enforcer.rules.DependencyConvergence) { r ->
r.enabled
r.enforcerLevel
r.phases
r.failOnDynamicVersions
r.failOnChangingVersions
r.failOnNonReproducibleResolution
r.activateDependencyLocking
r.deactivateDependencyLocking
}
rule(enforcer.rules.ForceDependencies) { r ->
r.dependencies.add('net.java.dev.jna:jna:5.14.0')
r.dependencies.add('net.java.dev.jna:jna-platform:5.14.0')
r.dependencies.add('commons-io:commons-io:2.16.1')
r.dependencies.add('com.github.Osiris-Team:jansi:2.4.2')
r.dependencies.add('org.jetbrains:annotations:23.0.0')
r.dependencies.add('org.jetbrains.kotlin:kotlin-stdlib:1.9.23')
r.dependencies.add('org.jetbrains.kotlin:kotlin-stdlib-common:1.9.23')
r.dependencies.add('org.slf4j:slf4j-api:2.0.13')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ public BackupConfig() throws IOException, DuplicateKeyException, YamlReaderExcep
backup_upload_password = put(name, "upload", "password");
backup_upload_path = put(name, "upload", "path").setComments(
"Set the folder, in which the backup should be stored.");
backup_upload_rsa = put(name, "upload", "rsa-key").setComments(
"Leave this field blank when using FTPS.");
backup_upload_rsa = put(name, "upload", "rsa-key-path").setComments(
"Leave this field blank when using FTPS.",
"Otherwise enter a relative or absolute path to the file containing the key.",
"Usually the path is something like this: /home/username/.ssh/id_rsa");

save();
unlockFile();
Expand Down
77 changes: 49 additions & 28 deletions src/main/java/com/osiris/autoplug/client/tasks/backup/Upload.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Osiris-Team.
* Copyright (c) 2024 Osiris-Team.
* All rights reserved.
*
* This software is copyrighted work, licensed under the terms
Expand All @@ -16,11 +16,21 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Base64;

/**
* @author kastenklicker
* Handles file uploads via SFTP and FTPS.
*
* Usage:
* - To upload via SFTP, call the {@link #sftp(String)} method with the RSA key path.
* - To upload via FTPS, call the {@link #ftps()} method.
*
* Ensure that the host, port, user, password, path, and zipFile are properly set via the constructor.
*
* Note: Password handling in this code is basic and should be improved for production use.
*
* Author: kastenklicker
*/
public class Upload {
private final String host, user, password, path;
Expand All @@ -37,54 +47,65 @@ public Upload(String host, int port, String user, String password, String path,
this.zipFile = zipFile;
}

public void sftp(String rsa) throws JSchException, SftpException {
JSch jSch = new JSch();
public void sftp(String rsaKeyPath) throws JSchException, SftpException, IOException {
JSch jsch = new JSch();

// Load the private key from the file
jsch.addIdentity(rsaKeyPath);

//HostKey verification
byte[] key = Base64.getDecoder().decode(rsa);
HostKey hostKey1 = new HostKey(host, key);
jSch.getHostKeyRepository().add(hostKey1, null);
// HostKey verification
// This is a basic implementation. Improve as needed.
//jsch.setKnownHosts("/path/to/known_hosts");

//Connect
Session session = jSch.getSession(user, host, port);
// Connect
Session session = jsch.getSession(user, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();

//Upload
channel.put(zipFile.getPath(), path + this.zipFile.getName());

//Disconnect
channel.exit();
session.disconnect();
try {
// Upload
channel.put(zipFile.getPath(), path + zipFile.getName());
} catch (SftpException e) {
throw e;
} finally {
// Disconnect
channel.disconnect();
session.disconnect();
}
}

public void ftps() throws Exception {

FileInputStream zipFileStream = new FileInputStream(zipFile);

public void ftps() throws IOException {
FTPSClient ftps = new FTPSClient();
ftps.setConnectTimeout(5000);

//Connect
// Connect
ftps.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
ftps.connect(host, port);
ftps.execPBSZ(0);
ftps.execPROT("P");
int reply = ftps.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftps.disconnect();
throw new Exception("Exception in connecting to FTPS Server.");
throw new IOException("Exception in connecting to FTPS Server.");
}
ftps.login(user, password);
ftps.setFileType(FTP.BINARY_FILE_TYPE);
ftps.enterLocalPassiveMode();

//Upload
if (!ftps.storeFile(path + zipFile.getName(), zipFileStream))
throw new Exception("Exception in uploading to FTPS Server.");
ftps.logout();
ftps.disconnect();
try (FileInputStream zipFileStream = new FileInputStream(zipFile)) {
// Upload
if (!ftps.storeFile(path + zipFile.getName(), zipFileStream)) {
throw new IOException("Exception in uploading to FTPS Server.");
}

} catch (IOException e) {
throw e;
} finally {
ftps.logout();
ftps.disconnect();
}
}
}

0 comments on commit e6dc487

Please sign in to comment.