Skip to content

Commit

Permalink
Merge pull request #58 from w6d-io/develop
Browse files Browse the repository at this point in the history
add documentation + cleanup
  • Loading branch information
nhaquet-w6d authored Jul 24, 2024
2 parents 3a612b5 + df24716 commit e86a94a
Show file tree
Hide file tree
Showing 14 changed files with 658 additions and 632 deletions.
947 changes: 457 additions & 490 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ async-trait = "0.1"
axum = "0.7.*"
tower = "0.4.13"
tower-http = { version = "0.5.1", features = ["request-id", "trace"] }
tokio = { version = "1.36.*", features = ["rt-multi-thread", "macros", "sync"]}
tokio = { version = "1.38.*", features = ["rt-multi-thread", "macros", "sync"]}
serde = "1.0.*"
serde_json = "1.0.*"
rs-utils = {git = "https://github.com/w6d-io/rs-utils",features = ["kratos", "anyhow-rocket"]}
figment = "0.10.*"
tracing = { version = "0.1.37", features = ["log"] }
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
hyper = "0.14.23"
ory-kratos-client = "1.0.0"
hyper = "1.4.1"
ory-kratos-client = "1.1.0"
futures = "0.3.26"
thiserror = "1.0.38"
axum-extra = { version = "0.9.2", features = ["cookie"] }
tonic = "0.11.*"
prost = "0.12.*"
reqwest = "0.11.14"
tonic = "0.12.*"
prost = "0.13.*"
reqwest = "0.11.27"
serde-email = "3.0.0"
uuid = { version = "^1.5", features = ["serde"] }
stream-cancel = "0.8.2"
Expand All @@ -37,11 +37,11 @@ branch = "develop"
features = ["async", "anyhow"]

[build-dependencies]
tonic-build = "0.11.*"
tonic-build = "0.12.*"

[dev-dependencies]
mime = "0.3.17"
mockito = "1.0.0"
mockito = "1.4.0"
uuid = { version = "1.3.1", features = ["v4"] }

[features]
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.75-bullseye AS build
FROM rust:1.79-bullseye AS build
ARG JOB_TOKEN
ARG JOB_USER
ENV CARGO_NET_GIT_FETCH_WITH_CLI true
Expand Down
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# sirius

## SIRIUS
Sirius is an api that use to list and modify kratos identity.

### routes

``/api/iam/project``:
This route is used to add or list projects in the identity
send a GET request with a kratos cookie to list the projects.
send a POST request with a kratos cookie and the projects to add.

``/api/iam/group``:
This route is used to list groups in an identity or add projects to a group
send a GET request with a kratos cookie to list the groups in the identity.
send a POST request with a kratos cookie and the projects or users to add.

``/api/iam/organisation``:
This route is used to list organisations in an identity or add to a group
send a GET request with a kratos cookie to list the organizations in the identity.
send a POST request with a kratos cookie and the projects, groups or users to add.

In all the POST case you must use the payload json:
```json
{
"id" = "string"
"resource_type" = "string"
"ressource_id" = "string"
"value" = json value
}
```
The id field represent the id of the identity to modify(can be anid or an email
depending on the route).

The resource_type field represent the type of permission to modify:
- user
- group
- organization

The ressource id represent the id of resource to modify.

the value field represent the data to modify the identity with
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ pub fn main() {
.type_attribute("Input", "#[derive(serde::Deserialize, serde::Serialize)]")
.build_server(true)
.compile(&proto_files, &["."])
.unwrap_or_else(|e| panic!("protobuf compile error: {}", e));
println!("cargo:rerun-if-changed={:?}", proto_files);
.unwrap_or_else(|e| panic!("protobuf compile error: {e}"));
println!("cargo:rerun-if-changed={proto_files:?}");
}
17 changes: 11 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::permission::iam_client::IamClient;

pub const CONFIG_FALLBACK: &str = "test/config.toml";

