-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
easy: commit cloud: Move acl helpers to not be internal only
Summary: ACL validation layer is open to oss aswell, so let's move them out of the inetrnal only commit cloud helpers Reviewed By: mitrandir77 Differential Revision: D59963026 fbshipit-source-id: 74a84e291314e35cea9efe6533acd42cb86692c0
- Loading branch information
1 parent
a2deb43
commit 1953a55
Showing
2 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ use regex::Regex; | |
const WORKSPACE_NAME_PATTERN: &str = r"user/([^/]+)/.+"; | ||
const EMAIL_PATTERN: &str = r"^([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9_\-.]+)\.([a-zA-Z]{2,5})$"; | ||
const LINUX_USER_PATTERN: &str = r"^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}\\$)$"; | ||
#[allow(unused)] | ||
const VALID_ACL_PATTERN: &str = "^[a-zA-Z0-9,=_\\.\\-]+([/][a-zA-Z0-9,=_\\.\\-]+)*$"; | ||
|
||
pub fn is_valid_workspace_structure(name: &str) -> (bool, Option<String>) { | ||
let validator = | ||
|
@@ -36,8 +38,33 @@ pub fn sanity_check_workspace_name(name: &str) -> bool { | |
false | ||
} | ||
|
||
#[allow(unused)] | ||
pub fn is_valid_acl_name(acl_name: &str) -> bool { | ||
let validator = Regex::new(VALID_ACL_PATTERN).expect("Error while creating email regex"); | ||
validator.is_match(acl_name) | ||
} | ||
|
||
#[allow(unused)] | ||
pub fn decorate_workspace_name_to_valid_acl_name(name: &str) -> String { | ||
let mut workspace_decorated = name.to_string(); | ||
let allowed_punctuation = [',', '=', '_', '.', '-', '/']; | ||
workspace_decorated = workspace_decorated | ||
.chars() | ||
.map(|c| { | ||
if c.is_alphanumeric() || allowed_punctuation.contains(&c) { | ||
c | ||
} else { | ||
'_' | ||
} | ||
}) | ||
.collect(); | ||
workspace_decorated | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::decorate_workspace_name_to_valid_acl_name; | ||
use crate::is_valid_acl_name; | ||
use crate::sanity_check_workspace_name; | ||
|
||
#[test] | ||
|
@@ -65,4 +92,35 @@ mod test { | |
"user/[email protected]/other name with spaces" | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_acl_names() { | ||
assert!(!is_valid_acl_name( | ||
"user/[email protected]/other name with spaces" | ||
)); | ||
assert!(!is_valid_acl_name("user/[email protected]/default")); | ||
} | ||
|
||
#[test] | ||
fn test_valid_acl_names() { | ||
assert!(is_valid_acl_name("user/testuser/default")); | ||
} | ||
|
||
#[test] | ||
fn test_workspace_name_decorate_for_acl() { | ||
assert_eq!( | ||
decorate_workspace_name_to_valid_acl_name("user/testuser/default"), | ||
"user/testuser/default" | ||
); | ||
assert_eq!( | ||
decorate_workspace_name_to_valid_acl_name("user/[email protected]/default"), | ||
"user/testuser_oculus.com/default" | ||
); | ||
assert_eq!( | ||
decorate_workspace_name_to_valid_acl_name( | ||
"user/[email protected]/other name with spaces" | ||
), | ||
"user/testuser_oculus.com/other_name_with_spaces" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters