From: Aki Tuomi Date: Fri, 18 Aug 2023 07:59:54 +0000 (+0300) Subject: ws-auth.cc: Split apiServerTSIGKeyDetail to GET, PUT and DELETE variant X-Git-Tag: auth-4.9.0-alpha1~42^2~19 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ca740e49bd57b7036d64ded7582650445ffcd46e;p=thirdparty%2Fpdns.git ws-auth.cc: Split apiServerTSIGKeyDetail to GET, PUT and DELETE variant Enables us to specify method routes for this later. --- diff --git a/pdns/ws-auth.cc b/pdns/ws-auth.cc index bf359580ed..75c240679b 100644 --- a/pdns/ws-auth.cc +++ b/pdns/ws-auth.cc @@ -1635,67 +1635,85 @@ static void apiServerTSIGKeys(HttpRequest* req, HttpResponse* resp) { HttpMethodNotAllowedException(); } -static void apiServerTSIGKeyDetail(HttpRequest* req, HttpResponse* resp) { - UeberBackend B; - DNSName keyname = apiZoneIdToName(req->parameters["id"]); - DNSName algo; - string content; +// NOLINTBEGIN(cppcoreguidelines-macro-usage, readability-identifier-length) +#define TSIGKeyFromId(req) \ + UeberBackend B; \ + DNSName keyname = apiZoneIdToName((req)->parameters["id"]); \ + DNSName algo; \ + string content; \ + try { \ + if (!B.getTSIGKey(keyname, algo, content)) { \ + throw HttpNotFoundException("TSIG key with name '"+keyname.toLogString()+"' not found"); \ + } \ + } catch(const PDNSException &e) { \ + throw HttpInternalServerErrorException("Could not retrieve Domain Info: " + e.reason); \ + } \ + struct TSIGKey tsk; \ + tsk.name = keyname; \ + tsk.algorithm = algo; \ + tsk.key = std::move(content); +// NOLINTEND(cppcoreguidelines-macro-usage, readability-identifier-length) - if (!B.getTSIGKey(keyname, algo, content)) { - throw HttpNotFoundException("TSIG key with name '"+keyname.toLogString()+"' not found"); - } +static void apiServerTSIGKeyDetailGET(HttpRequest* req, HttpResponse* resp) { + TSIGKeyFromId(req); - struct TSIGKey tsk; - tsk.name = keyname; - tsk.algorithm = algo; - tsk.key = std::move(content); + resp->setJsonBody(makeJSONTSIGKey(tsk)); +} - if (req->method == "GET") { - resp->setJsonBody(makeJSONTSIGKey(tsk)); - } else if (req->method == "PUT") { - json11::Json document; - if (!req->body.empty()) { - document = req->json(); - } - if (document["name"].is_string()) { - tsk.name = DNSName(document["name"].string_value()); - } - if (document["algorithm"].is_string()) { - tsk.algorithm = DNSName(document["algorithm"].string_value()); +static void apiServerTSIGKeyDetailPUT(HttpRequest* req, HttpResponse* resp) { + TSIGKeyFromId(req); - TSIGHashEnum the; - if (!getTSIGHashEnum(tsk.algorithm, the)) { - throw ApiException("Unknown TSIG algorithm: " + tsk.algorithm.toLogString()); - } - } - if (document["key"].is_string()) { - string new_content = document["key"].string_value(); - string decoded; - if (B64Decode(new_content, decoded) == -1) { - throw ApiException("Can not base64 decode key content '" + new_content + "'"); - } - tsk.key = std::move(new_content); - } - if (!B.setTSIGKey(tsk.name, tsk.algorithm, tsk.key)) { - throw HttpInternalServerErrorException("Unable to save TSIG Key"); + const auto& document = req->json(); + + if (document["name"].is_string()) { + tsk.name = DNSName(document["name"].string_value()); + } + if (document["algorithm"].is_string()) { + tsk.algorithm = DNSName(document["algorithm"].string_value()); + + TSIGHashEnum the; // NOLINT(cppcoreguidelines-init-variables): Gets initialized on next line + if (!getTSIGHashEnum(tsk.algorithm, the)) { + throw ApiException("Unknown TSIG algorithm: " + tsk.algorithm.toLogString()); } - if (tsk.name != keyname) { - // Remove the old key - if (!B.deleteTSIGKey(keyname)) { - throw HttpInternalServerErrorException("Unable to remove TSIG key '" + keyname.toStringNoDot() + "'"); - } + } + if (document["key"].is_string()) { + string new_content = document["key"].string_value(); + string decoded; + if (B64Decode(new_content, decoded) == -1) { + throw ApiException("Can not base64 decode key content '" + new_content + "'"); } - resp->setJsonBody(makeJSONTSIGKey(tsk)); - } else if (req->method == "DELETE") { + tsk.key = std::move(new_content); + } + if (!B.setTSIGKey(tsk.name, tsk.algorithm, tsk.key)) { + throw HttpInternalServerErrorException("Unable to save TSIG Key"); + } + if (tsk.name != keyname) { + // Remove the old key if (!B.deleteTSIGKey(keyname)) { throw HttpInternalServerErrorException("Unable to remove TSIG key '" + keyname.toStringNoDot() + "'"); - } else { - resp->body = ""; - resp->status = 204; } - } else { - throw HttpMethodNotAllowedException(); } + resp->setJsonBody(makeJSONTSIGKey(tsk)); +} + +static void apiServerTSIGKeyDetailDELETE(HttpRequest* req, HttpResponse* resp) { + TSIGKeyFromId(req); + if (!B.deleteTSIGKey(keyname)) { + throw HttpInternalServerErrorException("Unable to remove TSIG key '" + keyname.toStringNoDot() + "'"); + } + resp->body = ""; + resp->status = 204; +} + +static void apiServerTSIGKeyDetail(HttpRequest* req, HttpResponse* resp) { + if (req->method == "GET") + apiServerTSIGKeyDetailGET(req, resp); + else if (req->method == "PUT") + apiServerTSIGKeyDetailPUT(req, resp); + else if (req->method == "DELETE") + apiServerTSIGKeyDetailDELETE(req, resp); + else + throw HttpMethodNotAllowedException(); } static void apiServerAutoprimaryDetail(HttpRequest* req, HttpResponse* resp) {