///structure containing kafka consumer data
/// Structure representing kafka producer config.
#[derive(Deserialize, Clone, Default)]
pub struct Producer {
pub topics: Vec<String>,
Expand All @@ -41,14 +41,14 @@ impl fmt::Debug for Producer {
}

impl Producer {
///update the producer Producers if needed.
/// Update the producer if needed.
pub fn update(&mut self, broker: &str) -> Result<()> {
let mut new_producer = HashMap::new();
let producer = match self.clients {
Some(ref mut prod) => prod,
None => &mut new_producer,
};
for topic in self.topics.iter() {
for topic in &self.topics {
producer.insert(
topic.to_owned(),
Arc::new(KafkaProducer::<FutureProducer, DefaultFutureContext>::new(
Expand All @@ -61,6 +61,7 @@ impl Producer {
}
}

/// Structure representing the kafka config.
#[derive(Deserialize, Clone, Default, Debug)]
pub struct Kafka {
pub broker: String,
Expand All @@ -74,32 +75,36 @@ impl Kafka {
}
}

/// Structure representing the service port config.
#[derive(Deserialize, Clone, Default, Debug)]
pub struct Ports {
pub main: String,
pub health: String,
}

/// Structure representing the service config.
#[derive(Deserialize, Clone, Default, Debug)]
pub struct Service {
pub addr: String,
pub ports: Ports,
}

/// Structure representing the iam connection config.
#[derive(Deserialize, Clone, Default, Debug)]
pub struct Iam {
pub service: Service,
#[serde(skip)]
pub client: Option<IamClient<Channel>>,
}

/// Structure representing the opa connection config.
#[derive(Deserialize, Clone, Default, Debug)]
pub struct Opa {
pub addr: String,
pub mode: String,
}

///structure containing the configuaration of the application
/// Structure containing the configuaration of the application.
#[derive(Deserialize, Clone, Default, Debug)]
pub struct SiriusConfig {
// pub prefix: String,
Expand All @@ -113,7 +118,7 @@ pub struct SiriusConfig {
}

impl Iam {
async fn update(&mut self) -> Result<()> {
fn update(&mut self) -> Result<()> {
let addr = "http://".to_string()
+ &self.service.addr as &str
+ ":"
Expand Down Expand Up @@ -143,7 +148,7 @@ impl Config for SiriusConfig {
}
let mut config: SiriusConfig = Figment::new().merge(Toml::file(path)).extract()?;
config.kratos.update();
config.iam.update().await?;
config.iam.update()?;
config.set_path(path);
config.kafka.update()?;
*self = config;
Expand Down
36 changes: 19 additions & 17 deletions src/controller/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ use tracing::{debug, error, info};

use crate::config::SiriusConfig;

/// Populate the projects id HashSet with the given data.
fn populate_set(projects: &mut HashSet<String>, mut data: Value) -> Result<()> {
let data = data.take();
match data {
Value::Object(map) => {
if !map.is_empty() {
for (key, _) in map.iter() {
for (key, _) in &map {
projects.insert(key.to_owned());
}
}
}
Value::Array(array) => {
if !array.is_empty() {
for project in array.iter() {
for project in &array {
let project = project
.as_str()
.ok_or_else(|| anyhow!(" this shoud be a string"))?;
Expand All @@ -33,13 +34,14 @@ fn populate_set(projects: &mut HashSet<String>, mut data: Value) -> Result<()> {
Ok(())
}

/// Extract the projects id from the identity and store them in the projects hashset.
fn extract_projects(projects: &mut HashSet<String>, mut data: Value) -> Result<()> {
debug!("{data:?}");
let data = data
.as_object_mut()
.ok_or_else(|| anyhow!("this should be a map!"))?;
if !data.is_empty() {
for (_, val) in data.into_iter() {
for (_, val) in &mut *data {
if let Some(proj) = val.get_mut("project") {
populate_set(projects, proj.take())?;
}
Expand All @@ -48,6 +50,8 @@ fn extract_projects(projects: &mut HashSet<String>, mut data: Value) -> Result<(
Ok(())
}

/// extract projects id from the identity in project, group and organisation and returns them in a
/// hashset.
pub async fn list_project_controller(
identity: Identity,
config: &SiriusConfig,
Expand All @@ -60,12 +64,11 @@ pub async fn list_project_controller(
_ => bail!("Invalid mode! please put a valid mode (admin, public or trait) in the config"),
};

let mut metadata = match meta {
Some(mut metadata) => metadata.take(),
None => {
error!("no metadata in this user!");
bail!("no metadata in this user!")
}
let mut metadata = if let Some(mut metadata) = meta {
metadata.take()
} else {
error!("no metadata in this user!");
bail!("no metadata in this user!")
};
if let Some(data) = metadata.get_mut("project") {
info!("extracting project from project");
Expand All @@ -82,6 +85,8 @@ pub async fn list_project_controller(
Ok(projects)
}

/// Extract the ask data type of a given identity.
/// Where it recuperate them is configured with mode in the opa section of te config file.
pub async fn list_controller(
identity: Identity,
data_type: &str,
Expand All @@ -95,20 +100,17 @@ pub async fn list_controller(
_ => bail!("Invalid mode! please put a valid mode (admin, public or trait) in the config"),
};

let metadata = match meta {
Some(metadata) => metadata,
None => {
error!("no metadata in this user!");
bail!("no metadata in this user!")
}
let Some(metadata) = meta else {
error!("no metadata in this user!");
bail!("no metadata in this user!")
};
if let Some(data) = metadata.get(data_type) {
info!("estracting: {data_type}");
info!("extracting: {data_type}");
let data = data
.as_object()
.ok_or_else(|| anyhow!("this should be a map!"))?;
if !data.is_empty() {
for (uuid, map) in data.iter() {
for (uuid, map) in data {
let val = map.get("name").ok_or_else(|| anyhow!("no name found !"))?;
let name = val
.as_str()
Expand Down
Loading

0 comments on commit e86a94a

Please sign in to comment.