Skip to content

Commit

Permalink
Do not match partial paths. Use hex numbers for tracing numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiPolo committed May 23, 2020
1 parent 442269c commit c9a9dab
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 56 deletions.
82 changes: 41 additions & 41 deletions Cargo.lock

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

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ termcolor = "1.0"
uuid = { version = ">= 0.6", features = ["v4"] }
rand = "0.7.0"
chrono = "0.4"
openapiv3 = "0.3.0"
openapiv3 = "0.3.2"
#openapi_utils = { path = "../../oasproxy/openapi_utils" }
openapi_utils = "0.2"
thiserror = "1.0"
Expand All @@ -41,11 +41,9 @@ itertools = "0.9"
comfy-table = "0.1.0"
lazy_static = "*"
tokio = { version = "0.2", features = ["full"] }
#smauth-client = { path = "../mauth-client-rust"}
#mauth-client = { path = "../mauth-client-rust"}
hyper = "*"

[patch.crates-io]
openapiv3 = { git = "https://github.com/glademiller/openapiv3", branch = "master" }

[profile.dev]
debug = 0
Expand Down
24 changes: 18 additions & 6 deletions src/known_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ impl KnownParam {
}
}

// We want:
// pattern {user_uuid} path: /v1/users/{user_uuid} -> true
// pattern {user_uuid} path: /v1/users/{user_uuid}/onboard -> true
// pattern {user_uuid} path: /v1/users/{user_uuid}/friends/{friend_uuid} -> false
fn path_matches(&self, full_path: &str) -> bool {
self.context == "path" && full_path.contains(&self.pattern)
self.context == "path" && full_path.contains(&self.pattern) &&
full_path.matches("}").count() == self.pattern.matches("}").count()
}

fn query_matches(&self, query_param_name: &str) -> bool {
Expand All @@ -34,11 +39,10 @@ impl KnownParamCollection {
}
}

pub fn find_by_path(&self, path: &str) -> Option<KnownParam> {
self.params
.clone()
.into_iter()
.find(|param| param.path_matches(path))
// a path may be /users/{uuid}/friends/{uuid2}
pub fn retrieve_known_path(&self, path: &str) -> Option<String> {
let conversion = self.find_by_path(path)?;
Some(str::replace(path, &conversion.pattern, &conversion.value))
}

pub fn param_known(&self, name: &str) -> bool {
Expand All @@ -54,6 +58,14 @@ impl KnownParamCollection {
.to_string()
}


fn find_by_path(&self, path: &str) -> Option<KnownParam> {
self.params
.clone()
.into_iter()
.find(|param| param.path_matches(path))
}

fn read_known_params(conversions: &str) -> Vec<KnownParam> {
match KnownParamCollection::_read_known_params(conversions) {
Err(_) => {
Expand Down
3 changes: 1 addition & 2 deletions src/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,7 @@ impl Mutator {
match mutagen {
Mutagen::PathProper => {
if path.contains('}') {
let conversion = self.known_params.find_by_path(path)?;
Some(str::replace(path, &conversion.pattern, &conversion.value))
self.known_params.retrieve_known_path(path)
} else {
Some(String::from(path))
}
Expand Down
6 changes: 3 additions & 3 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ impl Request {
pub fn headers(&self) -> HeaderMap {
let mut request_headers = HeaderMap::new();
let mut rng = rand::thread_rng();
let trace_id = rng.gen::<u128>().to_string();
let span_id = rng.gen::<u64>().to_string();

let trace_id = format!("{:x}", rng.gen::<u128>());
let span_id = format!("{:x}", rng.gen::<u64>());
// TODO: Verify apps receive and use thsese keys
request_headers.insert("Accept", HeaderValue::from_static("application/json"));
request_headers.insert("X-B3-Sampled", HeaderValue::from_static("0"));
request_headers.insert("X-B3-TraceId", HeaderValue::from_str(&trace_id).unwrap());
Expand Down

0 comments on commit c9a9dab

Please sign in to comment.