Fix certificate uploading

This commit is contained in:
Minecon724 2025-07-30 20:29:16 +02:00
commit c43a11fdd2
Signed by: Minecon724
GPG key ID: A02E6E67AB961189
2 changed files with 12 additions and 11 deletions

View file

@ -4,13 +4,14 @@
#include <filesystem>
#include <system_error>
void write_to_file(std::filesystem::path path, const std::string& content) {
std::ofstream private_key_file;
private_key_file.exceptions(std::ios::failbit | std::ios::badbit);
#include <iostream>
// These lines will throw
private_key_file.open(path);
private_key_file << content;
void write_to_file(const std::filesystem::path& path, const std::string& content) {
std::ofstream file;
file.exceptions(std::ios::failbit | std::ios::badbit);
file.open(path);
file << content;
}
CertificateManager::CertificateManager(const std::filesystem::path& certificate_root_path)
@ -26,10 +27,10 @@ bool CertificateManager::certificate_exists(const std::string& domain) const {
void CertificateManager::write_private_key(const std::string& domain, const std::string& private_key) const {
std::filesystem::path path = get_certificate_directory(domain) / "privkey.pem";
write_to_file(domain, private_key.c_str());
write_to_file(path, private_key.c_str());
}
void CertificateManager::write_certificate(const std::string& domain, const std::string& private_key) const {
std::filesystem::path path = get_certificate_directory(domain) / "fullchain.pem";
write_to_file(domain, private_key.c_str());
write_to_file(path, private_key.c_str());
}

View file

@ -38,7 +38,7 @@ void ControlServer::setup_routes() {
catch(const std::exception& e)
{
response_server_error(res, e.what(), httplib::StatusCode::InternalServerError_500);
std::cout << "Failed to reload Nginx: " << e.what() << std::endl;
std::cerr << "Failed to reload Nginx: " << e.what() << std::endl;
}
});
@ -49,7 +49,7 @@ void ControlServer::setup_routes() {
server_.Post("/certificate/:domain", [this](const httplib::Request& req, httplib::Response &res) {
// TODO do we need to sanitize?
std::string domain = req.get_param_value("domain");
std::string domain = req.path_params.at("domain");
if (!req.form.has_file("certificate") || !req.form.has_file("private_key")) {
response_client_error(res, "Missing certificate and/or private_key fields", httplib::StatusCode::BadRequest_400);
@ -70,7 +70,7 @@ void ControlServer::setup_routes() {
certificate_manager_.write_certificate(domain, certificate_field.content);
} catch (const std::exception& e) {
response_server_error(res, e.what(), httplib::StatusCode::InternalServerError_500);
std::cout << "Failed to load certificates: " << e.what() << std::endl;
std::cerr << "Failed to save certificates: " << e.what() << std::endl;
}
});
}