• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PowerDNS / pdns / 20618548088

31 Dec 2025 12:00PM UTC coverage: 72.648% (-0.7%) from 73.336%
20618548088

Pull #16693

github

web-flow
Merge 3f7d9a75b into 65de281db
Pull Request #16693: auth: plumbing for structured logging

39009 of 65430 branches covered (59.62%)

Branch coverage included in aggregate %.

807 of 2400 new or added lines in 58 files covered. (33.63%)

200 existing lines in 39 files now uncovered.

129187 of 166092 relevant lines covered (77.78%)

5266744.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

43.09
/modules/remotebackend/httpconnector.cc
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of version 2 of the GNU General Public License as
7
 * published by the Free Software Foundation.
8
 *
9
 * In addition, for the avoidance of any doubt, permission is granted to
10
 * link this program with OpenSSL and to (re)distribute the binaries
11
 * produced as the result of such linking.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
 */
22
#ifdef HAVE_CONFIG_H
23
#include "config.h"
24
#endif
25
#include "remotebackend.hh"
26
#include <sys/socket.h>
27
#include <unistd.h>
28
#include <fcntl.h>
29

30
#include <sstream>
31
#include "pdns/lock.hh"
32

33
#ifndef UNIX_PATH_MAX
34
#define UNIX_PATH_MAX 108
35
#endif
36

37
HTTPConnector::HTTPConnector(std::shared_ptr<Logr::Logger> log, std::map<std::string, std::string> options) :
38
  d_socket(nullptr)
39
{
23✔
40
  d_slog = log;
23✔
41

42
  if (options.find("url") == options.end()) {
23!
43
    throw PDNSException("Cannot find 'url' option in the remote backend HTTP connector's parameters");
×
44
  }
×
45

46
  this->d_url = options.find("url")->second;
23✔
47

48
  try {
23✔
49
    YaHTTP::URL url(d_url);
23✔
50
    d_host = url.host;
23✔
51
    d_port = url.port;
23✔
52
  }
23✔
53
  catch (const std::exception& e) {
23✔
54
    throw PDNSException("Error parsing the 'url' option provided to the remote backend HTTP connector: " + std::string(e.what()));
×
55
  }
×
56

57
  if (options.find("url-suffix") != options.end()) {
23!
58
    this->d_url_suffix = options.find("url-suffix")->second;
×
59
  }
×
60
  else {
23✔
61
    this->d_url_suffix = "";
23✔
62
  }
23✔
63
  this->timeout = 2;
23✔
64
  this->d_post = false;
23✔
65
  this->d_post_json = false;
23✔
66

67
  if (options.find("timeout") != options.end()) {
23✔
68
    this->timeout = std::stoi(options.find("timeout")->second) / 1000;
12✔
69
  }
12✔
70
  if (options.find("post") != options.end()) {
23!
71
    std::string val = options.find("post")->second;
×
72
    if (val == "yes" || val == "true" || val == "on" || val == "1") {
×
73
      this->d_post = true;
×
74
    }
×
75
  }
×
76
  if (options.find("post_json") != options.end()) {
23!
77
    std::string val = options.find("post_json")->second;
×
78
    if (val == "yes" || val == "true" || val == "on" || val == "1") {
×
79
      this->d_post_json = true;
×
80
    }
×
81
  }
×
82
}
23✔
83

84
HTTPConnector::~HTTPConnector() = default;
18✔
85

86
void HTTPConnector::addUrlComponent(const Json& parameters, const string& element, std::stringstream& ss)
87
{
1,009✔
88
  std::string sparam;
1,009✔
89
  if (parameters[element] != Json()) {
1,009✔
90
    ss << "/" << YaHTTP::Utility::encodeURL(asString(parameters[element]), false);
257✔
91
  }
257✔
92
}
1,009✔
93

