SKALE-1762 Add error handling

parent 97768b50
......@@ -29,9 +29,7 @@ Json::Value GetUnsignedCSRsImpl(){
try{
std::vector<std::string> hashes_vect = csrDb->writeKeysToVector1(MAX_CSR_NUM);
//std::cerr << " vector size is " << hashes_vect.size() << std::endl;
for (int i = 0; i < hashes_vect.size(); i++){
//std::cerr << " vector element is " << hashes_vect.at(i) << std::endl;
result["hashes"][i] = hashes_vect.at(i);
}
} catch (RPCException &_e) {
......@@ -55,6 +53,9 @@ Json::Value SignByHashImpl(const std::string& hash, int status){
std::string csr_db_key = "CSR:HASH:" + hash;
std::shared_ptr<std::string> csr_ptr = csrDb->readString(csr_db_key);
if (csr_ptr == nullptr){
throw RPCException(KEY_SHARE_DOES_NOT_EXIST, "HASH DOES NOT EXIST IN DB");
}
if (status == 0) {
std::string csr_name = "cert/" + hash + ".csr";
......@@ -62,6 +63,7 @@ Json::Value SignByHashImpl(const std::string& hash, int status){
outfile << *csr_ptr << std::endl;
outfile.close();
if (access(csr_name.c_str(), F_OK) != 0) {
csrDb->deleteKey(csr_db_key);
throw RPCException(FILE_NOT_FOUND, "Csr does not exist");
}
......
......@@ -70,6 +70,8 @@ Json::Value SignCertificateImpl(const std::string& csr, bool auto_sign = false){
}
else{
std::cerr << "CLIENT CERTIFICATE GENERATION FAILED" << std::endl;
std::string status_db_key = "CSR:HASH:" + hash + "STATUS:";
csrStatusDb->writeDataUnique(status_db_key, std::to_string(FAIL_TO_CREATE_CERTIFICATE));
throw RPCException(FAIL_TO_CREATE_CERTIFICATE, "CLIENT CERTIFICATE GENERATION FAILED");
//exit(-1);
}
......@@ -93,21 +95,13 @@ Json::Value SignCertificateImpl(const std::string& csr, bool auto_sign = false){
Json::Value GetSertificateImpl(const std::string& hash){
Json::Value result;
result["status"] = 1;
result["errorMessage"] = "";
std::string cert;
try{
// std::string rejected_name = "rejected_" + hash + ".txt";
// if (access(rejected_name.c_str(), F_OK) == 0){
// result["status"] = 2;
// result["cert"] = "";
// return result;
// }
std::string db_key = "CSR:HASH:" + hash + "STATUS:";
std::shared_ptr<string> status_str_ptr = csrStatusDb->readString(db_key);
if (status_str_ptr == nullptr){
throw RPCException(FILE_NOT_FOUND, "Data with this name does not exist in csr db");
throw RPCException(KEY_SHARE_DOES_NOT_EXIST, "Data with this name does not exist in csr db");
}
int status = std::atoi(status_str_ptr->c_str());
......@@ -117,6 +111,9 @@ Json::Value GetSertificateImpl(const std::string& hash){
//if (access(crt_name.c_str(), F_OK) == 0){
std::ifstream infile(crt_name);
if (!infile.is_open()) {
std::string status_db_key = "CSR:HASH:" + hash + "STATUS:";
csrStatusDb->deleteKey(status_db_key);
csrStatusDb->writeDataUnique(status_db_key, std::to_string(FILE_NOT_FOUND));
throw RPCException(FILE_NOT_FOUND, "Certificate does not exist");
} else {
ostringstream ss;
......@@ -124,23 +121,19 @@ Json::Value GetSertificateImpl(const std::string& hash){
cert = ss.str();
infile.close();
std::string remove_crt = "cd cert && rm -rf" + hash + ".crt";
std::string remove_crt = "cd cert && rm -rf" + hash + ".crt && rm -rf " + hash + ".csr";
system(remove_crt.c_str());
// result["cert"] = cert;
// result["status"] = 0;
}
}
// else if (access(crt_name.c_str(), F_OK) != 0){
// result["status"] = 1;
// result["cert"] = "";
// }
result["status"] = status;
result["cert"] = cert;
} catch (RPCException &_e) {
std::cerr << " err str " << _e.errString << std::endl;
result["status"] = _e.status;
result["errorMessage"] = _e.errString;
result["status"] = 1;
}
return result;
......
//
// Created by kladko on 12/24/19.
//
#ifndef SGXD_ABSTRACTCSRMANAGERSERVER_H
#define SGXD_ABSTRACTCSRMANAGERSERVER_H
#include <jsonrpccpp/server.h>
#include <iostream>
class abstractCSRManagerServer : public jsonrpc::AbstractServer<abstractCSRManagerServer> {
public:
abstractCSRManagerServer(jsonrpc::AbstractServerConnector &conn, jsonrpc::serverVersion_t type = jsonrpc::JSONRPC_SERVER_V2) : jsonrpc::AbstractServer<abstractCSRManagerServer>(conn, type)
{
this->bindAndAddMethod(jsonrpc::Procedure("GetUnsignedCSRs", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT, NULL), &abstractCSRManagerServer::GetUnsignedCSRsI);
this->bindAndAddMethod(jsonrpc::Procedure("SignByHash", jsonrpc::PARAMS_BY_NAME, jsonrpc::JSON_OBJECT,"hash",jsonrpc::JSON_STRING, "status", jsonrpc::JSON_INTEGER, NULL), &abstractCSRManagerServer::SignByHashI);
}
inline virtual void GetUnsignedCSRsI(const Json::Value &request, Json::Value &response)
{
(void)request;
response = this->GetUnsignedCSRs();
}
inline virtual void SignByHashI(const Json::Value &request, Json::Value &response)
{
response = this->SignByHash( request["hash"].asString(), request["status"].asInt());
}
virtual Json::Value GetUnsignedCSRs() = 0;
virtual Json::Value SignByHash(const std::string& hash, int status) = 0;
};
#endif //SGXD_ABSTRACTCSRMANAGERSERVER_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment