Skip to content

Commit ce86e37

Browse files
imlk0claude
andcommitted
feat(as/policy): switch to RegoVM suspendable host-await, drop spawn_blocking
Replace the synchronous regorus Extension + tokio::task::spawn_blocking evaluation path with RegoVM's suspendable execution model. Builtins that need async I/O (query_reference_value, query_artifact_server) now suspend the VM via __builtin_host_await and are resumed by the host once the future resolves, so policy evaluation no longer requires a blocking runtime thread. - Enable regorus 'rvm' and 'arc' features; add postcard transitive dep - Drop tokio/rt from policy-rvps (only tokio/time still needed) - Rewrite query_reference_value/query_artifact_server extensions as RegoVmHostAwaitFunction closures returning pinned futures - build_extensions() appends per-builtin host-await wrapper to policy source; dispatch() routes suspended VM arguments to resolvers - Gate REFERENCE_QUERY_TIMEOUT and tokio::time::timeout off wasm32 (?Send futures, no tokio time support) - Move is_valid_policy_id to module scope; fix tests for the new path Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Kun Lai <laikun@linux.alibaba.com>
1 parent 029f8e4 commit ce86e37

7 files changed

Lines changed: 590 additions & 173 deletions

File tree

‎.github/workflows/rust-check.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,5 @@ jobs:
252252
run: |
253253
RUSTUP_TOOLCHAIN=nightly-2025-07-07 cargo check -p eventlog --target wasm32-unknown-unknown
254254
RUSTUP_TOOLCHAIN=nightly-2025-07-07 cargo check -p reference-value-provider-service --target wasm32-unknown-unknown --no-default-features # no "fs" feature in wasm
255-
RUSTUP_TOOLCHAIN=nightly-2025-07-07 cargo check -p attestation-service --target wasm32-unknown-unknown --no-default-features --features tdx-dcap-rust
255+
RUSTUP_TOOLCHAIN=nightly-2025-07-07 cargo check -p attestation-service --target wasm32-unknown-unknown --no-default-features --features tdx-dcap-rust,policy-artifact-server,policy-rvps
256256
RUSTUP_TOOLCHAIN=nightly-2025-07-07 cargo check -p verifier --target wasm32-unknown-unknown --no-default-features --features tdx-dcap-rust

‎Cargo.lock‎

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ regorus = { version = "0.11.0", default-features = false, features = [
4949
"base64",
5050
"time",
5151
"std",
52+
"rvm",
53+
"arc",
5254
] }
5355
reqwest = { version = "0.12", default-features = false, features = ["charset", "http2", "rustls-tls-native-roots", "json"] }
5456
rstest = "0.18.1"

‎attestation-service/Cargo.toml‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ default = [
1818
# file, signer key/cert read by path. Turn off (`default-features = false`) for a
1919
# pure, fs-free library (e.g. wasm builds).
2020
fs = ["tokio/fs", "reference-value-provider-service/fs"]
21-
# Runtime bridge used by synchronous Regorus extensions to make bounded
22-
# asynchronous RVPS queries. Normal AS binaries enable this through `fs`.
23-
policy-rvps = ["tokio/rt", "tokio/time"]
21+
policy-rvps = ["tokio/time"]
2422
all-verifier = ["verifier/all-verifier"]
2523
# Like `all-verifier`, but the TDX verifier uses the dcap-qvl backend and SGX is
2624
# dropped (it would still link the DCAP shared library). See

‎attestation-service/src/policy_engine/opa/fs.rs‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::sync::Arc;
1313

1414
use crate::policy_engine::{EvaluationResult, PolicyDigest, PolicyEngine, PolicyError};
1515

16-
#[derive(Debug, Clone)]
16+
#[derive(Debug)]
1717
pub struct OPA {
1818
policy_dir_path: PathBuf,
1919

@@ -496,11 +496,4 @@ allow = query_reference_value("svn") == [7]
496496
let error = evaluate_unavailable_rvps(false).await;
497497
assert!(error.contains("RVPS backend unavailable"), "{error}");
498498
}
499-
500-
#[cfg(feature = "policy-rvps")]
501-
#[tokio::test]
502-
async fn query_extension_times_out() {
503-
let error = evaluate_unavailable_rvps(true).await;
504-
assert!(error.contains("timed out"), "{error}");
505-
}
506499
}

‎attestation-service/src/policy_engine/opa/in_memory.rs‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl OPAInMemory {
3333
pub fn with_raw_default_policy(
3434
raw_default_policy: &str,
3535
default_policy_id: &str,
36+
#[cfg_attr(not(feature = "policy-artifact-server"), allow(unused_variables))]
3637
artifact_server_address: &str,
3738
) -> Result<Self, PolicyError> {
3839
#[cfg(not(feature = "policy-artifact-server"))]
@@ -50,12 +51,6 @@ impl OPAInMemory {
5051
),
5152
})
5253
}
53-
54-
fn is_valid_policy_id(policy_id: &str) -> bool {
55-
policy_id
56-
.chars()
57-
.all(|c| c.is_alphanumeric() || c == '_' || c == '-')
58-
}
5954
}
6055

6156
#[cfg_attr(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown"), async_trait::async_trait(?Send))]
@@ -101,7 +96,7 @@ impl PolicyEngine for OPAInMemory {
10196
}
10297

10398
async fn set_policy(&self, policy_id: String, policy: String) -> Result<(), PolicyError> {
104-
if !Self::is_valid_policy_id(&policy_id) {
99+
if !super::is_valid_policy_id(&policy_id) {
105100
return Err(PolicyError::InvalidPolicyId);
106101
}
107102
let bytes = URL_SAFE_NO_PAD.decode(policy)?;
@@ -142,7 +137,7 @@ impl PolicyEngine for OPAInMemory {
142137
}
143138

144139
async fn delete_policy(&self, policy_id: String) -> Result<(), PolicyError> {
145-
if !Self::is_valid_policy_id(&policy_id) {
140+
if !super::is_valid_policy_id(&policy_id) {
146141
return Err(PolicyError::InvalidPolicyId);
147142
}
148143
if policy_id == "default" {
@@ -217,4 +212,37 @@ mod tests {
217212
.unwrap();
218213
assert!(res.rules_result.contains_key("allow"));
219214
}
215+
216+
#[cfg(feature = "policy-rvps")]
217+
#[tokio::test]
218+
async fn evaluate_with_host_await_reference_value() {
219+
use crate::rvps::test_resolver;
220+
let eng = OPAInMemory::with_raw_default_policy(
221+
RAW_ALLOW_POLICY,
222+
"test",
223+
DEFAULT_ARTIFACT_SERVER_ADDRESS,
224+
)
225+
.unwrap();
226+
let policy = r#"package policy
227+
import rego.v1
228+
allow if {
229+
input.x == query_reference_value("k")
230+
}
231+
"#;
232+
eng.set_policy("p".into(), URL_SAFE_NO_PAD.encode(policy))
233+
.await
234+
.unwrap();
235+
let rvps = test_resolver(std::collections::HashMap::from([(
236+
"k".to_string(),
237+
serde_json::json!(1),
238+
)]));
239+
let res = eng
240+
.evaluate(r#"{"x":1}"#, "p", vec!["allow".into()], rvps)
241+
.await
242+
.unwrap();
243+
assert_eq!(
244+
res.rules_result.get("allow"),
245+
Some(&serde_json::Value::Bool(true))
246+
);
247+
}
220248
}

0 commit comments

Comments
 (0)