Skip to content

Commit

Permalink
refactor(rust): hide repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
etorreborre committed Nov 20, 2023
1 parent 4a9acb0 commit b10a06b
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 112 deletions.
62 changes: 55 additions & 7 deletions Cargo.lock

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

40 changes: 19 additions & 21 deletions implementations/rust/ockam/ockam_api/src/cli_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ use miette::Diagnostic;
use rand::random;
use thiserror::Error;

use ockam::identity::storage::{PurposeKeysRepository, PurposeKeysSqlxDatabase};
use ockam::identity::{
ChangeHistoryRepository, ChangeHistorySqlxDatabase, Identities, IdentityAttributesRepository,
IdentityAttributesSqlxDatabase,
};
use ockam::identity::storage::{PurposeKeysRepository, PurposeKeysSqlxDatabase};
use ockam::SqlxDatabase;
use ockam_abac::{PoliciesRepository, PolicySqlxDatabase};
use ockam_core::compat::sync::Arc;
use ockam_core::env::get_env_with_default;
use ockam_core::Error;
use ockam_node::Executor;
use ockam_vault::storage::{SecretsRepository, SecretsSqlxDatabase};
pub use projects_repository::*;
pub use projects_repository_sql::*;
pub use spaces_repository::*;
Expand Down Expand Up @@ -42,9 +42,11 @@ pub mod credentials;
pub mod enrollments;
pub mod identities;
pub mod nodes;
pub mod policies;
pub mod projects;
pub mod projects_repository;
pub mod projects_repository_sql;
pub mod secure_channels;
pub mod spaces;
pub mod spaces_repository;
pub mod spaces_repository_sql;
Expand Down Expand Up @@ -74,8 +76,8 @@ pub enum CliStateError {

#[error("A {resource} named {name} already exists")]
#[diagnostic(
code("OCK409"),
help("Please try using a different name or delete the existing {resource}")
code("OCK409"),
help("Please try using a different name or delete the existing {resource}")
)]
AlreadyExists { resource: String, name: String },

Expand All @@ -101,8 +103,8 @@ pub enum CliStateError {

#[error("Invalid configuration version '{0}'")]
#[diagnostic(
code("OCK500"),
help("Please try running 'ockam reset' to reset your local configuration")
code("OCK500"),
help("Please try running 'ockam reset' to reset your local configuration")
)]
InvalidVersion(String),
}
Expand All @@ -117,7 +119,7 @@ impl From<CliStateError> for ockam_core::Error {
fn from(e: CliStateError) -> Self {
match e {
CliStateError::Ockam(e) => e,
_ => ockam_core::Error::new(
_ => Error::new(
ockam_core::errcode::Origin::Application,
ockam_core::errcode::Kind::Internal,
e,
Expand Down Expand Up @@ -156,27 +158,23 @@ impl CliState {
self.dir.clone()
}

pub async fn change_history_repository(&self) -> Result<Arc<dyn ChangeHistoryRepository>> {
async fn change_history_repository(&self) -> Result<Arc<dyn ChangeHistoryRepository>> {
Ok(Arc::new(ChangeHistorySqlxDatabase::new(self.database())))
}

pub async fn secrets_repository(&self) -> Result<Arc<dyn SecretsRepository>> {
Ok(Arc::new(SecretsSqlxDatabase::new(self.database())))
}

pub async fn identity_attributes_repository(
async fn identity_attributes_repository(
&self,
) -> Result<Arc<dyn IdentityAttributesRepository>> {
Ok(Arc::new(IdentityAttributesSqlxDatabase::new(
self.database(),
)))
}

pub async fn identities_repository(&self) -> Result<Arc<dyn IdentitiesRepository>> {
async fn identities_repository(&self) -> Result<Arc<dyn IdentitiesRepository>> {
Ok(Arc::new(IdentitiesSqlxDatabase::new(self.database())))
}

pub async fn purpose_keys_repository(&self) -> Result<Arc<dyn PurposeKeysRepository>> {
async fn purpose_keys_repository(&self) -> Result<Arc<dyn PurposeKeysRepository>> {
Ok(Arc::new(PurposeKeysSqlxDatabase::new(self.database())))
}

Expand All @@ -192,27 +190,27 @@ impl CliState {
Ok(Arc::new(NodesSqlxDatabase::new(self.database())))
}

pub async fn policies_repository(&self) -> Result<Arc<dyn PoliciesRepository>> {
pub(super) async fn policies_repository(&self) -> Result<Arc<dyn PoliciesRepository>> {
Ok(Arc::new(PolicySqlxDatabase::new(self.database())))
}

pub async fn projects_repository(&self) -> Result<Arc<dyn ProjectsRepository>> {
async fn projects_repository(&self) -> Result<Arc<dyn ProjectsRepository>> {
Ok(Arc::new(ProjectsSqlxDatabase::new(self.database())))
}

pub async fn spaces_repository(&self) -> Result<Arc<dyn SpacesRepository>> {
async fn spaces_repository(&self) -> Result<Arc<dyn SpacesRepository>> {
Ok(Arc::new(SpacesSqlxDatabase::new(self.database())))
}

pub async fn users_repository(&self) -> Result<Arc<dyn UsersRepository>> {
async fn users_repository(&self) -> Result<Arc<dyn UsersRepository>> {
Ok(Arc::new(UsersSqlxDatabase::new(self.database())))
}

pub async fn credentials_repository(&self) -> Result<Arc<dyn CredentialsRepository>> {
async fn credentials_repository(&self) -> Result<Arc<dyn CredentialsRepository>> {
Ok(Arc::new(CredentialsSqlxDatabase::new(self.database())))
}

pub async fn trust_contexts_repository(&self) -> Result<Arc<dyn TrustContextsRepository>> {
async fn trust_contexts_repository(&self) -> Result<Arc<dyn TrustContextsRepository>> {
Ok(Arc::new(TrustContextsSqlxDatabase::new(self.database())))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::sync::Arc;

use ockam::identity::{
ChangeHistoryRepository, IdentityAttributesRepository, SecureChannelRegistry, SecureChannels,
Vault,
ChangeHistoryRepository, IdentityAttributesRepository, SecureChannels, Vault,
};

use crate::bootstrapped_identities_store::{
Expand Down Expand Up @@ -46,20 +45,4 @@ impl CliState {
.build();
Ok(secure_channels)
}

/// Build a SecureChannels struct for a specific vault if one is specified
/// and reuse the secure_channel_registry
pub(crate) async fn build_secure_channels(
&self,
vault_name: &str,
secure_channel_registry: SecureChannelRegistry,
) -> Result<Arc<SecureChannels>> {
let vault = self.get_vault_by_name(vault_name).await?;
Ok(SecureChannels::builder()
.with_vault(vault)
.with_change_history_repository(self.change_history_repository().await?)
.with_secure_channels_registry(secure_channel_registry)
.with_purpose_keys_repository(self.purpose_keys_repository().await?)
.build())
}
}
Loading

0 comments on commit b10a06b

Please sign in to comment.