Copilot commented on code in PR #2153:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2153#discussion_r3022772389
##########
extensions/gcp/controllerservices/GCPCredentialsControllerService.cpp:
##########
@@ -18,34 +18,36 @@
#include "GCPCredentialsControllerService.h"
-#include "core/Resource.h"
#include "google/cloud/storage/client.h"
-#include "utils/ProcessorConfigUtils.h"
-#include "utils/file/FileUtils.h"
namespace org::apache::nifi::minifi::extensions::gcp {
-void GCPCredentialsControllerService::initialize() {
- setSupportedProperties(Properties);
+namespace {
+// TODO(MINIFICPP-2763) use utils::file::get_content instead
+std::string get_content(const std::filesystem::path& file_name) {
+ std::ifstream file(file_name, std::ifstream::binary);
+ std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
+ return content;
+}
Review Comment:
get_content() does not validate that the file opened successfully or that
reading succeeded. If opening fails, it will return an empty string and
MakeServiceAccountCredentials() will be called with empty/partial content,
which can produce misleading failures. Add an explicit open/read failure check
and return nullptr (or an error status) from createCredentialsFromJsonPath()
when the file cannot be read.
##########
extensions/gcp/controllerservices/GCPCredentialsControllerService.cpp:
##########
@@ -18,34 +18,36 @@
#include "GCPCredentialsControllerService.h"
-#include "core/Resource.h"
#include "google/cloud/storage/client.h"
-#include "utils/ProcessorConfigUtils.h"
-#include "utils/file/FileUtils.h"
namespace org::apache::nifi::minifi::extensions::gcp {
-void GCPCredentialsControllerService::initialize() {
- setSupportedProperties(Properties);
+namespace {
+// TODO(MINIFICPP-2763) use utils::file::get_content instead
+std::string get_content(const std::filesystem::path& file_name) {
+ std::ifstream file(file_name, std::ifstream::binary);
+ std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
+ return content;
+}
}
-std::shared_ptr<google::cloud::Credentials>
GCPCredentialsControllerService::createCredentialsFromJsonPath() const {
- const auto json_path = getProperty(JsonFilePath.name);
+std::shared_ptr<google::cloud::Credentials>
GCPCredentialsControllerService::createCredentialsFromJsonPath(api::core::ControllerServiceContext&
ctx) const {
+ const auto json_path = ctx.getProperty(JsonFilePath.name);
if (!json_path) {
logger_->log_error("Missing or invalid {}", JsonFilePath.name);
return nullptr;
}
- if (!utils::file::exists(*json_path)) {
+ if (std::error_code ec; !std::filesystem::exists(*json_path, ec) || ec) {
logger_->log_error("JSON file for GCP credentials '{}' does not exist",
*json_path);
return nullptr;
}
- return
google::cloud::MakeServiceAccountCredentials(utils::file::get_content(*json_path));
+ return google::cloud::MakeServiceAccountCredentials(get_content(*json_path));
Review Comment:
get_content() does not validate that the file opened successfully or that
reading succeeded. If opening fails, it will return an empty string and
MakeServiceAccountCredentials() will be called with empty/partial content,
which can produce misleading failures. Add an explicit open/read failure check
and return nullptr (or an error status) from createCredentialsFromJsonPath()
when the file cannot be read.
##########
extensions/gcp/processors/GCSProcessor.cpp:
##########
@@ -17,47 +17,43 @@
#include "GCSProcessor.h"
-#include "utils/ProcessorConfigUtils.h"
-
#include "../controllerservices/GCPCredentialsControllerService.h"
-#include "minifi-cpp/core/ProcessContext.h"
-#include "core/ProcessSession.h"
+#include "api/utils/ProcessorConfigUtils.h"
namespace gcs = ::google::cloud::storage;
namespace org::apache::nifi::minifi::extensions::gcp {
-std::shared_ptr<google::cloud::Credentials>
GCSProcessor::getCredentials(core::ProcessContext& context) const {
- auto gcp_credentials_controller_service =
utils::parseOptionalControllerService<GCPCredentialsControllerService>(context,
GCSProcessor::GCPCredentials, getUUID());
- if (gcp_credentials_controller_service) {
+std::shared_ptr<google::cloud::Credentials> GCSProcessor::getCredentials(const
api::core::ProcessContext& context) {
+ if (const auto gcp_credentials_controller_service =
api::utils::parseOptionalControllerService<GCPCredentialsControllerService>(context,
+ GCPCredentials)) {
return gcp_credentials_controller_service->getCredentials();
}
return nullptr;
}
-void GCSProcessor::onSchedule(core::ProcessContext& context,
core::ProcessSessionFactory&) {
- if (auto number_of_retries = utils::parseOptionalU64Property(context,
NumberOfRetries)) {
+MinifiStatus GCSProcessor::onScheduleImpl(api::core::ProcessContext& context) {
+ if (const auto number_of_retries =
api::utils::parseOptionalU64Property(context, NumberOfRetries)) {
retry_policy_ =
std::make_shared<google::cloud::storage::LimitedErrorCountRetryPolicy>(gsl::narrow<int>(*number_of_retries));
}
gcp_credentials_ = getCredentials(context);
if (!gcp_credentials_) {
- throw minifi::Exception(ExceptionType::PROCESS_SCHEDULE_EXCEPTION,
"Missing GCP Credentials");
+ logger_->log_error("Couldnt find valid credentials");
Review Comment:
Fix typo in log message: 'Couldnt' -> 'Couldn't'.
```suggestion
logger_->log_error("Couldn't find valid credentials");
```
##########
extensions/gcp/processors/PutGCSObject.cpp:
##########
@@ -102,80 +102,78 @@ class UploadToGCSCallback {
} // namespace
-void PutGCSObject::initialize() {
- setSupportedProperties(Properties);
- setSupportedRelationships(Relationships);
-}
-
+MinifiStatus PutGCSObject::onScheduleImpl(api::core::ProcessContext& context) {
+ const auto status = GCSProcessor::onScheduleImpl(context);
+ if (status != MinifiStatus::MINIFI_STATUS_SUCCESS) {
+ return status;
+ }
-void PutGCSObject::onSchedule(core::ProcessContext& context,
core::ProcessSessionFactory& session_factory) {
- GCSProcessor::onSchedule(context, session_factory);
if (auto encryption_key = context.getProperty(EncryptionKey)) {
try {
encryption_key_ = gcs::EncryptionKey::FromBase64Key(*encryption_key);
} catch (const google::cloud::RuntimeStatusError&) {
throw minifi::Exception(ExceptionType::PROCESS_SCHEDULE_EXCEPTION,
"Could not decode the base64-encoded encryption key from property " +
std::string(EncryptionKey.name));
}
}
+ return MINIFI_STATUS_SUCCESS;
}
Review Comment:
onScheduleImpl() is now part of the status-based C API lifecycle, but it
still throws an exception on invalid EncryptionKey. This is inconsistent with
the other migrated processors (e.g., FetchGCSObject::onScheduleImpl returns a
non-success status) and can lead to unpredictable error handling during
scheduling. Prefer logging and returning a non-success MinifiStatus instead of
throwing from onScheduleImpl().
##########
extensions/gcp/ExtensionInitializer.cpp:
##########
@@ -0,0 +1,52 @@
+/**
+ * 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 "api/core/Resource.h"
+#include "api/utils/minifi-c-utils.h"
+#include "processors/DeleteGCSObject.h"
+#include "processors/FetchGCSObject.h"
+#include "processors/ListGCSBucket.h"
+#include "processors/PutGCSObject.h"
+
+#define MKSOC(x) #x
+#define MAKESTRING(x) MKSOC(x) // NOLINT(cppcoreguidelines-macro-usage)
+
+namespace minifi = org::apache::nifi::minifi;
+
+CEXTENSIONAPI const uint32_t MinifiApiVersion = MINIFI_API_VERSION;
+
+CEXTENSIONAPI void MinifiInitExtension(MinifiExtensionContext*
extension_context) {
+ MinifiExtensionCreateInfo ext_create_info{.name =
minifi::api::utils::toStringView(MAKESTRING(EXTENSION_NAME)),
+ .version =
minifi::api::utils::toStringView(MAKESTRING(EXTENSION_VERSION)),
+ .deinit = nullptr,
+ .user_data = nullptr};
+ auto* extension = MinifiCreateExtension(extension_context, &ext_create_info);
Review Comment:
MinifiCreateExtension(...) may fail and return nullptr; the current code
immediately dereferences/uses `extension` in downstream registration calls. Add
a null check after creation and return early (or otherwise signal failure) to
avoid a null dereference during extension initialization.
```suggestion
auto* extension = MinifiCreateExtension(extension_context,
&ext_create_info);
if (extension == nullptr) {
return;
}
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]