7 #include <log/logger.hpp>
10 PostgresJobProvider::PostgresJobProvider( const std::string& server, const std::string& user, const std::string& password, const std::string& database ):
11 c("dbname="+database+" host="+server+" user="+user+" password=" + password + " client_encoding=UTF-8 application_name=cassiopeia-client"){
12 // TODO better connection string generation??
16 std::shared_ptr<Job> PostgresJobProvider::fetchJob() {
17 std::string q = "SELECT id, \"targetId\", task, \"executeFrom\", \"executeTo\", warning FROM jobs WHERE state='open' AND warning < 3";
19 pqxx::result result = txn.exec(q);
22 auto job = std::make_shared<Job>();
24 if( result.size() == 0 ) {
28 job->id = result[0]["id"].as<std::string>();
29 job->target = result[0]["\"targetId\""].as<std::string>();
30 job->task = result[0]["task"].as<std::string>();
31 job->from = result[0]["\"executeFrom\""].as<std::string>("");
32 job->to = result[0]["\"executeTo\""].as<std::string>("");
33 job->warning = result[0]["warning"].as<std::string>();
35 logger::notef( "Got a job: (id=%s, target=%s, task=%s, from=%s, to=%s, warnings=%s)", job->id, job->target, job->task, job->from, job->to, job->warning );
40 void PostgresJobProvider::finishJob( std::shared_ptr<Job> job ) {
43 std::string q = "UPDATE jobs SET state='done' WHERE id=" + txn.quote( job->id );
44 pqxx::result r = txn.exec(q);
46 if( r.affected_rows() != 1 ) {
47 throw std::runtime_error("No database entry found.");
52 void PostgresJobProvider::failJob( std::shared_ptr<Job> job ) {
55 std::string q = "UPDATE jobs SET warning = warning + 1 WHERE id=" + txn.quote( job->id );
56 pqxx::result r = txn.exec(q);
58 if( r.affected_rows() != 1 ) {
59 throw std::runtime_error("No database entry found.");
64 std::shared_ptr<TBSCertificate> PostgresJobProvider::fetchTBSCert( std::shared_ptr<Job> job ) {
66 auto cert = std::make_shared<TBSCertificate>();
67 std::string q = "SELECT md, profile, csr_name, csr_type, keyname FROM certs INNER JOIN profiles ON profiles.id = certs.profile WHERE certs.id=" + txn.quote( job->target );
68 pqxx::result r = txn.exec(q);
71 throw std::runtime_error("Error, no or multiple certs found");
75 std::string profileName = ro["keyname"].as<std::string>();
77 cert->md = ro["md"].as<std::string>();
78 std::string profileId = ro["profile"].as<std::string>();
80 while( profileId.size() < 4 ) {
81 profileId = "0" + profileId;
84 cert->profile = profileId + "-" + profileName;
86 cert->csr = ro["csr_name"].as<std::string>();
87 cert->csr_type = ro["csr_type"].as<std::string>();
89 cert->SANs = std::vector<std::shared_ptr<SAN>>();
91 q = "SELECT contents, type FROM \"subjectAlternativeNames\" WHERE \"certId\"=" + txn.quote( job->target );
94 std::cout << "Fetching SANs" << std::endl;
96 for( auto row = r.begin(); row != r.end(); ++row) {
97 auto nSAN = std::make_shared<SAN>();
98 nSAN->content = row["contents"].as<std::string>();
99 nSAN->type = row["type"].as<std::string>();
100 cert->SANs.push_back( nSAN );
103 q = "SELECT name, value FROM \"certAvas\" WHERE \"certId\"=" + txn.quote( job->target );
106 for( auto row = r.begin(); row != r.end(); ++row) {
107 auto nAVA = std::make_shared<AVA>();
108 nAVA->name = row["name"].as<std::string>();
109 nAVA->value = row["value"].as<std::string>();
110 cert->AVAs.push_back( nAVA );
116 std::string pgTime( std::string isoTime){
117 return isoTime.substr(0, 8) + " " + isoTime.substr(8, 6);
120 void PostgresJobProvider::writeBack( std::shared_ptr<Job> job, std::shared_ptr<SignedCertificate> res ) {
122 std::string id = "SELECT id FROM cacerts WHERE keyname=" + txn.quote( res->ca_name );
123 pqxx::result r = txn.exec(id);
128 throw std::runtime_error("Error while inserting new ca cert not found");
130 read_id = r[0]["id"].as<std::string>();
132 std::string serial = res->serial;
133 std::transform(serial.begin(), serial.end(), serial.begin(), ::tolower);
134 if(serial[0] == '0'){
135 serial = serial.substr(1);
137 std::string q = "UPDATE certs SET crt_name=" + txn.quote( res->crt_name ) + ", serial=" + txn.quote( serial ) + ", \"caid\" = " + txn.quote( read_id ) + ", created=" + txn.quote( pgTime(res->before) ) + ", expire=" + txn.quote( pgTime(res->after) ) + " WHERE id=" + txn.quote( job->target );
138 // TODO write more thingies back
141 if( r.affected_rows() != 1 ){
142 throw std::runtime_error("Only one row should be updated.");
147 std::pair<std::string, std::string> PostgresJobProvider::getRevocationInfo( std::shared_ptr<Job> job ) {
149 std::string q = "SELECT certs.serial, cacerts.keyname FROM certs INNER JOIN cacerts ON certs.\"caid\" = cacerts.id WHERE certs.id = " + txn.quote( job->target );
151 pqxx::result r = txn.exec( q );
153 throw std::runtime_error("Only one row expected but multiple found.");
157 return {r[0][0].as<std::string>(), r[0][1].as<std::string>()};
160 void PostgresJobProvider::writeBackRevocation( std::shared_ptr<Job> job, std::string date ) {
161 logger::notef( "Revoking at %s", date);
163 logger::note( "executing" );
164 pqxx::result r = txn.exec( "UPDATE certs SET revoked = " + txn.quote( pgTime( date ) ) + " WHERE id = " + txn.quote( job->target ) );
165 if( r.affected_rows() != 1 ){
166 throw std::runtime_error("Only one row should be updated.");
168 logger::note( "committing" );
170 logger::note( "committed" );