94
std::string HTTPConnector::buildMemberListArgs(const std::string& prefix, const Json& args)
95
{
×
96
  std::stringstream stream;
×
97

98
  for (const auto& pair : args.object_items()) {
×
99
    stream << prefix << "[" << YaHTTP::Utility::encodeURL(pair.first, false) << "]=";
×
100
    if (pair.second.is_bool()) {
×
101
      stream << (pair.second.bool_value() ? "1" : "0");
×
102
    }
×
103
    else if (!pair.second.is_null()) {
×
104
      stream << YaHTTP::Utility::encodeURL(HTTPConnector::asString(pair.second), false);
×
105
    }
×
106
    stream << "&";
×
107
  }
×
108

109
  return stream.str().substr(0, stream.str().size() - 1); // snip the trailing &
×
110
}
×
111

112
// builds our request (near-restful)
113
void HTTPConnector::restful_requestbuilder(const std::string& method, const Json& parameters, YaHTTP::Request& req)
114
{
143✔
115
  std::stringstream ss;
143✔
116
  std::string sparam;
143✔
117
  std::string verb;
143✔
118

119
  // special names are qname, name, zonename, kind, others go to headers
120

121
  ss << d_url;
143✔
122

123
  ss << "/" << method;
143✔
124

125
  // add the url components, if found, in following order.
126
  // id must be first due to the fact that the qname/name can be empty
127

128
  addUrlComponent(parameters, "id", ss);
143✔
129
  addUrlComponent(parameters, "domain_id", ss);
143✔
130
  addUrlComponent(parameters, "zonename", ss);
143✔
131
  addUrlComponent(parameters, "qname", ss);
143✔
132
  addUrlComponent(parameters, "name", ss);
143✔
133
  addUrlComponent(parameters, "kind", ss);
143✔
134
  addUrlComponent(parameters, "qtype", ss);
143✔
135

136
  // set the correct type of request based on method
137
  if (method == "activateDomainKey" || method == "deactivateDomainKey" || method == "publishDomainKey" || method == "unpublishDomainKey") {
143!
138
    // create an empty post
139
    req.preparePost();
×
140
    verb = "POST";
×
141
  }
×
142
  else if (method == "setTSIGKey") {
143!
143
    req.POST()["algorithm"] = parameters["algorithm"].string_value();
×
144
    req.POST()["content"] = parameters["content"].string_value();
×
145
    req.preparePost();
×
146
    verb = "PATCH";
×
147
  }
×
148
  else if (method == "deleteTSIGKey") {
143!
149
    verb = "DELETE";
×
150
  }
×
151
  else if (method == "addDomainKey") {
143✔
152
    const Json& param = parameters["key"];
2✔
153
    req.POST()["flags"] = asString(param["flags"]);
2✔
154
    req.POST()["active"] = (param["active"].bool_value() ? "1" : "0");
2!
155
    req.POST()["published"] = (param["published"].bool_value() ? "1" : "0");
2!
156
    req.POST()["content"] = param["content"].string_value();
2✔
157
    req.preparePost();
2✔
158
    verb = "PUT";
2✔
159
  }
2✔
160
  else if (method == "superMasterBackend") {
141!
161
    std::stringstream ss2;
×
162
    addUrlComponent(parameters, "ip", ss);
×
163
    addUrlComponent(parameters, "domain", ss);
×
164
    // then we need to serialize rrset payload into POST
165
    for (size_t index = 0; index < parameters["nsset"].array_items().size(); index++) {
×
166
      ss2 << buildMemberListArgs("nsset[" + std::to_string(index) + "]", parameters["nsset"][index]) << "&";
×
167
    }
×
168
    req.body = ss2.str().substr(0, ss2.str().size() - 1);
×
169
    req.headers["content-type"] = "application/x-www-form-urlencoded; charset=utf-8";
×
170
    req.headers["content-length"] = std::to_string(req.body.size());
×
171
    verb = "POST";
×
172
  }
×
173
  else if (method == "createSlaveDomain") {
141!
174
    addUrlComponent(parameters, "ip", ss);
×
175
    addUrlComponent(parameters, "domain", ss);
×
176
    if (!parameters["account"].is_null() && parameters["account"].is_string()) {
×
177
      req.POST()["account"] = parameters["account"].string_value();
×
178
    }
×
179
    req.preparePost();
×
180
    verb = "PUT";
×
181
  }
×
182
  else if (method == "replaceRRSet") {
141!
183
    std::stringstream ss2;
×
184
    for (size_t index = 0; index < parameters["rrset"].array_items().size(); index++) {
×
185
      ss2 << buildMemberListArgs("rrset[" + std::to_string(index) + "]", parameters["rrset"][index]) << "&";
×
186
    }
×
187
    req.body = ss2.str().substr(0, ss2.str().size() - 1); // remove trailing &
×
188
    req.headers["content-type"] = "application/x-www-form-urlencoded; charset=utf-8";
×
189
    req.headers["content-length"] = std::to_string(req.body.size());
×
190
    verb = "PATCH";
×
191
  }
×
192
  else if (method == "feedRecord") {
141!
193
    addUrlComponent(parameters, "trxid", ss);
×
194
    req.body = buildMemberListArgs("rr", parameters["rr"]);
×
195
    req.headers["content-type"] = "application/x-www-form-urlencoded; charset=utf-8";
×
196
    req.headers["content-length"] = std::to_string(req.body.size());
×
197
    verb = "PATCH";
×
198
  }
×
199
  else if (method == "feedEnts") {
141!
200
    std::stringstream ss2;
×
201
    addUrlComponent(parameters, "trxid", ss);
×
202
    for (const auto& param : parameters["nonterm"].array_items()) {
×
203
      ss2 << "nonterm[]=" << YaHTTP::Utility::encodeURL(param.string_value(), false) << "&";
×
204
    }
×
205
    for (const auto& param : parameters["auth"].array_items()) {
×
206
      ss2 << "auth[]=" << (param["auth"].bool_value() ? "1" : "0") << "&";
×
207
    }
×
208
    req.body = ss2.str().substr(0, ss2.str().size() - 1);
×
209
    req.headers["content-type"] = "application/x-www-form-urlencoded; charset=utf-8";
×
210
    req.headers["content-length"] = std::to_string(req.body.size());
×
211
    verb = "PATCH";
×
212
  }
×
213
  else if (method == "feedEnts3") {
141!
214
    std::stringstream ss2;
×
215
    addUrlComponent(parameters, "domain", ss);
×
216
    addUrlComponent(parameters, "trxid", ss);
×
217
    ss2 << "times=" << parameters["times"].int_value() << "&salt=" << YaHTTP::Utility::encodeURL(parameters["salt"].string_value(), false) << "&narrow=" << (parameters["narrow"].bool_value() ? 1 : 0) << "&";
×
218
    for (const auto& param : parameters["nonterm"].array_items()) {
×
219
      ss2 << "nonterm[]=" << YaHTTP::Utility::encodeURL(param.string_value(), false) << "&";
×
220
    }
×
221
    for (const auto& param : parameters["auth"].array_items()) {
×
222
      ss2 << "auth[]=" << (param["auth"].bool_value() ? "1" : "0") << "&";
×
223
    }
×
224
    req.body = ss2.str().substr(0, ss2.str().size() - 1);
×
225
    req.headers["content-type"] = "application/x-www-form-urlencoded; charset=utf-8";
×
226
    req.headers["content-length"] = std::to_string(req.body.size());
×
227
    verb = "PATCH";
×
228
  }
×
229
  else if (method == "startTransaction") {
141✔
230
    addUrlComponent(parameters, "domain", ss);
4✔
231
    addUrlComponent(parameters, "trxid", ss);
4✔
232
    req.preparePost();
4✔
233
    verb = "POST";
4✔
234
  }
4✔
235
  else if (method == "commitTransaction" || method == "abortTransaction") {
137!
236
    addUrlComponent(parameters, "trxid", ss);
×
237
    req.preparePost();
×
238
    verb = "POST";
×
239
  }
×
240
  else if (method == "setDomainMetadata") {
137!
241
    // copy all metadata values into post
242
    std::stringstream ss2;
×
243
    // this one has values too
244
    if (parameters["value"].is_array()) {
×
245
      for (const auto& val : parameters["value"].array_items()) {
×
246
        ss2 << "value[]=" << YaHTTP::Utility::encodeURL(val.string_value(), false) << "&";
×
247
      }
×
248
    }
×
249
    req.body = ss2.str().substr(0, ss2.str().size() - 1);
×
250
    req.headers["content-type"] = "application/x-www-form-urlencoded; charset=utf-8";
×
251
    req.headers["content-length"] = std::to_string(req.body.size());
×
252
    verb = "PATCH";
×
253
  }
×
254
  else if (method == "removeDomainKey") {
137!
255
    // this one is delete
256
    verb = "DELETE";
×
257
  }
×
258
  else if (method == "setNotified") {
137!
259
    req.POST()["serial"] = asString(parameters["serial"]);
×
260
    req.preparePost();
×
261
    verb = "PATCH";
×
262
  }
×
263
  else if (method == "setStale") {
137!
264
    req.preparePost();
×
265
    verb = "PATCH";
×
266
  }
×
267
  else if (method == "setFresh") {
137!
268
    req.preparePost();
×
269
    verb = "PATCH";
×
270
  }
×
271
  else if (method == "directBackendCmd") {
137✔
272
    req.POST()["query"] = parameters["query"].string_value();
2✔
273
    req.preparePost();
2✔
274
    verb = "POST";
2✔
275
  }
2✔
276
  else if (method == "searchRecords" || method == "searchComments") {
135!
277
    req.GET()["pattern"] = parameters["pattern"].string_value();
×
278
    req.GET()["maxResults"] = std::to_string(parameters["maxResults"].int_value());
×
279
    verb = "GET";
×
280
  }
×
281
  else if (method == "getAllDomains") {
135!
282
    req.GET()["includeDisabled"] = (parameters["include_disabled"].bool_value() ? "true" : "false");
×
283
    verb = "GET";
×
284
  }
×
285
  else {
135✔
286
    // perform normal get
287
    verb = "GET";
135✔
288
  }
135✔
289

290
  // put everything else into headers
291
  for (const auto& pair : parameters.object_items()) {
413✔
292
    std::string member = pair.first;
413✔
293
    // whitelist header parameters
294
    if ((member == "trxid" || member == "local" || member == "remote" || member == "real-remote" || member == "zone-id")) {
413✔
295
      std::string hdr = "x-remotebackend-" + member;
152✔
296
      req.headers[hdr] = asString(pair.second);
152✔
297
    }
152✔
298
  };
413✔
299

300
  // finally add suffix and store url
301
  ss << d_url_suffix;
143✔
302

303
  req.setup(verb, ss.str());
143✔
304
  req.headers["accept"] = "application/json";
143✔
305
}
143✔
306

307
void HTTPConnector::post_requestbuilder(const Json& input, YaHTTP::Request& req)
308
{
×
309
  if (this->d_post_json) {
×
310
    std::string out = input.dump();
×
311
    req.setup("POST", d_url);
×
312
    // simple case, POST JSON into url. nothing fancy.
313
    req.headers["Content-Type"] = "text/javascript; charset=utf-8";
×
314
    req.headers["Content-Length"] = std::to_string(out.size());
×
315
    req.headers["accept"] = "application/json";
×
316
    req.body = out;
×
317
  }
×
318
  else {
×
319
    std::stringstream url;
×
320
    std::stringstream content;
×
321
    // call url/method.suffix
322
    url << d_url << "/" << input["method"].string_value() << d_url_suffix;
×
323
    req.setup("POST", url.str());
×
324
    // then build content
325
    req.POST()["parameters"] = input["parameters"].dump();
×
326
    req.preparePost();
×
327
    req.headers["accept"] = "application/json";
×
328
  }
×
329
}
×
330

331
int HTTPConnector::send_message(const Json& input)
332
{
143✔
333
  int rv = 0;
143✔
334
  int ec = 0;
143✔
335
  int fd = 0;
143✔
336

337
  std::vector<std::string> members;
143✔
338
  std::string method;
143✔
339
  std::ostringstream out;
143✔
340

341
  // perform request
342
  YaHTTP::Request req;
143✔
343

344
  if (d_post) {
143!
345
    post_requestbuilder(input, req);
×
346
  }
×
347
  else {
143✔
348
    restful_requestbuilder(input["method"].string_value(), input["parameters"], req);
143✔
349
  }
143✔
350

351
  rv = -1;
143✔
352
  req.headers["connection"] = "Keep-Alive"; // see if we can streamline requests (not needed, strictly speaking)
143✔
353

354
  out << req;
143✔
355

356
  // try sending with current socket, if it fails retry with new socket
357
  if (this->d_socket != nullptr) {
143✔
358
    fd = this->d_socket->getHandle();
125✔
359
    // there should be no data waiting
360
    if (waitForRWData(fd, true, 0, 1) < 1) {
125!
361
      try {
×
362
        d_socket->writenWithTimeout(out.str().c_str(), out.str().size(), timeout);
×
363
        rv = 1;
×
364
      }
×
365
      catch (NetworkError& ne) {
×
NEW
366
        SLOG(g_log << Logger::Error << "While writing to HTTP endpoint " << d_addr.toStringWithPort() << ": " << ne.what() << std::endl,
×
NEW
367
             d_slog->error(Logr::Error, ne.what(), "network error writing to HTTP endpoint", "endpoint", Logging::Loggable(d_addr.toStringWithPort())));
×
368
      }
×
369
      catch (...) {
×
NEW
370
        SLOG(g_log << Logger::Error << "While writing to HTTP endpoint " << d_addr.toStringWithPort() << ": exception caught" << std::endl,
×
NEW
371
             d_slog->info(Logr::Error, "exception caught while writing to HTTP endpoint", "endpoint", Logging::Loggable(d_addr.toStringWithPort())));
×
372
      }
×
373
    }
×
374
  }
125✔
375

376
  if (rv == 1) {
143!
377
    return rv;
×
378
  }
×
379

380
  this->d_socket.reset();
143✔
381

382
  // connect using tcp
383
  struct addrinfo* gAddr = nullptr;
143✔
384
  struct addrinfo* gAddrPtr = nullptr;
143✔
385
  struct addrinfo hints{};
143✔
386
  std::string sPort = std::to_string(d_port);
143✔
387
  memset(&hints, 0, sizeof hints);
143✔
388
  hints.ai_family = AF_UNSPEC;
143✔
389
  hints.ai_flags = AI_ADDRCONFIG;
143✔
390
  hints.ai_socktype = SOCK_STREAM;
143✔
391
  hints.ai_protocol = IPPROTO_TCP;
143✔
392
  if ((ec = getaddrinfo(d_host.c_str(), sPort.c_str(), &hints, &gAddr)) == 0) {
143!
393
    // try to connect to each address.
394
    gAddrPtr = gAddr;
143✔
395

396
    while (gAddrPtr != nullptr) {
143!
397
      try {
143✔
398
        d_socket = std::make_unique<Socket>(gAddrPtr->ai_family, gAddrPtr->ai_socktype, gAddrPtr->ai_protocol);
143✔
399
        d_addr.setSockaddr(gAddrPtr->ai_addr, gAddrPtr->ai_addrlen);
143✔
400
        d_socket->connect(d_addr);
143✔
401
        d_socket->setNonBlocking();
143✔
402
        d_socket->writenWithTimeout(out.str().c_str(), out.str().size(), timeout);
143✔
403
        rv = 1;
143✔
404
      }
143✔
405
      catch (NetworkError& ne) {
143✔
NEW
406
        SLOG(g_log << Logger::Error << "While writing to HTTP endpoint " << d_addr.toStringWithPort() << ": " << ne.what() << std::endl,
×
NEW
407
             d_slog->error(Logr::Error, ne.what(), "network error writing to HTTP endpoint", "endpoint", Logging::Loggable(d_addr.toStringWithPort())));
×
UNCOV
408
      }
×
409
      catch (...) {
143✔
NEW
410
        SLOG(g_log << Logger::Error << "While writing to HTTP endpoint " << d_addr.toStringWithPort() << ": exception caught" << std::endl,
×
NEW
411
             d_slog->info(Logr::Error, "exception caught while writing to HTTP endpoint", "endpoint", Logging::Loggable(d_addr.toStringWithPort())));
×
UNCOV
412
      }
×
413

414
      if (rv > -1) {
143!
415
        break;
143✔
416
      }
143✔
417
      d_socket.reset();
×
418
      gAddrPtr = gAddrPtr->ai_next;
×
419
    }
×
420
    freeaddrinfo(gAddr);
143✔
421
  }
143✔
422
  else {
×
NEW
423
    SLOG(g_log << Logger::Error << "Unable to resolve " << d_host << ": " << gai_strerror(ec) << std::endl,
×
NEW
424
         d_slog->error(Logr::Error, gai_strerror(ec), "unable to resolve hostname", "host", Logging::Loggable(d_host)));
×
UNCOV
425
  }
×
426

427
  return rv;
143✔
428
}
143✔
429

