From 69b0490adc653abc7654e179739f3dfc9b6fc6c9 Mon Sep 17 00:00:00 2001 From: Ishan Gupta-AWS <74676865+ig15@users.noreply.github.com> Date: Wed, 20 Nov 2024 12:55:09 +0530 Subject: [PATCH] Fix clang-formatting issues (#477) * Modified condition to create default sample shadow directory * Updated versions in build.yml for aws credential fetch * Updated build.yml to revert version of configure-aws-credentials to v1 * Updated aws-credentials fetching mechanism version in build.yml * Add job document comparator to ignore pre-signed difference in s3 url * Remove unit test for the compareJobsDocument and make it private * Refactor compareJobDocuments comments * Fix clang-format in multiple files --------- Co-authored-by: Ubuntu --- source/config/Config.cpp | 8 ++- .../fleetprovisioning/FleetProvisioning.cpp | 57 ++++++++++++------- source/fleetprovisioning/FleetProvisioning.h | 12 ++-- source/jobs/JobsFeature.cpp | 38 +++++++------ source/jobs/JobsFeature.h | 7 +-- test/jobs/TestJobsFeature.cpp | 2 +- 6 files changed, 77 insertions(+), 47 deletions(-) diff --git a/source/config/Config.cpp b/source/config/Config.cpp index 33e45763..585e48b2 100644 --- a/source/config/Config.cpp +++ b/source/config/Config.cpp @@ -1773,13 +1773,17 @@ bool PlainConfig::SampleShadow::LoadFromCliArgs(const CliArgs &cliArgs) .c_str(); } - // setting `shadowOutputFile` value to default if no value was passed by user via CLI or JSON config, provided sample shadow feature is enabled. + // setting `shadowOutputFile` value to default if no value was passed by user via CLI or JSON config, provided + // sample shadow feature is enabled. if (enabled && ((!shadowOutputFile.has_value() || shadowOutputFile->empty()) && !createShadowOutputFile())) { return false; } - LOGM_INFO(Config::TAG, "Not creating directory %s since sample shadow is disabled", Config::DEFAULT_SAMPLE_SHADOW_OUTPUT_DIR); + LOGM_INFO( + Config::TAG, + "Not creating directory %s since sample shadow is disabled", + Config::DEFAULT_SAMPLE_SHADOW_OUTPUT_DIR); return true; } diff --git a/source/fleetprovisioning/FleetProvisioning.cpp b/source/fleetprovisioning/FleetProvisioning.cpp index 8ef4c8f6..b815536f 100644 --- a/source/fleetprovisioning/FleetProvisioning.cpp +++ b/source/fleetprovisioning/FleetProvisioning.cpp @@ -60,8 +60,7 @@ bool FleetProvisioning::WriteKeyAndCertToDirectory(CreateKeysAndCertificateRespo if (FileUtils::StoreValueInFile(response->CertificatePem->c_str(), certPath.c_str()) && FileUtils::StoreValueInFile(response->PrivateKey->c_str(), keyPath.c_str())) { - LOGM_INFO( - TAG, "Stored certificate and private key in %s and %s files", certPath.c_str(), keyPath.c_str()); + LOGM_INFO(TAG, "Stored certificate and private key in %s and %s files", certPath.c_str(), keyPath.c_str()); LOG_INFO(TAG, "Attempting to set permissions for certificate and private key..."); chmod(certPath.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); @@ -121,7 +120,8 @@ bool FleetProvisioning::WriteCSRCertToDirectory(CreateCertificateFromCsrResponse bool FleetProvisioning::CreateCertificateAndKey(Iotidentity::IotIdentityClient identityClient) { LOG_INFO(TAG, "Provisioning new device certificate and private key using CreateKeysAndCertificate API"); - auto onKeysAcceptedSubAck = [this](int ioErr) { + auto onKeysAcceptedSubAck = [this](int ioErr) + { if (ioErr != AWS_OP_SUCCESS) { LOGM_ERROR( @@ -133,7 +133,8 @@ bool FleetProvisioning::CreateCertificateAndKey(Iotidentity::IotIdentityClient i keysAcceptedCompletedPromise.set_value(ioErr == AWS_OP_SUCCESS); }; - auto onKeysRejectedSubAck = [this](int ioErr) { + auto onKeysRejectedSubAck = [this](int ioErr) + { if (ioErr != AWS_OP_SUCCESS) { LOGM_ERROR( @@ -145,7 +146,8 @@ bool FleetProvisioning::CreateCertificateAndKey(Iotidentity::IotIdentityClient i keysRejectedCompletedPromise.set_value(ioErr == AWS_OP_SUCCESS); }; - auto onKeysPublishSubAck = [this](int ioErr) { + auto onKeysPublishSubAck = [this](int ioErr) + { if (ioErr != AWS_OP_SUCCESS) { LOGM_ERROR( @@ -157,13 +159,15 @@ bool FleetProvisioning::CreateCertificateAndKey(Iotidentity::IotIdentityClient i keysPublishCompletedPromise.set_value(ioErr == AWS_OP_SUCCESS); }; - auto onKeysAccepted = [this](CreateKeysAndCertificateResponse *response, int ioErr) { + auto onKeysAccepted = [this](CreateKeysAndCertificateResponse *response, int ioErr) + { if (ioErr == AWS_OP_SUCCESS) { LOGM_INFO(TAG, "CreateKeysAndCertificateResponse certificateId: %s.", response->CertificateId->c_str()); certificateOwnershipToken = *response->CertificateOwnershipToken; Aws::Crt::String certificateID = response->CertificateId->c_str(); - bool writeSucceeded = WriteKeyAndCertToDirectory(response, certificateID.c_str()) && WriteKeyAndCertToDirectory(response, "active"); + bool writeSucceeded = WriteKeyAndCertToDirectory(response, certificateID.c_str()) && + WriteKeyAndCertToDirectory(response, "active"); keysCreationCompletedPromise.set_value(writeSucceeded); } else @@ -177,7 +181,8 @@ bool FleetProvisioning::CreateCertificateAndKey(Iotidentity::IotIdentityClient i } }; - auto onKeysRejected = [this](ErrorResponse *error, int ioErr) { + auto onKeysRejected = [this](ErrorResponse *error, int ioErr) + { if (ioErr == AWS_OP_SUCCESS) { LOGM_ERROR( @@ -252,7 +257,8 @@ bool FleetProvisioning::CreateCertificateAndKey(Iotidentity::IotIdentityClient i bool FleetProvisioning::CreateCertificateUsingCSR(Iotidentity::IotIdentityClient identityClient) { LOG_INFO(TAG, "Provisioning new device certificate using CreateCertificateFromCsr API"); - auto onCsrAcceptedSubAck = [this](int ioErr) { + auto onCsrAcceptedSubAck = [this](int ioErr) + { if (ioErr != AWS_OP_SUCCESS) { LOGM_ERROR( @@ -264,7 +270,8 @@ bool FleetProvisioning::CreateCertificateUsingCSR(Iotidentity::IotIdentityClient csrAcceptedCompletedPromise.set_value(ioErr == AWS_OP_SUCCESS); }; - auto onCsrRejectedSubAck = [this](int ioErr) { + auto onCsrRejectedSubAck = [this](int ioErr) + { if (ioErr != AWS_OP_SUCCESS) { LOGM_ERROR( @@ -276,7 +283,8 @@ bool FleetProvisioning::CreateCertificateUsingCSR(Iotidentity::IotIdentityClient csrRejectedCompletedPromise.set_value(ioErr == AWS_OP_SUCCESS); }; - auto onCsrPublishSubAck = [this](int ioErr) { + auto onCsrPublishSubAck = [this](int ioErr) + { if (ioErr != AWS_OP_SUCCESS) { LOGM_ERROR( @@ -288,13 +296,15 @@ bool FleetProvisioning::CreateCertificateUsingCSR(Iotidentity::IotIdentityClient csrPublishCompletedPromise.set_value(ioErr == AWS_OP_SUCCESS); }; - auto onCsrAccepted = [this](CreateCertificateFromCsrResponse *response, int ioErr) { + auto onCsrAccepted = [this](CreateCertificateFromCsrResponse *response, int ioErr) + { if (ioErr == AWS_OP_SUCCESS) { LOGM_INFO(TAG, "CreateCertificateFromCsrResponse certificateId: %s. ***", response->CertificateId->c_str()); certificateOwnershipToken = *response->CertificateOwnershipToken; Aws::Crt::String certificateID = response->CertificateId->c_str(); - bool writeSucceeded = WriteCSRCertToDirectory(response, certificateID.c_str()) && WriteCSRCertToDirectory(response, "active"); + bool writeSucceeded = + WriteCSRCertToDirectory(response, certificateID.c_str()) && WriteCSRCertToDirectory(response, "active"); csrCreationCompletedPromise.set_value(writeSucceeded); } else @@ -308,7 +318,8 @@ bool FleetProvisioning::CreateCertificateUsingCSR(Iotidentity::IotIdentityClient } }; - auto onCsrRejected = [this](ErrorResponse *error, int ioErr) { + auto onCsrRejected = [this](ErrorResponse *error, int ioErr) + { if (ioErr == AWS_OP_SUCCESS) { LOGM_ERROR( @@ -386,7 +397,8 @@ bool FleetProvisioning::CreateCertificateUsingCSR(Iotidentity::IotIdentityClient } bool FleetProvisioning::RegisterThing(Iotidentity::IotIdentityClient identityClient) { - auto onRegisterAcceptedSubAck = [this](int ioErr) { + auto onRegisterAcceptedSubAck = [this](int ioErr) + { if (ioErr != AWS_OP_SUCCESS) { LOGM_ERROR( @@ -398,7 +410,8 @@ bool FleetProvisioning::RegisterThing(Iotidentity::IotIdentityClient identityCli registerAcceptedCompletedPromise.set_value(ioErr == AWS_OP_SUCCESS); }; - auto onRegisterRejectedSubAck = [this](int ioErr) { + auto onRegisterRejectedSubAck = [this](int ioErr) + { if (ioErr != AWS_OP_SUCCESS) { LOGM_ERROR( @@ -410,7 +423,8 @@ bool FleetProvisioning::RegisterThing(Iotidentity::IotIdentityClient identityCli registerRejectedCompletedPromise.set_value(ioErr == AWS_OP_SUCCESS); }; - auto onRegisterPublishSubAck = [this](int ioErr) { + auto onRegisterPublishSubAck = [this](int ioErr) + { if (ioErr != AWS_OP_SUCCESS) { LOGM_ERROR( @@ -422,7 +436,8 @@ bool FleetProvisioning::RegisterThing(Iotidentity::IotIdentityClient identityCli registerPublishCompletedPromise.set_value(ioErr == AWS_OP_SUCCESS); }; - auto onRegisterAccepted = [this](RegisterThingResponse *response, int ioErr) { + auto onRegisterAccepted = [this](RegisterThingResponse *response, int ioErr) + { if (ioErr == AWS_OP_SUCCESS) { LOGM_INFO(TAG, "RegisterThingResponse ThingName: %s.", response->ThingName->c_str()); @@ -437,7 +452,8 @@ bool FleetProvisioning::RegisterThing(Iotidentity::IotIdentityClient identityCli registerThingCompletedPromise.set_value(ioErr == AWS_OP_SUCCESS); }; - auto onRegisterRejected = [this](ErrorResponse *error, int ioErr) { + auto onRegisterRejected = [this](ErrorResponse *error, int ioErr) + { if (ioErr == AWS_OP_SUCCESS) { LOGM_ERROR( @@ -529,7 +545,8 @@ bool FleetProvisioning::ProvisionDevice(shared_ptr fpC LOG_INFO(TAG, "Fleet Provisioning Feature has been started."); collectSystemInformation = config.fleetProvisioning.collectSystemInformation; - bool didSetup = FileUtils::CreateDirectoryWithPermissions(keyDir.c_str(), S_IRWXU) && FileUtils::CreateDirectoryWithPermissions( + bool didSetup = FileUtils::CreateDirectoryWithPermissions(keyDir.c_str(), S_IRWXU) && + FileUtils::CreateDirectoryWithPermissions( Config::DEFAULT_CONFIG_DIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH | S_IXOTH); if (!didSetup) { diff --git a/source/fleetprovisioning/FleetProvisioning.h b/source/fleetprovisioning/FleetProvisioning.h index 062f9360..57982000 100644 --- a/source/fleetprovisioning/FleetProvisioning.h +++ b/source/fleetprovisioning/FleetProvisioning.h @@ -172,7 +172,9 @@ namespace Aws * @param response from IotIdentity CreateKeysAndCertificate call * @return returns true if successfully written to directory */ - bool WriteKeyAndCertToDirectory(Iotidentity::CreateKeysAndCertificateResponse *response, std::string fileName); + bool WriteKeyAndCertToDirectory( + Iotidentity::CreateKeysAndCertificateResponse *response, + std::string fileName); /** * \brief writes contents of the cert from CSR to the device client config directory. @@ -180,7 +182,9 @@ namespace Aws * @param response from IotIdentity CreateCertificateFromCsrResponse call * @return returns true if successfully written to directory */ - bool WriteCSRCertToDirectory(Iotidentity::CreateCertificateFromCsrResponse *response, std::string fileName); + bool WriteCSRCertToDirectory( + Iotidentity::CreateCertificateFromCsrResponse *response, + std::string fileName); /** * \brief creates a new certificate and private key using the AWS certificate authority @@ -274,6 +278,6 @@ namespace Aws bool ObtainCertificateSerialID(const char *certPath); }; } // namespace FleetProvisioningNS - } // namespace DeviceClient - } // namespace Iot + } // namespace DeviceClient + } // namespace Iot } // namespace Aws diff --git a/source/jobs/JobsFeature.cpp b/source/jobs/JobsFeature.cpp index d036339b..0b00048a 100644 --- a/source/jobs/JobsFeature.cpp +++ b/source/jobs/JobsFeature.cpp @@ -425,7 +425,8 @@ void JobsFeature::publishUpdateJobExecutionStatusWithRetry( retryConfig.needStopFlag = nullptr; } - auto publishLambda = [this, data, statusInfo, statusDetails]() -> bool { + auto publishLambda = [this, data, statusInfo, statusDetails]() -> bool + { // We first need to make sure that we haven't previously leaked any promises into our map unique_lock leakLock(updateJobExecutionPromisesLock); for (auto keyPromise = updateJobExecutionPromises.cbegin(); keyPromise != updateJobExecutionPromises.cend(); @@ -513,9 +514,9 @@ void JobsFeature::publishUpdateJobExecutionStatusWithRetry( this->updateJobExecutionPromises.erase(clientToken.c_str()); return finished; }; - std::thread updateJobExecutionThread([retryConfig, publishLambda, onCompleteCallback] { - Retry::exponentialBackoff(retryConfig, publishLambda, onCompleteCallback); - }); + std::thread updateJobExecutionThread( + [retryConfig, publishLambda, onCompleteCallback] + { Retry::exponentialBackoff(retryConfig, publishLambda, onCompleteCallback); }); updateJobExecutionThread.detach(); } @@ -527,8 +528,8 @@ void JobsFeature::copyJobsNotification(Iotjobs::JobExecutionData job) latestJobsNotification.ExecutionNumber = job.ExecutionNumber.value(); } - -bool JobsFeature::compareJobDocuments(const Aws::Crt::JsonObject& job1, const Aws::Crt::JsonObject& job2) { +bool JobsFeature::compareJobDocuments(const Aws::Crt::JsonObject &job1, const Aws::Crt::JsonObject &job2) +{ std::string str1 = job1.View().WriteCompact().c_str(); std::string str2 = job2.View().WriteCompact().c_str(); @@ -536,14 +537,16 @@ bool JobsFeature::compareJobDocuments(const Aws::Crt::JsonObject& job1, const Aw std::regex s3UrlRegex(R"((https://[^.\s"]+\.s3[.-](?:[^.\s"]+\.)?amazonaws\.com/[^?\s"]+)(\?[^"\s]+)?)"); // Function to replace only the pre-signed portion of S3 URLs - auto processPresignedUrls = [&s3UrlRegex](std::string& s) -> int { + auto processPresignedUrls = [&s3UrlRegex](std::string &s) -> int + { int count = 0; std::string result; std::sregex_iterator it(s.begin(), s.end(), s3UrlRegex); std::sregex_iterator end; size_t lastPos = 0; - for (; it != end; ++it) { + for (; it != end; ++it) + { count++; // Adding the rest of the job document before the s3 URL @@ -553,7 +556,7 @@ bool JobsFeature::compareJobDocuments(const Aws::Crt::JsonObject& job1, const Aw result += it->str(1); // Replace only the presigned portion with the placeholder - result += "?aws:iot:s3-presigned-suffix:PLACEHOLDER"; + result += "?aws:iot:s3-presigned-suffix:PLACEHOLDER"; lastPos = it->position() + it->length(); } result += s.substr(lastPos); @@ -566,7 +569,8 @@ bool JobsFeature::compareJobDocuments(const Aws::Crt::JsonObject& job1, const Aw int count2 = processPresignedUrls(str2); // If the number of pre-signed URLs differs, the documents are different - if (count1 != count2) { + if (count1 != count2) + { return false; } return str1 == str2; @@ -588,9 +592,8 @@ bool JobsFeature::isDuplicateNotification(JobExecutionData job) return false; } - if (!compareJobDocuments( - job.JobDocument.value(), - latestJobsNotification.JobDocument.value())) { + if (!compareJobDocuments(job.JobDocument.value(), latestJobsNotification.JobDocument.value())) + { LOG_DEBUG(TAG, "Job document differs"); return false; } @@ -607,7 +610,8 @@ bool JobsFeature::isDuplicateNotification(JobExecutionData job) void JobsFeature::initJob(const JobExecutionData &job) { - auto shutdownHandler = [this]() -> void { + auto shutdownHandler = [this]() -> void + { handlingJob.store(false); if (needStop.load()) { @@ -638,7 +642,8 @@ void JobsFeature::executeJob(const Iotjobs::JobExecutionData &job, const PlainJo { LOGM_INFO(TAG, "Executing job: %s", job.JobId->c_str()); - auto shutdownHandler = [this]() -> void { + auto shutdownHandler = [this]() -> void + { handlingJob.store(false); if (needStop.load()) { @@ -647,7 +652,8 @@ void JobsFeature::executeJob(const Iotjobs::JobExecutionData &job, const PlainJo } }; // TODO: Add support for checking condition - auto runJob = [this, job, jobDocument, shutdownHandler]() { + auto runJob = [this, job, jobDocument, shutdownHandler]() + { auto engine = createJobEngine(); // execute all action steps in sequence as provided in job document int executionStatus = engine->exec_steps(jobDocument, jobHandlerDir); diff --git a/source/jobs/JobsFeature.h b/source/jobs/JobsFeature.h index 91f44868..05786afc 100644 --- a/source/jobs/JobsFeature.h +++ b/source/jobs/JobsFeature.h @@ -322,8 +322,7 @@ namespace Aws * @return true if the documents are equivalent (ignoring pre-signed URLs), false otherwise */ - bool compareJobDocuments(const Aws::Crt::JsonObject& job1, const Aws::Crt::JsonObject& job2); - + bool compareJobDocuments(const Aws::Crt::JsonObject &job1, const Aws::Crt::JsonObject &job2); /** * \brief Stores information about a job notification @@ -340,8 +339,8 @@ namespace Aws virtual std::shared_ptr createJobEngine(); }; } // namespace Jobs - } // namespace DeviceClient - } // namespace Iot + } // namespace DeviceClient + } // namespace Iot } // namespace Aws #endif // DEVICE_CLIENT_JOBSFEATURE_H diff --git a/test/jobs/TestJobsFeature.cpp b/test/jobs/TestJobsFeature.cpp index d3b859bc..69fde02b 100644 --- a/test/jobs/TestJobsFeature.cpp +++ b/test/jobs/TestJobsFeature.cpp @@ -12,10 +12,10 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include #include #include #include -#include using namespace std; using namespace testing;