Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/configmap/pod-cm1.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/configmap/pod-cm2.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/configmap/pod-cm3.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/job/test-job.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/job/test-job2.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-exec.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-lifecycle.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-many-layers.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-one-container.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-spark.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/pod/pod-ubuntu.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/replica-set/replica2.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/stateful-set/web.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/stateful-set/web2.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod1.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod2.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod3.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod4.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod5.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod6.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook/webhook-pod7.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook2/webhook-pod8.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook2/webhook-pod9.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook3/dns-test.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/agent/samples/policy/yaml/webhook3/many-layers.yaml

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions src/agent/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ pub async fn is_allowed_copy_file(req: &protocols::agent::CopyFileRequest) -> tt
allow_request(&mut policy, "CopyFileRequest", &request).await
}

/// PolicyCreateContainerRequest is very similar to CreateContainerRequest from src/libs/protocols, except:
/// - It wraps the base CreateContainerRequest.
/// - It has an env_map field which is a map of environment variable names to expanded values.
/// This makes it easier to validate the environment variables inside the rego rules.
#[derive(Debug, serde::Serialize)]
struct PolicyCreateContainerRequest {
base: protocols::agent::CreateContainerRequest,
// a map of environment variable names to value
env_map: std::collections::BTreeMap<String, String>,
}

pub async fn is_allowed_create_container(
req: &protocols::agent::CreateContainerRequest,
) -> ttrpc::Result<()> {
let env_map = get_env_map(&req.OCI.Process.Env);

let create_container_request = PolicyCreateContainerRequest {
base: req.clone(),
env_map,
};

let request = serde_json::to_string(&create_container_request).unwrap();
let mut policy = AGENT_POLICY.lock().await;
allow_request(&mut policy, "CreateContainerRequest", &request).await
}

pub async fn do_set_policy(req: &protocols::agent::SetPolicyRequest) -> ttrpc::Result<()> {
let request = serde_json::to_string(req).unwrap();
let mut policy = AGENT_POLICY.lock().await;
Expand Down Expand Up @@ -331,3 +357,20 @@ pub fn check_policy_hash(policy: &str) -> Result<()> {

Ok(())
}

// todo: move to common crate shared with genpolicy
fn get_env_map(env: &[String]) -> std::collections::BTreeMap<String, String> {
let env_map: std::collections::BTreeMap<String, String> = env
.iter()
.filter_map(|v| {
// split by leftmost '='
let split = v.split_once('=');
if let Some((key, value)) = split {
Some((key.to_string(), value.to_string()))
} else {
None
}
})
.collect();
env_map
}
11 changes: 9 additions & 2 deletions src/agent/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use crate::trace_rpc_call;
use crate::tracer::extract_carrier_from_ttrpc;

#[cfg(feature = "agent-policy")]
use crate::policy::{do_set_policy, is_allowed, is_allowed_copy_file};
use crate::policy::{do_set_policy, is_allowed, is_allowed_copy_file, is_allowed_create_container};

use opentelemetry::global;
use tracing::span;
Expand Down Expand Up @@ -134,6 +134,13 @@ async fn is_allowed_copy_file(_req: &CopyFileRequest) -> ttrpc::Result<()> {
Ok(())
}

#[cfg(not(feature = "agent-policy"))]
async fn is_allowed_create_container(
_req: &protocols::agent::CreateContainerRequest,
) -> ttrpc::Result<()> {
Ok(())
}

fn same<E>(e: E) -> E {
e
}
Expand Down Expand Up @@ -641,7 +648,7 @@ impl agent_ttrpc::AgentService for AgentService {
req: protocols::agent::CreateContainerRequest,
) -> ttrpc::Result<Empty> {
trace_rpc_call!(ctx, "create_container", req);
is_allowed(&req).await?;
is_allowed_create_container(&req).await?;
self.do_create_container(req).await.map_ttrpc_err(same)?;
Ok(Empty::new())
}
Expand Down
2 changes: 2 additions & 0 deletions src/tools/genpolicy/genpolicy-settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@
"dns_label": "[a-zA-Z0-9_\\.\\-]+",
"s_source1": "^..2[0-9]{3}_[0-1][0-9]_[0-3][0-9]_[0-2][0-9]_[0-5][0-9]_[0-5][0-9]\\.[0-9]{1,10}$",
"s_source2": "^..data/",
"dns_subdomain": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",
"pod_uid": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$",
"default_caps": [
"CAP_CHOWN",
"CAP_DAC_OVERRIDE",
Expand Down
Loading
Loading