Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

pkg/k8s: add support for initContainer #376

Merged
merged 1 commit into from
Nov 17, 2021
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: 2 additions & 0 deletions auditors/apparmor/apparmor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func TestAuditAppArmor(t *testing.T) {
}{
{"apparmor-enabled.yml", nil, true},
{"apparmor-annotation-missing.yml", []string{AppArmorAnnotationMissing}, true},
{"apparmor-annotation-init-container-enabled.yml", nil, true},
{"apparmor-annotation-init-container-missing.yml", []string{AppArmorAnnotationMissing}, true},
// These are invalid manifests so we should only test it in manifest mode as kubernetes will fail to apply it
{"apparmor-disabled.yml", []string{AppArmorDisabled}, false},
{"apparmor-invalid-annotation.yml", []string{AppArmorInvalidAnnotation}, false},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
namespace: apparmor-annotation-init-container-enabled
annotations:
container.apparmor.security.beta.kubernetes.io/container: localhost/someval
container.apparmor.security.beta.kubernetes.io/init-container: localhost/someval
spec:
initContainers:
- name: init-container
image: scratch
containers:
- name: container
image: scratch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: pod
namespace: apparmor-annotation-init-container-missing
annotations:
container.apparmor.security.beta.kubernetes.io/container: localhost/someval
spec:
initContainers:
- name: init-container
image: scratch
containers:
- name: container
image: scratch
21 changes: 19 additions & 2 deletions pkg/k8s/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,26 @@ func GetContainers(resource Resource) []*ContainerV1 {
return nil
}

containers := make([]*ContainerV1, len(podSpec.Containers))
var containers []*ContainerV1
for i := range podSpec.Containers {
containers[i] = &podSpec.Containers[i]
containers = append(containers, &podSpec.Containers[i])
}

if len(podSpec.InitContainers) > 0 {
containers = append(containers, GetInitContainers(resource)...)
}
return containers
}

func GetInitContainers(resource Resource) []*ContainerV1 {
podSpec := GetPodSpec(resource)
if podSpec == nil {
return nil
}

containers := make([]*ContainerV1, len(podSpec.InitContainers))
for i := range podSpec.InitContainers {
containers[i] = &podSpec.InitContainers[i]
}
return containers
}
Expand Down