2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2014-10-06 08:21:18 -04:00
|
|
|
|
2014-10-13 07:43:05 -04:00
|
|
|
#include "cli/pkinewcertcommand.hpp"
|
2017-09-05 08:44:56 -04:00
|
|
|
#include "remote/pkiutility.hpp"
|
2014-10-19 08:21:12 -04:00
|
|
|
#include "base/logger.hpp"
|
2014-10-06 08:21:18 -04:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
2014-10-13 06:34:31 -04:00
|
|
|
namespace po = boost::program_options;
|
2014-10-06 08:21:18 -04:00
|
|
|
|
2014-10-13 07:43:05 -04:00
|
|
|
REGISTER_CLICOMMAND("pki/new-cert", PKINewCertCommand);
|
2014-10-06 08:21:18 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
String PKINewCertCommand::GetDescription() const
|
2014-10-06 08:21:18 -04:00
|
|
|
{
|
2014-10-13 07:43:05 -04:00
|
|
|
return "Creates a new Certificate Signing Request, a self-signed X509 certificate or both.";
|
2014-10-06 08:21:18 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
String PKINewCertCommand::GetShortDescription() const
|
2014-10-06 08:21:18 -04:00
|
|
|
{
|
2014-10-13 06:34:31 -04:00
|
|
|
return "creates a new CSR";
|
2014-10-06 08:21:18 -04:00
|
|
|
}
|
|
|
|
|
|
2014-10-13 07:43:05 -04:00
|
|
|
void PKINewCertCommand::InitParameters(boost::program_options::options_description& visibleDesc,
|
2026-01-23 07:31:01 -05:00
|
|
|
[[maybe_unused]] boost::program_options::options_description& hiddenDesc) const
|
2014-10-06 08:21:18 -04:00
|
|
|
{
|
2014-10-13 06:34:31 -04:00
|
|
|
visibleDesc.add_options()
|
|
|
|
|
("cn", po::value<std::string>(), "Common Name")
|
2017-06-18 09:13:16 -04:00
|
|
|
("key", po::value<std::string>(), "Key file path (output)")
|
2014-10-22 09:36:39 -04:00
|
|
|
("csr", po::value<std::string>(), "CSR file path (optional, output)")
|
|
|
|
|
("cert", po::value<std::string>(), "Certificate file path (optional, output)");
|
2014-10-17 09:54:46 -04:00
|
|
|
}
|
2014-10-16 08:33:58 -04:00
|
|
|
|
2014-10-17 09:54:46 -04:00
|
|
|
std::vector<String> PKINewCertCommand::GetArgumentSuggestions(const String& argument, const String& word) const
|
|
|
|
|
{
|
2014-10-22 09:36:39 -04:00
|
|
|
if (argument == "key" || argument == "csr" || argument == "cert")
|
2014-10-17 09:54:46 -04:00
|
|
|
return GetBashCompletionSuggestions("file", word);
|
|
|
|
|
else
|
|
|
|
|
return CLICommand::GetArgumentSuggestions(argument, word);
|
2014-10-06 08:21:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-10-13 07:43:05 -04:00
|
|
|
* The entry point for the "pki new-cert" CLI command.
|
2014-10-06 08:21:18 -04:00
|
|
|
*
|
|
|
|
|
* @returns An exit status.
|
|
|
|
|
*/
|
2026-01-23 07:31:01 -05:00
|
|
|
int PKINewCertCommand::Run(const boost::program_options::variables_map& vm, [[maybe_unused]] const std::vector<std::string>& ap) const
|
2014-10-06 08:21:18 -04:00
|
|
|
{
|
2014-10-13 06:34:31 -04:00
|
|
|
if (!vm.count("cn")) {
|
|
|
|
|
Log(LogCritical, "cli", "Common name (--cn) must be specified.");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2014-10-13 12:07:52 -04:00
|
|
|
|
2014-10-22 09:36:39 -04:00
|
|
|
if (!vm.count("key")) {
|
|
|
|
|
Log(LogCritical, "cli", "Key file path (--key) must be specified.");
|
2014-10-13 06:34:31 -04:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-22 09:36:39 -04:00
|
|
|
String csr, cert;
|
2014-10-13 12:07:52 -04:00
|
|
|
|
2014-10-22 09:36:39 -04:00
|
|
|
if (vm.count("csr"))
|
|
|
|
|
csr = vm["csr"].as<std::string>();
|
2014-10-13 07:43:05 -04:00
|
|
|
|
2014-10-22 09:36:39 -04:00
|
|
|
if (vm.count("cert"))
|
|
|
|
|
cert = vm["cert"].as<std::string>();
|
2014-10-13 12:07:52 -04:00
|
|
|
|
2014-10-22 09:36:39 -04:00
|
|
|
return PkiUtility::NewCert(vm["cn"].as<std::string>(), vm["key"].as<std::string>(), csr, cert);
|
2014-10-06 08:21:18 -04:00
|
|
|
}
|