Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trace Tag Replacer: functionality to scrub sensitive data from spans #111

Merged
merged 27 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ NOTICE @Datadog/libdatadog
rustfmt.toml @Datadog/libdatadog
README.md @Datadog/libdatadog
trace-normalization @Datadog/serverless
trace-obfuscation @Datadog/serverless
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"ddtelemetry-ffi",
"tools",
"trace-normalization",
"trace-obfuscation",
]
# https://doc.rust-lang.org/cargo/reference/resolver.html#feature-resolver-version-2
resolver = "2"
Expand Down
2 changes: 1 addition & 1 deletion LICENSE-3rdparty.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
root_name: "datadog-profiling, ddcommon, datadog-profiling-ffi, ddcommon-ffi, ddtelemetry, ddtelemetry-ffi, tools"
root_name: "datadog-profiling, ddcommon, datadog-profiling-ffi, ddcommon-ffi, ddtelemetry, ddtelemetry-ffi, tools, datadog-trace-normalization"
third_party_libraries:
- package_name: aho-corasick
package_version: 0.7.20
Expand Down
1 change: 1 addition & 0 deletions tools/docker/Dockerfile.build
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ COPY "ddtelemetry-ffi/Cargo.toml" "ddtelemetry-ffi/"
COPY "profiling/Cargo.toml" "profiling/"
COPY "profiling-ffi/Cargo.toml" "profiling-ffi/"
COPY "trace-normalization/Cargo.toml" "trace-normalization/"
COPY "trace-obfuscation/Cargo.toml" "trace-obfuscation/"
COPY "tools/Cargo.toml" "tools/"
RUN find -name "Cargo.toml" | sed -e s#Cargo.toml#src/lib.rs#g | xargs -n 1 sh -c 'mkdir -p $(dirname $1); touch $1; echo $1' create_stubs
RUN echo ddtelemetry/benches/ipc.rs tools/src/bin/dedup_headers.rs ddtelemetry/examples/tm-worker-test.rs | xargs -n 1 sh -c 'mkdir -p $(dirname $1); touch $1; echo $1' create_stubs
Expand Down
13 changes: 13 additions & 0 deletions trace-obfuscation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "datadog-trace-obfuscation"
version = "2.0.0"
authors = ["David Lee <david.lee@datadoghq.com>"]
edition = "2021"

[dependencies]
prost = "0.11.6"
anyhow = "1.0"
regex = "1"

[dev-dependencies]
duplicate = "0.4.1"
12 changes: 12 additions & 0 deletions trace-obfuscation/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0. This product includes software
// developed at Datadog (https://www.datadoghq.com/). Copyright 2023-Present
// Datadog, Inc.

#![deny(clippy::all)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this clippy setting still required?


pub mod pb {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with this setup, the protocol buffer definitions will be included in the binary multiple times. Also might mean the normalization and obfuscation crates are using different types. Might be best to either bundle them all into a single crate, or move this protocol buffer definitions into a single dedicated crate and reference that instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved all the protobuf files + definitions into it's own dir/crate in the libdatadog root trace-protobuf, though we will likely re-organize the structure of all our agentless related code in the near future

include!("../../trace-normalization/src/pb/pb.rs");
pawelchcki marked this conversation as resolved.
Show resolved Hide resolved
}

pub mod replacer;
198 changes: 198 additions & 0 deletions trace-obfuscation/src/replacer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0. This product includes software
// developed at Datadog (https://www.datadoghq.com/). Copyright 2023-Present
// Datadog, Inc.

use crate::pb;
use regex::Regex;

pub trait TraceTagReplacer {
fn replace_trace_tags(trace: &mut [pb::Span], rules: &[ReplaceRule]);
}

#[derive(Debug)]
pub struct ReplaceRule<'a> {
// name specifies the name of the tag that the replace rule addresses. However,
// some exceptions apply such as:
// • "resource.name" will target the resource
// • "*" will target all tags and the resource
name: &'a str,

// re holds the regex pattern for matching.
re: regex::Regex,

// repl specifies the replacement string to be used when Pattern matches.
repl: &'a str,
}

struct DefaultTraceTagReplacer {}

impl TraceTagReplacer for DefaultTraceTagReplacer {
/// replace_trace_tags replaces the tag values of all spans within a trace with a given set of rules.
fn replace_trace_tags(trace: &mut [pb::Span], rules: &[ReplaceRule]) {
for rule in rules {
for span in &mut *trace {
match rule.name {
"*" => {
for (_, val) in span.meta.iter_mut() {
*val = rule.re.replace_all(val, rule.repl).to_string();
}
}
"resource.name" => {
span.resource = rule.re.replace_all(&span.resource, rule.repl).to_string();
}
_ => {
if let Some(val) = span.meta.get_mut(rule.name) {
let replaced_tag = rule.re.replace_all(val, rule.repl).to_string();
*val = replaced_tag;
}
}
}
}
}
}
}

