arpadboda commented on a change in pull request #558: MINIFICPP-542 - Add 
PutSFTP processor
URL: https://github.com/apache/nifi-minifi-cpp/pull/558#discussion_r285025689
 
 

 ##########
 File path: extensions/sftp/client/SFTPClient.cpp
 ##########
 @@ -0,0 +1,713 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+#include "SFTPClient.h"
+#include <memory>
+#include <set>
+#include <vector>
+#include <string>
+#include <exception>
+#include <sstream>
+#include <iomanip>
+#include "utils/StringUtils.h"
+#include "utils/ScopeGuard.h"
+#include "utils/StringUtils.h"
+#include "utils/base64.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+#define SFTP_ERROR(CODE) case CODE: \
+                          return #CODE
+static const char* sftp_strerror(unsigned long err) {
+  switch (err) {
+    SFTP_ERROR(LIBSSH2_FX_OK);
+    SFTP_ERROR(LIBSSH2_FX_EOF);
+    SFTP_ERROR(LIBSSH2_FX_NO_SUCH_FILE);
+    SFTP_ERROR(LIBSSH2_FX_PERMISSION_DENIED);
+    SFTP_ERROR(LIBSSH2_FX_FAILURE);
+    SFTP_ERROR(LIBSSH2_FX_BAD_MESSAGE);
+    SFTP_ERROR(LIBSSH2_FX_NO_CONNECTION);
+    SFTP_ERROR(LIBSSH2_FX_CONNECTION_LOST);
+    SFTP_ERROR(LIBSSH2_FX_OP_UNSUPPORTED);
+    SFTP_ERROR(LIBSSH2_FX_INVALID_HANDLE);
+    SFTP_ERROR(LIBSSH2_FX_NO_SUCH_PATH);
+    SFTP_ERROR(LIBSSH2_FX_FILE_ALREADY_EXISTS);
+    SFTP_ERROR(LIBSSH2_FX_WRITE_PROTECT);
+    SFTP_ERROR(LIBSSH2_FX_NO_MEDIA);
+    SFTP_ERROR(LIBSSH2_FX_NO_SPACE_ON_FILESYSTEM);
+    SFTP_ERROR(LIBSSH2_FX_QUOTA_EXCEEDED);
+    SFTP_ERROR(LIBSSH2_FX_UNKNOWN_PRINCIPAL);
+    SFTP_ERROR(LIBSSH2_FX_LOCK_CONFLICT);
+    SFTP_ERROR(LIBSSH2_FX_DIR_NOT_EMPTY);
+    SFTP_ERROR(LIBSSH2_FX_NOT_A_DIRECTORY);
+    SFTP_ERROR(LIBSSH2_FX_INVALID_FILENAME);
+    SFTP_ERROR(LIBSSH2_FX_LINK_LOOP);
+    default:
+      return "Unknown error";
+  }
+}
+
+constexpr size_t SFTPClient::MAX_BUFFER_SIZE;
+
+SFTPClient::SFTPClient(const std::string &hostname, uint16_t port, const 
std::string& username)
+    : logger_(logging::LoggerFactory<SFTPClient>::getLogger()),
+      hostname_(hostname),
+      port_(port),
+      username_(username),
+      ssh_known_hosts_(nullptr),
+      strict_host_checking_(false),
+      password_authentication_enabled_(false),
+      public_key_authentication_enabled_(false),
+      data_timeout_(0),
+      send_keepalive_(false),
+      curl_errorbuffer_(CURL_ERROR_SIZE, '\0'),
+      easy_(nullptr),
+      ssh_session_(nullptr),
+      sftp_session_(nullptr),
+      connected_(false) {
+  SFTPClientInitializer::getInstance()->initialize();
+  easy_ = curl_easy_init();
+  if (easy_ == nullptr) {
+    throw std::runtime_error("Cannot create curl easy handle");
+  }
+  ssh_session_ = libssh2_session_init();
+  if (ssh_session_ == nullptr) {
+    curl_easy_cleanup(easy_);
+    throw std::runtime_error("Cannot create ssh session handler");
+  }
+}
+
+SFTPClient::~SFTPClient() {
+    if (sftp_session_ != nullptr) {
+      libssh2_sftp_shutdown(sftp_session_);
+    }
+    if (ssh_known_hosts_ != nullptr) {
+      libssh2_knownhost_free(ssh_known_hosts_);
+    }
+    if (ssh_session_ != nullptr) {
+      libssh2_session_disconnect(ssh_session_, "Normal Shutdown");
+      libssh2_session_free(ssh_session_);
+    }
+    if (easy_ != nullptr) {
+      curl_easy_cleanup(easy_);
+    }
+  logger_->log_trace("Closing SFTPClient for %s:%hu", hostname_, port_);
+}
+
+bool SFTPClient::setVerbose() {
+  if (curl_easy_setopt(easy_, CURLOPT_VERBOSE, 1L) != CURLE_OK) {
+    return false;
+  }
+  return true;
+}
+
+bool SFTPClient::setHostKeyFile(const std::string& host_key_file_path, bool 
strict_host_checking) {
+  if (ssh_known_hosts_ != nullptr) {
+    return false;
+  }
+  ssh_known_hosts_ = libssh2_knownhost_init(ssh_session_);
+  if (ssh_known_hosts_ == nullptr) {
+    char *err_msg = nullptr;
+    libssh2_session_last_error(ssh_session_, &err_msg, nullptr, 0);
+    logger_->log_error("Failed to init knownhost structure, error: %s", 
err_msg);
+    return false;
+  }
+  if (libssh2_knownhost_readfile(ssh_known_hosts_, host_key_file_path.c_str(), 
LIBSSH2_KNOWNHOST_FILE_OPENSSH) <= 0) {
+    char *err_msg = nullptr;
+    libssh2_session_last_error(ssh_session_, &err_msg, nullptr, 0);
+    logger_->log_error("Failed to read host file %s, error: %s", 
host_key_file_path.c_str(), err_msg);
+    return false;
+  }
+  strict_host_checking_ = strict_host_checking;
+  return true;
+}
+
+void SFTPClient::setPasswordAuthenticationCredentials(const std::string& 
password) {
+  password_authentication_enabled_ = true;
+  password_ = password;
+}
+
+void SFTPClient::setPublicKeyAuthenticationCredentials(const std::string& 
private_key_file_path, const std::string& private_key_passphrase) {
+  public_key_authentication_enabled_ = true;
+  private_key_file_path_ = private_key_file_path;
+  private_key_passphrase_ = private_key_passphrase;
+}
+
+bool SFTPClient::setProxy(ProxyType type, const utils::HTTPProxy& proxy) {
+  switch (type) {
+    case ProxyType::Http:
+      if (curl_easy_setopt(easy_, CURLOPT_PROXYTYPE, CURLPROXY_HTTP) != 
CURLE_OK) {
+        return false;
+      }
+      if (curl_easy_setopt(easy_, CURLOPT_HTTPPROXYTUNNEL, 1L) != CURLE_OK) {
+        return false;
+      }
+      break;
+    case ProxyType::Socks:
+      if (curl_easy_setopt(easy_, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5) != 
CURLE_OK) {
+        return false;
+      }
+      break;
+  }
+  std::stringstream proxy_string;
+  proxy_string << proxy.host << ":" << proxy.port;
+  if (curl_easy_setopt(easy_, CURLOPT_PROXY, proxy_string.str().c_str()) != 
CURLE_OK) {
+    return false;
+  }
+  return true;
+}
+
+bool SFTPClient::setConnectionTimeout(int64_t timeout) {
+  if (curl_easy_setopt(easy_, CURLOPT_CONNECTTIMEOUT_MS, timeout) != CURLE_OK) 
{
 
 Review comment:
   There are a couple of this, why not just 
   ```
   return (curl_easy_setopt(easy_, CURLOPT_CONNECTTIMEOUT_MS, timeout) == 
CURLE_OK);
   ```
   ?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to