Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Direct backend command for Remote backend #21

Merged
merged 3 commits into from
Jun 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions docs/markdown/authoritative/backend-remote.md
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,42 @@ Content-Type: text/javascript; charset=utf-8
{"result":2013060501}
```

### `directBackendCmd`
Can be used to send arbitrary commands to your backend using (backend-cmd)(dnssec.md#pdnssec).

* Mandatory: no
* Parameters: query
* Reply: anything but boolean false for success, false for failure

#### Example JSON/RPC
Query:
```
{"method":"directBackendCmd","parameters":{"query":"PING"}}
```

Response:
```
{"result":"PONG"}
```

#### Example HTTP/RPC
Query:
```
POST /dnsapi/directBackendCmd
Content-Type: application/x-www-form-urlencoded
Content-Length: 10

query=PING
```

Response:
```
HTTP/1.1 200 OK
Content-Type: text/javascript; charset=utf-8

{"result":"PONG"}
```

# Examples
## Scenario: SOA lookup via pipe or unix connector
Query:
Expand Down
5 changes: 5 additions & 0 deletions modules/remotebackend/httpconnector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ void HTTPConnector::restful_requestbuilder(const std::string &method, const rapi
req.POST()["serial"] = sparam;
req.preparePost();
verb = "PATCH";
} else if (method == "directBackendCmd") {
json2string(parameters["query"],sparam);
req.POST()["query"] = sparam;
req.preparePost();
verb = "POST";
} else {
// perform normal get
verb = "GET";
Expand Down
4 changes: 4 additions & 0 deletions modules/remotebackend/regression-tests/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,8 @@ def do_setdomainmetadata(args)
end
return true
end

def do_directbackendcmd(args)
return [args["query"]]
end
end
3 changes: 3 additions & 0 deletions modules/remotebackend/regression-tests/direct-command/command
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

../pdns/pdnssec --config-name=remote --config-dir=. backend-cmd remote HELLO
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tests that direct backend command works
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
== HELLO
HELLO
16 changes: 16 additions & 0 deletions modules/remotebackend/remotebackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,22 @@ bool RemoteBackend::calculateSOASerial(const string& domain, const SOAData& sd,
return true;
}

string RemoteBackend::directBackendCmd(const string& querystr) {
rapidjson::Document query,answer;
rapidjson::Value parameters;

query.SetObject();
JSON_ADD_MEMBER(query, "method", "directBackendCmd", query.GetAllocator());
parameters.SetObject();
JSON_ADD_MEMBER(parameters, "query", querystr.c_str(), query.GetAllocator());
query.AddMember("parameters", parameters, query.GetAllocator());

if (this->send(query) == false || this->recv(answer) == false)
return "backend command failed";

return getString(answer["result"]);
}

// some rapidjson helpers
bool RemoteBackend::getBool(rapidjson::Value &value) {
if (value.IsNull()) return false;
Expand Down
1 change: 1 addition & 0 deletions modules/remotebackend/remotebackend.hh
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class RemoteBackend : public DNSBackend
virtual bool setTSIGKey(const string& name, const string& algorithm, const string& content);
virtual bool deleteTSIGKey(const string& name);
virtual bool getTSIGKeys(std::vector< struct TSIGKey > &keys);
virtual string directBackendCmd(const string& querystr);

static DNSBackend *maker();

Expand Down
5 changes: 5 additions & 0 deletions modules/remotebackend/test-remotebackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,9 @@ BOOST_AUTO_TEST_CASE(test_method_calculateSOASerial) {
BOOST_CHECK_EQUAL(serial, 2013060300);
}

BOOST_AUTO_TEST_CASE(test_method_directBackendCmd) {
BOOST_TEST_MESSAGE("Testing directBackendCmd method");
BOOST_CHECK_EQUAL(be->directBackendCmd("PING 1234"), "PING 1234");
}

BOOST_AUTO_TEST_SUITE_END();
4 changes: 4 additions & 0 deletions modules/remotebackend/unittest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,9 @@ def do_calculatesoaserial(args)
return [2013060300] if args["sd"]["qname"] == "unit.test"
[false]
end

def do_directbackendcmd(args)
[args["query"]]
end
end