You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2021/11/15 13:10:23 UTC

[GitHub] [nifi-minifi-cpp] adam-markovics commented on a change in pull request #1211: MINIFICPP-1676 - Verify build id of extension before load

adam-markovics commented on a change in pull request #1211:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1211#discussion_r749214852



##########
File path: libminifi/include/core/extension/Utils.h
##########
@@ -0,0 +1,111 @@
+/**
+ * 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.
+ */
+
+#pragma once
+
+#include <optional>
+#include <functional>
+#include <utility>
+#include <memory>
+#include <string>
+
+#include "utils/gsl.h"
+#include "utils/file/FileView.h"
+#include "utils/StringUtils.h"
+
+namespace org::apache::nifi::minifi::core::extension::internal {
+
+template<typename Callback>
+struct Timer {
+  explicit Timer(Callback cb): cb_(std::move(cb)) {}
+  ~Timer() {
+    cb_(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_));
+  }
+  std::chrono::steady_clock::time_point start_{std::chrono::steady_clock::now()};
+  Callback cb_;
+};
+
+struct LibraryDescriptor {
+  std::string name;

Review comment:
       _ postfix is missing for members.

##########
File path: libminifi/include/utils/file/FileView.h
##########
@@ -0,0 +1,184 @@
+/**
+ * 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.
+ */
+
+#pragma once
+
+#include <filesystem>
+#include <fstream>
+#include <string>
+#include <memory>
+
+#include "utils/gsl.h"
+#include "core/logging/LoggerConfiguration.h"
+
+#ifdef WIN32
+#include <stdio.h>
+#include <Windows.h>
+#else
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <unistd.h>
+#endif
+
+namespace org::apache::nifi::minifi::utils::file {
+
+class FileView {
+  static std::string getLastError() {
+#ifdef WIN32
+    return std::system_category().message(GetLastError());
+#else
+    return std::system_category().message(errno);
+#endif
+  }
+#ifdef WIN32
+  struct FileHandle {
+    explicit FileHandle(const std::filesystem::path& file) {
+      handle_ = CreateFile(file.string().c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+      if (handle_ == INVALID_HANDLE_VALUE) {
+        throw Failure("Open failed: " + getLastError());
+      }
+    }
+    [[nodiscard]]
+    size_t getFileSize() const {
+      LARGE_INTEGER size;
+      if (!GetFileSizeEx(handle_, &size)) {
+        return static_cast<size_t>(-1);
+      }
+      return size.QuadPart;
+    }
+    ~FileHandle() {
+      CloseHandle(handle_);
+    }
+    HANDLE handle_;
+  };
+
+  struct FileMapping {
+    FileMapping(const FileHandle& file, size_t /*size*/): file_(file) {
+      mapping_ = CreateFileMapping(file_.handle_, NULL, PAGE_READONLY, 0, 0, NULL);
+      if (mapping_ == NULL) {
+        throw Failure("CreateFileMapping failed: " + getLastError());
+      }
+
+      data_ = MapViewOfFile(mapping_, FILE_MAP_READ, 0, 0, 0);
+      if (data_ == NULL) {
+        std::string view_error = getLastError();
+        if (!CloseHandle(mapping_)) {
+          logger_->log_error("CloseHandle failed: %s", getLastError());
+        }
+        throw Failure("MapViewOfFile failed: " + view_error);
+      }
+    }
+
+    [[nodiscard]]
+    const char* data() const {
+      return reinterpret_cast<const char*>(data_);
+    }
+
+    ~FileMapping() {
+      if (!UnmapViewOfFile(data_)) {
+        logger_->log_error("UnmapViewOfFile failed: %s", getLastError());
+      }
+      if (!CloseHandle(mapping_)) {
+        logger_->log_error("CloseHandle failed: %s", getLastError());
+      }
+    }
+
+    const FileHandle& file_;
+    HANDLE mapping_;
+    LPVOID data_;
+  };
+#else
+  struct FileHandle {
+    explicit FileHandle(const std::filesystem::path& file) {
+      fd_ = open(file.string().c_str(), O_RDONLY);
+      if (fd_ == -1) {
+        throw Failure("Open failed: " + getLastError());
+      }
+    }
+    [[nodiscard]]
+    size_t getFileSize() const {
+      return lseek(fd_, 0L, SEEK_END);
+    }
+    ~FileHandle() {
+      if (close(fd_) == -1) {
+        logger_->log_error("Failed to close file %d: %s", fd_, getLastError());
+      }
+    }
+    int fd_;
+  };
+  struct FileMapping {
+    FileMapping(const FileHandle& file, size_t size): file_(file), size_(size) {
+      data_ = mmap(nullptr, size_, PROT_READ, MAP_PRIVATE, file_.fd_, 0);
+      if (data_ == reinterpret_cast<void*>(-1)) {
+        throw Failure("Mmap failed: " + getLastError());
+      }
+    }
+    ~FileMapping() {
+      if (munmap(data_, size_) == -1) {
+        logger_->log_error("Failed to unmap file %d: %s", file_.fd_, getLastError());
+      }
+    }
+    [[nodiscard]]
+    const char* data() const {
+      return static_cast<const char*>(data_);
+    }
+
+    const FileHandle& file_;
+    void* data_;
+    size_t size_;
+  };
+#endif
+
+ public:
+  class Failure : public std::runtime_error {
+    using runtime_error::runtime_error;
+  };
+  explicit FileView(const std::filesystem::path& file): file_(file) {
+    file_size_ = file_.getFileSize();

Review comment:
       Why not std::filesystem::file_size? Did you want to use platform API everywhere?

##########
File path: libminifi/include/core/extension/Utils.h
##########
@@ -0,0 +1,111 @@
+/**
+ * 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.
+ */
+
+#pragma once
+
+#include <optional>
+#include <functional>
+#include <utility>
+#include <memory>
+#include <string>
+
+#include "utils/gsl.h"
+#include "utils/file/FileView.h"
+#include "utils/StringUtils.h"
+
+namespace org::apache::nifi::minifi::core::extension::internal {
+
+template<typename Callback>
+struct Timer {
+  explicit Timer(Callback cb): cb_(std::move(cb)) {}
+  ~Timer() {
+    cb_(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_));
+  }
+  std::chrono::steady_clock::time_point start_{std::chrono::steady_clock::now()};
+  Callback cb_;
+};
+
+struct LibraryDescriptor {
+  std::string name;
+  std::filesystem::path dir;
+  std::string filename;
+
+  [[nodiscard]]
+  bool verify(const std::shared_ptr<logging::Logger>& logger) const {
+    auto path = getFullPath();
+    const Timer timer{[&](const std::chrono::milliseconds elapsed) {
+      core::logging::LOG_DEBUG(logger) << "Verification for '" << path << "' took " << elapsed.count() << " ms";
+    }};
+    try {
+      utils::file::FileView file(path);
+      const std::string_view begin_marker = "__EXTENSION_BUILD_IDENTIFIER_BEGIN__";
+      const std::string_view end_marker = "__EXTENSION_BUILD_IDENTIFIER_END__";
+      auto build_id_begin = std::search(file.begin(), file.end(), begin_marker.begin(), begin_marker.end());
+      if (build_id_begin == file.end()) {
+        logger->log_error("Couldn't find start of build identifier in '%s'", path.string());
+        return false;
+      }
+      std::advance(build_id_begin, begin_marker.length());
+      auto build_id_end = std::search(build_id_begin, file.end(), end_marker.begin(), end_marker.end());
+      if (build_id_end == file.end()) {
+        logger->log_error("Couldn't find end of build identifier in '%s'", path.string());
+        return false;
+      }
+      std::string build_id(build_id_begin, build_id_end);
+      if (build_id != AgentBuild::BUILD_IDENTIFIER) {
+        logger->log_error("Build identifier does not match in '%s', expected '%s', got '%s'", path.string(), AgentBuild::BUILD_IDENTIFIER, build_id);
+        return false;
+      }
+    } catch (const utils::file::FileView::Failure& file_error) {
+      logger->log_error("Error while verifying library '%s': %s", path.string(), file_error.what());
+      return false;
+    }
+    return true;
+  }
+
+  [[nodiscard]]
+  std::filesystem::path getFullPath() const {
+    return dir / filename;
+  }
+};
+
+std::optional<LibraryDescriptor> asDynamicLibrary(const std::filesystem::path& path) {
+#if defined(WIN32)
+  const std::string extension = ".dll";
+#elif defined(__APPLE__)
+  const std::string extension = ".dylib";
+#else
+  const std::string extension = ".so";
+#endif
+
+#ifdef WIN32
+  const std::string prefix = "";
+#else
+  const std::string prefix = "lib";
+#endif
+  std::string filename = path.filename().string();

Review comment:
       This could also be const.

##########
File path: libminifi/include/core/extension/Utils.h
##########
@@ -0,0 +1,111 @@
+/**
+ * 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.
+ */
+
+#pragma once
+
+#include <optional>
+#include <functional>
+#include <utility>
+#include <memory>
+#include <string>
+
+#include "utils/gsl.h"
+#include "utils/file/FileView.h"
+#include "utils/StringUtils.h"
+
+namespace org::apache::nifi::minifi::core::extension::internal {
+
+template<typename Callback>
+struct Timer {
+  explicit Timer(Callback cb): cb_(std::move(cb)) {}
+  ~Timer() {
+    cb_(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_));
+  }
+  std::chrono::steady_clock::time_point start_{std::chrono::steady_clock::now()};
+  Callback cb_;
+};
+
+struct LibraryDescriptor {
+  std::string name;
+  std::filesystem::path dir;
+  std::string filename;
+
+  [[nodiscard]]
+  bool verify(const std::shared_ptr<logging::Logger>& logger) const {
+    auto path = getFullPath();
+    const Timer timer{[&](const std::chrono::milliseconds elapsed) {
+      core::logging::LOG_DEBUG(logger) << "Verification for '" << path << "' took " << elapsed.count() << " ms";
+    }};
+    try {
+      utils::file::FileView file(path);
+      const std::string_view begin_marker = "__EXTENSION_BUILD_IDENTIFIER_BEGIN__";
+      const std::string_view end_marker = "__EXTENSION_BUILD_IDENTIFIER_END__";
+      auto build_id_begin = std::search(file.begin(), file.end(), begin_marker.begin(), begin_marker.end());
+      if (build_id_begin == file.end()) {
+        logger->log_error("Couldn't find start of build identifier in '%s'", path.string());
+        return false;
+      }
+      std::advance(build_id_begin, begin_marker.length());
+      auto build_id_end = std::search(build_id_begin, file.end(), end_marker.begin(), end_marker.end());
+      if (build_id_end == file.end()) {
+        logger->log_error("Couldn't find end of build identifier in '%s'", path.string());
+        return false;
+      }
+      std::string build_id(build_id_begin, build_id_end);
+      if (build_id != AgentBuild::BUILD_IDENTIFIER) {
+        logger->log_error("Build identifier does not match in '%s', expected '%s', got '%s'", path.string(), AgentBuild::BUILD_IDENTIFIER, build_id);
+        return false;
+      }
+    } catch (const utils::file::FileView::Failure& file_error) {
+      logger->log_error("Error while verifying library '%s': %s", path.string(), file_error.what());
+      return false;
+    }
+    return true;
+  }
+
+  [[nodiscard]]
+  std::filesystem::path getFullPath() const {
+    return dir / filename;
+  }
+};
+
+std::optional<LibraryDescriptor> asDynamicLibrary(const std::filesystem::path& path) {
+#if defined(WIN32)
+  const std::string extension = ".dll";

Review comment:
       These could be static.




-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org