Skip to content

Commit 83db0f5

Browse files
authored
Merge pull request #130 from wa5i/v0.2-feature-sync-handler
Add sync_handler feature for v0.2.
2 parents 0339c60 + 9322de7 commit 83db0f5

File tree

15 files changed

+164
-60
lines changed

15 files changed

+164
-60
lines changed

.github/workflows/rust.yml

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
- ".github/workflows/website.yml"
99
- ".github/workflows/deploy-website.yml"
1010
pull_request:
11-
branches: [ "main" ]
11+
branches: [ "main", "v0.2" ]
1212
paths-ignore:
1313
- "docs/**"
1414
- ".github/workflows/website.yml"
@@ -35,6 +35,8 @@ jobs:
3535
run: ulimit -n 65535
3636
- name: Run tests
3737
run: cargo test --verbose
38+
- name: Run tests with sync_handler features
39+
run: cargo test --features sync_handler --verbose
3840
- name: debug with ssh tunnel
3941
if: ${{ failure() }}
4042
uses: wa5i/ssh-to-actions@main
@@ -77,6 +79,10 @@ jobs:
7779
run : |
7880
export LD_LIBRARY_PATH=${RUNNER_TEMP}/tongsuo/lib
7981
OPENSSL_DIR=${RUNNER_TEMP}/tongsuo RUSTFLAGS="-C link-args=-Wl,-rpath,${RUNNER_TEMP}/tongsuo/lib" cargo test --verbose --features crypto_adaptor_tongsuo --no-default-features
82+
- name: Run tests with sync_handler features
83+
run : |
84+
export LD_LIBRARY_PATH=${RUNNER_TEMP}/tongsuo/lib
85+
OPENSSL_DIR=${RUNNER_TEMP}/tongsuo RUSTFLAGS="-C link-args=-Wl,-rpath,${RUNNER_TEMP}/tongsuo/lib" cargo test --verbose --features crypto_adaptor_tongsuo --features sync_handler --no-default-features
8086
- name: debug with ssh tunnel
8187
if: ${{ failure() }}
8288
uses: wa5i/ssh-to-actions@main
@@ -136,6 +142,8 @@ jobs:
136142
run: cargo build --verbose
137143
- name: Run tests
138144
run: cargo test --verbose
145+
- name: Run tests with sync_handler features
146+
run: cargo test --features sync_handler --verbose
139147

140148
windows-mysql-test:
141149
strategy:

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ stretto = "0.8"
7878
itertools = "0.14"
7979
priority-queue = "2.1"
8080
crossbeam-channel = "0.5"
81+
maybe-async = { version = "0.2", optional = false }
8182

8283
# optional dependencies
8384
openssl = { version = "0.10.64", optional = true }
@@ -96,6 +97,7 @@ default = ["crypto_adaptor_openssl"]
9697
storage_mysql = ["diesel", "r2d2", "r2d2-diesel"]
9798
crypto_adaptor_openssl = ["dep:openssl", "dep:openssl-sys"]
9899
crypto_adaptor_tongsuo = ["dep:openssl", "dep:openssl-sys"]
100+
sync_handler = ["maybe-async/is_sync"]
99101

100102
[target.'cfg(unix)'.dependencies]
101103
daemonize = "0.5"

src/core.rs

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ impl Default for Core {
106106
}
107107
}
108108

