summaryrefslogtreecommitdiffstats
path: root/lib/cli/calistcommand.cpp
blob: f693ad72fd7467164ae336fb81a54a222117006c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "cli/calistcommand.hpp"
#include "remote/apilistener.hpp"
#include "remote/pkiutility.hpp"
#include "base/logger.hpp"
#include "base/application.hpp"
#include "base/tlsutility.hpp"
#include "base/json.hpp"
#include <iostream>

using namespace icinga;
namespace po = boost::program_options;

REGISTER_CLICOMMAND("ca/list", CAListCommand);

/**
 * Provide a long CLI description sentence.
 *
 * @return text
 */
String CAListCommand::GetDescription() const
{
	return "Lists pending certificate signing requests.";
}

/**
 * Provide a short CLI description.
 *
 * @return text
 */
String CAListCommand::GetShortDescription() const
{
	return "lists pending certificate signing requests";
}

/**
 * Initialize available CLI parameters.
 *
 * @param visibleDesc Register visible parameters.
 * @param hiddenDesc Register hidden parameters.
 */
void CAListCommand::InitParameters(boost::program_options::options_description& visibleDesc,
	boost::program_options::options_description& hiddenDesc) const
{
	visibleDesc.add_options()
		("all", "List all certificate signing requests, including signed. Note: Old requests are automatically cleaned by Icinga after 1 week.")
		("removed", "List all removed CSRs (for use with 'ca restore')")
		("json", "encode output as JSON");
}

/**
 * The entry point for the "ca list" CLI command.
 *
 * @return An exit status.
 */
int CAListCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
{
	Dictionary::Ptr requests = PkiUtility::GetCertificateRequests(vm.count("removed"));

	if (vm.count("json"))
		std::cout << JsonEncode(requests);
	else {
		ObjectLock olock(requests);

		std::cout << "Fingerprint                                                      | Timestamp                | Signed | Subject\n";
		std::cout << "-----------------------------------------------------------------|--------------------------|--------|--------\n";

		for (auto& kv : requests) {
			Dictionary::Ptr request = kv.second;

			/* Skip signed requests by default. */
			if (!vm.count("all") && request->Contains("cert_response"))
				continue;

			std::cout << kv.first
				<< " | "
/*			    << Utility::FormatDateTime("%Y/%m/%d %H:%M:%S", request->Get("timestamp")) */
				<< request->Get("timestamp")
				<< " | "
				<< (request->Contains("cert_response") ? "*" : " ") << "     "
				<< " | "
				<< request->Get("subject")
				<< "\n";
		}
	}

	return 0;
}