Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

genpolicy: Support fsGroup #256

Draft
wants to merge 1 commit into
base: msft-main
Choose a base branch
from
Draft
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
12 changes: 12 additions & 0 deletions src/tools/genpolicy/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/tools/genpolicy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ clap = { version = "4.1.8", features = ["derive"] }
# YAML file serialization/deserialization.
base64 = "0.21.0"
serde = { version = "1.0.159", features = ["derive"] }
serde_repr = "0.1"

# Newer serde_yaml versions are using unsafe-libyaml instead of yaml-rust,
# and incorrectly change on serialization:
Expand Down
17 changes: 13 additions & 4 deletions src/tools/genpolicy/src/agent.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use serde_repr::*;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Storage {
Expand All @@ -8,11 +9,19 @@ pub struct Storage {
pub fstype: String,
pub options: Vec<String>,
pub mount_point: String,
pub fs_group: Option<SerializedFsGroup>,
pub fs_group: Option<FSGroup>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SerializedFsGroup {
pub group_id: u32,
pub group_change_policy: u32,
pub struct FSGroup {
pub group_id: i64,
pub group_change_policy: FSGroupChangePolicy,
}

#[derive(Clone, Debug, Default, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum FSGroupChangePolicy {
#[default]
Always = 0,
OnRootMismatch = 1,
}
1 change: 1 addition & 0 deletions src/tools/genpolicy/src/cronjob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl yaml::K8sResource for CronJob {
container,
settings,
volumes,
None,
);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tools/genpolicy/src/daemon_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ impl yaml::K8sResource for DaemonSet {
container,
settings,
volumes,
None,
)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tools/genpolicy/src/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ impl yaml::K8sResource for Deployment {
container,
settings,
volumes,
None,
);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tools/genpolicy/src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl yaml::K8sResource for Job {
container,
settings,
volumes,
None,
);
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/tools/genpolicy/src/mount_and_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub fn get_mount_and_storage(
persistent_volume_claims: &[pvc::PersistentVolumeClaim],
yaml_volume: &volume::Volume,
yaml_mount: &pod::VolumeMount,
pod_fs_group: Option<i64>,
) {
let propagation = match &yaml_mount.mountPropagation {
Some(p) if p == "Bidirectional" => "rshared",
Expand Down Expand Up @@ -171,6 +172,7 @@ pub fn get_mount_and_storage(
storages,
persistent_volume_claims,
mount_options,
pod_fs_group,
);
} else if yaml_volume.azureFile.is_some() {
get_shared_bind_mount(yaml_mount, p_mounts, mount_options);
Expand All @@ -191,6 +193,7 @@ pub fn get_mount_and_storage(
p_mounts,
storages,
mount_options,
pod_fs_group,
);
} else {
todo!("Unsupported volume type {:?}", yaml_volume);
Expand Down Expand Up @@ -258,6 +261,7 @@ fn get_persistent_volume_claim_mount(
storages: &mut Vec<agent::Storage>,
persistent_volume_claims: &[pvc::PersistentVolumeClaim],
mount_options: (&str, &str),
pod_fs_group: Option<i64>,
) {
let volume_pvc = yaml_volume.persistentVolumeClaim.as_ref().unwrap();
let pvc_name = &volume_pvc.claimName;
Expand Down Expand Up @@ -292,6 +296,7 @@ fn get_persistent_volume_claim_mount(
storages,
mount_options,
smb_mount_options,
pod_fs_group,
);
}

Expand Down Expand Up @@ -457,6 +462,7 @@ fn get_ephemeral_mount(
p_mounts: &mut Vec<policy::KataMount>,
storages: &mut Vec<agent::Storage>,
mount_options: (&str, &str),
pod_fs_group: Option<i64>,
) {
let storage_class = yaml_volume
.ephemeral
Expand All @@ -477,6 +483,7 @@ fn get_ephemeral_mount(
storages,
mount_options,
smb_mount_options,
pod_fs_group,
);
}

Expand All @@ -487,19 +494,29 @@ pub fn handle_persistent_volume_claim(
p_mounts: &mut Vec<policy::KataMount>,
storages: &mut Vec<agent::Storage>,
mount_options: (&str, &str),
smb_mount_options: Option<Vec<String>>, // Pass SMB mount options
smb_mount_options: Option<Vec<String>>,
pod_fs_group: Option<i64>,
) {
if is_blk_mount || is_smb_mount {
let source = "$(spath)/$(b64-direct-vol-path)".to_string();

let fs_group = if is_blk_mount {
pod_fs_group.map(|group_id| agent::FSGroup {
group_id,
group_change_policy: Default::default(),
})
} else {
None
};

storages.push(agent::Storage {
driver: if is_blk_mount {
"blk".to_string()
} else {
"smb".to_string()
},
driver_options: Vec::new(),
fs_group: None,
fs_group: fs_group,
source: "$(direct-vol-path)".to_string(),
mount_point: source.to_string(),
fstype: "$(fs-type)".to_string(),
Expand Down
11 changes: 11 additions & 0 deletions src/tools/genpolicy/src/pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ struct SecurityContext {
#[serde(skip_serializing_if = "Option::is_none")]
runAsUser: Option<i64>,

#[serde(skip_serializing_if = "Option::is_none")]
fsGroup: Option<i64>,

#[serde(skip_serializing_if = "Option::is_none")]
seccompProfile: Option<SeccompProfile>,
}
Expand All @@ -313,6 +316,9 @@ struct SeccompProfile {
pub struct PodSecurityContext {
#[serde(skip_serializing_if = "Option::is_none")]
pub runAsUser: Option<i64>,

#[serde(skip_serializing_if = "Option::is_none")]
pub fsGroup: Option<i64>,
// TODO: additional fields.
}

Expand Down Expand Up @@ -844,6 +850,7 @@ impl yaml::K8sResource for Pod {
container,
settings,
volumes,
self.spec.securityContext.as_ref().and_then(|sctx| sctx.fsGroup),
);
}
}
Expand Down Expand Up @@ -935,6 +942,9 @@ impl Container {
if let Some(uid) = context.runAsUser {
process.User.UID = uid.try_into().unwrap();
}
if let Some(group_id) = context.fsGroup {
process.User.AdditionalGids.push(group_id.try_into().unwrap());
}
if let Some(allow) = context.allowPrivilegeEscalation {
process.NoNewPrivileges = !allow
}
Expand Down Expand Up @@ -983,6 +993,7 @@ pub async fn add_pause_container(containers: &mut Vec<Container>, config: &Confi
privileged: None,
capabilities: None,
runAsUser: None,
fsGroup: None,
seccompProfile: None,
}),
..Default::default()
Expand Down
1 change: 1 addition & 0 deletions src/tools/genpolicy/src/replica_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl yaml::K8sResource for ReplicaSet {
container,
settings,
volumes,
None,
);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tools/genpolicy/src/replication_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ impl yaml::K8sResource for ReplicationController {
container,
settings,
volumes,
None,
);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/tools/genpolicy/src/stateful_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl yaml::K8sResource for StatefulSet {
container,
settings,
volumes,
None,
);
}

Expand Down Expand Up @@ -156,6 +157,7 @@ impl yaml::K8sResource for StatefulSet {
settings,
volume_mounts,
claims,
None,
);
}
}
Expand Down Expand Up @@ -206,6 +208,7 @@ impl StatefulSet {
settings: &settings::Settings,
volume_mounts: &Vec<pod::VolumeMount>,
claims: &[pvc::PersistentVolumeClaim],
pod_fs_group: Option<i64>,
) {
debug!("StatefulSet::get_mounts_and_storages");
for mount in volume_mounts {
Expand Down Expand Up @@ -236,6 +239,7 @@ impl StatefulSet {
storages,
mount_options,
smb_mount_options,
pod_fs_group,
);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/tools/genpolicy/src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ pub fn get_container_mounts_and_storages(
container: &pod::Container,
settings: &settings::Settings,
volumes: &Vec<volume::Volume>,
pod_fs_group: Option<i64>,
) {
if let Some(volume_mounts) = &container.volumeMounts {
for volume in volumes {
Expand All @@ -269,6 +270,7 @@ pub fn get_container_mounts_and_storages(
persistent_volume_claims,
volume,
volume_mount,
pod_fs_group,
);
}
}
Expand Down Expand Up @@ -348,5 +350,8 @@ pub fn get_process_fields(
if let Some(uid) = context.runAsUser {
process.User.UID = uid.try_into().unwrap();
}
if let Some(group_id) = context.fsGroup {
process.User.AdditionalGids.push(group_id.try_into().unwrap());
}
}
}
Loading