Skip to content

Commit 6bf2c8c

Browse files
committed
policy: support policy document versioning
On genpolicy side, encode the policy docuemtn version being used. On the agent side, read the version from the policy document and allow request accordingly. This allows the agent to handle multiple policy document versions. Signed-off-by: Saul Paredes <saulparedes@microsoft.com>
1 parent 9db7002 commit 6bf2c8c

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/agent/policy/src/policy.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ pub struct AgentPolicy {
6363

6464
/// Regorus engine
6565
engine: regorus::Engine,
66+
67+
policy_document_version: PolicyDocumentVersion,
6668
}
6769

6870
#[derive(serde::Deserialize, Debug)]
@@ -127,6 +129,7 @@ impl AgentPolicy {
127129

128130
self.engine.add_policy_from_file(default_policy_file)?;
129131
self.update_allow_failures_flag().await?;
132+
self.update_policy_version().await?;
130133
Ok(())
131134
}
132135

@@ -149,6 +152,12 @@ impl AgentPolicy {
149152

150153
/// Ask regorus if an API call should be allowed or not.
151154
pub async fn allow_request(&mut self, ep: &str, ep_input: &str) -> Result<(bool, String)> {
155+
match self.policy_document_version {
156+
PolicyDocumentVersion::V1 => self.allow_request_default(ep, ep_input).await,
157+
}
158+
}
159+
160+
async fn allow_request_default(&mut self, ep: &str, ep_input: &str) -> Result<(bool, String)> {
152161
debug!(sl!(), "policy check: {ep}");
153162
self.log_request(ep, ep_input).await;
154163

@@ -266,6 +275,35 @@ impl AgentPolicy {
266275
};
267276
Ok(())
268277
}
278+
279+
async fn update_policy_version(&mut self) -> Result<()> {
280+
let query = format!("data.agent_policy.policy_data.version");
281+
self.engine.set_input_json("{}")?;
282+
283+
let results = self.engine.eval_query(query.clone(), false)?;
284+
285+
if results.result.len() < 1 || results.result[0].expressions.len() < 1 {
286+
warn!(
287+
sl!(),
288+
"policy: failed to parse policy version. Assuming default policy version"
289+
);
290+
return Ok(());
291+
}
292+
293+
self.policy_document_version =
294+
match serde_json::from_str(&results.result[0].expressions[0].value.to_string()) {
295+
Ok(v) => v,
296+
Err(e) => {
297+
warn!(
298+
sl!(),
299+
"policy: failed to parse policy version: {e}. Assuming default policy version"
300+
);
301+
PolicyDocumentVersion::default()
302+
}
303+
};
304+
305+
return Ok(());
306+
}
269307
}
270308

271309
pub fn check_policy_hash(policy: &str) -> Result<()> {
@@ -288,3 +326,9 @@ pub fn check_policy_hash(policy: &str) -> Result<()> {
288326

289327
Ok(())
290328
}
329+
330+
#[derive(Debug, serde::Serialize, serde::Deserialize, Default)]
331+
pub enum PolicyDocumentVersion {
332+
#[default]
333+
V1,
334+
}

src/tools/genpolicy/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ tonic = "0.9.2"
6565
tower = "0.4.13"
6666
[target.'cfg(target_os = "linux")'.dependencies]
6767
containerd-client = "0.4.0"
68-
69-
[dev-dependencies]
7068
kata-agent-policy = { path = "../../agent/policy" }
71-
slog = "2.5.2"
7269
# Kata Agent protocol.
7370
protocols = { path = "../../libs/protocols", features = ["with-serde"] }
71+
72+
[dev-dependencies]
73+
slog = "2.5.2"
74+

src/tools/genpolicy/src/policy.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ pub struct PolicyData {
7474
/// Settings read from genpolicy-settings.json, related directly to each
7575
/// kata agent endpoint, that get added to the output policy.
7676
pub request_defaults: RequestDefaults,
77+
78+
/// Policy Document version
79+
pub version: kata_agent_policy::policy::PolicyDocumentVersion,
7780
}
7881

7982
/// OCI Container spec. This struct is very similar to the Spec struct from
@@ -560,6 +563,7 @@ impl AgentPolicy {
560563
request_defaults: self.settings.request_defaults.clone(),
561564
common: self.settings.common.clone(),
562565
sandbox: self.settings.sandbox.clone(),
566+
version: kata_agent_policy::policy::PolicyDocumentVersion::V1,
563567
};
564568

565569
let json_data = serde_json::to_string_pretty(&policy_data).unwrap();

0 commit comments

Comments
 (0)