Skip to content

Commit

Permalink
feat(jans-lock): add example of run cedarling Authz and updated Readme
Browse files Browse the repository at this point in the history
Signed-off-by: Oleh Bohzok <[email protected]>
  • Loading branch information
olehbozhok committed Sep 19, 2024
1 parent 8ab3e42 commit 52aa103
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
32 changes: 32 additions & 0 deletions jans-lock/cedarling/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Cedarling

The Cedarling is a performant local authorization service that runs the Rust Cedar Engine.
Cedar policies and schema are loaded at startup from a locally cached "Policy Store".
In simple terms, the Cedarling returns the answer: should the application allow this action on this resource given these JWT tokens.
Expand All @@ -7,10 +8,41 @@ For example, why display form fields that a user is not authorized to see?
The Cedarling is a more productive and flexible way to handle authorization.

## Rust Cedarling

Cedarling is written in the Rust programming language (folder `cedarling`). And you can import it into your project as a dependency.

## Cedarling bindings

We have support binding for this platforms:

- [ ] Python
- [ ] Wasm

## Examples of rust Cedarling

Currently we have example of using Cedarling with initialization logger.
We support 4 types of loggers. To run examples you need execute next command according to the logger:

- `off`

```bash
cargo run -p cedarling --example log_init -- off
```

- `stdout`

```bash
cargo run -p cedarling --example log_init -- stdout
```

- `memory`

```bash
cargo run -p cedarling --example log_init -- memory 60
```

- `lock` (in development)

```bash
cargo run -p cedarling --example log_init -- lock
```
72 changes: 72 additions & 0 deletions jans-lock/cedarling/cedarling/examples/log_init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use cedarling::{Authz, BootstrapConfig, LogConfig, LogType, MemoryLogConfig};
use std::{
env,
io::{self, Write},
};

fn main() {
// Collect command-line arguments
let args: Vec<String> = env::args().collect();

// Ensure at least one argument is provided (the program name itself is the first argument)
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);
}

// Parse the log type from the first argument
let log_type_arg = &args[1];
let log_type = match log_type_arg.as_str() {
"off" => LogType::Off,
"stdout" => LogType::StdOut,
"lock" => LogType::Lock,
"memory" => extract_memory_config(args),
_ => {
eprintln!("Invalid log type, defaulting to StdOut.");
LogType::StdOut
},
};

println!("Authz initialized with log type: {:?}", log_type);

// Create the Authz instance with the selected log type
let authz = Authz::new(BootstrapConfig {
application_name: "test_app".to_string(),
log_config: LogConfig { log_type },
});

println!("Stage 1:");
let logs_ids = authz.get_log_ids();
println!(
"Show results of get_logs(): returns a list of all log ids: {:?}",
&logs_ids
);
println!("\n\n Stage 2:\nShow result of get_log_by_id for each key.");
for id in logs_ids {
let entry = authz
.get_log_by_id(&id)
.map(|v| serde_json::json!(v).to_string());
println!("\nkey:{}\nvalue:{:?}", id, entry);
}

println!("\n\n Stage 3:\nShow result of pop_logs");
for (i, entry) in authz.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: {:?}", authz.get_log_ids().len());
}

fn extract_memory_config(args: Vec<String>) -> LogType {
if args.len() < 3 {
eprintln!("Memory log type requires two additional arguments: ttl value in seconds");
std::process::exit(1);
}
// Parse additional arguments for MemoryLogConfig
let log_ttl: u64 = args[2]
.parse()
.expect("Invalid ttl value, should be integer");
LogType::Memory(MemoryLogConfig { log_ttl })
}

0 comments on commit 52aa103

Please sign in to comment.