Skip to content

Commit

Permalink
update develop
Browse files Browse the repository at this point in the history
  • Loading branch information
naalong committed Jun 8, 2021
2 parents ec8fb4d + 6744dfc commit 93b9b4f
Show file tree
Hide file tree
Showing 393 changed files with 66,392 additions and 287 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@ test-config.ini
/tests/web/coverage-data/*
!/tests/web/coverage-data/README.md

<<<<<<< HEAD

# Ignore dependencies directory
/dependencies

# Composer lock file
composer.lock
composer.*
=======
# Dependencies directory
/dependencies
>>>>>>> develop
141 changes: 67 additions & 74 deletions classes/ServerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

namespace IU\RedCapEtlModule;

use phpseclib\Crypt\RSA;
use phpseclib\Net\SCP;
use phpseclib\Net\SFTP;
use phpseclib\Net\SSH2;
use phpseclib3\Net\SFTP;
use phpseclib3\Crypt\PublicKeyLoader;


class ServerConfig implements \JsonSerializable
{
Expand Down Expand Up @@ -227,6 +226,9 @@ public function run($etlConfig, $isCronJob = false, $moduleLog = null, $runWorkf
$properties[Configuration::PRINT_LOGGING] = false;
}

$properties = $etlConfig->getPropertiesArray();
$properties[Configuration::PRINT_LOGGING] = false;

$logger = new \IU\REDCapETL\Logger('REDCap-ETL');
$logId = $logger->getLogId();

Expand All @@ -250,48 +252,13 @@ public function run($etlConfig, $isCronJob = false, $moduleLog = null, $runWorkf
# Remote server
#-------------------------------------------------

if (empty($this->serverAddress)) {
$message = $this->createServerErrorMessageForUser('no server address specified');
throw new \Exception($message);
}

if ($this->authMethod == self::AUTH_METHOD_PASSWORD) {
$ssh = new SSH2($this->serverAddress);
$ssh->login($username, $this->password);

} elseif ($this->authMethod == self::AUTH_METHOD_SSH_KEY) {
$keyFile = $this->getSshKeyFile();

if (empty($keyFile)) {
$message = $this->createServerErrorMessageForUser('no SSH key file was specified');
throw new \Exception($message);
}

#\REDCap::logEvent('REDCap-ETL run current user: '.get_current_user());

$key = new RSA();
$key->setPassword($this->sshKeyPassword);

$keyFileContents = file_get_contents($keyFile);

if ($keyFileContents === false) {
$message = $this->createServerErrorMessageForUser('SSH key file could not be accessed');
throw new \Exception($message);
}
$key->loadKey($keyFileContents);
$ssh = new SSH2($this->serverAddress);
$ssh->login($this->username, $key);
} else {
$message = $this->createServerErrorMessageForUser('unrecognized authentication menthod');
throw new \Exception($message);
}
$ssh = $this->getRemoteConnection();

#------------------------------------------------
# Copy configuration file and transformation
# rules file (if any) to the server.
#------------------------------------------------
$fileNameSuffix = uniqid('', true);
$scp = new SCP($ssh);

if ($runWorkflow) {
$propertiesJson = Configuration::getRedCapEtlJsonProperties($runWorkflow, $etlConfig);
Expand All @@ -300,10 +267,17 @@ public function run($etlConfig, $isCronJob = false, $moduleLog = null, $runWorkf
}
$configFileName = 'etl_config_' . $fileNameSuffix . '.json';
$configFilePath = $this->configDir . '/' . $configFileName;
$scpResult = $scp->put($configFilePath, $propertiesJson);
if (!$scpResult) {

$sftpResult = $ssh->put($configFilePath, $propertiesJson);
if (!$sftpResult) {
$message = 'Copy of ETL configuration to server failed.'
. ' Please contact your system administrator for assistance.';

$error = error_get_last();
if (isset($error) && is_array($error) && array_key_exists('message', $error)) {
$message .= " Error: " . $error['message'];
}

throw new \Exception($message);
}

Expand All @@ -325,51 +299,70 @@ public function run($etlConfig, $isCronJob = false, $moduleLog = null, $runWorkf
return $output;
}

/**
* Returns an ssh/ftp connection for the server represented by this class.
*
* @return SFTP ssh/sftp connection.
*/
public function getRemoteConnection()
{
$ssh = null;

if (empty($this->serverAddress)) {
$message = $this->createServerErrorMessageForUser('no server address specified');
throw new \Exception($message);
}

if ($this->authMethod == self::AUTH_METHOD_PASSWORD) {
$ssh = new SFTP($this->serverAddress); # SFTP class provides SSH functionality also
$ssh->login($this->username, $this->password);
} elseif ($this->authMethod == self::AUTH_METHOD_SSH_KEY) {
$keyFile = $this->getSshKeyFile();

if (empty($keyFile)) {
$message = $this->createServerErrorMessageForUser('no SSH key file was specified');
throw new \Exception($message);
}

$keyFileContents = file_get_contents($keyFile);

if ($keyFileContents === false) {
$message = $this->createServerErrorMessageForUser('SSH key file could not be accessed');
throw new \Exception($message);
}

$key = PublicKeyLoader::load($keyFileContents, $this->sshKeyPassword);
$ssh = new SFTP($this->serverAddress);
$ssh->login($this->username, $key);
} else {
$message = $this->createServerErrorMessageForUser('unrecognized authentication menthod');
throw new \Exception($message);
}
return $ssh;
}

