Fix certificate uploading
This commit is contained in:
parent
53b23a1563
commit
c43a11fdd2
2 changed files with 12 additions and 11 deletions
|
|
@ -4,13 +4,14 @@
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <system_error>
|
#include <system_error>
|
||||||
|
|
||||||
void write_to_file(std::filesystem::path path, const std::string& content) {
|
#include <iostream>
|
||||||
std::ofstream private_key_file;
|
|
||||||
private_key_file.exceptions(std::ios::failbit | std::ios::badbit);
|
|
||||||
|
|
||||||
// These lines will throw
|
void write_to_file(const std::filesystem::path& path, const std::string& content) {
|
||||||
private_key_file.open(path);
|
std::ofstream file;
|
||||||
private_key_file << content;
|
file.exceptions(std::ios::failbit | std::ios::badbit);
|
||||||
|
|
||||||
|
file.open(path);
|
||||||
|
file << content;
|
||||||
}
|
}
|
||||||
|
|
||||||
CertificateManager::CertificateManager(const std::filesystem::path& certificate_root_path)
|
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 {
|
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";
|
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 {
|
void CertificateManager::write_certificate(const std::string& domain, const std::string& private_key) const {
|
||||||
std::filesystem::path path = get_certificate_directory(domain) / "fullchain.pem";
|
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());
|
||||||
}
|
}
|
||||||
|
|
@ -38,7 +38,7 @@ void ControlServer::setup_routes() {
|
||||||
catch(const std::exception& e)
|
catch(const std::exception& e)
|
||||||
{
|
{
|
||||||
response_server_error(res, e.what(), httplib::StatusCode::InternalServerError_500);
|
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) {
|
server_.Post("/certificate/:domain", [this](const httplib::Request& req, httplib::Response &res) {
|
||||||
// TODO do we need to sanitize?
|
// 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")) {
|
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);
|
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);
|
certificate_manager_.write_certificate(domain, certificate_field.content);
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
response_server_error(res, e.what(), httplib::StatusCode::InternalServerError_500);
|
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;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue