]> granicus.if.org Git - icinga2/blob - lib/cli/pkinewcertcommand.cpp
add some object locking to the Dump method (which could theoreticylly suffer from...
[icinga2] / lib / cli / pkinewcertcommand.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "cli/pkinewcertcommand.hpp"
4 #include "remote/pkiutility.hpp"
5 #include "base/logger.hpp"
6
7 using namespace icinga;
8 namespace po = boost::program_options;
9
10 REGISTER_CLICOMMAND("pki/new-cert", PKINewCertCommand);
11
12 String PKINewCertCommand::GetDescription() const
13 {
14         return "Creates a new Certificate Signing Request, a self-signed X509 certificate or both.";
15 }
16
17 String PKINewCertCommand::GetShortDescription() const
18 {
19         return "creates a new CSR";
20 }
21
22 void PKINewCertCommand::InitParameters(boost::program_options::options_description& visibleDesc,
23         boost::program_options::options_description& hiddenDesc) const
24 {
25         visibleDesc.add_options()
26                 ("cn", po::value<std::string>(), "Common Name")
27                 ("key", po::value<std::string>(), "Key file path (output)")
28                 ("csr", po::value<std::string>(), "CSR file path (optional, output)")
29                 ("cert", po::value<std::string>(), "Certificate file path (optional, output)");
30 }
31
32 std::vector<String> PKINewCertCommand::GetArgumentSuggestions(const String& argument, const String& word) const
33 {
34         if (argument == "key" || argument == "csr" || argument == "cert")
35                 return GetBashCompletionSuggestions("file", word);
36         else
37                 return CLICommand::GetArgumentSuggestions(argument, word);
38 }
39
40 /**
41  * The entry point for the "pki new-cert" CLI command.
42  *
43  * @returns An exit status.
44  */
45 int PKINewCertCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
46 {
47         if (!vm.count("cn")) {
48                 Log(LogCritical, "cli", "Common name (--cn) must be specified.");
49                 return 1;
50         }
51
52         if (!vm.count("key")) {
53                 Log(LogCritical, "cli", "Key file path (--key) must be specified.");
54                 return 1;
55         }
56
57         String csr, cert;
58
59         if (vm.count("csr"))
60                 csr = vm["csr"].as<std::string>();
61
62         if (vm.count("cert"))
63                 cert = vm["cert"].as<std::string>();
64
65         return PkiUtility::NewCert(vm["cn"].as<std::string>(), vm["key"].as<std::string>(), csr, cert);
66 }