From: Aki Tuomi Date: Fri, 18 Aug 2023 07:51:30 +0000 (+0300) Subject: ws-auth.cc: Split apiServerTSIGKeys to GET and POST variant X-Git-Tag: auth-4.9.0-alpha1~42^2~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2775cadd55a7742c850ae32670bac863f258bd9b;p=thirdparty%2Fpdns.git ws-auth.cc: Split apiServerTSIGKeys to GET and POST variant Enables us to specify method routes for this later. --- diff --git a/pdns/ws-auth.cc b/pdns/ws-auth.cc index fba63872b2..bf359580ed 100644 --- a/pdns/ws-auth.cc +++ b/pdns/ws-auth.cc @@ -1584,47 +1584,55 @@ static Json::object makeJSONTSIGKey(const struct TSIGKey& key, bool doContent=tr return makeJSONTSIGKey(key.name, key.algorithm, doContent ? key.key : ""); } -static void apiServerTSIGKeys(HttpRequest* req, HttpResponse* resp) { - UeberBackend B; - if (req->method == "GET") { - vector keys; - - if (!B.getTSIGKeys(keys)) { - throw HttpInternalServerErrorException("Unable to retrieve TSIG keys"); - } +static void apiServerTSIGKeysGET(HttpRequest* /* req */, HttpResponse* resp) { + UeberBackend B; // NOLINT(readability-identifier-length) + vector keys; - Json::array doc; + if (!B.getTSIGKeys(keys)) { + throw HttpInternalServerErrorException("Unable to retrieve TSIG keys"); + } - for(const auto &key : keys) { - doc.push_back(makeJSONTSIGKey(key, false)); - } - resp->setJsonBody(doc); - } else if (req->method == "POST") { - auto document = req->json(); - DNSName keyname(stringFromJson(document, "name")); - DNSName algo(stringFromJson(document, "algorithm")); - string content = document["key"].string_value(); + Json::array doc; - if (content.empty()) { - try { - content = makeTSIGKey(algo); - } catch (const PDNSException& e) { - throw HttpBadRequestException(e.reason); - } - } + for(const auto &key : keys) { + doc.push_back(makeJSONTSIGKey(key, false)); + } + resp->setJsonBody(doc); +} - // Will throw an ApiException or HttpConflictException on error - checkTSIGKey(B, keyname, algo, content); +static void apiServerTSIGKeysPOST(HttpRequest* req, HttpResponse* resp) { + UeberBackend B; // NOLINT(readability-identifier-length) + const auto& document = req->json(); + DNSName keyname(stringFromJson(document, "name")); + DNSName algo(stringFromJson(document, "algorithm")); + string content = document["key"].string_value(); - if(!B.setTSIGKey(keyname, algo, content)) { - throw HttpInternalServerErrorException("Unable to add TSIG key"); + if (content.empty()) { + try { + content = makeTSIGKey(algo); + } catch (const PDNSException& exc) { + throw HttpBadRequestException(exc.reason); } + } - resp->status = 201; - resp->setJsonBody(makeJSONTSIGKey(keyname, algo, content)); - } else { - throw HttpMethodNotAllowedException(); + // Will throw an ApiException or HttpConflictException on error + checkTSIGKey(B, keyname, algo, content); + + if(!B.setTSIGKey(keyname, algo, content)) { + throw HttpInternalServerErrorException("Unable to add TSIG key"); } + + resp->status = 201; + resp->setJsonBody(makeJSONTSIGKey(keyname, algo, content)); +} + +static void apiServerTSIGKeys(HttpRequest* req, HttpResponse* resp) { + if (req->method == "GET") + apiServerTSIGKeysGET(req, resp); + else if (req->method == "POST") + apiServerTSIGKeysPOST(req, resp); + else + HttpMethodNotAllowedException(); } static void apiServerTSIGKeyDetail(HttpRequest* req, HttpResponse* resp) {