#![cfg(not(target_family = "wasm"))]
use cedarling::{
AuthorizationConfig, BootstrapConfig, Cedarling, DataStoreConfig, HttpClientConfig, JwtConfig,
LogConfig, LogLevel, LogStorage, LogTypeConfig, MemoryLogConfig, PolicyStoreConfig,
PolicyStoreSource, log_config::StdOutLoggerMode,
};
use std::env;
static POLICY_STORE_RAW: &str = include_str!("../../test_files/policy-store_ok.yaml");
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
eprintln!("Usage: {} <log_type> [ttl in seconds]", args[0]);
eprintln!("<log_type> can be one of off,stdout,memory");
std::process::exit(1);
}
let log_type_arg = &args[1];
let log_type = match log_type_arg.as_str() {
"off" => LogTypeConfig::Off,
"stdout" => LogTypeConfig::StdOut(StdOutLoggerMode::Immediate),
"lock" => unimplemented!(),
"memory" => extract_memory_config(&args),
_ => {
eprintln!("Invalid log type, defaulting to StdOut.");
LogTypeConfig::StdOut(StdOutLoggerMode::Immediate)
},
};
println!("Cedarling initialized with log type: {log_type:?}");
let cedarling = Cedarling::new(&BootstrapConfig {
application_name: "test_app".to_string(),
log_config: LogConfig {
log_type,
log_level: LogLevel::INFO,
},
policy_store_config: PolicyStoreConfig {
source: PolicyStoreSource::Yaml(POLICY_STORE_RAW.to_string()),
..Default::default()
},
jwt_config: JwtConfig::new_without_validation(),
authorization_config: AuthorizationConfig::default(),
lock_config: None,
max_default_entities: None,
max_base64_size: None,
data_store_config: DataStoreConfig::default(),
http_client_config: HttpClientConfig::default(),
})
.await?;
println!("Stage 1:");
let logs_ids = cedarling.get_log_ids();
println!(
"Show results of get_logs(): returns a list of all log ids: {:?}",
&logs_ids
);
println!("\n\nStage 2:\nShow result of get_log_by_id for each key.");
for id in logs_ids {
let entry = cedarling
.get_log_by_id(&id)
.map(|v| serde_json::json!(v).to_string());
println!("\nkey:{id}\nvalue:{entry:?}");
}
println!("\n\n Stage 3:\nShow result of pop_logs");
for (i, entry) in cedarling.pop_logs().iter().enumerate() {
println!("entry n:{i}\nvalue: {}", serde_json::json!(entry));
}
println!("\n\n Stage 4:\nShow len of keys left using get_log_ids");
println!("Number of keys left: {:?}", cedarling.get_log_ids().len());
Ok(())
}
fn extract_memory_config(args: &[String]) -> LogTypeConfig {
if args.len() < 3 {
eprintln!("Memory log type requires two additional arguments: ttl value in seconds");
std::process::exit(1);
}
let log_ttl: u64 = args[2]
.parse()
.expect("Invalid ttl value, should be integer");
LogTypeConfig::Memory(MemoryLogConfig {
log_ttl,
max_item_size: None,
max_items: None,
})
}