Skip to content

Commit 102231d

Browse files
authored
feat(obfuscation)!: feature parity on span obfuscation [APMSP-2671] (#1788)
# What does this PR do? Integrate the work that has been done on specific string obfuscation (sql, redis, json, etc...) in `obfuscate_span` which is the high level function that can be used externally. # Motivation Reach 100% parity between obfuscation libs. # Additional Notes Anything else we should know when reviewing? # How to test the change? - [x] Add tests from parity checker Co-authored-by: oscar.ledauphin <oscar.ledauphin@datadoghq.com>
1 parent a29b90b commit 102231d

File tree

13 files changed

+972
-250
lines changed

13 files changed

+972
-250
lines changed

Cargo.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE-3rdparty.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5024,7 +5024,7 @@ third_party_libraries:
50245024
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
50255025
DEALINGS IN THE SOFTWARE.
50265026
- package_name: bolero
5027-
package_version: 0.13.0
5027+
package_version: 0.13.4
50285028
repository: https://github.com/camshaft/bolero
50295029
license: MIT
50305030
licenses:
@@ -5038,21 +5038,21 @@ third_party_libraries:
50385038
- license: MIT
50395039
text: NOT FOUND
50405040
- package_name: bolero-engine
5041-
package_version: 0.13.0
5041+
package_version: 0.13.4
50425042
repository: https://github.com/camshaft/bolero
50435043
license: MIT
50445044
licenses:
50455045
- license: MIT
50465046
text: NOT FOUND
50475047
- package_name: bolero-generator
5048-
package_version: 0.13.1
5048+
package_version: 0.13.5
50495049
repository: https://github.com/camshaft/bolero
50505050
license: MIT
50515051
licenses:
50525052
- license: MIT
50535053
text: NOT FOUND
50545054
- package_name: bolero-generator-derive
5055-
package_version: 0.13.0
5055+
package_version: 0.13.4
50565056
repository: https://github.com/camshaft/bolero
50575057
license: MIT
50585058
licenses:
@@ -30600,9 +30600,9 @@ third_party_libraries:
3060030600
- package_name: stringmetrics
3060130601
package_version: 2.2.2
3060230602
repository: https://github.com/pluots/stringmetrics
30603-
license: License specified in file ($CARGO_HOME/registry/src/index.crates.io-6f17d22bba15001f/stringmetrics-2.2.2/LICENSE)
30603+
license: License specified in file ($CARGO_HOME/registry/src/index.crates.io-1949cf8c6b5b557f/stringmetrics-2.2.2/LICENSE)
3060430604
licenses:
30605-
- license: License specified in file ($CARGO_HOME/registry/src/index.crates.io-6f17d22bba15001f/stringmetrics-2.2.2/LICENSE)
30605+
- license: License specified in file ($CARGO_HOME/registry/src/index.crates.io-1949cf8c6b5b557f/stringmetrics-2.2.2/LICENSE)
3060630606
text: |
3060730607
Copyright 2022 Trevor Gross
3060830608

libdd-trace-obfuscation/src/json.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
// Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::collections::HashSet;
5-
64
use crate::json_scanner::{Op, Scanner};
7-
8-
type Transformer = Box<dyn Fn(&str) -> String + Send + Sync>;
5+
use crate::obfuscation_config::{JsonObfuscatorConfig, JsonStringTransformer};
96

107
/// Obfuscates a JSON string by replacing all leaf values with `"?"`, unless the value
118
/// belongs to a key listed in `keep_keys`, in which case it is left verbatim.
@@ -15,9 +12,7 @@ type Transformer = Box<dyn Fn(&str) -> String + Send + Sync>;
1512
/// Multiple concatenated JSON objects in the input are each obfuscated independently.
1613
/// On a parse error the output so far is returned with `"..."` appended.
1714
pub struct JsonObfuscator {
18-
keep_keys: HashSet<String>,
19-
transform_keys: HashSet<String>,
20-
transformer: Option<Transformer>,
15+
config: JsonObfuscatorConfig,
2116
}
2217

2318
enum ClosureKind {
@@ -26,16 +21,8 @@ enum ClosureKind {
2621
}
2722

2823
impl JsonObfuscator {
29-
pub fn new(
30-
keep_keys: impl IntoIterator<Item = String>,
31-
transform_keys: impl IntoIterator<Item = String>,
32-
transformer: Option<Transformer>,
33-
) -> Self {
34-
Self {
35-
keep_keys: keep_keys.into_iter().collect(),
36-
transform_keys: transform_keys.into_iter().collect(),
37-
transformer,
38-
}
24+
pub fn new(config: JsonObfuscatorConfig) -> Self {
25+
Self { config }
3926
}
4027

4128
/// Obfuscates json string and return an optional error on malformatted json
@@ -80,7 +67,7 @@ impl JsonObfuscator {
8067
&mut transforming_value,
8168
&mut keep_depth,
8269
depth,
83-
self.transformer.as_deref(),
70+
self.config.transformer.as_ref(),
8471
);
8572
}
8673
Op::ObjectValue | Op::ArrayValue => {
@@ -92,7 +79,7 @@ impl JsonObfuscator {
9279
&mut transforming_value,
9380
&mut keep_depth,
9481
depth,
95-
self.transformer.as_deref(),
82+
self.config.transformer.as_ref(),
9683
);
9784
}
9885
Op::BeginLiteral | Op::Continue => {
@@ -111,12 +98,12 @@ impl JsonObfuscator {
11198
}
11299
Op::ObjectKey => {
113100
let k = buf.trim_matches('"');
114-
if !keeping && self.keep_keys.contains(k) {
101+
if !keeping && self.config.keep_keys.contains(k) {
115102
keeping = true;
116103
keep_depth = depth + 1;
117104
} else if !transforming_value
118-
&& self.transformer.is_some()
119-
&& self.transform_keys.contains(k)
105+
&& self.config.transformer.is_some()
106+
&& self.config.transform_keys.contains(k)
120107
{
121108
transforming_value = true;
122109
}
@@ -158,7 +145,7 @@ fn handle_value_done(
158145
transforming_value: &mut bool,
159146
keep_depth: &mut usize,
160147
depth: usize,
161-
transformer: Option<&(dyn Fn(&str) -> String + Send + Sync)>,
148+
transformer: Option<&JsonStringTransformer>,
162149
) {
163150
if *transforming_value {
164151
if let Some(t) = transformer {
@@ -183,18 +170,23 @@ mod tests {
183170
use serde_json::json;
184171

185172
use super::JsonObfuscator;
186-
use crate::sql::obfuscate_sql_string;
173+
use crate::{obfuscation_config::JsonObfuscatorConfig, sql::obfuscate_sql_string};
187174

188175
fn obf(keep_keys: &[&str]) -> JsonObfuscator {
189-
JsonObfuscator::new(keep_keys.iter().map(|s| s.to_string()), [], None)
176+
JsonObfuscator::new(JsonObfuscatorConfig {
177+
enabled: true,
178+
keep_keys: keep_keys.iter().map(|key| key.to_string()).collect(),
179+
..Default::default()
180+
})
190181
}
191182

192183
fn obf_sql(keep_keys: &[&str], transform_keys: &[&str]) -> JsonObfuscator {
193-
JsonObfuscator::new(
194-
keep_keys.iter().map(|s| s.to_string()),
195-
transform_keys.iter().map(|s| s.to_string()),
196-
Some(Box::new(obfuscate_sql_string)),
197-
)
184+
JsonObfuscator::new(JsonObfuscatorConfig {
185+
enabled: true,
186+
keep_keys: keep_keys.iter().map(|s| s.to_string()).collect(),
187+
transform_keys: transform_keys.iter().map(|s| s.to_string()).collect(),
188+
transformer: Some(obfuscate_sql_string),
189+
})
198190
}
199191

200192
fn assert_json_eq(result: &str, expected: &str) {

0 commit comments

Comments
 (0)