Skip to content

Commit

Permalink
Fix clang-formatting issues (#477)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
ig15 and Ubuntu authored Nov 20, 2024
1 parent 0f4288e commit 69b0490
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 47 deletions.
8 changes: 6 additions & 2 deletions source/config/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
57 changes: 37 additions & 20 deletions source/fleetprovisioning/FleetProvisioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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());
Expand All @@ -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(
Expand Down Expand Up @@ -529,7 +545,8 @@ bool FleetProvisioning::ProvisionDevice(shared_ptr<SharedCrtResourceManager> 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)
{
Expand Down
12 changes: 8 additions & 4 deletions source/fleetprovisioning/FleetProvisioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,19 @@ 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.
*
* @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
Expand Down Expand Up @@ -274,6 +278,6 @@ namespace Aws
bool ObtainCertificateSerialID(const char *certPath);
};
} // namespace FleetProvisioningNS
} // namespace DeviceClient
} // namespace Iot
} // namespace DeviceClient
} // namespace Iot
} // namespace Aws
38 changes: 22 additions & 16 deletions source/jobs/JobsFeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<mutex> leakLock(updateJobExecutionPromisesLock);
for (auto keyPromise = updateJobExecutionPromises.cbegin(); keyPromise != updateJobExecutionPromises.cend();
Expand Down Expand Up @@ -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();
}

Expand All @@ -527,23 +528,25 @@ 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();

// Regular expression to match S3 URLs and capture the non-presigned parts
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
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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())
{
Expand Down Expand Up @@ -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())
{
Expand All @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions source/jobs/JobsFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -340,8 +339,8 @@ namespace Aws
virtual std::shared_ptr<JobEngine> createJobEngine();
};
} // namespace Jobs
} // namespace DeviceClient
} // namespace Iot
} // namespace DeviceClient
} // namespace Iot
} // namespace Aws

#endif // DEVICE_CLIENT_JOBSFEATURE_H
2 changes: 1 addition & 1 deletion test/jobs/TestJobsFeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <aws/crt/Api.h>
#include <aws/iotjobs/RejectedError.h>
#include <aws/iotjobs/StartNextJobExecutionResponse.h>
#include <aws/iotjobs/UpdateJobExecutionResponse.h>
#include <aws/crt/Api.h>

using namespace std;
using namespace testing;
Expand Down

0 comments on commit 69b0490

Please sign in to comment.