]> granicus.if.org Git - icinga2/commitdiff
Improve auto-completion for the PKI commands
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 16 Oct 2014 12:33:58 +0000 (14:33 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 16 Oct 2014 12:33:58 +0000 (14:33 +0200)
refs #7247

doc/4-monitoring-remote-systems.md
lib/cli/pkinewcertcommand.cpp
lib/cli/pkirequestcommand.cpp
lib/cli/pkisigncsrcommand.cpp

index 523a09c9514830e652a02adbafbe4762f927853f..6bf302c3364df0948731e879105d408ad8fa9c03 100644 (file)
@@ -198,7 +198,7 @@ object name.
 Example:
 
     # icinga2 pki new-cert --cn icinga2a --keyfile icinga2a.key --csrfile icinga2a.csr
-    # icinga2 pki sign-csr < icinga2a.csr > icinga2a.crt
+    # icinga2 pki sign-csr --csrfile icinga2a.csr --certfile icinga2a.crt
 
     # vim cluster.conf
 
@@ -242,7 +242,7 @@ Now create a certificate and key file for each node running the following comman
 (replace `icinga2a` with the required hostname):
 
     # icinga2 pki new-cert --cn icinga2a --keyfile icinga2a.key --csrfile icinga2a.csr
-    # icinga2 pki sign-csr < icinga2a.csr > icinga2a.crt
+    # icinga2 pki sign-csr --csrfile icinga2a.csr --certfile icinga2a.crt
 
 Repeat the step for all nodes in your cluster scenario.
 
index 3c38cf2b8ff6ba9d2f23fcc8bf24c7ab94033ab3..012f31994d8d338966fc2d73755f204faa3b197b 100644 (file)
@@ -43,9 +43,13 @@ void PKINewCertCommand::InitParameters(boost::program_options::options_descripti
 {
        visibleDesc.add_options()
                ("cn", po::value<std::string>(), "Common Name")
-               ("keyfile", po::value<std::string>(), "Key file path")
-               ("csrfile", po::value<std::string>(), "CSR file path (optional)")
-               ("certfile", po::value<std::string>(), "Certificate file path (optional)");
+               ("keyfile", po::value<std::string>(), "Key file path (output")
+               ("csrfile", po::value<std::string>(), "CSR file path (optional, output)")
+               ("certfile", po::value<std::string>(), "Certificate file path (optional, output)");
+
+       argCompletionDesc["keyfile"] = BashArgumentCompletion("file");
+       argCompletionDesc["csrfile"] = BashArgumentCompletion("file");
+       argCompletionDesc["certfile"] = BashArgumentCompletion("file");
 }
 
 /**
index e8d9ce81f8ffc005b4b91cdee1e85afbe8a748fb..04ddd61e0f6a657880bc6ed938b5de27db0e7d4c 100644 (file)
@@ -54,6 +54,12 @@ void PKIRequestCommand::InitParameters(boost::program_options::options_descripti
            ("host", po::value<std::string>(), "Icinga 2 host")
            ("port", po::value<std::string>(), "Icinga 2 port")
            ("ticket", po::value<std::string>(), "Icinga 2 PKI ticket");
+
+       argCompletionDesc["keyfile"] = BashArgumentCompletion("file");
+       argCompletionDesc["certfile"] = BashArgumentCompletion("file");
+       argCompletionDesc["cafile"] = BashArgumentCompletion("file");
+       argCompletionDesc["host"] = BashArgumentCompletion("hostname");
+       argCompletionDesc["port"] = BashArgumentCompletion("service");
 }
 
 /**
index d5af4d906b505784dbbc5cf7c329081d0f370f69..a6b2ed09b3a35ed50b44e22530f6f75a7ede853f 100644 (file)
@@ -22,6 +22,7 @@
 #include "base/clicommand.hpp"
 #include "base/tlsutility.hpp"
 #include "base/application.hpp"
+#include <fstream>
 
 using namespace icinga;
 namespace po = boost::program_options;
@@ -42,7 +43,12 @@ void PKISignCSRCommand::InitParameters(boost::program_options::options_descripti
     boost::program_options::options_description& hiddenDesc,
     ArgumentCompletionDescription& argCompletionDesc) const
 {
-       /* Command doesn't support any parameters. */
+       visibleDesc.add_options()
+           ("csrfile", po::value<std::string>(), "CSR file path (input)")
+           ("certfile", po::value<std::string>(), "Certificate file path (output)");
+
+       argCompletionDesc["csrfile"] = BashArgumentCompletion("file");
+       argCompletionDesc["certfile"] = BashArgumentCompletion("file");
 }
 
 /**
@@ -52,16 +58,28 @@ void PKISignCSRCommand::InitParameters(boost::program_options::options_descripti
  */
 int PKISignCSRCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
 {
+       if (!vm.count("csrfile")) {
+               Log(LogCritical, "cli", "Certificate signing request file path (--csrfile) must be specified.");
+               return 1;
+       }
+
+       if (!vm.count("certfile")) {
+               Log(LogCritical, "cli", "Certificate file path (--certfile) must be specified.");
+               return 1;
+       }
+
        std::stringstream msgbuf;
        char errbuf[120];
 
        InitializeOpenSSL();
 
-       BIO *csrbio = BIO_new_fp(stdin, BIO_NOCLOSE);
+       String csrfile = vm["csrfile"].as<std::string>();
+
+       BIO *csrbio = BIO_new_file(csrfile.CStr(), "r");
        X509_REQ *req = PEM_read_bio_X509_REQ(csrbio, NULL, NULL, NULL);
 
        if (!req) {
-               msgbuf << "Could not parse X509 certificate request: " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
+               msgbuf << "Could not read X509 certificate request from '" + csrfile + "': " << ERR_peek_error() << ", \"" << ERR_error_string(ERR_peek_error(), errbuf) << "\"";
                Log(LogCritical, "SSL", msgbuf.str());
                return 1;
        }
@@ -72,7 +90,18 @@ int PKISignCSRCommand::Run(const boost::program_options::variables_map& vm, cons
 
        X509_REQ_free(req);
 
-       std::cout << CertificateToString(cert);
+       String certfile = vm["certfile"].as<std::string>();
+
+       std::ofstream fpcert;
+       fpcert.open(certfile.CStr());
+
+       if (!fpcert) {
+               Log(LogCritical, "cli", "Failed to open certificate file '" + certfile + "' for output");
+               return 1;
+       }
+
+       fpcert << CertificateToString(cert);
+       fpcert.close();
 
        return 0;
 }