]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
ws-auth.cc: Split apiServerTSIGKeys to GET and POST variant
authorAki Tuomi <cmouse@cmouse.fi>
Fri, 18 Aug 2023 07:51:30 +0000 (10:51 +0300)
committerAki Tuomi <cmouse@cmouse.fi>
Fri, 15 Dec 2023 07:59:57 +0000 (09:59 +0200)
Enables us to specify method routes for this later.

pdns/ws-auth.cc

index fba63872b2351ad3e3467d287f1ab521ad008ded..bf359580edb04e2fc50cea4c28a6cab808905a17 100644 (file)
@@ -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<struct TSIGKey> 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<struct TSIGKey> 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) {