Skip to content

Commit

Permalink
feat(rust): remove the intermediate encoding of bound values
Browse files Browse the repository at this point in the history
  • Loading branch information
etorreborre committed Jun 26, 2024
1 parent 97a0593 commit 02e62f6
Show file tree
Hide file tree
Showing 52 changed files with 645 additions and 505 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion implementations/rust/ockam/ockam_abac/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ockam_executor = { version = "0.80.0", path = "../ockam_executor", default-featu
regex = { version = "1.10.5", default-features = false, optional = true }
rustyline = { version = "14.0.0", optional = true }
rustyline-derive = { version = "0.10.0", optional = true }
sqlx = { git = "https://github.com/etorreborre/sqlx", rev = "1b24b40de97db1cefe387bd590089bf915598547", optional = true, features = ["runtime-tokio", "sqlite", "postgres", "migrate", "any"] }
sqlx = { git = "https://github.com/etorreborre/sqlx", rev = "5fec648d2de0cbeed738dcf1c6f5bc9194fc439b", optional = true, features = ["runtime-tokio", "sqlite", "postgres", "migrate", "any"] }
str-buf = "3.0.3"
tokio = { version = "1.38", default-features = false, optional = true, features = ["sync", "time", "rt", "rt-multi-thread", "macros"] }
tracing = { version = "0.1", default-features = false, features = ["attributes"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tracing::debug;
use ockam_core::async_trait;
use ockam_core::compat::vec::Vec;
use ockam_core::Result;
use ockam_node::database::{FromSqlxError, SqlxDatabase, SqlxType, ToSqlxType, ToVoid};
use ockam_node::database::{FromSqlxError, SqlxDatabase, ToVoid};

use crate::{Action, Expr, ResourceName, ResourcePoliciesRepository, ResourcePolicy};

Expand Down Expand Up @@ -48,9 +48,9 @@ impl ResourcePoliciesRepository for ResourcePolicySqlxDatabase {
ON CONFLICT (resource_name, action, node_name)
DO UPDATE SET expression = $3"#,
)
.bind(resource_name.to_sql())
.bind(action.to_sql())
.bind(expression.to_string())
.bind(resource_name)
.bind(action)
.bind(expression)
.bind(&self.node_name);
query.execute(&*self.database.pool).await.void()
}
Expand All @@ -66,8 +66,8 @@ impl ResourcePoliciesRepository for ResourcePolicySqlxDatabase {
WHERE node_name = $1 and resource_name = $2 and action = $3"#,
)
.bind(&self.node_name)
.bind(resource_name.to_sql())
.bind(action.to_sql());
.bind(resource_name)
.bind(action);
let row: Option<PolicyRow> = query
.fetch_optional(&*self.database.pool)
.await
Expand Down Expand Up @@ -98,7 +98,7 @@ impl ResourcePoliciesRepository for ResourcePolicySqlxDatabase {
WHERE node_name = $1 and resource_name = $2"#,
)
.bind(&self.node_name)
.bind(resource_name.to_sql());
.bind(resource_name);
let row: Vec<PolicyRow> = query.fetch_all(&*self.database.pool).await.into_core()?;
row.into_iter()
.map(|r| r.try_into())
Expand All @@ -111,26 +111,12 @@ impl ResourcePoliciesRepository for ResourcePolicySqlxDatabase {
WHERE node_name = $1 and resource_name = $2 and action = $3"#,
)
.bind(&self.node_name)
.bind(resource_name.to_sql())
.bind(action.to_sql());
.bind(resource_name)
.bind(action);
query.execute(&*self.database.pool).await.void()
}
}

// Database serialization / deserialization

impl ToSqlxType for ResourceName {
fn to_sql(&self) -> SqlxType {
SqlxType::Text(self.as_str().to_string())
}
}

impl ToSqlxType for Action {
fn to_sql(&self) -> SqlxType {
SqlxType::Text(self.to_string())
}
}

