diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:34:54 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:34:54 +0000 |
commit | 0915b3ef56dfac3113cce55a59a5765dc94976be (patch) | |
tree | a8fea11d50b4f083e1bf0f90025ece7f0824784a /lib/cli/pkinewcertcommand.cpp | |
parent | Initial commit. (diff) | |
download | icinga2-0915b3ef56dfac3113cce55a59a5765dc94976be.tar.xz icinga2-0915b3ef56dfac3113cce55a59a5765dc94976be.zip |
Adding upstream version 2.13.6.upstream/2.13.6upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/cli/pkinewcertcommand.cpp')
-rw-r--r-- | lib/cli/pkinewcertcommand.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/cli/pkinewcertcommand.cpp b/lib/cli/pkinewcertcommand.cpp new file mode 100644 index 0000000..5201d92 --- /dev/null +++ b/lib/cli/pkinewcertcommand.cpp @@ -0,0 +1,66 @@ +/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ + +#include "cli/pkinewcertcommand.hpp" +#include "remote/pkiutility.hpp" +#include "base/logger.hpp" + +using namespace icinga; +namespace po = boost::program_options; + +REGISTER_CLICOMMAND("pki/new-cert", PKINewCertCommand); + +String PKINewCertCommand::GetDescription() const +{ + return "Creates a new Certificate Signing Request, a self-signed X509 certificate or both."; +} + +String PKINewCertCommand::GetShortDescription() const +{ + return "creates a new CSR"; +} + +void PKINewCertCommand::InitParameters(boost::program_options::options_description& visibleDesc, + boost::program_options::options_description& hiddenDesc) const +{ + visibleDesc.add_options() + ("cn", po::value<std::string>(), "Common Name") + ("key", po::value<std::string>(), "Key file path (output)") + ("csr", po::value<std::string>(), "CSR file path (optional, output)") + ("cert", po::value<std::string>(), "Certificate file path (optional, output)"); +} + +std::vector<String> PKINewCertCommand::GetArgumentSuggestions(const String& argument, const String& word) const +{ + if (argument == "key" || argument == "csr" || argument == "cert") + return GetBashCompletionSuggestions("file", word); + else + return CLICommand::GetArgumentSuggestions(argument, word); +} + +/** + * The entry point for the "pki new-cert" CLI command. + * + * @returns An exit status. + */ +int PKINewCertCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const +{ + if (!vm.count("cn")) { + Log(LogCritical, "cli", "Common name (--cn) must be specified."); + return 1; + } + + if (!vm.count("key")) { + Log(LogCritical, "cli", "Key file path (--key) must be specified."); + return 1; + } + + String csr, cert; + + if (vm.count("csr")) + csr = vm["csr"].as<std::string>(); + + if (vm.count("cert")) + cert = vm["cert"].as<std::string>(); + + return PkiUtility::NewCert(vm["cn"].as<std::string>(), vm["key"].as<std::string>(), csr, cert); +} |