Refactor the node wizard/setup CLI commands

refs #5450
This commit is contained in:
Michael Friedrich 2017-09-06 17:33:54 +02:00 committed by Gunnar Beutner
parent 181b91b759
commit e424017c15
3 changed files with 533 additions and 505 deletions

View File

@ -131,14 +131,14 @@ int NodeSetupCommand::SetupMaster(const boost::program_options::variables_map& v
cn = vm["cn"].as<std::string>();
/* check whether the user wants to generate a new certificate or not */
String existing_path = ApiListener::GetCertsDir() + "/" + cn + ".crt";
String existingPath = ApiListener::GetCertsDir() + "/" + cn + ".crt";
Log(LogInformation, "cli")
<< "Checking for existing certificates for common name '" << cn << "'...";
<< "Checking in existing certificates for common name '" << cn << "'...";
if (Utility::PathExists(existing_path)) {
if (Utility::PathExists(existingPath)) {
Log(LogWarning, "cli")
<< "Certificate '" << existing_path << "' for CN '" << cn << "' already exists. Not generating new certificate.";
<< "Certificate '" << existingPath << "' for CN '" << cn << "' already exists. Not generating new certificate.";
} else {
Log(LogInformation, "cli")
<< "Certificates not yet generated. Running 'api setup' now.";
@ -157,13 +157,11 @@ int NodeSetupCommand::SetupMaster(const boost::program_options::variables_map& v
}
/* write zones.conf and update with zone + endpoint information */
Log(LogInformation, "cli", "Generating zone and object configuration.");
NodeUtility::GenerateNodeMasterIcingaConfig();
/* update the ApiListener config - SetupMaster() will always enable it */
Log(LogInformation, "cli", "Updating the APIListener feature.");
String apipath = FeatureUtility::GetFeaturesAvailablePath() + "/api.conf";
@ -263,7 +261,8 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm,
/* require master host information for auto-signing requests */
if (!vm.count("master_host")) {
Log(LogCritical, "cli", "Please pass the master host connection information for auto-signing using '--master_host <host>'");
Log(LogCritical, "cli", "Please pass the master host connection information for auto-signing using '--master_host <host>'. This can also be a direct parent satellite since 2.8.");
return 1;
}
@ -279,13 +278,13 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm,
master_port = tokens[1];
Log(LogInformation, "cli")
<< "Verifying master host connection information: host '" << master_host << "', port '" << master_port << "'.";
<< "Verifying parent host connection information: host '" << master_host << "', port '" << master_port << "'.";
/* trusted cert must be passed (retrieved by the user with 'pki save-cert' before) */
if (!vm.count("trustedcert")) {
Log(LogCritical, "cli")
<< "Please pass the trusted cert retrieved from the master\n"
<< "Please pass the trusted cert retrieved from the parent node (master or satellite)\n"
<< "(Hint: 'icinga2 pki save-cert --host <masterhost> --port <5665> --key local.key --cert local.crt --trustedcert master.crt').";
return 1;
}
@ -337,10 +336,10 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm,
<< "Cannot set ownership for user '" << user << "' group '" << group << "' on file '" << key << "'. Verify it yourself!";
}
Log(LogInformation, "cli", "Requesting a signed certificate from the master.");
Log(LogInformation, "cli", "Requesting a signed certificate from the parent Icinga node.");
if (PkiUtility::RequestCertificate(master_host, master_port, key, cert, ca, trustedcert, ticket) != 0) {
Log(LogCritical, "cli", "Failed to request certificate from Icinga 2 master.");
Log(LogCritical, "cli", "Failed to request certificate from parent Icinga node.");
return 1;
}
@ -431,7 +430,7 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm,
/* update constants.conf with NodeName = CN */
if (cn != Utility::GetFQDN()) {
Log(LogWarning, "cli")
<< "CN '" << cn << "' does not match the default FQDN '" << Utility::GetFQDN() << "'. Requires update for NodeName constant in constants.conf!";
<< "CN '" << cn << "' does not match the default FQDN '" << Utility::GetFQDN() << "'. Requires an update for the NodeName constant in constants.conf!";
}
Log(LogInformation, "cli", "Updating constants.conf.");

View File

@ -78,12 +78,10 @@ int NodeWizardCommand::Run(const boost::program_options::variables_map& vm,
std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundBlue)
<< "Welcome to the Icinga 2 Setup Wizard!\n"
<< "\n"
<< "We'll guide you through all required configuration details.\n"
<< "We will guide you through all required configuration details.\n"
<< "\n"
<< ConsoleColorTag(Console_Normal);
//TODO: Add sort of bash completion to path input?
/* 0. master or node setup?
* 1. Ticket
* 2. Master information for autosigning
@ -100,11 +98,9 @@ int NodeWizardCommand::Run(const boost::program_options::variables_map& vm,
*/
std::string answer;
bool is_node_setup = true;
/* master or node setup */
/* master or satellite/client setup */
std::cout << ConsoleColorTag(Console_Bold)
<< "Please specify if this is a satellite setup "
<< "Please specify if this is a satellite/client setup "
<< "('n' installs a master setup)" << ConsoleColorTag(Console_Normal)
<< " [Y/n]: ";
std::getline (std::cin, answer);
@ -113,12 +109,36 @@ int NodeWizardCommand::Run(const boost::program_options::variables_map& vm,
String choice = answer;
if (choice.Contains("n"))
is_node_setup = false;
std::cout << "\n";
if (is_node_setup) {
/* node setup part */
std::cout << "Starting the Node setup routine...\n";
int res = 0;
if (choice.Contains("n"))
res = MasterSetup();
else
res = ClientSetup();
if (res != 0)
return res;
std::cout << "\n";
std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundGreen)
<< "Done.\n\n"
<< ConsoleColorTag(Console_Normal);
std::cout << ConsoleColorTag(Console_Bold | Console_ForegroundRed)
<< "Now restart your Icinga 2 daemon to finish the installation!\n\n"
<< ConsoleColorTag(Console_Normal);
return 0;
}
int NodeWizardCommand::ClientSetup(void) const
{
std::string answer;
String choice;
std::cout << "Starting the Client/Satellite setup routine...\n";
/* CN */
std::cout << ConsoleColorTag(Console_Bold)
@ -136,30 +156,30 @@ int NodeWizardCommand::Run(const boost::program_options::variables_map& vm,
std::vector<std::string> endpoints;
String endpoint_buffer;
String endpointBuffer;
std::cout << ConsoleColorTag(Console_Bold)
<< "Please specify the master endpoint(s) this node should connect to:"
<< "Please specify the parent endpoint(s) (master or satellite) where this node should connect to:"
<< ConsoleColorTag(Console_Normal) << "\n";
String master_endpoint_name;
String parentEndpointName;
wizard_endpoint_loop_start:
std::cout << ConsoleColorTag(Console_Bold)
<< "Master Common Name" << ConsoleColorTag(Console_Normal)
<< " (CN from your master setup): ";
<< "Master/Satellite Common Name" << ConsoleColorTag(Console_Normal)
<< " (CN from your master/satellite node): ";
std::getline(std::cin, answer);
if (answer.empty()) {
Log(LogWarning, "cli", "Master CN is required! Please retry.");
Log(LogWarning, "cli", "Master/Satellite CN is required! Please retry.");
goto wizard_endpoint_loop_start;
}
endpoint_buffer = answer;
endpoint_buffer = endpoint_buffer.Trim();
endpointBuffer = answer;
endpointBuffer = endpointBuffer.Trim();
std::cout << "Do you want to establish a connection to the master "
std::cout << "Do you want to establish a connection to the parent node "
<< ConsoleColorTag(Console_Bold) << "from this node?"
<< ConsoleColorTag(Console_Normal) << " [Y/n]: ";
@ -167,91 +187,91 @@ wizard_endpoint_loop_start:
boost::algorithm::to_lower(answer);
choice = answer;
String tmpPort = "5665";
String parentEndpointPort = "5665";
if (choice.Contains("n")) {
Log(LogWarning, "cli", "Node to master connection setup skipped");
std::cout << "Connection setup skipped. Please configure your master to connect to this node.\n";
Log(LogWarning, "cli", "Node to master/satellite connection setup skipped");
std::cout << "Connection setup skipped. Please configure your parent node to connect to this node by setting the 'host' attribute for the node Endpoint object.\n";
} else {
std::cout << ConsoleColorTag(Console_Bold)
<< "Please fill out the master connection information:"
<< "Please specify the master/satellite connection information:"
<< ConsoleColorTag(Console_Normal) << "\n"
<< ConsoleColorTag(Console_Bold) << "Master endpoint host"
<< ConsoleColorTag(Console_Normal) << " (Your master's IP address or FQDN): ";
<< ConsoleColorTag(Console_Bold) << "Master/Satellite endpoint host"
<< ConsoleColorTag(Console_Normal) << " (IP address or FQDN): ";
std::getline(std::cin, answer);
if (answer.empty()) {
Log(LogWarning, "cli", "Please enter the master's endpoint connection information.");
Log(LogWarning, "cli", "Please enter the parent endpoint (master/satellite) connection information.");
goto wizard_endpoint_loop_start;
}
String tmp = answer;
tmp = tmp.Trim();
endpoint_buffer += "," + tmp;
master_endpoint_name = tmp; //store the endpoint name for later
endpointBuffer += "," + tmp;
parentEndpointName = tmp;
std::cout << ConsoleColorTag(Console_Bold)
<< "Master endpoint port" << ConsoleColorTag(Console_Normal)
<< " [" << tmpPort << "]: ";
<< "Master/Satellite endpoint port" << ConsoleColorTag(Console_Normal)
<< " [" << parentEndpointPort << "]: ";
std::getline(std::cin, answer);
if (!answer.empty())
tmpPort = answer;
parentEndpointPort = answer;
endpoint_buffer += "," + tmpPort.Trim();
endpointBuffer += "," + parentEndpointPort.Trim();
}
endpoints.push_back(endpoint_buffer);
endpoints.push_back(endpointBuffer);
std::cout << ConsoleColorTag(Console_Bold) << "Add more master endpoints?"
std::cout << ConsoleColorTag(Console_Bold) << "Add more master/satellite endpoints?"
<< ConsoleColorTag(Console_Normal) << " [y/N]: ";
std::getline (std::cin, answer);
boost::algorithm::to_lower(answer);
String choice = answer;
choice = answer;
if (choice.Contains("y"))
goto wizard_endpoint_loop_start;
String master_host, master_port;
String parentHost, parentPort;
for (const String& endpoint : endpoints) {
std::vector<String> tokens = endpoint.Split(",");
if (tokens.size() > 1)
master_host = tokens[1];
parentHost = tokens[1];
if (tokens.size() > 2)
master_port = tokens[2];
parentPort = tokens[2];
}
/* workaround for fetching the master cert */
String pki_path = ApiListener::GetCertsDir();
Utility::MkDirP(pki_path, 0700);
String certsDir = ApiListener::GetCertsDir();
Utility::MkDirP(certsDir, 0700);
String user = ScriptGlobal::Get("RunAsUser");
String group = ScriptGlobal::Get("RunAsGroup");
if (!Utility::SetFileOwnership(pki_path, user, group)) {
if (!Utility::SetFileOwnership(certsDir, user, group)) {
Log(LogWarning, "cli")
<< "Cannot set ownership for user '" << user
<< "' group '" << group
<< "' on file '" << pki_path << "'. Verify it yourself!";
<< "' on file '" << certsDir << "'. Verify it yourself!";
}
String node_cert = pki_path + "/" + cn + ".crt";
String node_key = pki_path + "/" + cn + ".key";
String nodeCert = certsDir + "/" + cn + ".crt";
String nodeKey = certsDir + "/" + cn + ".key";
if (Utility::PathExists(node_key))
NodeUtility::CreateBackupFile(node_key, true);
if (Utility::PathExists(node_cert))
NodeUtility::CreateBackupFile(node_cert);
if (Utility::PathExists(nodeKey))
NodeUtility::CreateBackupFile(nodeKey, true);
if (Utility::PathExists(nodeCert))
NodeUtility::CreateBackupFile(nodeCert);
if (PkiUtility::NewCert(cn, node_key, Empty, node_cert) > 0) {
if (PkiUtility::NewCert(cn, nodeKey, Empty, nodeCert) > 0) {
Log(LogCritical, "cli")
<< "Failed to create new self-signed certificate for CN '"
<< cn << "'. Please try again.";
@ -259,29 +279,29 @@ wizard_endpoint_loop_start:
}
/* fix permissions: root -> icinga daemon user */
if (!Utility::SetFileOwnership(node_key, user, group)) {
if (!Utility::SetFileOwnership(nodeKey, user, group)) {
Log(LogWarning, "cli")
<< "Cannot set ownership for user '" << user
<< "' group '" << group
<< "' on file '" << node_key << "'. Verify it yourself!";
<< "' on file '" << nodeKey << "'. Verify it yourself!";
}
boost::shared_ptr<X509> trustedcert;
boost::shared_ptr<X509> trustedParentCert;
if (!master_host.IsEmpty()) {
if (!parentHost.IsEmpty()) {
//save-cert and store the master certificate somewhere
Log(LogInformation, "cli")
<< "Fetching public certificate from master ("
<< master_host << ", " << master_port << "):\n";
<< parentHost << ", " << parentPort << "):\n";
trustedcert = PkiUtility::FetchCert(master_host, master_port);
if (!trustedcert) {
trustedParentCert = PkiUtility::FetchCert(parentHost, parentPort);
if (!trustedParentCert) {
Log(LogCritical, "cli", "Peer did not present a valid certificate.");
return 1;
}
std::cout << ConsoleColorTag(Console_Bold) << "Certificate information:\n"
<< ConsoleColorTag(Console_Normal) << PkiUtility::GetCertificateInformation(trustedcert)
std::cout << ConsoleColorTag(Console_Bold) << "Parent certificate information:\n"
<< ConsoleColorTag(Console_Normal) << PkiUtility::GetCertificateInformation(trustedParentCert)
<< ConsoleColorTag(Console_Bold) << "\nIs this information correct?"
<< ConsoleColorTag(Console_Normal) << " [y/N]: ";
@ -292,7 +312,7 @@ wizard_endpoint_loop_start:
return 1;
}
Log(LogInformation, "cli", "Received trusted master certificate.\n");
Log(LogInformation, "cli", "Received trusted parent certificate.\n");
}
wizard_ticket:
@ -313,7 +333,7 @@ wizard_ticket:
String ticket = answer;
ticket = ticket.Trim();
if (!master_host.IsEmpty()) {
if (!parentHost.IsEmpty()) {
if (ticket.IsEmpty()) {
Log(LogInformation, "cli")
<< "Requesting certificate without a ticket.";
@ -322,31 +342,33 @@ wizard_ticket:
<< "Requesting certificate with ticket '" << ticket << "'.";
}
String target_ca = pki_path + "/ca.crt";
String nodeCA = certsDir + "/ca.crt";
if (Utility::PathExists(target_ca))
NodeUtility::CreateBackupFile(target_ca);
if (Utility::PathExists(node_cert))
NodeUtility::CreateBackupFile(node_cert);
if (Utility::PathExists(nodeCA))
NodeUtility::CreateBackupFile(nodeCA);
if (Utility::PathExists(nodeCert))
NodeUtility::CreateBackupFile(nodeCert);
if (PkiUtility::RequestCertificate(master_host, master_port, node_key,
node_cert, target_ca, trustedcert, ticket) > 0) {
if (PkiUtility::RequestCertificate(parentHost, parentPort, nodeKey,
nodeCert, nodeCA, trustedParentCert, ticket) > 0) {
Log(LogCritical, "cli")
<< "Failed to fetch signed certificate from master '"
<< master_host << ", "
<< master_port <<"'. Please try again.";
<< parentHost << ", "
<< parentPort <<"'. Please try again.";
goto wizard_ticket;
}
/* fix permissions (again) when updating the signed certificate */
if (!Utility::SetFileOwnership(node_cert, user, group)) {
if (!Utility::SetFileOwnership(nodeCert, user, group)) {
Log(LogWarning, "cli")
<< "Cannot set ownership for user '" << user
<< "' group '" << group << "' on file '"
<< node_cert << "'. Verify it yourself!";
<< nodeCert << "'. Verify it yourself!";
}
}
std::cout << "\n";
/* apilistener config */
std::cout << ConsoleColorTag(Console_Bold)
<< "Please specify the API bind host/port"
@ -356,33 +378,35 @@ wizard_ticket:
std::getline(std::cin, answer);
String bind_host = answer;
bind_host = bind_host.Trim();
String bindHost = answer;
bindHost = bindHost.Trim();
std::cout << "Bind Port []: ";
std::getline(std::cin, answer);
String bind_port = answer;
bind_port = bind_port.Trim();
String bindPort = answer;
bindPort = bindPort.Trim();
std::cout << ConsoleColorTag(Console_Bold) << "\n"
<< "Accept config from master?" << ConsoleColorTag(Console_Normal)
<< "Accept config from parent node?" << ConsoleColorTag(Console_Normal)
<< " [y/N]: ";
std::getline(std::cin, answer);
boost::algorithm::to_lower(answer);
choice = answer;
String accept_config = choice.Contains("y") ? "true" : "false";
String acceptConfig = choice.Contains("y") ? "true" : "false";
std::cout << ConsoleColorTag(Console_Bold)
<< "Accept commands from master?" << ConsoleColorTag(Console_Normal)
<< "Accept commands from parent node?" << ConsoleColorTag(Console_Normal)
<< " [y/N]: ";
std::getline(std::cin, answer);
boost::algorithm::to_lower(answer);
choice = answer;
String accept_commands = choice.Contains("y") ? "true" : "false";
String acceptCommands = choice.Contains("y") ? "true" : "false";
std::cout << "\n";
/* disable the notifications feature on client nodes */
Log(LogInformation, "cli", "Disabling the Notification feature.");
@ -397,11 +421,11 @@ wizard_ticket:
enable.push_back("api");
FeatureUtility::EnableFeatures(enable);
String apipath = FeatureUtility::GetFeaturesAvailablePath() + "/api.conf";
NodeUtility::CreateBackupFile(apipath);
String apiConfPath = FeatureUtility::GetFeaturesAvailablePath() + "/api.conf";
NodeUtility::CreateBackupFile(apiConfPath);
std::fstream fp;
String tempApiPath = Utility::CreateTempFile(apipath + ".XXXXXX", 0644, fp);
String tempApiConfPath = Utility::CreateTempFile(apiConfPath + ".XXXXXX", 0644, fp);
fp << "/**\n"
<< " * The API listener is used for distributed monitoring setups.\n"
@ -411,28 +435,27 @@ wizard_ticket:
<< " key_path = LocalStateDir + \"/lib/icinga2/certs/\" + NodeName + \".key\"\n"
<< " ca_path = LocalStateDir + \"/lib/icinga2/certs/ca.crt\"\n"
<< "\n"
<< " accept_config = " << accept_config << "\n"
<< " accept_commands = " << accept_commands << "\n";
<< " accept_config = " << acceptConfig << "\n"
<< " accept_commands = " << acceptCommands << "\n";
if (!bind_host.IsEmpty())
fp << " bind_host = \"" << bind_host << "\"\n";
if (!bind_port.IsEmpty())
fp << " bind_port = " << bind_port << "\n";
if (!bindHost.IsEmpty())
fp << " bind_host = \"" << bindHost << "\"\n";
if (!bindPort.IsEmpty())
fp << " bind_port = " << bindPort << "\n";
fp << "\n"
<< "}\n";
fp << "}\n";
fp.close();
#ifdef _WIN32
_unlink(apipath.CStr());
_unlink(apiConfPath.CStr());
#endif /* _WIN32 */
if (rename(tempApiPath.CStr(), apipath.CStr()) < 0) {
if (rename(tempApiConfPath.CStr(), apiConfPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempApiPath));
<< boost::errinfo_file_name(tempApiConfPath));
}
/* apilistener config */
@ -480,8 +503,15 @@ wizard_ticket:
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempTicketPath));
}
} else {
/* master setup */
return 0;
}
int NodeWizardCommand::MasterSetup(void) const
{
std::string answer;
String choice;
std::cout << ConsoleColorTag(Console_Bold) << "Starting the Master setup routine...\n";
/* CN */
@ -531,23 +561,23 @@ wizard_ticket:
std::getline(std::cin, answer);
String bind_host = answer;
bind_host = bind_host.Trim();
String bindHost = answer;
bindHost = bindHost.Trim();
std::cout << ConsoleColorTag(Console_Bold)
<< "Bind Port" << ConsoleColorTag(Console_Normal) << " []: ";
std::getline(std::cin, answer);
String bind_port = answer;
bind_port = bind_port.Trim();
String bindPort = answer;
bindPort = bindPort.Trim();
/* api feature is always enabled, check above */
String apipath = FeatureUtility::GetFeaturesAvailablePath() + "/api.conf";
NodeUtility::CreateBackupFile(apipath);
String apiConfPath = FeatureUtility::GetFeaturesAvailablePath() + "/api.conf";
NodeUtility::CreateBackupFile(apiConfPath);
std::fstream fp;
String tempApiPath = Utility::CreateTempFile(apipath + ".XXXXXX", 0644, fp);
String tempApiConfPath = Utility::CreateTempFile(apiConfPath + ".XXXXXX", 0644, fp);
fp << "/**\n"
<< " * The API listener is used for distributed monitoring setups.\n"
@ -557,10 +587,10 @@ wizard_ticket:
<< " key_path = LocalStateDir + \"/lib/icinga2/certs/\" + NodeName + \".key\"\n"
<< " ca_path = LocalStateDir + \"/lib/icinga2/certs/ca.crt\"\n";
if (!bind_host.IsEmpty())
fp << " bind_host = \"" << bind_host << "\"\n";
if (!bind_port.IsEmpty())
fp << " bind_port = " << bind_port << "\n";
if (!bindHost.IsEmpty())
fp << " bind_host = \"" << bindHost << "\"\n";
if (!bindPort.IsEmpty())
fp << " bind_port = " << bindPort << "\n";
fp << "\n"
<< " ticket_salt = TicketSalt\n"
@ -569,21 +599,21 @@ wizard_ticket:
fp.close();
#ifdef _WIN32
_unlink(apipath.CStr());
_unlink(apiConfPath.CStr());
#endif /* _WIN32 */
if (rename(tempApiPath.CStr(), apipath.CStr()) < 0) {
if (rename(tempApiConfPath.CStr(), apiConfPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempApiPath));
<< boost::errinfo_file_name(tempApiConfPath));
}
/* update constants.conf with NodeName = CN + TicketSalt = random value */
if (cn != Utility::GetFQDN()) {
Log(LogWarning, "cli")
<< "CN '" << cn << "' does not match the default FQDN '"
<< Utility::GetFQDN() << "'. Requires update for NodeName constant in constants.conf!";
<< Utility::GetFQDN() << "'. Requires an update for the NodeName constant in constants.conf!";
}
Log(LogInformation, "cli", "Updating constants.conf.");
@ -598,11 +628,6 @@ wizard_ticket:
String salt = RandomString(16);
NodeUtility::UpdateConstant("TicketSalt", salt);
}
std::cout << "Done.\n\n";
std::cout << "Now restart your Icinga 2 daemon to finish the installation!\n\n";
return 0;
}

View File

@ -40,6 +40,10 @@ public:
virtual int GetMaxArguments(void) const override;
virtual int Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const override;
virtual ImpersonationLevel GetImpersonationLevel(void) const override;
private:
int ClientSetup(void) const;
int MasterSetup(void) const;
};
}