ApiListener#Start(): auto-renew own cert if CA owner

otherwise that particular cert would expire.
This commit is contained in:
Alexander A. Klimov 2022-03-29 16:45:18 +02:00
parent 6d470a3ca5
commit 3753f86c80
2 changed files with 35 additions and 0 deletions

View File

@ -235,6 +235,7 @@ void ApiListener::Start(bool runtimeCreated)
<< "'" << GetName() << "' started.";
SyncLocalZoneDirs();
RenewOwnCert();
ObjectImpl<ApiListener>::Start(runtimeCreated);
@ -285,6 +286,39 @@ void ApiListener::Start(bool runtimeCreated)
OnMasterChanged(true);
}
void ApiListener::RenewOwnCert()
{
if (!Utility::PathExists(GetIcingaCADir() + "/ca.key")) {
return;
}
auto certPath (GetDefaultCertPath());
auto cert (GetX509Certificate(certPath));
if (IsCertUptodate(cert)) {
return;
}
Log(LogInformation, "ApiListener")
<< "Our certificate will expire soon, but we own the CA. Renewing.";
cert = RenewCert(cert);
if (!cert) {
return;
}
std::fstream certfp;
auto tempCertPath (Utility::CreateTempFile(certPath + ".XXXXXX", 0644, certfp));
certfp.exceptions(std::ofstream::failbit | std::ofstream::badbit);
certfp << CertificateToString(cert);
certfp.close();
Utility::RenameFile(tempCertPath, certPath);
UpdateSSLContext();
}
void ApiListener::Stop(bool runtimeDeleted)
{
ObjectImpl<ApiListener>::Stop(runtimeDeleted);

View File

@ -222,6 +222,7 @@ private:
void SyncLocalZoneDirs() const;
void SyncLocalZoneDir(const Zone::Ptr& zone) const;
void RenewOwnCert();
void SendConfigUpdate(const JsonRpcConnection::Ptr& aclient);