/// Low-level representation of a row in the resource_policy table
#[derive(FromRow)]
struct PolicyRow {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use core::str::FromStr;
use sqlx::database::HasArguments;
use sqlx::encode::IsNull;
use sqlx::*;
use tracing::debug;

use ockam_core::async_trait;
use ockam_core::Result;
use ockam_node::database::{FromSqlxError, SqlxDatabase, ToSqlxType, ToVoid};
use ockam_node::database::{FromSqlxError, SqlxDatabase, ToVoid};

use crate::{Resource, ResourceName, ResourceType, ResourcesRepository};

Expand Down Expand Up @@ -42,8 +44,8 @@ impl ResourcesRepository for ResourcesSqlxDatabase {
VALUES ($1, $2, $3)
ON CONFLICT DO NOTHING"#,
)
.bind(resource.resource_name.to_sql())
.bind(resource.resource_type.to_sql())
.bind(&resource.resource_name)
.bind(&resource.resource_type)
.bind(&self.node_name);
query.execute(&*self.database.pool).await.void()
}
Expand All @@ -55,7 +57,7 @@ impl ResourcesRepository for ResourcesSqlxDatabase {
WHERE node_name = $1 and resource_name = $2"#,
)
.bind(&self.node_name)
.bind(resource_name.to_sql());
.bind(resource_name);
let row: Option<ResourceRow> = query
.fetch_optional(&*self.database.pool)
.await
Expand All @@ -71,21 +73,35 @@ impl ResourcesRepository for ResourcesSqlxDatabase {
WHERE node_name = $1 and resource_name = $2"#,
)
.bind(&self.node_name)
.bind(resource_name.to_sql());
.bind(resource_name);
query.execute(&mut *transaction).await.void()?;

let query = sqlx::query(
r#"DELETE FROM resource_policy
WHERE node_name = $1 and resource_name = $2"#,
)
.bind(&self.node_name)
.bind(resource_name.to_sql());
.bind(resource_name);
query.execute(&mut *transaction).await.void()?;

transaction.commit().await.void()
}
}

// Database serialization / deserialization

impl Type<Any> for ResourceName {
fn type_info() -> <Any as Database>::TypeInfo {
<String as Type<Any>>::type_info()
}
}

impl sqlx::Encode<'_, Any> for ResourceName {
fn encode_by_ref(&self, buf: &mut <Any as HasArguments>::ArgumentBuffer) -> IsNull {
<String as sqlx::Encode<'_, Any>>::encode_by_ref(&self.to_string(), buf)
}
}

/// Low-level representation of a row in the resource_type_policy table
#[derive(FromRow)]
#[allow(dead_code)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use core::str::FromStr;
use sqlx::database::HasArguments;
use sqlx::encode::IsNull;
use sqlx::*;
use tracing::debug;

use ockam_core::async_trait;
use ockam_core::compat::vec::Vec;
use ockam_core::Result;
use ockam_node::database::{FromSqlxError, SqlxDatabase, SqlxType, ToSqlxType, ToVoid};
use ockam_node::database::{FromSqlxError, SqlxDatabase, ToVoid};

use crate::policy::ResourceTypePolicy;
use crate::{Action, Expr, ResourceType, ResourceTypePoliciesRepository};
Expand Down Expand Up @@ -50,9 +52,9 @@ impl ResourceTypePoliciesRepository for ResourceTypePolicySqlxDatabase {
ON CONFLICT (node_name, resource_type, action)
DO UPDATE SET expression = $3"#,
)
.bind(resource_type.to_sql())
.bind(action.to_sql())
.bind(expression.to_string())
.bind(resource_type)
.bind(action)
.bind(expression)
.bind(&self.node_name);
query.execute(&*self.database.pool).await.void()
}
Expand All @@ -68,8 +70,8 @@ impl ResourceTypePoliciesRepository for ResourceTypePolicySqlxDatabase {
WHERE node_name = $1 and resource_type = $2 and action = $3"#,
)
.bind(&self.node_name)
.bind(resource_type.to_sql())
.bind(action.to_sql());
.bind(resource_type)
.bind(action);
let row: Option<PolicyRow> = query
.fetch_optional(&*self.database.pool)
.await
Expand Down Expand Up @@ -98,7 +100,7 @@ impl ResourceTypePoliciesRepository for ResourceTypePolicySqlxDatabase {
FROM resource_type_policy where node_name = $1 and resource_type = $2"#,
)
.bind(&self.node_name)
.bind(resource_type.to_sql());
.bind(resource_type);
let row: Vec<PolicyRow> = query.fetch_all(&*self.database.pool).await.into_core()?;
row.into_iter()
.map(|r| r.try_into())
Expand All @@ -111,12 +113,50 @@ impl ResourceTypePoliciesRepository for ResourceTypePolicySqlxDatabase {
WHERE node_name = $1 and resource_type = $2 and action = $3"#,
)
.bind(&self.node_name)
.bind(resource_type.to_sql())
.bind(action.to_sql());
.bind(resource_type)
.bind(action);
query.execute(&*self.database.pool).await.void()
}
}

// Database serialization / deserialization

impl Type<Any> for ResourceType {
fn type_info() -> <Any as Database>::TypeInfo {
<String as Type<Any>>::type_info()
}
}

impl Encode<'_, Any> for ResourceType {
fn encode_by_ref(&self, buf: &mut <Any as HasArguments>::ArgumentBuffer) -> IsNull {
<String as Encode<'_, Any>>::encode_by_ref(&self.to_string(), buf)
}
}

impl Type<Any> for Action {
fn type_info() -> <Any as Database>::TypeInfo {
<String as Type<Any>>::type_info()
}
}

impl sqlx::Encode<'_, Any> for Action {
fn encode_by_ref(&self, buf: &mut <Any as HasArguments>::ArgumentBuffer) -> IsNull {
<String as Encode<'_, Any>>::encode_by_ref(&self.to_string(), buf)
}
}

impl Type<Any> for Expr {
fn type_info() -> <Any as Database>::TypeInfo {
<String as Type<Any>>::type_info()
}
}

impl Encode<'_, Any> for Expr {
fn encode_by_ref(&self, buf: &mut <Any as HasArguments>::ArgumentBuffer) -> IsNull {
<String as Encode<'_, Any>>::encode_by_ref(&self.to_string(), buf)
}
}

/// Low-level representation of a row in the resource_type_policy table
#[derive(FromRow)]
struct PolicyRow {
Expand Down Expand Up @@ -151,14 +191,6 @@ impl TryFrom<PolicyRow> for ResourceTypePolicy {
}
}

// Database serialization / deserialization

impl ToSqlxType for ResourceType {
fn to_sql(&self) -> SqlxType {
SqlxType::Text(self.to_string())
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
4 changes: 4 additions & 0 deletions implementations/rust/ockam/ockam_abac/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ macro_rules! define {
pub fn as_str(&self) -> &str {
&self.0
}

pub fn to_string(&self) -> String {
self.as_str().to_string()
}
}

impl From<&str> for $t {
Expand Down
2 changes: 1 addition & 1 deletion implementations/rust/ockam/ockam_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.118"
sha2 = "0.10.8"
#sqlx = { version = "0.7.4", features = ["runtime-tokio", "sqlite", "postgres", "migrate", "any"] }
sqlx = { git = "https://github.com/etorreborre/sqlx", rev = "1b24b40de97db1cefe387bd590089bf915598547", features = ["runtime-tokio", "sqlite", "postgres", "migrate", "any"] }
sqlx = { git = "https://github.com/etorreborre/sqlx", rev = "5fec648d2de0cbeed738dcf1c6f5bc9194fc439b", features = ["runtime-tokio", "sqlite", "postgres", "migrate", "any"] }
strip-ansi-escapes = "0.2"
sysinfo = "0.30"
thiserror = "1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use ockam_core::compat::string::{String, ToString};
use ockam_core::errcode::{Kind, Origin};
use ockam_core::Error;
use ockam_core::Result;
use ockam_node::database::{SqlxType, ToSqlxType};
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};

Expand Down Expand Up @@ -91,12 +90,6 @@ impl<'de> Deserialize<'de> for OneTimeCode {
}
}

impl ToSqlxType for OneTimeCode {
fn to_sql(&self) -> SqlxType {
SqlxType::Text(self.to_string())
}
}

/// Create an Identity Error
fn error(message: String) -> Error {
Error::new(Origin::Identity, Kind::Invalid, message.as_str())
Expand Down
Loading

0 comments on commit 02e62f6

Please sign in to comment.