109+
#[maybe_async::maybe_async]
109110
impl Core {
110111
pub fn config(&mut self, core: Arc<RwLock<Core>>, config: Option<&Config>) -> Result<(), RvError> {
111112
if let Some(conf) = config {
@@ -414,6 +415,7 @@ impl Core {
414415
Ok(())
415416
}
416417

418+
#[maybe_async::maybe_async]
417419
pub async fn handle_request(&self, req: &mut Request) -> Result<Option<Response>, RvError> {
418420
let mut resp = None;
419421
let mut err: Option<RvError> = None;

src/handler.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//! for instance.
88
99
use derive_more::Display;
10-
use async_trait::async_trait;
1110

1211
use crate::{
1312
core::Core,
@@ -16,7 +15,7 @@ use crate::{
1615
logical::{request::Request, response::Response, Auth},
1716
};
1817

19-
#[async_trait]
18+
#[maybe_async::maybe_async]
2019
pub trait Handler: Send + Sync {
2120
fn name(&self) -> String;
2221

@@ -41,7 +40,7 @@ pub trait Handler: Send + Sync {
4140
}
4241
}
4342

44-
#[async_trait]
43+
#[maybe_async::maybe_async]
4544
pub trait AuthHandler: Send + Sync {
4645
fn name(&self) -> String;
4746

src/http/logical.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ async fn logical_request_handler(
8181
r.operation = Operation::List;
8282
}
8383
}
84+
#[cfg(feature = "sync_handler")]
85+
let ret = core.read()?.handle_request(&mut r)?;
86+
#[cfg(not(feature = "sync_handler"))]
87+
let ret = core.read()?.handle_request(&mut r).await?;
8488

85-
match core.read()?.handle_request(&mut r).await? {
89+
match ret {
8690
Some(resp) => response_logical(&resp, &r.path),
8791
None => {
8892
if matches!(r.operation, Operation::Read | Operation::List) {

src/http/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ pub fn response_json_ok<T: Serialize>(cookie: Option<Cookie>, body: T) -> HttpRe
196196

197197
pub async fn handle_request(core: web::Data<Arc<RwLock<Core>>>, req: &mut Request) -> Result<HttpResponse, RvError> {
198198
let core = core.read()?;
199+
#[cfg(feature = "sync_handler")]
200+
let resp = core.handle_request(req)?;
201+
#[cfg(not(feature = "sync_handler"))]
199202
let resp = core.handle_request(req).await?;
200203
if resp.is_none() {
201204
Ok(response_ok(None, None))

src/modules/auth/token_store.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::{
99
time::{Duration, SystemTime, UNIX_EPOCH},
1010
};
1111

12-
use async_trait::async_trait;
1312
use better_default::Default;
1413
use humantime::parse_duration;
1514
use lazy_static::lazy_static;
@@ -721,7 +720,7 @@ impl TokenStore {
721720
}
722721
}
723722

724-
#[async_trait]
723+
#[maybe_async::maybe_async]
725724
impl Handler for TokenStore {
726725
fn name(&self) -> String {
727726
"auth_token".to_string()

src/modules/credential/approle/mod.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,19 @@ mod test {
216216
test_utils::{test_delete_api, test_mount_auth_api, test_read_api, test_rusty_vault_init, test_write_api},
217217
};
218218

219-
pub async fn test_read_role(core: &Core, token: &str, path: &str, role_name: &str) -> Result<Option<Response>, RvError> {
219+
#[maybe_async::maybe_async]
220+
pub async fn test_read_role(
221+
core: &Core,
222+
token: &str,
223+
path: &str,
224+
role_name: &str,
225+
) -> Result<Option<Response>, RvError> {
220226
let resp = test_read_api(core, token, format!("auth/{}/role/{}", path, role_name).as_str(), true).await;
221227
assert!(resp.is_ok());
222228
resp
223229
}
224230

231+
#[maybe_async::maybe_async]
225232
pub async fn test_write_role(
226233
core: &Core,
227234
token: &str,
@@ -251,10 +258,13 @@ mod test {
251258
test_write_api(core, token, format!("auth/{}/role/{}", path, role_name).as_str(), expect, Some(role_data)).await;
252259
}
253260

261+
#[maybe_async::maybe_async]
254262
pub async fn test_delete_role(core: &Core, token: &str, path: &str, role_name: &str) {
255-
assert!(test_delete_api(core, token, format!("auth/{}/role/{}", path, role_name).as_str(), true, None).await.is_ok());
263+
let resp = test_delete_api(core, token, format!("auth/{}/role/{}", path, role_name).as_str(), true, None).await;
264+
assert!(resp.is_ok());
256265
}
257266

267+
#[maybe_async::maybe_async]
258268
pub async fn generate_secret_id(core: &Core, token: &str, path: &str, role_name: &str) -> (String, String) {
259269
let resp =
260270
test_write_api(core, token, format!("auth/{}/role/{}/secret-id", path, role_name).as_str(), true, None).await;
@@ -266,6 +276,7 @@ mod test {
266276
(secret_id.to_string(), secret_id_accessor.to_string())
267277
}
268278

279+
#[maybe_async::maybe_async]
269280
pub async fn test_login(
270281
core: &Core,
271282
path: &str,
@@ -299,6 +310,7 @@ mod test {
299310
resp
300311
}
301312

313+
#[maybe_async::maybe_async]
302314
async fn test_approle(core: &Core, token: &str, path: &str, role_name: &str) {
303315
// Create a role
304316
let resp = test_write_api(core, token, format!("auth/{}/role/{}", path, role_name).as_str(), true, None).await;
@@ -446,6 +458,7 @@ mod test {
446458
let _ = test_login(core, path, role_id, &secret_id, false).await;
447459
}
448460

461+
#[maybe_async::maybe_async]
449462
async fn test_approle_role_service(core: &Core, token: &str, path: &str, role_name: &str) {
450463
// Create a role
451464
let mut data = json!({
@@ -533,7 +546,7 @@ mod test {
533546
println!("resp_data: {:?}", resp_data);
534547
}
535548

536-
#[tokio::test]
549+
#[maybe_async::test(feature = "sync_handler", async(all(not(feature = "sync_handler")), tokio::test))]
537550
async fn test_credential_approle_module() {
538551
let (root_token, core) = test_rusty_vault_init("test_approle_module");
539552
let core = core.read().unwrap();

0 commit comments

Comments
 (0)