Skip to content

Commit

Permalink
feat(jans-lock): add Authz to the Cedarling instance
Browse files Browse the repository at this point in the history
Signed-off-by: Oleh Bohzok <olehbozhok@gmail.com>
  • Loading branch information
olehbozhok committed Sep 21, 2024
1 parent 412df18 commit f290efa
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 27 deletions.
8 changes: 6 additions & 2 deletions jans-lock/cedarling/cedarling/examples/log_init.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use cedarling::{BootstrapConfig, Cedarling, LogConfig, LogStorage, LogType, MemoryLogConfig};
use cedarling::{
AuthzConfig, BootstrapConfig, Cedarling, LogConfig, LogStorage, LogType, MemoryLogConfig,
};
use std::env;

fn main() {
Expand Down Expand Up @@ -29,7 +31,9 @@ fn main() {

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

Expand Down
25 changes: 4 additions & 21 deletions jans-lock/cedarling/cedarling/src/authz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,24 @@
//! - evaluate if authorization is granted for *user*
//! - evaluate if authorization is granted for *client*

use crate::log::LogStorage;
use crate::log::{init_logger, log_strategy::LogStrategy, LogWriter};
use crate::log::{LogWriter, Logger};
use crate::models::authz_config::AuthzConfig;
use crate::models::log_entry::{LogEntry, LogType};
use crate::BootstrapConfig;
use uuid7::{uuid4, Uuid};

/// Authorization Service
/// The primary service of the Cedarling application responsible for evaluating authorization requests.
/// It leverages other services as needed to complete its evaluations.
#[allow(dead_code)]
pub struct Authz {
log_service: LogStrategy,
log_service: Logger,
pdp_id: Uuid,
application_name: String,
}

impl Authz {
/// Create a new Authorization Service
pub fn new(config: BootstrapConfig) -> Self {
let log = init_logger(config.log_config);
pub fn new(config: AuthzConfig, log: Logger) -> Self {
// we use uuid v4 because it is generated based on random numbers.
let pdp_id = uuid4();
let application_name = config.application_name;
Expand All @@ -44,19 +42,4 @@ impl Authz {
application_name,
}
}

/// return logs and remove them from the storage
pub fn pop_logs(&self) -> Vec<LogEntry> {
self.log_service.pop_logs()
}

/// get specific log entry
pub fn get_log_by_id(&self, id: &str) -> Option<LogEntry> {
self.log_service.get_log_by_id(id)
}

/// returns a list of all log ids
pub fn get_log_ids(&self) -> Vec<String> {
self.log_service.get_log_ids()
}
}
13 changes: 11 additions & 2 deletions jans-lock/cedarling/cedarling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,33 @@ mod models;
#[cfg(test)]
mod tests;

use std::rc::Rc;

use authz::Authz;
use log::init_logger;
pub use log::LogStorage;
pub use models::authz_config::AuthzConfig;
pub use models::bootstrap_config::*;
pub use models::log_config::*;

/// The instance of the Cedarling application.
#[derive(Clone)]
pub struct Cedarling {
log: log::Logger,
// authz: Authz, //TODO: add instance of authz
#[allow(dead_code)]
authz: Rc<Authz>,
}

impl Cedarling {
/// Create a new instance of the Cedarling application.
pub fn new(config: BootstrapConfig) -> Cedarling {
let log = init_logger(config.log_config);
let authz = Authz::new(config.authz_config, log.clone());

Cedarling { log }
Cedarling {
log,
authz: Rc::new(authz),
}
}
}

Expand Down
1 change: 1 addition & 0 deletions jans-lock/cedarling/cedarling/src/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ use std::rc::Rc;

use crate::models::log_config::LogConfig;
pub use interface::LogStorage;
pub(crate) use interface::LogWriter;
pub(crate) use log_strategy::LogStrategy;

/// Type alias for logger that is used in application
Expand Down
13 changes: 13 additions & 0 deletions jans-lock/cedarling/cedarling/src/models/authz_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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.
*/

/// A set of properties used to configure `Authz` in the `Cedarling` application.
pub struct AuthzConfig {
/// `CEDARLING_APPLICATION_NAME` in [bootstrap properties](https://github.com/JanssenProject/jans/wiki/Cedarling-Nativity-Plan#bootstrap-properties) documentation.
pub application_name: String,
// TODO: Add more config params
}
5 changes: 3 additions & 2 deletions jans-lock/cedarling/cedarling/src/models/bootstrap_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
* Copyright (c) 2024, Gluu, Inc.
*/

use super::authz_config::AuthzConfig;
use super::log_config::LogConfig;

/// Bootstrap configuration
/// properties for configuration `Cedarling` application.
/// [link](https://github.com/JanssenProject/jans/wiki/Cedarling-Nativity-Plan#bootstrap-properties) to the documentation.
pub struct BootstrapConfig {
/// `CEDARLING_APPLICATION_NAME` in [bootstrap properties](https://github.com/JanssenProject/jans/wiki/Cedarling-Nativity-Plan#bootstrap-properties) documentation.
pub application_name: String,
/// A set of properties used to configure `Authz` in the `Cedarling` application.
pub authz_config: AuthzConfig,
/// A set of properties used to configure logging in the `Cedarling` application.
pub log_config: LogConfig,
}
1 change: 1 addition & 0 deletions jans-lock/cedarling/cedarling/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! This package provides the core data models for the *Cedarling* application,
//! defining the structures and types essential for its functionality.

pub mod authz_config;
pub mod bootstrap_config;
pub mod log_config;
pub mod log_entry;

0 comments on commit f290efa

Please sign in to comment.