/// parse_rules_from_string takes an array of rules, represented as an array of length 3 arrays
/// holding the tag name, regex pattern, and replacement string as strings.
/// * returns a vec of ReplaceRules
pub fn parse_rules_from_string<'a>(
Copy link
Contributor Author

@thedavl thedavl Feb 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may be changed in the future depending on how the "agentless" code consumes config variables, and how we want to parse the "replace_tags" config variables.

rules: &'a [[&'a str; 3]],
) -> anyhow::Result<Vec<ReplaceRule<'a>>> {
let mut vec: Vec<ReplaceRule> = Vec::with_capacity(rules.len());

for [name, pattern, repl] in rules {
let compiled_regex = match Regex::new(pattern) {
Ok(res) => res,
Err(err) => {
anyhow::bail!(format!(
"Obfuscator Error: Error while parsing rule: {}",
thedavl marked this conversation as resolved.
Show resolved Hide resolved
err
))
}
};
vec.push(ReplaceRule {
name,
re: compiled_regex,
repl,
});
}
Ok(vec)
}

#[cfg(test)]
mod tests {

use crate::pb;
use crate::replacer;
use duplicate::duplicate_item;
use std::collections::HashMap;

use super::TraceTagReplacer;

fn new_test_span_with_tags(tags: HashMap<&str, &str>) -> pb::Span {
let mut span = pb::Span {
duration: 10000000,
error: 0,
resource: "GET /some/raclette".to_string(),
service: "django".to_string(),
name: "django.controller".to_string(),
span_id: 123,
start: 1448466874000000000,
trace_id: 424242,
meta: HashMap::new(),
metrics: HashMap::from([("cheese_weight".to_string(), 100000.0)]),
parent_id: 1111,
r#type: "http".to_string(),
meta_struct: HashMap::new(),
};
for (key, val) in tags {
match key {
"resource.name" => {
span.resource = val.to_string();
}
_ => {
span.meta.insert(key.to_string(), val.to_string());
}
}
}
span
}

#[duplicate_item(
pawelchcki marked this conversation as resolved.
Show resolved Hide resolved
[
test_name [test_replace_tags]
rules [&[
["http.url", "(token/)([^/]*)", "${1}?"],
["http.url", "guid", "[REDACTED]"],
["custom.tag", "(/foo/bar/).*", "${1}extra"],
]]
input [
HashMap::from([
("http.url", "some/guid/token/abcdef/abc"),
("custom.tag", "/foo/bar/foo"),
])
]
expected [
HashMap::from([
("http.url", "some/[REDACTED]/token/?/abc"),
("custom.tag", "/foo/bar/extra"),
])
];
]
[
test_name [test_replace_tags_with_exceptions]
rules [&[
["*", "(token/)([^/]*)", "${1}?"],
["*", "this", "that"],
["http.url", "guid", "[REDACTED]"],
["custom.tag", "(/foo/bar/).*", "${1}extra"],
["resource.name", "prod", "stage"],
]]
input [
HashMap::from([
("resource.name", "this is prod"),
("http.url", "some/[REDACTED]/token/abcdef/abc"),
("other.url", "some/guid/token/abcdef/abc"),
("custom.tag", "/foo/bar/foo"),
])
]
expected [
HashMap::from([
("resource.name", "this is stage"),
("http.url", "some/[REDACTED]/token/?/abc"),
("other.url", "some/guid/token/?/abc"),
("custom.tag", "/foo/bar/extra"),
])
];
]
)]
#[test]
fn test_name() {
let parsed_rules = replacer::parse_rules_from_string(rules);
let root_span = new_test_span_with_tags(input);
let child_span = new_test_span_with_tags(input);
let mut trace = [root_span, child_span];

replacer::DefaultTraceTagReplacer::replace_trace_tags(&mut trace, &parsed_rules.unwrap());

for (key, val) in expected {
match key {
"resource.name" => {
assert_eq!(val, trace[0].resource);
assert_eq!(val, trace[1].resource);
}
_ => {
assert_eq!(val, trace[0].meta.get(key).unwrap());
assert_eq!(val, trace[1].meta.get(key).unwrap());
}
}
}
}

#[test]
fn test_parse_rules_invalid_regex() {
let result = replacer::parse_rules_from_string(&[["http.url", ")", "${1}?"]]);
assert!(result.is_err());
}
}