91 lines
3.3 KiB
Diff
91 lines
3.3 KiB
Diff
From 32f9270f0cf4ef2ee9c1ababf66c24c7cf10bd17 Mon Sep 17 00:00:00 2001
|
|
From: xuraoqing <xuraoqing@huawei.com>
|
|
Date: Fri, 13 Sep 2024 17:21:47 +0800
|
|
Subject: [PATCH] use id when get policy
|
|
|
|
---
|
|
.../attestation-service/service/src/lib.rs | 10 +++++++++-
|
|
.../attestation-service/service/src/restapi/mod.rs | 11 +++++++++--
|
|
.../attestation/attestation-service/tests/src/lib.rs | 6 ++++++
|
|
3 files changed, 24 insertions(+), 3 deletions(-)
|
|
|
|
diff --git a/service/attestation/attestation-service/service/src/lib.rs b/service/attestation/attestation-service/service/src/lib.rs
|
|
index 31e6305..1c5c907 100644
|
|
--- a/service/attestation/attestation-service/service/src/lib.rs
|
|
+++ b/service/attestation/attestation-service/service/src/lib.rs
|
|
@@ -185,7 +185,7 @@ impl AttestationService {
|
|
.await
|
|
}
|
|
|
|
- pub async fn get_policy(&self,
|
|
+ pub async fn get_all_policy(&self,
|
|
policy_dir: &String,
|
|
) -> Result<String, PolicyEngineError> {
|
|
let engine = OPA::new(policy_dir).await;
|
|
@@ -203,6 +203,14 @@ impl AttestationService {
|
|
}
|
|
}
|
|
|
|
+ pub async fn get_policy(&self,
|
|
+ policy_dir: &String,
|
|
+ id: &String
|
|
+ ) -> Result<String, PolicyEngineError> {
|
|
+ let engine = OPA::new(policy_dir).await?;
|
|
+ Ok(engine.get_policy(id).await?)
|
|
+ }
|
|
+
|
|
pub async fn register_reference(&self,
|
|
ref_set: &String
|
|
) -> Result<(), RefOpError> {
|
|
diff --git a/service/attestation/attestation-service/service/src/restapi/mod.rs b/service/attestation/attestation-service/service/src/restapi/mod.rs
|
|
index f49d175..d47698a 100644
|
|
--- a/service/attestation/attestation-service/service/src/restapi/mod.rs
|
|
+++ b/service/attestation/attestation-service/service/src/restapi/mod.rs
|
|
@@ -122,13 +122,20 @@ pub async fn set_policy(
|
|
Ok(HttpResponse::Ok().body("set policy success"))
|
|
}
|
|
|
|
+#[derive(Deserialize, Serialize, Debug)]
|
|
+pub struct PolicyGetRequest {
|
|
+ policy_id: String,
|
|
+}
|
|
+
|
|
#[get("/policy")]
|
|
pub async fn get_policy(
|
|
- request: HttpRequest,
|
|
+ request: web::Json<PolicyGetRequest>,
|
|
service: web::Data<Arc<RwLock<AttestationService>>>,
|
|
) -> Result<HttpResponse> {
|
|
+ let request = request.0;
|
|
log::debug!("get policy request: {:?}", request);
|
|
+ let id = request.policy_id.clone();
|
|
let dir:String = String::from(DEFAULT_POLICY_DIR);
|
|
- let ret = service.read().await.get_policy(&dir).await?;
|
|
+ let ret = service.read().await.get_policy(&dir, &id.to_string()).await?;
|
|
Ok(HttpResponse::Ok().body(ret))
|
|
}
|
|
diff --git a/service/attestation/attestation-service/tests/src/lib.rs b/service/attestation/attestation-service/tests/src/lib.rs
|
|
index abd099f..b8adb1e 100644
|
|
--- a/service/attestation/attestation-service/tests/src/lib.rs
|
|
+++ b/service/attestation/attestation-service/tests/src/lib.rs
|
|
@@ -121,10 +121,16 @@ mod tests {
|
|
|
|
#[test]
|
|
fn api_get_policy() {
|
|
+ let request_body = json!({
|
|
+ "policy_id":"test_policy.rego"
|
|
+ }
|
|
+ );
|
|
let client: Client = Client::new();
|
|
let endpoint = "http://127.0.0.1:8080/policy";
|
|
let res = client
|
|
.get(endpoint)
|
|
+ .header("Content-Type", "application/json")
|
|
+ .body(request_body.to_string())
|
|
.send()
|
|
.unwrap();
|
|
assert_eq!(res.status(), reqwest::StatusCode::OK);
|
|
--
|
|
2.33.0
|
|
|