@@ -29,65 +29,117 @@ check_apparmor_profile() {
29
29
30
30
# clean up the variance in the recorded apparmor profile
31
31
yq -i " .spec" $APPARMOR_PROFILE_FILE
32
+ local name=" $( grep -o ' \btest-recording_test-pod[^ ]*\b' $APPARMOR_PROFILE_FILE ) "
32
33
sed -i -e " s/\btest-recording_test-pod[^ ]*\b/test-sleep/g" $APPARMOR_PROFILE_FILE
33
34
34
35
diff $APPARMOR_REFERENCE_PROFILE_FILE $APPARMOR_PROFILE_FILE
36
+ echo " ${name} "
35
37
}
36
38
37
- record_apparmor_profile () {
38
- echo " Enable Apparmor profile"
39
- k patch spod spod --type=merge -p ' {"spec":{"enableAppArmor":true}}'
40
- k rollout status ds spod --timeout 360s
41
- k_wait spod spod
42
-
43
- echo " Recording apparmor profile"
44
-
45
- TMP_DIR=$( mktemp -d)
46
- trap ' rm -rf $TMP_DIR' EXIT
47
-
48
- echo " Creating profile recording"
49
- k apply -f $APPARMOR_RECORDING_FILE
50
-
51
- POD_FILE=" $TMP_DIR /pod.yml"
52
- cat << EOT >"$POD_FILE "
39
+ create_pod () {
40
+ local pod_name=" $1 "
41
+ local pod_file=" $2 "
42
+ local apparmor_profile=" ${3-} "
43
+ cat << EOT >"$pod_file "
53
44
---
54
45
apiVersion: v1
55
46
kind: Pod
56
47
metadata:
57
- name: $PODNAME
48
+ name: $pod_name
58
49
labels:
59
50
app: alpine
60
51
spec:
61
52
restartPolicy: Never
62
53
containers:
63
- - name: $PODNAME
54
+ - name: $pod_name
64
55
image: alpine:3
65
- command: ["sleep", "20 "]
56
+ command: ["sleep", "30 "]
66
57
EOT
67
- echo " Creating pod:"
68
- cat " $POD_FILE "
69
58
70
- k apply -f " $POD_FILE "
59
+ if [[ -n " $apparmor_profile " ]]; then
60
+ cat << EOT >>"$pod_file "
61
+ securityContext:
62
+ appArmorProfile:
63
+ type: Localhost
64
+ localhostProfile: $apparmor_profile
65
+ EOT
66
+ fi
67
+ cat " $pod_file "
68
+ k apply -f " $pod_file "
69
+ }
71
70
72
- echo " Waiting for pod to be completed"
71
+ wait_for_pod_status () {
72
+ local pod_name=" $1 "
73
+ local status=" $2 "
74
+ echo " Waiting for pod status: $status "
73
75
for (( i = 0 ; i < 10 ; i++ )) ; do
74
- if k get pods $PODNAME | grep -q Completed ; then
75
- echo " Pod completed "
76
+ if k get pods $pod_name | grep -q $status ; then
77
+ echo " Pod reached status: $status "
76
78
break
77
79
fi
78
80
echo " Still waiting ($i )"
79
81
sleep 5
80
82
done
83
+ }
81
84
82
- echo " Deleting pod"
83
- k delete -f " $POD_FILE "
85
+ check_profile_enforcement () {
86
+ local comamnd=" $1 "
87
+ local apparmor_profile=" $2 "
88
+ local pid=" $( pidof $comamnd ) "
89
+ local enforce=" $( cat /proc/${pid} /attr/current) "
90
+ local reference=" $apparmor_profile (enforce)"
91
+ if [[ " $reference " != " $enforce " ]]; then
92
+ echo " Apparmor profile $apparmor_profile not enforced: $enforce "
93
+ exit 1
94
+ fi
95
+ echo " Apparmor profile successfully enforced: $enforce "
96
+ }
84
97
85
- wait_for apparmorprofile $APPARMOR_PROFILE_NAME
98
+ record_apparmor_profile () {
99
+ echo " Enable Apparmor profile"
100
+ k patch spod spod --type=merge -p ' {"spec":{"enableAppArmor":true}}'
101
+ k rollout status ds spod --timeout 360s
102
+ k_wait spod spod
103
+
104
+ echo " Recording apparmor profile"
105
+ echo " --------------------------"
106
+
107
+ echo " Creating profile recording $RECORDING_NAME "
108
+ k apply -f $APPARMOR_RECORDING_FILE
86
109
87
- check_apparmor_profile
110
+ TMP_DIR=$( mktemp -d)
111
+ trap ' rm -rf $TMP_DIR' EXIT
88
112
89
- echo " Cleaning up profile $APPARMOR_PROFILE_NAME and recording $RECORDING_NAME resources"
113
+ echo " Creating pod $PODNAME and start recording its apparmor profile"
114
+ pod_file=" ${TMP_DIR} /${PODNAME} .yml"
115
+ create_pod $PODNAME $pod_file
116
+ wait_for_pod_status " $PODNAME " " Completed"
117
+ echo " Deleting pod $PODNAME "
118
+ k delete -f " $pod_file "
119
+
120
+ echo " Deleting profile recoridng $RECORDING_NAME "
90
121
k delete -f " $APPARMOR_RECORDING_FILE "
122
+
123
+ wait_for apparmorprofile $APPARMOR_PROFILE_NAME
124
+
125
+ echo " Verifing apparmor profile"
126
+ echo " -------------------------"
127
+
128
+ echo " Checking the recorded appamror profile matches the reference"
129
+ apparmor_profile=$( check_apparmor_profile)
130
+
131
+ echo " Creating pod $PODNAME with recorded profile in security context"
132
+ sec_pod_file=" ${TMP_DIR} /${PODNAME} -apparmor.yml"
133
+ create_pod $PODNAME $sec_pod_file $apparmor_profile
134
+ wait_for_pod_status " $PODNAME " " Running"
135
+
136
+ echo " Checking apparmor profile enforcement on container"
137
+ check_profile_enforcement " sleep" $apparmor_profile
138
+
139
+ echo " Deleting pod $PODNAME "
140
+ k delete -f " $sec_pod_file "
141
+
142
+ echo " Deleting apparmor profile $APPARMOR_PROFILE_NAME "
91
143
k delete apparmorprofile $APPARMOR_PROFILE_NAME
92
144
}
93
145
0 commit comments