]> granicus.if.org Git - icinga2/blob - lib/cli/pkinewcertcommand.cpp
ApiListener#ApiTimerHandler(): delete all replayed logs
[icinga2] / lib / cli / pkinewcertcommand.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "cli/pkinewcertcommand.hpp"
21 #include "remote/pkiutility.hpp"
22 #include "base/logger.hpp"
23
24 using namespace icinga;
25 namespace po = boost::program_options;
26
27 REGISTER_CLICOMMAND("pki/new-cert", PKINewCertCommand);
28
29 String PKINewCertCommand::GetDescription() const
30 {
31         return "Creates a new Certificate Signing Request, a self-signed X509 certificate or both.";
32 }
33
34 String PKINewCertCommand::GetShortDescription() const
35 {
36         return "creates a new CSR";
37 }
38
39 void PKINewCertCommand::InitParameters(boost::program_options::options_description& visibleDesc,
40         boost::program_options::options_description& hiddenDesc) const
41 {
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)");
47 }
48
49 std::vector<String> PKINewCertCommand::GetArgumentSuggestions(const String& argument, const String& word) const
50 {
51         if (argument == "key" || argument == "csr" || argument == "cert")
52                 return GetBashCompletionSuggestions("file", word);
53         else
54                 return CLICommand::GetArgumentSuggestions(argument, word);
55 }
56
57 /**
58  * The entry point for the "pki new-cert" CLI command.
59  *
60  * @returns An exit status.
61  */
62 int PKINewCertCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
63 {
64         if (!vm.count("cn")) {
65                 Log(LogCritical, "cli", "Common name (--cn) must be specified.");
66                 return 1;
67         }
68
69         if (!vm.count("key")) {
70                 Log(LogCritical, "cli", "Key file path (--key) must be specified.");
71                 return 1;
72         }
73
74         String csr, cert;
75
76         if (vm.count("csr"))
77                 csr = vm["csr"].as<std::string>();
78
79         if (vm.count("cert"))
80                 cert = vm["cert"].as<std::string>();
81
82         return PkiUtility::NewCert(vm["cn"].as<std::string>(), vm["key"].as<std::string>(), csr, cert);
83 }