Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
maggie98choy committed Aug 16, 2024
1 parent a0740a3 commit d3643a2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 26 deletions.
9 changes: 4 additions & 5 deletions core/main/src/state/cap/generic_cap_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ impl GenericCapState {
for cap in request {
result.insert(cap.clone(), supported_cap.contains(&cap));
}

result
}

Expand All @@ -107,10 +106,10 @@ impl GenericCapState {
.map(|fb_perm| fb_perm.cap.clone())
.collect();

// debug!(
// "checking supported caps request={:?}, not_supported={:?}, supported: {:?}",
// request, not_supported, supported
// );
debug!(
"checking supported caps request={:?}, not_supported={:?}, supported: {:?}",
request, not_supported, supported
);

if !not_supported.is_empty() {
return Err(DenyReasonWithCap::new(
Expand Down
68 changes: 63 additions & 5 deletions core/sdk/src/api/firebolt/fb_capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,67 @@ pub struct FireboltPermission {
pub role: CapabilityRole,
}

impl FireboltPermission {
pub fn from_vec_string(
perm_strings: Vec<String>,
role_based_support: bool,
) -> Vec<FireboltPermission> {
let mut perm_list: Vec<FireboltPermission> = Vec::new();
for perm in perm_strings {
if role_based_support {
let pattern = r"^xrn:firebolt:capability:([a-z0-9\\-]+)((:[a-z0-9\\-]+)?)$";
if Regex::new(pattern).unwrap().is_match(perm.as_str()) {
// Default Capability which without [role] at the end of capability string for e.g `xrn:firebolt:capability:account:session`,
// we add use role to the capability
perm_list.push(FireboltPermission {
cap: FireboltCap::Full(perm.to_owned()),
role: CapabilityRole::Use,
});
} else if perm.ends_with("[manage]") {
let mut cap = perm.clone();
cap.truncate(perm.len() - "[manage]".len());

perm_list.push(FireboltPermission {
cap: FireboltCap::Full(cap.to_owned()),
role: CapabilityRole::Use,
});
perm_list.push(FireboltPermission {
cap: FireboltCap::Full(cap),
role: CapabilityRole::Manage,
});
}
} else if !(perm.ends_with("[manage]") || perm.ends_with("[provide]")) {
// Default Capability which without [role] at the end of capability string for e.g `xrn:firebolt:capability:account:session`,
// we add use, manage and provide roles to the capability
perm_list.push(FireboltPermission {
cap: FireboltCap::Full(perm.to_owned()),
role: CapabilityRole::Use,
});
perm_list.push(FireboltPermission {
cap: FireboltCap::Full(perm.to_owned()),
role: CapabilityRole::Manage,
});
perm_list.push(FireboltPermission {
cap: FireboltCap::Full(perm),
role: CapabilityRole::Provide,
});
} else if perm.ends_with("[manage]") {
let mut cap = perm.clone();
cap.truncate(perm.len() - "[manage]".len());
perm_list.push(FireboltPermission {
cap: FireboltCap::Full(cap.to_owned()),
role: CapabilityRole::Use,
});
perm_list.push(FireboltPermission {
cap: FireboltCap::Full(cap.to_owned()),
role: CapabilityRole::Manage,
});
}
}
perm_list
}
}

impl From<RoleInfo> for FireboltPermission {
fn from(role_info: RoleInfo) -> Self {
FireboltPermission {
Expand Down Expand Up @@ -225,7 +286,7 @@ impl Serialize for FireboltPermission {
{
let s = self.cap.as_str();
let suffix = match self.role {
CapabilityRole::Use => "[use]",
CapabilityRole::Use => "",
CapabilityRole::Manage => "[manage]",
CapabilityRole::Provide => "[provide]",
};
Expand Down Expand Up @@ -621,10 +682,7 @@ mod tests {
role: CapabilityRole::Use,
};
let serialized = serde_json::to_string(&perm).unwrap();
assert_eq!(
serialized,
"\"xrn:firebolt:capability:account:session[use]\""
);
assert_eq!(serialized, "\"xrn:firebolt:capability:account:session\"");
}

#[test]
Expand Down
23 changes: 7 additions & 16 deletions core/sdk/src/api/manifest/device_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use crate::{
use super::{apps::AppManifest, exclusory::ExclusoryImpl, remote_feature::FeatureFlag};
pub const PARTNER_EXCLUSION_REFRESH_TIMEOUT: u32 = 12 * 60 * 60; // 12 hours
pub const METRICS_LOGGING_PERCENTAGE_DEFAULT: u32 = 10;
use serde_json::json;

#[derive(Deserialize, Debug, Clone)]
pub struct RippleConfiguration {
Expand Down Expand Up @@ -792,22 +791,9 @@ impl DeviceManifest {