Kea  1.9.9-git
redact_config.cc
Go to the documentation of this file.
1 // Copyright (C) 2021 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
10 
11 #include <boost/algorithm/string.hpp>
12 
13 using namespace isc;
14 using namespace isc::data;
15 using namespace std;
16 
17 namespace {
18 
19 template <typename ElementPtrType>
20 ElementPtrType
21 redact(ElementPtrType const& element, list<string> json_path) {
22  if (!element) {
23  isc_throw(BadValue, "redact() got a null pointer");
24  }
25 
26  string const next_key(json_path.empty() ? string() : json_path.front());
27  ElementPtr result;
28  if (element->getType() == Element::list) {
29  // If we are looking for a list...
30  if (next_key == "*" || next_key == "[]") {
31  // But if we are looking specifically for a list...
32  if (next_key == "[]") {
33  // Then advance in the path.
34  json_path.pop_front();
35  }
36  // Then redact all children.
37  result = Element::createList();
38  for (ElementPtr const& child : element->listValue()) {
39  result->add(redact(child, json_path));
40  }
41  return result;
42  }
43  } else if (element->getType() == Element::map) {
44  // If we are looking for anything or if we have reached the end of a
46  if (next_key == "*" || json_path.empty()) {
47  // Then iterate through all the children.
48  result = Element::createMap();
49  for (auto kv : element->mapValue()) {
50  std::string const& key(kv.first);
51  ConstElementPtr const& value(kv.second);
52 
53  if (boost::algorithm::ends_with(key, "password") ||
54  boost::algorithm::ends_with(key, "secret")) {
55  // Sensitive data
56  result->set(key, Element::create(string("*****")));
57  } else if (key == "user-context") {
58  // Skip user contexts.
59  result->set(key, value);
60  } else {
61  if (json_path.empty()) {
62  // End of path means no sensitive data expected in this
63  // subtree, so we stop here.
64  result->set(key, value);
65  } else {
66  // We are looking for anything '*' so redact further.
67  result->set(key, redact(value, json_path));
68  }
69  }
70  }
71  return result;
72  } else {
73  ConstElementPtr child(element->get(next_key));
74  if (child) {
75  result = isc::data::copy(element, 1);
76  json_path.pop_front();
77  result->set(next_key, redact(child, json_path));
78  return result;
79  }
80  }
81  }
82 
83  return element;
84 }
85 
86 } // namespace
87 
88 namespace isc {
89 namespace process {
90 
92 redactConfig(ConstElementPtr const& element, list<string> const& json_path) {
93  return redact(element, json_path);
94 }
95 
96 } // namespace process
97 } // namespace isc
ConstElementPtr redactConfig(ConstElementPtr const &element, list< string > const &json_path)
Redact a configuration.
boost::shared_ptr< Element > ElementPtr
Definition: data.h:20
STL namespace.
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition: data.cc:267
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition: data.cc:262
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
ElementPtr copy(ConstElementPtr from, int level)
Copy the data up to a nesting level.
Definition: data.cc:1097
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
Defines the logger used by the top-level component of kea-dhcp-ddns.
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:222