]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
ws-auth.cc: Split apiZoneMetadataKind to GET, PUT and DELETE variants
authorAki Tuomi <cmouse@cmouse.fi>
Fri, 18 Aug 2023 07:48:39 +0000 (10:48 +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.

docs/http-api/swagger/authoritative-api-swagger.yaml
pdns/ws-auth.cc
regression-tests.api/test_Zones.py

index e4f006b3c16d16b95450644c4526d06a2f1de482..c231b9d1fdd1b571adb54559a726b75ccdb44006 100644 (file)
@@ -625,7 +625,7 @@ paths:
           required: true
           description: The kind of metadata
       responses:
-        '200':
+        '204':
           description: OK
         <<: *commonErrors
 
index ed366050472e8ac3003bf29c1f5dcad611f1af82..fba63872b2351ad3e3467d287f1ab521ad008ded 100644 (file)
@@ -1060,68 +1060,96 @@ static void apiZoneMetadata(HttpRequest *req, HttpResponse* resp)
     throw HttpMethodNotAllowedException();
 }
 
-static void apiZoneMetadataKind(HttpRequest* req, HttpResponse* resp) {
+static void apiZoneMetadataKindGET(HttpRequest* req, HttpResponse* resp) {
   zoneFromId(req);
 
   string kind = req->parameters["kind"];
 
-  if (req->method == "GET") {
-    vector<string> metadata;
-    Json::object document;
-    Json::array entries;
+  vector<string> metadata;
+  Json::object document;
+  Json::array entries;
+
+  if (!B.getDomainMetadata(zonename, kind, metadata)) {
+    throw HttpNotFoundException();
+  }
+  if (!isValidMetadataKind(kind, true)) {
+    throw ApiException("Unsupported metadata kind '" + kind + "'");
+  }
 
-    if (!B.getDomainMetadata(zonename, kind, metadata))
-      throw HttpNotFoundException();
-    else if (!isValidMetadataKind(kind, true))
-      throw ApiException("Unsupported metadata kind '" + kind + "'");
+  document["type"] = "Metadata";
+  document["kind"] = kind;
 
-    document["type"] = "Metadata";
-    document["kind"] = kind;
+  for (const string& value : metadata) {
+    entries.push_back(value);
+  }
 
-    for (const string& i : metadata)
-      entries.push_back(i);
+  document["metadata"] = entries;
+  resp->setJsonBody(document);
+}
 
-    document["metadata"] = entries;
-    resp->setJsonBody(document);
-  } else if (req->method == "PUT") {
-    auto document = req->json();
+static void apiZoneMetadataKindPUT(HttpRequest* req, HttpResponse* resp) {
+  zoneFromId(req);
+
+  string kind = req->parameters["kind"];
 
-    if (!isValidMetadataKind(kind, false))
-      throw ApiException("Unsupported metadata kind '" + kind + "'");
+  const auto& document = req->json();
 
-    vector<string> vecMetadata;
-    auto& metadata = document["metadata"];
-    if (!metadata.is_array())
-      throw ApiException("metadata is not specified or not an array");
+  if (!isValidMetadataKind(kind, false)) {
+    throw ApiException("Unsupported metadata kind '" + kind + "'");
+  }
 
-    for (const auto& i : metadata.array_items()) {
-      if (!i.is_string())
-        throw ApiException("metadata must be strings");
-      vecMetadata.push_back(i.string_value());
+  vector<string> vecMetadata;
+  const auto& metadata = document["metadata"];
+  if (!metadata.is_array()) {
+    throw ApiException("metadata is not specified or not an array");
+  }
+  for (const auto& value : metadata.array_items()) {
+    if (!value.is_string()) {
+      throw ApiException("metadata must be strings");
     }
+    vecMetadata.push_back(value.string_value());
+  }
 
-    if (!B.setDomainMetadata(zonename, kind, vecMetadata))
-      throw ApiException("Could not update metadata entries for domain '" + zonename.toString() + "'");
+  if (!B.setDomainMetadata(zonename, kind, vecMetadata)) {
+    throw ApiException("Could not update metadata entries for domain '" + zonename.toString() + "'");
+  }
 
-    DNSSECKeeper::clearMetaCache(zonename);
+  DNSSECKeeper::clearMetaCache(zonename);
 
-    Json::object key {
-      { "type", "Metadata" },
-      { "kind", kind },
-      { "metadata", metadata }
-    };
+  Json::object key {
+    { "type", "Metadata" },
+    { "kind", kind },
+    { "metadata", metadata }
+  };
 
-    resp->setJsonBody(key);
-  } else if (req->method == "DELETE") {
-    if (!isValidMetadataKind(kind, false))
-      throw ApiException("Unsupported metadata kind '" + kind + "'");
+  resp->setJsonBody(key);
+}
 
-    vector<string> md;  // an empty vector will do it
-    if (!B.setDomainMetadata(zonename, kind, md))
-      throw ApiException("Could not delete metadata for domain '" + zonename.toString() + "' (" + kind + ")");
+static void apiZoneMetadataKindDELETE(HttpRequest* req, HttpResponse* resp) {
+  zoneFromId(req);
+
+  const string& kind = req->parameters["kind"];
+  if (!isValidMetadataKind(kind, false)) {
+    throw ApiException("Unsupported metadata kind '" + kind + "'");
+  }
+
+  vector<string> metadata;  // an empty vector will do it
+  if (!B.setDomainMetadata(zonename, kind, metadata)) {
+    throw ApiException("Could not delete metadata for domain '" + zonename.toString() + "' (" + kind + ")");
+  }
 
-    DNSSECKeeper::clearMetaCache(zonename);
-  } else
+  DNSSECKeeper::clearMetaCache(zonename);
+  resp->status = 204;
+}
+
+static void apiZoneMetadataKind(HttpRequest* req, HttpResponse* resp) {
+  if (req->method == "GET")
+    apiZoneMetadataKindGET(req, resp);
+  else if (req->method == "PUT")
+    apiZoneMetadataKindPUT(req, resp);
+  else if (req->method == "DELETE")
+    apiZoneMetadataKindDELETE(req, resp);
+  else
     throw HttpMethodNotAllowedException();
 }
 
index 6bd02bfc872a4043467165c495da88496aef9675..45ec5cee95bb76915e80b656cac10aeb93ca3843 100644 (file)
@@ -729,7 +729,7 @@ class AuthZones(ApiTestCase, AuthZonesHelperMixin):
 
     def test_delete_zone_metadata(self):
         r = self.session.delete(self.url("/api/v1/servers/localhost/zones/example.com/metadata/AXFR-SOURCE"))
-        self.assertEqual(r.status_code, 200)
+        self.assertEqual(r.status_code, 204)
         r = self.session.get(self.url("/api/v1/servers/localhost/zones/example.com/metadata/AXFR-SOURCE"))
         rdata = r.json()
         self.assertEqual(r.status_code, 200)
@@ -2710,4 +2710,4 @@ class AuthZoneKeys(ApiTestCase, AuthZonesHelperMixin):
         self.assertEqual(len(keydata), 4)
 
         r = self.session.delete(self.url("/api/v1/servers/localhost/zones/powerdnssec.org./metadata/PUBLISH-CDS"))
-        self.assertEqual(r.status_code, 200)
+        self.assertEqual(r.status_code, 204)