-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jans-cedarling): add an example that runs profiling (#10979)
* add an example that runs profiling --------- Signed-off-by: rmarinn <[email protected]>
- Loading branch information
Showing
4 changed files
with
145 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
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
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
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 |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// This software is available under the Apache-2.0 license. | ||
// See https://www.apache.org/licenses/LICENSE-2.0.txt for full text. | ||
// | ||
// Copyright (c) 2024, Gluu, Inc. | ||
|
||
use cedarling::*; | ||
use std::collections::{HashMap, HashSet}; | ||
use std::fs::File; | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let cedarling = init_cedarling().await; | ||
let (access_token, id_token, userinfo_token) = get_tokens(); | ||
|
||
// init profiler guard | ||
let guard = pprof::ProfilerGuardBuilder::default() | ||
.frequency(1000) | ||
.blocklist(&["libc", "libgcc", "pthread", "vdso"]) | ||
.build() | ||
.unwrap(); | ||
|
||
// calls Cedarling::authorize | ||
call_authorize(&cedarling, &access_token, &id_token, &userinfo_token).await; | ||
|
||
if let Ok(report) = guard.report().build() { | ||
println!("report: {:?}", &report); | ||
|
||
// write output flamegraph to an SVG file | ||
let file = File::create(format!( | ||
"{}/../{}", | ||
env!("CARGO_MANIFEST_DIR"), | ||
"cedarling_profiling_flamegraph.svg", | ||
)) | ||
.unwrap(); | ||
let mut options = pprof::flamegraph::Options::default(); | ||
options.image_width = Some(3000); | ||
report.flamegraph_with_options(file, &mut options).unwrap(); | ||
}; | ||
|
||
Ok(()) | ||
} | ||
|
||
static POLICY_STORE_RAW: &str = include_str!("../../test_files/policy-store_ok.yaml"); | ||
|
||
async fn init_cedarling() -> Cedarling { | ||
let cedarling = Cedarling::new(&BootstrapConfig { | ||
application_name: "test_app".to_string(), | ||
log_config: LogConfig { | ||
log_type: LogTypeConfig::Off, | ||
log_level: LogLevel::INFO, | ||
}, | ||
policy_store_config: PolicyStoreConfig { | ||
source: PolicyStoreSource::Yaml(POLICY_STORE_RAW.to_string()), | ||
}, | ||
jwt_config: JwtConfig { | ||
jwks: None, | ||
jwt_sig_validation: false, | ||
jwt_status_validation: false, | ||
signature_algorithms_supported: HashSet::new(), | ||
token_validation_settings: HashMap::from_iter( | ||
["access_token", "id_token", "userinfo_token", "custom_token"] | ||
.iter() | ||
.map(|tkn| (tkn.to_string(), TokenValidationConfig::default())), | ||
), | ||
} | ||
.allow_all_algorithms(), | ||
authorization_config: AuthorizationConfig { | ||
use_user_principal: true, | ||
use_workload_principal: true, | ||
user_workload_operator: WorkloadBoolOp::And, | ||
decision_log_default_jwt_id: "jti".to_string(), | ||
decision_log_user_claims: vec!["client_id".to_string(), "username".to_string()], | ||
decision_log_workload_claims: vec!["org_id".to_string()], | ||
id_token_trust_mode: IdTokenTrustMode::None, | ||
mapping_tokens: HashMap::from([ | ||
("access_token".to_string(), "Jans::Access_token".to_string()), | ||
("id_token".to_string(), "Jans::id_token".to_string()), | ||
( | ||
"userinfo_token".to_string(), | ||
"Jans::Userinfo_token".to_string(), | ||
), | ||
]) | ||
.into(), | ||
..Default::default() | ||
}, | ||
}) | ||
.await | ||
.expect("should initialize cedarling"); | ||
|
||
cedarling | ||
} | ||
|
||
fn get_tokens() -> (String, String, String) { | ||
let access_token = "eyJhbGciOiJIUz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kbWluQGdsdXUub3JnIiwiY291bnRyeSI6IlVTIiwianRpIjoidXNyaW5mb190a25fanRpIn0.NoR53vPZFpfb4vFk85JH9RPx7CHsaJMZwrH3fnB-N60".to_string(); | ||
|
||
(access_token, id_token, userinfo_token) | ||
} | ||
|
||
async fn call_authorize( | ||
cedarling: &Cedarling, | ||
access_token: &str, | ||
id_token: &str, | ||
userinfo_token: &str, | ||
) { | ||
let _result = cedarling | ||
.authorize(Request { | ||
tokens: HashMap::from([ | ||
("access_token".to_string(), access_token.to_string()), | ||
("id_token".to_string(), id_token.to_string()), | ||
("userinfo_token".to_string(), userinfo_token.to_string()), | ||
]), | ||
action: "Jans::Action::\"Update\"".to_string(), | ||
context: serde_json::json!({}), | ||
resource: ResourceData { | ||
id: "random_id".to_string(), | ||
resource_type: "Jans::Issue".to_string(), | ||
payload: HashMap::from_iter([ | ||
( | ||
"org_id".to_string(), | ||
serde_json::Value::String("some_long_id".to_string()), | ||
), | ||
( | ||
"country".to_string(), | ||
serde_json::Value::String("US".to_string()), | ||
), | ||
]), | ||
}, | ||
}) | ||
.await; | ||
} |