]> granicus.if.org Git - icinga2/blob - lib/cli/pkirequestcommand.cpp
add some object locking to the Dump method (which could theoreticylly suffer from...
[icinga2] / lib / cli / pkirequestcommand.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "cli/pkirequestcommand.hpp"
4 #include "remote/pkiutility.hpp"
5 #include "base/logger.hpp"
6 #include "base/tlsutility.hpp"
7 #include <iostream>
8
9 using namespace icinga;
10 namespace po = boost::program_options;
11
12 REGISTER_CLICOMMAND("pki/request", PKIRequestCommand);
13
14 String PKIRequestCommand::GetDescription() const
15 {
16         return "Sends a PKI request to Icinga 2.";
17 }
18
19 String PKIRequestCommand::GetShortDescription() const
20 {
21         return "requests a certificate";
22 }
23
24 void PKIRequestCommand::InitParameters(boost::program_options::options_description& visibleDesc,
25         boost::program_options::options_description& hiddenDesc) const
26 {
27         visibleDesc.add_options()
28                 ("key", po::value<std::string>(), "Key file path (input)")
29                 ("cert", po::value<std::string>(), "Certificate file path (input + output)")
30                 ("ca", po::value<std::string>(), "CA file path (output)")
31                 ("trustedcert", po::value<std::string>(), "Trusted certificate file path (input)")
32                 ("host", po::value<std::string>(), "Icinga 2 host")
33                 ("port", po::value<std::string>(), "Icinga 2 port")
34                 ("ticket", po::value<std::string>(), "Icinga 2 PKI ticket");
35 }
36
37 std::vector<String> PKIRequestCommand::GetArgumentSuggestions(const String& argument, const String& word) const
38 {
39         if (argument == "key" || argument == "cert" || argument == "ca" || argument == "trustedcert")
40                 return GetBashCompletionSuggestions("file", word);
41         else if (argument == "host")
42                 return GetBashCompletionSuggestions("hostname", word);
43         else if (argument == "port")
44                 return GetBashCompletionSuggestions("service", word);
45         else
46                 return CLICommand::GetArgumentSuggestions(argument, word);
47 }
48
49 /**
50  * The entry point for the "pki request" CLI command.
51  *
52  * @returns An exit status.
53  */
54 int PKIRequestCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
55 {
56         if (!vm.count("host")) {
57                 Log(LogCritical, "cli", "Icinga 2 host (--host) must be specified.");
58                 return 1;
59         }
60
61         if (!vm.count("key")) {
62                 Log(LogCritical, "cli", "Key input file path (--key) must be specified.");
63                 return 1;
64         }
65
66         if (!vm.count("cert")) {
67                 Log(LogCritical, "cli", "Certificate output file path (--cert) must be specified.");
68                 return 1;
69         }
70
71         if (!vm.count("ca")) {
72                 Log(LogCritical, "cli", "CA certificate output file path (--ca) must be specified.");
73                 return 1;
74         }
75
76         if (!vm.count("trustedcert")) {
77                 Log(LogCritical, "cli", "Trusted certificate input file path (--trustedcert) must be specified.");
78                 return 1;
79         }
80
81         String port = "5665";
82         String ticket;
83
84         if (vm.count("port"))
85                 port = vm["port"].as<std::string>();
86
87         if (vm.count("ticket"))
88                 ticket = vm["ticket"].as<std::string>();
89
90         return PkiUtility::RequestCertificate(vm["host"].as<std::string>(), port, vm["key"].as<std::string>(),
91                 vm["cert"].as<std::string>(), vm["ca"].as<std::string>(), GetX509Certificate(vm["trustedcert"].as<std::string>()),
92                 ticket);
93 }