Skip to content

Commit

Permalink
easy: commit cloud: Move acl helpers to not be internal only
Browse files Browse the repository at this point in the history
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
lmvasquezg authored and facebook-github-bot committed Jul 21, 2024
1 parent a2deb43 commit 1953a55
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
58 changes: 58 additions & 0 deletions eden/mononoke/commit_cloud/helpers/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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]
Expand Down Expand Up @@ -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"
);
}
}
1 change: 0 additions & 1 deletion eden/mononoke/facebook/commit_cloud/TARGETS
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ rust_library(
deps = [
"fbsource//third-party/rust:anyhow",
"fbsource//third-party/rust:base64",
"fbsource//third-party/rust:regex",
"fbsource//third-party/rust:reqwest",
"fbsource//third-party/rust:serde",
"fbsource//third-party/rust:serde_json",
Expand Down

0 comments on commit 1953a55

Please sign in to comment.