430
int HTTPConnector::recv_message(Json& output)
431
{
143✔
432
  YaHTTP::AsyncResponseLoader arl;
143✔
433
  YaHTTP::Response resp;
143✔
434

435
  if (d_socket == nullptr) {
143!
436
    return -1; // cannot receive :(
×
437
  }
×
438
  std::array<char, 4096> buffer{};
143✔
439
  time_t time0 = 0;
143✔
440

441
  arl.initialize(&resp);
143✔
442

443
  try {
143✔
444
    time0 = time(nullptr);
143✔
445
    while (!arl.ready() && (labs(time(nullptr) - time0) <= timeout)) {
417!
446
      auto readBytes = d_socket->readWithTimeout(buffer.data(), buffer.size(), timeout);
274✔
447
      if (readBytes == 0) {
274!
448
        throw NetworkError("EOF while reading");
×
449
      }
×
450
      arl.feed(std::string(buffer.data(), readBytes));
274✔
451
    }
274✔
452
    // timeout occurred.
453
    if (!arl.ready()) {
143!
454
      throw NetworkError("timeout");
×
455
    }
×
456
  }
143✔
457
  catch (NetworkError& ne) {
143✔
458
    d_socket.reset();
×
459
    throw PDNSException("While reading from HTTP endpoint " + d_addr.toStringWithPort() + ": " + ne.what());
×
460
  }
×
461
  catch (...) {
143✔
462
    d_socket.reset();
×
463
    throw PDNSException("While reading from HTTP endpoint " + d_addr.toStringWithPort() + ": unknown error");
×
464
  }
×
465

466
  arl.finalize();
143✔
467

468
  if ((resp.status < 200 || resp.status >= 400) && resp.status != 404) {
143!
469
    // bad.
470
    throw PDNSException("Received unacceptable HTTP status code " + std::to_string(resp.status) + " from HTTP endpoint " + d_addr.toStringWithPort());
×
471
  }
×
472

473
  std::string err;
143✔
474
  output = Json::parse(resp.body, err);
143✔
475
  if (output != nullptr) {
143!
476
    return static_cast<int>(resp.body.size());
143✔
477
  }
143✔
NEW
478
  SLOG(g_log << Logger::Error << "Cannot parse JSON reply: " << err << endl,
×
NEW
479
       d_slog->error(Logr::Error, err, "cannot parse JSON reply"));
×
480

481
  return -1;
×
482
}
143✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc