2019-05-10 17:33:28 +02:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2018-07-28 00:25:33 +02:00
|
|
|
|
|
|
|
#include "cli/caremovecommand.hpp"
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/tlsutility.hpp"
|
2019-05-10 17:33:28 +02:00
|
|
|
#include "remote/apilistener.hpp"
|
2018-07-28 00:25:33 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_CLICOMMAND("ca/remove", CARemoveCommand);
|
|
|
|
|
2019-06-07 10:33:35 +02:00
|
|
|
/**
|
|
|
|
* Provide a long CLI description sentence.
|
|
|
|
*
|
|
|
|
* @return text
|
|
|
|
*/
|
2018-07-28 00:25:33 +02:00
|
|
|
String CARemoveCommand::GetDescription() const
|
|
|
|
{
|
|
|
|
return "Removes an outstanding certificate request.";
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:33:35 +02:00
|
|
|
/**
|
|
|
|
* Provide a short CLI description.
|
|
|
|
*
|
|
|
|
* @return text
|
|
|
|
*/
|
2018-07-28 00:25:33 +02:00
|
|
|
String CARemoveCommand::GetShortDescription() const
|
|
|
|
{
|
|
|
|
return "removes an outstanding certificate request";
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:33:35 +02:00
|
|
|
/**
|
|
|
|
* Define minimum arguments without key parameter.
|
|
|
|
*
|
|
|
|
* @return number of arguments
|
|
|
|
*/
|
2018-07-28 00:25:33 +02:00
|
|
|
int CARemoveCommand::GetMinArguments() const
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:33:35 +02:00
|
|
|
/**
|
|
|
|
* Impersonate as Icinga user.
|
|
|
|
*
|
|
|
|
* @return impersonate level
|
|
|
|
*/
|
2018-07-28 00:25:33 +02:00
|
|
|
ImpersonationLevel CARemoveCommand::GetImpersonationLevel() const
|
|
|
|
{
|
|
|
|
return ImpersonateIcinga;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The entry point for the "ca remove" CLI command.
|
|
|
|
*
|
|
|
|
* @returns An exit status.
|
|
|
|
*/
|
|
|
|
int CARemoveCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
|
|
|
|
{
|
2019-05-10 17:33:28 +02:00
|
|
|
String fingerPrint = ap[0];
|
|
|
|
String requestFile = ApiListener::GetCertificateRequestsDir() + "/" + fingerPrint + ".json";
|
2018-07-28 00:25:33 +02:00
|
|
|
|
|
|
|
if (!Utility::PathExists(requestFile)) {
|
|
|
|
Log(LogCritical, "cli")
|
2019-05-10 17:33:28 +02:00
|
|
|
<< "No request exists for fingerprint '" << fingerPrint << "'.";
|
2018-07-28 00:25:33 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2018-08-08 20:59:58 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr request = Utility::LoadJsonFile(requestFile);
|
|
|
|
std::shared_ptr<X509> certRequest = StringToCertificate(request->Get("cert_request"));
|
|
|
|
|
|
|
|
if (!certRequest) {
|
|
|
|
Log(LogCritical, "cli", "Certificate request is invalid. Could not parse X.509 certificate for the 'cert_request' attribute.");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-05-10 17:33:28 +02:00
|
|
|
|
|
|
|
String cn = GetCertificateCN(certRequest);
|
|
|
|
|
2018-08-08 20:59:58 +02:00
|
|
|
if (request->Contains("cert_response")) {
|
2019-05-10 17:33:28 +02:00
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Certificate request for CN '" << cn << "' already signed, removal is not possible.";
|
2018-08-08 20:59:58 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-05-10 17:33:28 +02:00
|
|
|
Utility::SaveJsonFile(ApiListener::GetCertificateRequestsDir() + "/" + fingerPrint + ".removed", 0600, request);
|
|
|
|
|
|
|
|
Utility::Remove(requestFile);
|
2018-07-28 00:25:33 +02:00
|
|
|
|
|
|
|
Log(LogInformation, "cli")
|
2019-05-10 17:33:28 +02:00
|
|
|
<< "Certificate request for CN " << cn << " removed.";
|
2018-07-28 00:25:33 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|