Skip to content

Commit 99be8f0

Browse files
authored
Fix 401 Unauthorized error in CosmosDB operations (#832)
* Fix 401 Unauthorized error in CosmosDB operations * Move the code for striping the leading slash to and Rewrite tests
1 parent edade07 commit 99be8f0

File tree

1 file changed

+49
-20
lines changed

1 file changed

+49
-20
lines changed

sdk/data_cosmos/src/authorization_policy.rs

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,15 @@ impl Policy for AuthorizationPolicy {
5757

5858
let time_nonce = TimeNonce::new();
5959

60-
let uri_path = request.path_and_query();
61-
trace!("uri_path used by AuthorizationPolicy == {:#?}", uri_path);
62-
6360
let auth = {
64-
let resource_link = generate_resource_link(&uri_path);
61+
let resource_link = generate_resource_link(request);
6562
trace!("resource_link == {}", resource_link);
6663
generate_authorization(
6764
&self.authorization_token,
6865
&request.method(),
6966
ctx.get()
7067
.expect("ResourceType must be in the Context at this point"),
71-
resource_link,
68+
&resource_link,
7269
time_nonce,
7370
)
7471
};
@@ -95,15 +92,17 @@ impl Policy for AuthorizationPolicy {
9592
}
9693
}
9794

98-
/// This function strips the resource name from the passed uri. It does not alter the uri if a
99-
/// resource name is not present. This is accomplished in three steps (with eager return):
100-
/// 1. Find if the uri ends with a ENDING_STRING. If so, strip it and return. Every ENDING_STRING
95+
/// This function strips the leading slash and the resource name from the uri of the passed request.
96+
/// It does not strip the resource name if the resource name is not present. This is accomplished in
97+
/// four steps (with eager return):
98+
/// 1. Strip leading slash from the uri of the passed request.
99+
/// 2. Find if the uri ends with a ENDING_STRING. If so, strip it and return. Every ENDING_STRING
101100
/// starts with a leading slash so this check will not match uri compsed **only** by the
102101
/// ENDING_STRING.
103-
/// 2. Find if the uri **is** the ending string (without the leading slash). If so return an empty
102+
/// 3. Find if the uri **is** the ending string (without the leading slash). If so return an empty
104103
/// string. This covers the exception of the rule above.
105-
/// 3. Return the received uri unchanged.
106-
fn generate_resource_link(uri: &str) -> &str {
104+
/// 4. Return the received uri unchanged.
105+
fn generate_resource_link(request: &Request) -> String {
107106
static ENDING_STRINGS: &[&str] = &[
108107
"/dbs",
109108
"/colls",
@@ -117,12 +116,17 @@ fn generate_resource_link(uri: &str) -> &str {
117116
"/triggers",
118117
];
119118

119+
// This strips the leading slash from the uri of the passed request.
120+
let uri_path = request.path_and_query();
121+
let uri = uri_path.trim_start_matches('/');
122+
trace!("uri used by AuthorizationPolicy == {:#?}", uri);
123+
120124
// We find the above resource names. If found, we strip it and eagerly return. Note that the
121125
// resource names have a leading slash so the suffix will match `test/users` but not
122126
// `test-users`.
123127
for ending in ENDING_STRINGS {
124128
if let Some(uri_without_ending) = uri.strip_suffix(ending) {
125-
return uri_without_ending;
129+
return uri_without_ending.to_string();
126130
}
127131
}
128132

@@ -134,9 +138,9 @@ fn generate_resource_link(uri: &str) -> &str {
134138
.map(|ending| &ending[1..]) // this is safe since every ENDING_STRING starts with a slash
135139
.any(|item| uri == item)
136140
{
137-
""
141+
"".to_string()
138142
} else {
139-
uri
143+
uri.to_string()
140144
}
141145
}
142146

@@ -319,12 +323,37 @@ mon, 01 jan 1900 01:00:00 gmt
319323

320324
#[test]
321325
fn generate_resource_link_00() {
322-
assert_eq!(generate_resource_link("dbs/second"), "dbs/second");
323-
assert_eq!(generate_resource_link("dbs"), "");
324-
assert_eq!(
325-
generate_resource_link("colls/second/third"),
326-
"colls/second/third"
326+
let request = Request::new(
327+
reqwest::Url::parse("https://.documents.azure.com/dbs/second").unwrap(),
328+
http::Method::GET,
329+
);
330+
assert_eq!(&generate_resource_link(&request), "dbs/second");
331+
}
332+
333+
#[test]
334+
fn generate_resource_link_01() {
335+
let request = Request::new(
336+
reqwest::Url::parse("https://.documents.azure.com/dbs").unwrap(),
337+
http::Method::GET,
338+
);
339+
assert_eq!(&generate_resource_link(&request), "");
340+
}
341+
342+
#[test]
343+
fn generate_resource_link_02() {
344+
let request = Request::new(
345+
reqwest::Url::parse("https://.documents.azure.com/colls/second/third").unwrap(),
346+
http::Method::GET,
347+
);
348+
assert_eq!(&generate_resource_link(&request), "colls/second/third");
349+
}
350+
351+
#[test]
352+
fn generate_resource_link_03() {
353+
let request = Request::new(
354+
reqwest::Url::parse("https://.documents.azure.com/dbs/test_db/colls").unwrap(),
355+
http::Method::GET,
327356
);
328-
assert_eq!(generate_resource_link("dbs/test_db/colls"), "dbs/test_db");
357+
assert_eq!(&generate_resource_link(&request), "dbs/test_db");
329358
}
330359
}

0 commit comments

Comments
 (0)