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

PowerDNS / pdns / 9418798096

07 Jun 2024 02:38PM UTC coverage: 64.612% (+0.002%) from 64.61%
9418798096

Pull #14057

github

web-flow
Merge 3ef28144e into b1d2ec1c5
Pull Request #14057: Auth: fix a crash and some cleanup in the auth-catalogzone.cc

37074 of 88162 branches covered (42.05%)

Branch coverage included in aggregate %.

23 of 50 new or added lines in 4 files covered. (46.0%)

45 existing lines in 14 files now uncovered.

124373 of 161708 relevant lines covered (76.91%)

5379791.12 hits per line

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

81.38
/pdns/auth-catalogzone.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

23
#ifdef HAVE_CONFIG_H
24
#include "config.h"
25
#endif
26

27
#include "dnsbackend.hh"
28
#include "json.hh"
29

30
bool CatalogInfo::parseJson(const std::string& json, CatalogType type)
31
{
756✔
32
  if (type == CatalogType::None) {
756!
UNCOV
33
    throw std::runtime_error("CatalogType is set to None");
×
34
  }
×
35

36
  d_type = type;
756✔
37

38
  if (json.empty()) {
756✔
39
    d_doc = nullptr;
608✔
40
    return false;
608✔
41
  }
608✔
42

43
  std::string err;
148✔
44
  d_doc = json11::Json::parse(json, err);
148✔
45
  if (d_doc.is_null()) {
148!
NEW
46
    throw std::runtime_error("Parsing of JSON options failed: " + err);
×
NEW
47
  }
×
48

49
  return !d_doc[getTypeString(d_type)].is_null();
148✔
50
}
148✔
51

52
void CatalogInfo::fromJson(const std::string& json, CatalogType type)
53
{
756✔
54
  if (parseJson(json, type)) {
756✔
55
    auto items = d_doc[getTypeString(type)].object_items();
148✔
56

57
    // coo property
58
    if (!items["coo"].is_null()) {
148✔
59
      d_coo = DNSName(stringFromJson(items, "coo"));
88✔
60
    }
88✔
61

62
    // unique property
63
    if (!items["unique"].is_null()) {
148✔
64
      d_unique = DNSName(stringFromJson(items, "unique"));
84✔
65
      if (d_unique.countLabels() != 1) {
84!
NEW
66
        throw std::out_of_range("Invalid number of labels in unique value");
×
UNCOV
67
      }
×
68
    }
84✔
69

70
    // group properties
71
    if (!items["group"].is_null()) {
148✔
72
      if (!items["group"].is_array()) {
36!
NEW
73
        throw std::out_of_range("Key 'group' is not an array");
×
UNCOV
74
      }
×
75
      for (const auto& value : items["group"].array_items()) {
72✔
76
        d_group.insert(value.string_value());
72✔
77
      }
72✔
78
    }
36✔
79
  }
148✔
80
}
756✔
81

82
std::string CatalogInfo::toJson() const
83
{
272✔
84
  if (d_type == CatalogType::None) {
272!
85
    throw std::runtime_error("CatalogType is set to None");
×
86
  }
×
87
  json11::Json::object object;
272✔
88

89
  // coo property
90
  if (!d_coo.empty()) {
272✔
91
    object["coo"] = d_coo.toString();
68✔
92
  }
68✔
93

94
  // unique property
95
  if (!d_unique.empty()) {
272✔
96
    if (d_unique.countLabels() != 1) {
232!
NEW
97
      throw std::out_of_range("Invalid number of labels in unique value");
×
UNCOV
98
    }
×
99
    object["unique"] = d_unique.toString();
232✔
100
  }
232✔
101

102
  // group properties
103
  if (!d_group.empty()) {
272✔
104
    json11::Json::array entries;
24✔
105
    for (const auto& group : d_group) {
48✔
106
      entries.push_back(group);
48✔
107
    }
48✔
108
    object["group"] = entries;
24✔
109
  }
24✔
110

111
  auto tmp = d_doc.object_items();
272✔
112
  tmp[getTypeString(d_type)] = object;
272✔
113
  const json11::Json ret = tmp;
272✔
114
  return ret.dump();
272✔
115
}
272✔
116

117
void CatalogInfo::updateCatalogHash(CatalogHashMap& hashes, const DomainInfo& di)
118
{
×
NEW
119
  CatalogInfo ci;
×
NEW
120
  hashes[di.catalog].process(std::to_string(di.id) + di.zone.toLogString());
×
NEW
121
  if (ci.parseJson(di.options, CatalogType::Producer)) {
×
NEW
122
    hashes[di.catalog].process(ci.d_doc["producer"].dump());
×
123
  }
×
124
}
×
125

126
DNSZoneRecord CatalogInfo::getCatalogVersionRecord(const DNSName& zone)
127
{
36✔
128
  DNSZoneRecord dzr;
36✔
129
  dzr.dr.d_name = DNSName("version") + zone;
36✔
130
  dzr.dr.d_type = QType::TXT;
36✔
131
  dzr.dr.setContent(std::make_shared<TXTRecordContent>("2"));
36✔
132
  return dzr;
36✔
133
}
36✔
134

135
void CatalogInfo::toDNSZoneRecords(const DNSName& zone, vector<DNSZoneRecord>& dzrs) const
136
{
600✔
137
  DNSName prefix;
600✔
138
  if (d_unique.empty()) {
600✔
139
    prefix = getUnique();
564✔
140
  }
564✔
141
  else {
36✔
142
    prefix = d_unique;
36✔
143
  }
36✔
144
  prefix += DNSName("zones") + zone;
600✔
145

146
  // member zone
147
  DNSZoneRecord dzr;
600✔
148
  dzr.dr.d_name = prefix;
600✔
149
  dzr.dr.d_type = QType::PTR;
600✔
150
  dzr.dr.setContent(std::make_shared<PTRRecordContent>(d_zone.toString()));
600✔
151
  dzrs.emplace_back(dzr);
600✔
152

153
  // coo property
154
  if (!d_coo.empty()) {
600✔
155
    dzr.dr.d_name = DNSName("coo") + prefix;
36✔
156
    dzr.dr.d_type = QType::PTR;
36✔
157
    dzr.dr.setContent(std::make_shared<PTRRecordContent>(d_coo));
36✔
158
    dzrs.emplace_back(dzr);
36✔
159
  }
36✔
160

161
  // group properties
162
  for (const auto& group : d_group) {
600✔
163
    dzr.dr.d_name = DNSName("group") + prefix;
72✔
164
    dzr.dr.d_type = QType::TXT;
72✔
165
    dzr.dr.setContent(std::make_shared<TXTRecordContent>("\"" + group + "\""));
72✔
166
    dzrs.emplace_back(dzr);
72✔
167
  }
72✔
168
}
600✔
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

© 2025 Coveralls, Inc