Skip to content

Commit e4774ad

Browse files
authored
refactor: Do not require traits to implement Clone (#488)
Replace `Box<dyn Trait>` with `Arc<dyn Trait>` and remove unnecessary Clone and Debug requirements from backend and api traits.
1 parent ef7ef46 commit e4774ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+1059
-1278
lines changed

src/application_credential/backend.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,17 @@ pub mod error;
1616
pub mod sql;
1717

1818
use async_trait::async_trait;
19-
use dyn_clone::DynClone;
2019

2120
use crate::application_credential::ApplicationCredentialProviderError;
2221
use crate::application_credential::types::*;
23-
use crate::config::Config;
2422
use crate::keystone::ServiceState;
2523

2624
pub use sql::SqlBackend;
2725

2826
/// Application Credential backend driver interface.
27+
#[cfg_attr(test, mockall::automock)]
2928
#[async_trait]
30-
pub trait ApplicationCredentialBackend: DynClone + Send + Sync + std::fmt::Debug {
31-
/// Set config
32-
fn set_config(&mut self, config: Config);
33-
29+
pub trait ApplicationCredentialBackend: Send + Sync {
3430
/// Create a new application credential.
3531
async fn create_application_credential(
3632
&self,
@@ -52,5 +48,3 @@ pub trait ApplicationCredentialBackend: DynClone + Send + Sync + std::fmt::Debug
5248
params: &ApplicationCredentialListParameters,
5349
) -> Result<Vec<ApplicationCredential>, ApplicationCredentialProviderError>;
5450
}
55-
56-
dyn_clone::clone_trait_object!(ApplicationCredentialBackend);

src/application_credential/backend/sql.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,17 @@ use super::super::types::*;
1818
use crate::application_credential::{
1919
ApplicationCredentialProviderError, backend::ApplicationCredentialBackend,
2020
};
21-
use crate::config::Config;
2221
use crate::keystone::ServiceState;
2322

2423
mod application_credential;
2524

2625
/// SQL backend provider implementing the ApplicationCredentialBackend
2726
/// interface.
28-
#[derive(Clone, Debug, Default)]
29-
pub struct SqlBackend {
30-
/// Config.
31-
pub config: Config,
32-
}
27+
#[derive(Default)]
28+
pub struct SqlBackend {}
3329

3430
#[async_trait]
3531
impl ApplicationCredentialBackend for SqlBackend {
36-
/// Set config
37-
fn set_config(&mut self, config: Config) {
38-
self.config = config;
39-
}
40-
4132
/// Create a new application credential.
4233
#[tracing::instrument(level = "debug", skip(self, state))]
4334
async fn create_application_credential(

src/application_credential/backend/sql/application_credential/get.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ mod tests {
8181
"app_cred_id",
8282
Some(12345),
8383
)]])
84-
.append_query_results([vec![get_role_mock("role_id")]])
84+
.append_query_results([vec![get_role_mock("role_id", "foo")]])
8585
.append_query_results([vec![get_access_rule_mock("app_cred_rule_id", None)]])
8686
.into_connection();
8787

src/application_credential/backend/sql/application_credential/list.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ mod tests {
133133
role_id: "role_id2".into(),
134134
},
135135
]])
136-
.append_query_results([vec![get_role_mock("role_id1"), get_role_mock("role_id2")]])
136+
.append_query_results([vec![
137+
get_role_mock("role_id1", "foo"),
138+
get_role_mock("role_id2", "foo"),
139+
]])
137140
.append_query_results([vec![
138141
db_application_credential_access_rule::Model {
139142
application_credential_id: 1,

src/application_credential/mock.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,4 @@ mock! {
4747
params: &ApplicationCredentialListParameters,
4848
) -> Result<Vec<ApplicationCredential>, ApplicationCredentialProviderError>;
4949
}
50-
51-
impl Clone for ApplicationCredentialProvider {
52-
fn clone(&self) -> Self;
53-
}
5450
}

src/application_credential/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ use async_trait::async_trait;
116116
use base64::{Engine as _, engine::general_purpose};
117117
use rand::{Rng, rng};
118118
use secrecy::SecretString;
119+
use std::sync::Arc;
119120
use uuid::Uuid;
120121
use validator::Validate;
121122

@@ -138,31 +139,30 @@ mod mock;
138139
pub mod types;
139140

140141
/// Application Credential Provider.
141-
#[derive(Clone, Debug)]
142+
#[derive(Clone)]
142143
pub struct ApplicationCredentialProvider {
143-
backend_driver: Box<dyn ApplicationCredentialBackend>,
144+
backend_driver: Arc<dyn ApplicationCredentialBackend>,
144145
}
145146

146147
impl ApplicationCredentialProvider {
147148
pub fn new(
148149
config: &Config,
149150
plugin_manager: &PluginManager,
150151
) -> Result<Self, ApplicationCredentialProviderError> {
151-
let mut backend_driver = if let Some(driver) = plugin_manager
152+
let backend_driver = if let Some(driver) = plugin_manager
152153
.get_application_credential_backend(config.application_credential.driver.clone())
153154
{
154155
driver.clone()
155156
} else {
156157
match config.application_credential.driver.as_str() {
157-
"sql" => Box::new(SqlBackend::default()),
158+
"sql" => Arc::new(SqlBackend::default()),
158159
other => {
159160
return Err(ApplicationCredentialProviderError::UnsupportedDriver(
160161
other.to_string(),
161162
));
162163
}
163164
}
164165
};
165-
backend_driver.set_config(config.clone());
166166
Ok(Self { backend_driver })
167167
}
168168
}

src/application_credential/types.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,8 @@
1414
//! # Application credential related types
1515
mod access_rule;
1616
mod application_credential;
17-
18-
use async_trait::async_trait;
19-
20-
use crate::application_credential::error::ApplicationCredentialProviderError;
21-
use crate::keystone::ServiceState;
17+
mod provider_api;
2218

2319
pub use access_rule::*;
2420
pub use application_credential::*;
25-
26-
/// Application credentials API.
27-
#[async_trait]
28-
pub trait ApplicationCredentialApi: Send + Sync + Clone {
29-
/// Create a new application credential.
30-
async fn create_application_credential(
31-
&self,
32-
state: &ServiceState,
33-
rec: ApplicationCredentialCreate,
34-
) -> Result<ApplicationCredentialCreateResponse, ApplicationCredentialProviderError>;
35-
36-
/// Get a single application credential by ID.
37-
async fn get_application_credential<'a>(
38-
&self,
39-
state: &ServiceState,
40-
id: &'a str,
41-
) -> Result<Option<ApplicationCredential>, ApplicationCredentialProviderError>;
42-
43-
/// List application credentials.
44-
async fn list_application_credentials(
45-
&self,
46-
state: &ServiceState,
47-
params: &ApplicationCredentialListParameters,
48-
) -> Result<impl IntoIterator<Item = ApplicationCredential>, ApplicationCredentialProviderError>;
49-
}
21+
pub use provider_api::*;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
//
13+
// SPDX-License-Identifier: Apache-2.0
14+
15+
use async_trait::async_trait;
16+
17+
use super::application_credential::*;
18+
use crate::application_credential::error::ApplicationCredentialProviderError;
19+
use crate::keystone::ServiceState;
20+
21+
/// Application credentials API.
22+
#[async_trait]
23+
pub trait ApplicationCredentialApi: Send + Sync {
24+
/// Create a new application credential.
25+
async fn create_application_credential(
26+
&self,
27+
state: &ServiceState,
28+
rec: ApplicationCredentialCreate,
29+
) -> Result<ApplicationCredentialCreateResponse, ApplicationCredentialProviderError>;
30+
31+
/// Get a single application credential by ID.
32+
async fn get_application_credential<'a>(
33+
&self,
34+
state: &ServiceState,
35+
id: &'a str,
36+
) -> Result<Option<ApplicationCredential>, ApplicationCredentialProviderError>;
37+
38+
/// List application credentials.
39+
async fn list_application_credentials(
40+
&self,
41+
state: &ServiceState,
42+
params: &ApplicationCredentialListParameters,
43+
) -> Result<impl IntoIterator<Item = ApplicationCredential>, ApplicationCredentialProviderError>;
44+
}

