Improve log messages and general code cleanup in CaModule
Change-Id: Ie455ec14594e7662800faa887da72574bff73407
diff --git a/src/challenge/challenge-email.cpp b/src/challenge/challenge-email.cpp
index c4de9ce..26d42f5 100644
--- a/src/challenge/challenge-email.cpp
+++ b/src/challenge/challenge-email.cpp
@@ -49,56 +49,59 @@
{
params.parse();
auto currentTime = time::system_clock::now();
+
if (request.status == Status::BEFORE_CHALLENGE) {
// for the first time, init the challenge
std::string emailAddress = readString(params.get(tlv::ParameterValue));
auto lastComponentRequested = readString(request.cert.getIdentity().get(-1));
if (lastComponentRequested != emailAddress) {
- NDN_LOG_TRACE("Email and requested name do not match. Email " << emailAddress
- << " - requested last component " << lastComponentRequested);
+ NDN_LOG_TRACE("Email and requested name do not match: email=" << emailAddress
+ << " requested=" << lastComponentRequested);
}
std::string emailCode = generateSecretCode();
JsonSection secretJson;
secretJson.add(PARAMETER_KEY_CODE, emailCode);
// send out the email
sendEmail(emailAddress, emailCode, request);
- NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId) << " : " << emailCode);
+ NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId) << " is " << emailCode);
return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson),
m_maxAttemptTimes, m_secretLifetime);
}
+
if (request.challengeState) {
if (request.challengeState->challengeStatus == NEED_CODE ||
request.challengeState->challengeStatus == WRONG_CODE) {
- NDN_LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.challengeState->challengeStatus);
+ NDN_LOG_TRACE("Challenge status: " << request.challengeState->challengeStatus);
// the incoming interest should bring the pin code
std::string givenCode = readString(params.get(tlv::ParameterValue));
auto secret = request.challengeState->secrets;
// check if run out of time
if (currentTime - request.challengeState->timestamp >= m_secretLifetime) {
+ NDN_LOG_TRACE("Secret expired");
return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired.");
}
// check if provided secret is correct
if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) {
// the code is correct
- NDN_LOG_TRACE("Correct secret code. Challenge succeeded.");
+ NDN_LOG_TRACE("Secret is correct, challenge succeeded");
return returnWithSuccess(request);
}
// otherwise, check remaining attempt times
if (request.challengeState->remainingTries > 1) {
auto remainTime = m_secretLifetime - (currentTime - request.challengeState->timestamp);
- NDN_LOG_TRACE("Wrong secret code provided. Remaining Tries - 1.");
+ NDN_LOG_TRACE("Wrong secret, remaining tries = " << request.challengeState->remainingTries - 1);
return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret),
request.challengeState->remainingTries - 1,
time::duration_cast<time::seconds>(remainTime));
}
else {
- // run out times
- NDN_LOG_TRACE("Wrong secret code provided. Ran out of tries. Challenge failed.");
+ NDN_LOG_TRACE("Wrong secret, no tries remaining");
return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out of tries.");
}
}
}
- return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status");
+
+ return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected challenge status.");
}
// For Client
@@ -116,7 +119,7 @@
result.emplace(PARAMETER_KEY_CODE, "Incorrect code, please try again");
}
else {
- NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
+ NDN_THROW(std::runtime_error("Unexpected challenge status"));
}
return result;
}
@@ -128,7 +131,7 @@
Block request(tlv::EncryptedPayload);
if (status == Status::BEFORE_CHALLENGE) {
if (params.size() != 1 || params.find(PARAMETER_KEY_EMAIL) == params.end()) {
- NDN_THROW(std::runtime_error("Wrong parameter provided."));
+ NDN_THROW(std::runtime_error("Wrong parameter provided"));
}
request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_EMAIL));
@@ -136,14 +139,14 @@
}
else if (status == Status::CHALLENGE && (challengeStatus == NEED_CODE || challengeStatus == WRONG_CODE)) {
if (params.size() != 1 || params.find(PARAMETER_KEY_CODE) == params.end()) {
- NDN_THROW(std::runtime_error("Wrong parameter provided."));
+ NDN_THROW(std::runtime_error("Wrong parameter provided"));
}
request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CODE));
request.push_back(ndn::makeStringBlock(tlv::ParameterValue, params.find(PARAMETER_KEY_CODE)->second));
}
else {
- NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
+ NDN_THROW(std::runtime_error("Unexpected challenge status"));
}
request.encode();
return request;
@@ -168,11 +171,10 @@
boost::process::child child(command);
child.wait();
if (child.exit_code() != 0) {
- NDN_LOG_TRACE("EmailSending Script " + m_sendEmailScript + " fails.");
+ NDN_LOG_ERROR("Email sending script " + m_sendEmailScript + " failed");
}
else {
- NDN_LOG_TRACE("EmailSending Script " + m_sendEmailScript +
- " was executed successfully with return value 0.");
+ NDN_LOG_TRACE("Email sending script " + m_sendEmailScript + " succeeded");
}
}
diff --git a/src/challenge/challenge-pin.cpp b/src/challenge/challenge-pin.cpp
index 2662055..f38c778 100644
--- a/src/challenge/challenge-pin.cpp
+++ b/src/challenge/challenge-pin.cpp
@@ -43,47 +43,49 @@
{
params.parse();
auto currentTime = time::system_clock::now();
+
if (request.status == Status::BEFORE_CHALLENGE) {
- NDN_LOG_TRACE("Challenge Interest arrives. Init the challenge");
+ NDN_LOG_TRACE("Begin challenge");
// for the first time, init the challenge
std::string secretCode = generateSecretCode();
JsonSection secretJson;
secretJson.add(PARAMETER_KEY_CODE, secretCode);
- NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId)
- << " : " << secretCode);
+ NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId) << " is " << secretCode);
return returnWithNewChallengeStatus(request, NEED_CODE, std::move(secretJson), m_maxAttemptTimes,
m_secretLifetime);
}
+
if (request.challengeState) {
if (request.challengeState->challengeStatus == NEED_CODE ||
request.challengeState->challengeStatus == WRONG_CODE) {
- NDN_LOG_TRACE("Challenge Interest arrives. Challenge Status: " << request.challengeState->challengeStatus);
+ NDN_LOG_TRACE("Challenge status: " << request.challengeState->challengeStatus);
// the incoming interest should bring the pin code
std::string givenCode = readString(params.get(tlv::ParameterValue));
auto secret = request.challengeState->secrets;
if (currentTime - request.challengeState->timestamp >= m_secretLifetime) {
+ NDN_LOG_TRACE("Secret expired");
return returnWithError(request, ErrorCode::OUT_OF_TIME, "Secret expired.");
}
if (givenCode == secret.get<std::string>(PARAMETER_KEY_CODE)) {
- NDN_LOG_TRACE("Correct PIN code. Challenge succeeded.");
+ NDN_LOG_TRACE("PIN is correct, challenge succeeded");
return returnWithSuccess(request);
}
// check rest attempt times
if (request.challengeState->remainingTries > 1) {
auto remainTime = m_secretLifetime - (currentTime - request.challengeState->timestamp);
- NDN_LOG_TRACE("Wrong PIN code provided. Remaining Tries - 1.");
+ NDN_LOG_TRACE("Wrong PIN, remaining tries = " << request.challengeState->remainingTries - 1);
return returnWithNewChallengeStatus(request, WRONG_CODE, std::move(secret),
request.challengeState->remainingTries - 1,
time::duration_cast<time::seconds>(remainTime));
}
else {
- // run out times
- NDN_LOG_TRACE("Wrong PIN code provided. Ran out tires. Challenge failed.");
- return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out tires.");
+ NDN_LOG_TRACE("Wrong PIN, no tries remaining");
+ return returnWithError(request, ErrorCode::OUT_OF_TRIES, "Ran out of tries.");
}
}
}
- return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected status or challenge status");
+
+ return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Unexpected challenge status.");
}
// For Client
@@ -101,7 +103,7 @@
result.emplace(PARAMETER_KEY_CODE, "Incorrect PIN code, please try again");
}
else {
- NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
+ NDN_THROW(std::runtime_error("Unexpected challenge status"));
}
return result;
}
@@ -116,14 +118,14 @@
}
else if (status == Status::CHALLENGE && (challengeStatus == NEED_CODE || challengeStatus == WRONG_CODE)) {
if (params.size() != 1 || params.find(PARAMETER_KEY_CODE) == params.end()) {
- NDN_THROW(std::runtime_error("Wrong parameter provided."));
+ NDN_THROW(std::runtime_error("Wrong parameter provided"));
}
request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
request.push_back(ndn::makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CODE));
request.push_back(ndn::makeStringBlock(tlv::ParameterValue, params.find(PARAMETER_KEY_CODE)->second));
}
else {
- NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
+ NDN_THROW(std::runtime_error("Unexpected challenge status"));
}
request.encode();
return request;
diff --git a/src/challenge/challenge-possession.cpp b/src/challenge/challenge-possession.cpp
index 2473f29..ae85a8c 100644
--- a/src/challenge/challenge-possession.cpp
+++ b/src/challenge/challenge-possession.cpp
@@ -42,7 +42,7 @@
: ChallengeModule("Possession", 1, time::seconds(60))
{
if (configPath.empty()) {
- m_configFile = std::string(NDNCERT_SYSCONFDIR) + "/ndncert/challenge-credential.conf";
+ m_configFile = NDNCERT_SYSCONFDIR "/ndncert/challenge-credential.conf";
}
else {
m_configFile = configPath;
@@ -62,7 +62,7 @@
}
if (config.begin() == config.end()) {
- NDN_THROW(std::runtime_error("Error processing configuration file: " + m_configFile + " no data"));
+ NDN_THROW(std::runtime_error("Error processing configuration file " + m_configFile + ": no data"));
}
m_trustAnchors.clear();
@@ -72,7 +72,7 @@
std::istringstream ss(it->second.get("certificate", ""));
auto cert = ndn::io::load<Certificate>(ss);
if (cert == nullptr) {
- NDN_LOG_ERROR("Cannot load the certificate from config file");
+ NDN_LOG_ERROR("Cannot load certificate from configuration file");
continue;
}
m_trustAnchors.push_back(*cert);
@@ -98,9 +98,8 @@
credential.wireDecode(elements[i + 1].blockFromValue());
}
catch (const std::exception& e) {
- NDN_LOG_ERROR("Cannot load challenge parameter: credential " << e.what());
- return returnWithError(request, ErrorCode::INVALID_PARAMETER,
- "Cannot challenge credential: credential."s + e.what());
+ NDN_LOG_ERROR("Cannot decode credential: " << e.what());
+ return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Malformed credential.");
}
}
else if (readString(elements[i]) == PARAMETER_KEY_PROOF) {
@@ -112,11 +111,11 @@
// verify the credential and the self-signed cert
if (request.status == Status::BEFORE_CHALLENGE) {
- NDN_LOG_TRACE("Challenge Interest arrives. Check certificate and init the challenge");
+ NDN_LOG_TRACE("Begin challenge");
// check the certificate
if (!credential.hasContent() || signatureLen != 0) {
- return returnWithError(request, ErrorCode::BAD_INTEREST_FORMAT, "Cannot find certificate");
+ return returnWithError(request, ErrorCode::BAD_INTEREST_FORMAT, "Cannot find certificate.");
}
auto keyLocator = credential.getSignatureInfo().getKeyLocator().getName();
ndn::security::transform::PublicKey key;
@@ -126,7 +125,7 @@
ndn::security::verifySignature(credential, anchor);
});
if (!checkOK) {
- return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Certificate cannot be verified");
+ return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Certificate cannot be verified.");
}
// for the first time, init the challenge
@@ -135,19 +134,19 @@
JsonSection secretJson;
secretJson.add(PARAMETER_KEY_NONCE, ndn::toHex(secretCode));
secretJson.add(PARAMETER_KEY_CREDENTIAL_CERT, ndn::toHex(credential.wireEncode()));
- NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId) << " : " << ndn::toHex(secretCode));
+ NDN_LOG_TRACE("Secret for request " << ndn::toHex(request.requestId) << " is " << ndn::toHex(secretCode));
return returnWithNewChallengeStatus(request, NEED_PROOF, std::move(secretJson), m_maxAttemptTimes, m_secretLifetime);
}
else if (request.challengeState && request.challengeState->challengeStatus == NEED_PROOF) {
- NDN_LOG_TRACE("Challenge Interest (proof) arrives. Check the proof");
- //check the format and load credential
+ NDN_LOG_TRACE("Checking proof");
+ // check the format and load credential
if (credential.hasContent() || signatureLen == 0) {
- return returnWithError(request, ErrorCode::BAD_INTEREST_FORMAT, "Cannot find certificate");
+ return returnWithError(request, ErrorCode::BAD_INTEREST_FORMAT, "Cannot find certificate.");
}
credential = Certificate(Block(ndn::fromHex(request.challengeState->secrets.get(PARAMETER_KEY_CREDENTIAL_CERT, ""))));
auto secretCode = *ndn::fromHex(request.challengeState->secrets.get(PARAMETER_KEY_NONCE, ""));
- //check the proof
+ // check the proof
ndn::security::transform::PublicKey key;
key.loadPkcs8(credential.getPublicKey());
if (ndn::security::verifySignature({secretCode}, {signature, signatureLen}, key)) {
@@ -156,8 +155,9 @@
return returnWithError(request, ErrorCode::INVALID_PARAMETER,
"Cannot verify the proof of private key against credential.");
}
- NDN_LOG_TRACE("Proof of possession: bad state");
- return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Fail to recognize the request.");
+
+ NDN_LOG_TRACE("Bad state");
+ return returnWithError(request, ErrorCode::INVALID_PARAMETER, "Cannot recognize the request.");
}
// For Client
@@ -173,7 +173,7 @@
result.emplace(PARAMETER_KEY_PROOF, "Please sign a Data packet with request ID as the content.");
}
else {
- NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
+ NDN_THROW(std::runtime_error("Unexpected challenge status"));
}
return result;
}
@@ -185,7 +185,7 @@
Block request(tlv::EncryptedPayload);
if (status == Status::BEFORE_CHALLENGE) {
if (params.size() != 1) {
- NDN_THROW(std::runtime_error("Wrong parameter provided."));
+ NDN_THROW(std::runtime_error("Wrong parameter provided"));
}
request.push_back(ndn::makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
for (const auto& item : params) {
@@ -198,13 +198,13 @@
request.push_back(valueBlock);
}
else {
- NDN_THROW(std::runtime_error("Wrong parameter provided."));
+ NDN_THROW(std::runtime_error("Wrong parameter provided"));
}
}
}
else if (status == Status::CHALLENGE && challengeStatus == NEED_PROOF) {
if (params.size() != 1) {
- NDN_THROW(std::runtime_error("Wrong parameter provided."));
+ NDN_THROW(std::runtime_error("Wrong parameter provided"));
}
for (const auto& item : params) {
if (std::get<0>(item) == PARAMETER_KEY_PROOF) {
@@ -212,12 +212,12 @@
request.push_back(ndn::makeStringBlock(tlv::ParameterValue, std::get<1>(item)));
}
else {
- NDN_THROW(std::runtime_error("Wrong parameter provided."));
+ NDN_THROW(std::runtime_error("Wrong parameter provided"));
}
}
}
else {
- NDN_THROW(std::runtime_error("Unexpected status or challenge status."));
+ NDN_THROW(std::runtime_error("Unexpected challenge status"));
}
request.encode();
return request;