public function test()
{
$testOutput = '';
try {
if ($this->isEmbeddedServer()) {
$testOutput = 'REDCap-ETL ' . \IU\REDCapETL\Version::RELEASE_NUMBER . ' found.';
} else {
$serverAddress = $this->getServerAddress();
if (empty($serverAddress)) {
throw new \Exception('No server address found.');
}

$username = $this->getUsername();
if ($this->getAuthMethod() == ServerConfig::AUTH_METHOD_SSH_KEY) {
$keyFile = $this->getSshKeyFile();
if (empty($keyFile)) {
throw new \Exception('SSH key file cound not be found.');
}

$keyPassword = $this->getSshKeyPassword();

$key = new RSA();

#if (!empty($keyPassword)) {
$key->setPassword($keyPassword);
#}

$keyFileContents = file_get_contents($keyFile);
if ($keyFileContents === false) {
throw new \Exception('SSH key file could not be accessed.');
}
$key->loadKey($keyFileContents);
$ssh = $this->getRemoteConnection();

$ssh = new SSH2($serverAddress);
$ssh->login($username, $key);
if ($this->getAuthMethod() == ServerConfig::AUTH_METHOD_SSH_KEY) {
$authMethod = "SSH key";
} else {
$password = $this->getPassword();

$ssh = new SSH2($serverAddress);
$ssh->login($username, $password);
$authMethod = "password";
}

$output = $ssh->exec('hostname');

if (!$output) {
$testOutput = "ERROR: ssh command failed.";
$err = $ssh->getLastError();
$testOutput = "ERROR: ssh command failed. Server: '{$this->serverAddress}'."
. " Username: '{$this->username}' . Authentication method: {$authMethod}."
;
} else {
$testOutput = "SUCCESS:\noutput of hostname command:\n"
. $output . "\n";
Expand Down
32 changes: 32 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name" : "iu-redcap/redcap-etl-module",
"license" : "BSD 3-Clause",
"description" : "REDCap-ETL Module",
"keywords" : [
"REDCap", "ETL", "REDCap-ETL"
],
"require" : {
"phpseclib/phpseclib" : "3.0.8",
"iu-redcap/redcap-etl" : "1.5.9"
},
"require-dev": {
"phpunit/phpunit": "9.5.2",
"squizlabs/php_codesniffer": "3.4.2",
"erusev/parsedown": "^1.7"
},
"autoload" : {
"psr-4" : {
"IU\\RedCapEtlModule\\" : ["./", "./classes/"]
}
},
"autoload-dev": {
"psr-4" : {
"IU\\RedCapEtlModule\\": [
"tests/unit"
],
"ExternalModules\\": [
"tests/external"
]
}
}
}
Loading

0 comments on commit 93b9b4f

Please sign in to comment.