| Kea 3.0.3-git

redact_config.cc
Go to the documentation of this file.
1// Copyright (C) 2021-2026 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
9#include <process/redact_config.h>
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>
20ElementPtrType
21 redact(ElementPtrType const& element, list<string> json_path,
22 string obscure, unsigned level) {
23 if (!element) {
24 isc_throw(BadValue, "redact() got a null pointer");
25 }
26 if (level == 0) {
27 isc_throw(BadValue, "redact() elements nested too deeply");
28 }
29
30 string const next_key(json_path.empty() ? string() : json_path.front());
31 ElementPtr result;
32 if (element->getType() == Element::list) {
33 // If we are looking for a list...
34 if (next_key == "*" || next_key == "[]") {
35 // But if we are looking specifically for a list...
36 if (next_key == "[]") {
37 // Then advance in the path.
38 json_path.pop_front();
39 }
40 // Then redact all children.
41 result = Element::createList();
42 for (ElementPtr const& child : element->listValue()) {
43 result->add(redact(child, json_path, obscure, level - 1));
44 }
45 return (result);
46 }
47 } else if (element->getType() == Element::map) {
48 // If we are looking for anything or if we have reached the end of a
50 if (next_key == "*" || json_path.empty()) {
51 // Then iterate through all the children.
52 result = Element::createMap();
53 for (auto const& kv : element->mapValue()) {
54 std::string const& key(kv.first);
55 ConstElementPtr const& value(kv.second);
56
57 if (boost::algorithm::ends_with(key, "password") ||
58 boost::algorithm::ends_with(key, "secret")) {
59 // Sensitive data
60 result->set(key, Element::create(obscure));
61 } else if (key == "user-context") {
62 // Skip user contexts.
63 result->set(key, value);
64 } else {
65 if (json_path.empty()) {
66 // End of path means no sensitive data expected in this
67 // subtree, so we stop here.
68 result->set(key, value);
69 } else {
70 // We are looking for anything '*' so redact further.
71 result->set(key, redact(value, json_path, obscure,
72 level - 1));
73 }
74 }
75 }
76 return (result);
77 } else {
78 ConstElementPtr child(element->get(next_key));
79 if (child) {
80 result = isc::data::copy(element, 1U);
81 json_path.pop_front();
82 result->set(next_key,
83 redact(child, json_path, obscure, level - 1));
84 return (result);
85 }
86 }
87 }
88
89 return (element);
90}
91
92} // namespace
93
94 namespace isc {
95 namespace process {
96
97 ConstElementPtr
98 redactConfig(ConstElementPtr const& element, list<string> const& json_path,
99 string obscure, unsigned max_nesting_depth) {
100 return (redact(element, json_path, obscure, max_nesting_depth));
101}
102
103} // namespace process
104} // namespace isc
Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Create a NullElement.
Definition data.cc:299
Element::map
@ map
Definition data.h:160
Element::list
@ list
Definition data.h:159
Element::createMap
static ElementPtr createMap(const Position &pos=ZERO_POSITION())
Creates an empty MapElement type ElementPtr.
Definition data.cc:354
Element::createList
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Definition data.cc:349
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::data
Definition base_stamped_element.cc:12
isc::data::copy
ElementPtr copy(ConstElementPtr from, unsigned level)
Copy the data up to a nesting level.
Definition data.cc:1517
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition data.h:29
isc::process
Definition cb_ctl_base.h:22
isc::process::redactConfig
ConstElementPtr redactConfig(ConstElementPtr const &element, list< string > const &json_path, string obscure, unsigned max_nesting_depth)
Redact a configuration.
Definition redact_config.cc:98
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
redact_config.h