]> granicus.if.org Git - pdns/commitdiff
Fix JSON API to return an object for /config/allow-from
authorChristian Hofstaedtler <christian@hofstaedtler.name>
Wed, 12 Feb 2014 15:32:18 +0000 (16:32 +0100)
committerChristian Hofstaedtler <christian@hofstaedtler.name>
Wed, 12 Feb 2014 15:32:18 +0000 (16:32 +0100)
Can't return arrays for stuff that isn't a collection.

pdns/ws-recursor.cc
regression-tests.api/test_RecursorConfig.py

index fc262d622a9ad62e653a1db1d11542cd4e6cc336..98109ca4a203a0df6870396dc179e2724dcbcc2b 100644 (file)
@@ -66,9 +66,14 @@ static void apiServerConfigAllowFrom(HttpRequest* req, HttpResponse* resp)
   if (req->method == "PUT") {
     Document document;
     req->json(document);
+    const Value &jlist = document["value"];
 
-    if (!document.IsArray()) {
-      throw ApiException("Supplied JSON must be an array");
+    if (!document.IsObject()) {
+      throw ApiException("Supplied JSON must be an object");
+    }
+
+    if (!jlist.IsArray()) {
+      throw ApiException("'value' must be an array");
     }
 
     ostringstream ss;
@@ -78,8 +83,8 @@ static void apiServerConfigAllowFrom(HttpRequest* req, HttpResponse* resp)
 
     // Clear allow-from, and provide a "parent" value
     ss << "allow-from=" << endl;
-    for (SizeType i = 0; i < document.Size(); ++i) {
-      ss << "allow-from+=" << document[i].GetString() << endl;
+    for (SizeType i = 0; i < jlist.Size(); ++i) {
+      ss << "allow-from+=" << jlist[i].GetString() << endl;
     }
 
     apiWriteConfigFile("allow-from", ss.str());
@@ -93,16 +98,22 @@ static void apiServerConfigAllowFrom(HttpRequest* req, HttpResponse* resp)
 
   // Return currently configured ACLs
   Document document;
-  document.SetArray();
+  document.SetObject();
+
+  Value jlist;
+  jlist.SetArray();
 
   vector<string> entries;
   t_allowFrom->toStringVector(&entries);
 
   BOOST_FOREACH(const string& entry, entries) {
     Value jentry(entry.c_str(), document.GetAllocator()); // copy
-    document.PushBack(jentry, document.GetAllocator());
+    jlist.PushBack(jentry, document.GetAllocator());
   }
 
+  document.AddMember("name", "allow-from", document.GetAllocator());
+  document.AddMember("value", jlist, document.GetAllocator());
+
   resp->setBody(document);
 }
 
index 7e10b66e8b63b0666091390da48db5d9595dcc4b..30ec071691e49f9c64bd156c0d3ad3efa53bdaf5 100644 (file)
@@ -12,11 +12,11 @@ class RecursorConfig(ApiTestCase):
         self.assertSuccessJson(r)
 
     def test_ConfigAllowFromReplace(self):
-        payload = ["127.0.0.1"]
+        payload = {'value': ["127.0.0.1"]}
         r = self.session.put(
             self.url("/servers/localhost/config/allow-from"),
             data=json.dumps(payload),
             headers={'content-type': 'application/json'})
         self.assertSuccessJson(r)
         data = r.json()
-        self.assertEquals("127.0.0.1/32", data[0])
+        self.assertEquals("127.0.0.1/32", data["value"][0])