1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "cli/pkinewcertcommand.hpp"
21 #include "remote/pkiutility.hpp"
22 #include "base/logger.hpp"
24 using namespace icinga;
25 namespace po = boost::program_options;
27 REGISTER_CLICOMMAND("pki/new-cert", PKINewCertCommand);
29 String PKINewCertCommand::GetDescription() const
31 return "Creates a new Certificate Signing Request, a self-signed X509 certificate or both.";
34 String PKINewCertCommand::GetShortDescription() const
36 return "creates a new CSR";
39 void PKINewCertCommand::InitParameters(boost::program_options::options_description& visibleDesc,
40 boost::program_options::options_description& hiddenDesc) const
42 visibleDesc.add_options()
43 ("cn", po::value<std::string>(), "Common Name")
44 ("key", po::value<std::string>(), "Key file path (output)")
45 ("csr", po::value<std::string>(), "CSR file path (optional, output)")
46 ("cert", po::value<std::string>(), "Certificate file path (optional, output)");
49 std::vector<String> PKINewCertCommand::GetArgumentSuggestions(const String& argument, const String& word) const
51 if (argument == "key" || argument == "csr" || argument == "cert")
52 return GetBashCompletionSuggestions("file", word);
54 return CLICommand::GetArgumentSuggestions(argument, word);
58 * The entry point for the "pki new-cert" CLI command.
60 * @returns An exit status.
62 int PKINewCertCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
64 if (!vm.count("cn")) {
65 Log(LogCritical, "cli", "Common name (--cn) must be specified.");
69 if (!vm.count("key")) {
70 Log(LogCritical, "cli", "Key file path (--key) must be specified.");
77 csr = vm["csr"].as<std::string>();
80 cert = vm["cert"].as<std::string>();
82 return PkiUtility::NewCert(vm["cn"].as<std::string>(), vm["key"].as<std::string>(), csr, cert);