|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +. namespace |
| 4 | +NRO_NAMESPACE=$(nro_namespace) |
| 5 | + |
| 6 | +SELINUX_INFO_DIR="/must-gather/selinux_info" |
| 7 | +mkdir -p "$SELINUX_INFO_DIR" |
| 8 | + |
| 9 | +# common variables |
| 10 | +OC_GET_PODS="oc get pods -n $NRO_NAMESPACE" |
| 11 | + |
| 12 | +function replace_dot_with_underscore() { |
| 13 | + echo $1 | sed 's/\./_/g' |
| 14 | +} |
| 15 | + |
| 16 | +function gather_selinux_data() { |
| 17 | + local rte_pods=$($OC_GET_PODS -l name='resource-topology' -o jsonpath='{.items[*].metadata.name}') |
| 18 | + |
| 19 | + for pod in $rte_pods; do |
| 20 | + local original_node_name=$($OC_GET_PODS $pod -o jsonpath='{.spec.nodeName}') |
| 21 | + local node_name=$(replace_dot_with_underscore $original_node_name) |
| 22 | + |
| 23 | + echo "Gathering SELinux data from node: $node_name" |
| 24 | + |
| 25 | + local node_dir="$SELINUX_INFO_DIR/$node_name" |
| 26 | + mkdir -p "$node_dir" |
| 27 | + |
| 28 | + # Use oc debug node to collect all data in one session |
| 29 | + oc debug node/$original_node_name -- bash -c " |
| 30 | + chroot /host bash -c ' |
| 31 | + echo \"=== SELinux context for /var/lib/kubelet ===\" > /tmp/contexts; |
| 32 | + ls -Z /var/lib/kubelet >> /tmp/contexts 2>&1; |
| 33 | + echo \"\" >> /tmp/contexts; |
| 34 | + echo \"=== SELinux context for kubelet.sock ===\" >> /tmp/contexts; |
| 35 | + ls -Z /var/lib/kubelet/pod-resources/kubelet.sock >> /tmp/contexts 2>&1; |
| 36 | + |
| 37 | + echo \"=== Kubelet service file ===\" > /tmp/kubelet.service; |
| 38 | + cat /etc/systemd/system/kubelet.service >> /tmp/kubelet.service 2>&1; |
| 39 | + |
| 40 | + echo \"=== SELinux audit logs ===\" > /tmp/audit_selinux.log; |
| 41 | + cat /var/log/audit/audit.log | grep -i selinux >> /tmp/audit_selinux.log 2>&1; |
| 42 | + |
| 43 | + echo \"=== Pod-resources related audit logs ===\" > /tmp/audit_podresources.log; |
| 44 | + cat /var/log/audit/audit.log | grep \"kubelet.*pod-resources\" >> /tmp/audit_podresources.log 2>&1; |
| 45 | + |
| 46 | + cat /tmp/contexts; |
| 47 | + echo \"__SEPARATOR_KUBELET_SERVICE__\"; |
| 48 | + cat /tmp/kubelet.service; |
| 49 | + echo \"__SEPARATOR_AUDIT_SELINUX__\"; |
| 50 | + cat /tmp/audit_selinux.log; |
| 51 | + echo \"__SEPARATOR_AUDIT_PODRESOURCES__\"; |
| 52 | + cat /tmp/audit_podresources.log; |
| 53 | + ' |
| 54 | + " 2>/dev/null | { |
| 55 | + # Read and separate the output |
| 56 | + contexts_section="" |
| 57 | + kubelet_service="" |
| 58 | + audit_selinux="" |
| 59 | + audit_podresources="" |
| 60 | + current_section="contexts" |
| 61 | + |
| 62 | + while IFS= read -r line; do |
| 63 | + case "$line" in |
| 64 | + "__SEPARATOR_KUBELET_SERVICE__") |
| 65 | + current_section="kubelet_service" |
| 66 | + ;; |
| 67 | + "__SEPARATOR_AUDIT_SELINUX__") |
| 68 | + current_section="audit_selinux" |
| 69 | + ;; |
| 70 | + "__SEPARATOR_AUDIT_PODRESOURCES__") |
| 71 | + current_section="audit_podresources" |
| 72 | + ;; |
| 73 | + *) |
| 74 | + case "$current_section" in |
| 75 | + "contexts") |
| 76 | + contexts_section="$contexts_section$line\n" |
| 77 | + ;; |
| 78 | + "kubelet_service") |
| 79 | + kubelet_service="$kubelet_service$line\n" |
| 80 | + ;; |
| 81 | + "audit_selinux") |
| 82 | + audit_selinux="$audit_selinux$line\n" |
| 83 | + ;; |
| 84 | + "audit_podresources") |
| 85 | + audit_podresources="$audit_podresources$line\n" |
| 86 | + ;; |
| 87 | + esac |
| 88 | + ;; |
| 89 | + esac |
| 90 | + done |
| 91 | + |
| 92 | + # Write to separate files |
| 93 | + echo -e "$contexts_section" > "$node_dir/contexts" |
| 94 | + echo -e "$kubelet_service" > "$node_dir/kubelet.service" |
| 95 | + echo -e "$audit_selinux" > "$node_dir/audit_selinux.log" |
| 96 | + echo -e "$audit_podresources" > "$node_dir/audit_podresources.log" |
| 97 | + } |
| 98 | + |
| 99 | + if [ $? -eq 0 ]; then |
| 100 | + echo "Successfully collected SELinux data from node: $node_name" |
| 101 | + else |
| 102 | + echo "Failed to collect SELinux data from node: $node_name" |
| 103 | + fi |
| 104 | + done |
| 105 | +} |
| 106 | + |
| 107 | +if [ -z "${NRO_NAMESPACE}" ]; then |
| 108 | + echo "NUMAResources Operator namespace not detected. Skipping SELinux data gathering" |
| 109 | +else |
| 110 | + gather_selinux_data |
| 111 | +fi |
0 commit comments