src/assignment/backend.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,16 @@ pub mod error;
1616
pub mod sql;
1717

1818
use async_trait::async_trait;
19-
use dyn_clone::DynClone;
2019

2120
use crate::assignment::AssignmentProviderError;
22-
use crate::config::Config;
2321
use crate::keystone::ServiceState;
2422

2523
use crate::assignment::types::{assignment::*, role::*};
2624
pub use sql::SqlBackend;
2725

26+
#[cfg_attr(test, mockall::automock)]
2827
#[async_trait]
29-
pub trait AssignmentBackend: DynClone + Send + Sync + std::fmt::Debug {
30-
/// Set config
31-
fn set_config(&mut self, config: Config);
32-
28+
pub trait AssignmentBackend: Send + Sync {
3329
/// Check assignment grant.
3430
async fn check_grant(
3531
&self,
@@ -91,5 +87,3 @@ pub trait AssignmentBackend: DynClone + Send + Sync + std::fmt::Debug {
9187
params: &RoleListParameters,
9288
) -> Result<Vec<Role>, AssignmentProviderError>;
9389
}
94-
95-
dyn_clone::clone_trait_object!(AssignmentBackend);

src/assignment/backend/sql.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,17 @@ use std::collections::HashSet;
1818
use super::super::types::*;
1919
use crate::assignment::backend::RoleCreate;
2020
use crate::assignment::{AssignmentProviderError, backend::AssignmentBackend};
21-
use crate::config::Config;
2221
use crate::keystone::ServiceState;
2322

2423
pub(crate) mod assignment;
2524
pub(crate) mod implied_role;
2625
pub(crate) mod role;
2726

28-
#[derive(Clone, Debug, Default)]
29-
pub struct SqlBackend {
30-
pub config: Config,
31-
}
32-
33-
impl SqlBackend {}
27+
#[derive(Default)]
28+
pub struct SqlBackend {}
3429

3530
#[async_trait]
3631
impl AssignmentBackend for SqlBackend {
37-
/// Set config.
38-
fn set_config(&mut self, config: Config) {
39-
self.config = config;
40-
}
41-
4232
/// Check assignment grant.
4333
#[tracing::instrument(level = "info", skip(self, state))]
4434
async fn check_grant(
@@ -76,7 +66,7 @@ impl AssignmentBackend for SqlBackend {
7666
state: &ServiceState,
7767
id: &'a str,
7868
) -> Result<Option<Role>, AssignmentProviderError> {
79-
Ok(role::get(&self.config, &state.db, id).await?)
69+
Ok(role::get(&state.db, id).await?)
8070
}
8171

8272
/// Expand implied roles.
@@ -118,7 +108,7 @@ impl AssignmentBackend for SqlBackend {
118108
state: &ServiceState,
119109
params: &RoleListParameters,
120110
) -> Result<Vec<Role>, AssignmentProviderError> {
121-
Ok(role::list(&self.config, &state.db, params).await?)
111+
Ok(role::list(&state.db, params).await?)
122112
}
123113

124114
/// List role assignments.
@@ -128,7 +118,7 @@ impl AssignmentBackend for SqlBackend {
128118
state: &ServiceState,
129119
params: &RoleAssignmentListParameters,
130120
) -> Result<Vec<Assignment>, AssignmentProviderError> {
131-
Ok(assignment::list(&self.config, &state.db, params).await?)
121+
Ok(assignment::list(&state.db, params).await?)
132122
}
133123

134124
/// List role assignments for multiple actors/targets.
@@ -138,9 +128,6 @@ impl AssignmentBackend for SqlBackend {
138128
state: &ServiceState,
139129
params: &RoleAssignmentListForMultipleActorTargetParameters,
140130
) -> Result<Vec<Assignment>, AssignmentProviderError> {
141-
Ok(
142-
assignment::list_for_multiple_actors_and_targets(&self.config, &state.db, params)
143-
.await?,
144-
)
131+
Ok(assignment::list_for_multiple_actors_and_targets(&state.db, params).await?)
145132
}
146133
}

0 commit comments

Comments
 (0)