]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
ws-auth.cc: Split apiServerTSIGKeyDetail to GET, PUT and DELETE variant
authorAki Tuomi <cmouse@cmouse.fi>
Fri, 18 Aug 2023 07:59:54 +0000 (10:59 +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 bf359580edb04e2fc50cea4c28a6cab808905a17..75c240679b397a0540c2be452f905b47fdbb3d6a 100644 (file)
@@ -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) {