Skip to content

Commit b364ebd

Browse files
authored
Kh add readonly for hostpath (#13)
* add test service account * minor change * fix read-only attribute for allowedHostPath
1 parent 331256c commit b364ebd

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

advisor/processor/generate.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/sysdiglabs/kube-psp-advisor/advisor/types"
99
"github.com/sysdiglabs/kube-psp-advisor/utils"
1010

11-
v1 "k8s.io/api/core/v1"
11+
"k8s.io/api/core/v1"
1212
"k8s.io/api/policy/v1beta1"
1313
"k8s.io/client-go/kubernetes"
1414
"k8s.io/client-go/tools/clientcmd"
@@ -22,6 +22,7 @@ type Processor struct {
2222
serverGitVersion string
2323
}
2424

25+
// NewProcessor returns a new processor
2526
func NewProcessor(kubeconfig string) (*Processor, error) {
2627
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
2728
if err != nil {
@@ -89,8 +90,12 @@ func (p *Processor) GeneratePSP(cssList []types.ContainerSecuritySpec, pssList [
8990
volumeTypes[t] = true
9091
}
9192

92-
for _, path := range sc.MountHostPaths {
93-
hostPaths[path] = true
93+
for path, readOnly := range sc.MountHostPaths {
94+
if _, exists := hostPaths[path]; !exists {
95+
hostPaths[path] = readOnly
96+
} else {
97+
hostPaths[path] = readOnly && hostPaths[path]
98+
}
9499
}
95100
}
96101

@@ -155,14 +160,12 @@ func (p *Processor) GeneratePSP(cssList []types.ContainerSecuritySpec, pssList [
155160
}
156161

157162
// set allowed host path
158-
hostPathList := utils.MapToArray(hostPaths)
159-
160-
readOnly, _ := utils.CompareVersion(p.serverGitVersion, types.Version1_11)
163+
enforceReadOnly, _ := utils.CompareVersion(p.serverGitVersion, types.Version1_11)
161164

162-
for _, path := range hostPathList {
165+
for path, readOnly := range hostPaths {
163166
psp.Spec.AllowedHostPaths = append(psp.Spec.AllowedHostPaths, v1beta1.AllowedHostPath{
164167
PathPrefix: path,
165-
ReadOnly: readOnly,
168+
ReadOnly: readOnly || enforceReadOnly,
166169
})
167170
}
168171

@@ -224,6 +227,7 @@ func (p *Processor) GeneratePSP(cssList []types.ContainerSecuritySpec, pssList [
224227
return psp
225228
}
226229

230+
// GenerateReport generate a JSON report
227231
func (p *Processor) GenerateReport(cssList []types.ContainerSecuritySpec, pssList []types.PodSecuritySpec) *report.Report {
228232
r := report.NewReport()
229233

advisor/processor/get.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func getSecuritySpec(metadata types.Metadata, namespace string, spec v1.PodSpec,
4242
Metadata: metadata,
4343
ContainerName: container.Name,
4444
ImageName: container.Image,
45+
PodName: metadata.Name,
4546
Namespace: namespace,
4647
HostName: spec.NodeName,
4748
Capabilities: getEffectiveCapablities(addCapList, dropCapList),
@@ -321,17 +322,30 @@ func getVolumeTypes(spec v1.PodSpec, sa v1.ServiceAccount) (volumeTypes []string
321322
return
322323
}
323324

324-
func getVolumeHostPaths(spec v1.PodSpec) (hostPaths []string) {
325+
func getVolumeHostPaths(spec v1.PodSpec) map[string]bool {
325326
hostPathMap := map[string]bool{}
326327

328+
containerMountMap := map[string]bool{}
329+
330+
for _, c := range spec.Containers {
331+
for _, vm := range c.VolumeMounts {
332+
if _, exists := containerMountMap[vm.Name]; !exists {
333+
containerMountMap[vm.Name] = vm.ReadOnly
334+
} else {
335+
containerMountMap[vm.Name] = containerMountMap[vm.Name] && vm.ReadOnly
336+
}
337+
}
338+
}
339+
327340
for _, v := range spec.Volumes {
328341
if v.HostPath != nil {
329-
hostPathMap[v.HostPath.Path] = true
342+
if _, exists := containerMountMap[v.Name]; exists {
343+
hostPathMap[v.HostPath.Path] = containerMountMap[v.Name]
344+
}
330345
}
331346
}
332347

333-
hostPaths = utils.MapToArray(hostPathMap)
334-
return
348+
return hostPathMap
335349
}
336350

337351
func getVolumeType(v v1.Volume) string {

advisor/types/securityspec.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ type ContainerSecuritySpec struct {
6060
}
6161

6262
type PodSecuritySpec struct {
63-
Metadata Metadata `json:"metadata"`
64-
Namespace string `json:"namespace"`
65-
HostPID bool `json:"hostPID,omitempty"`
66-
HostNetwork bool `json:"hostMetwork,omitempty"`
67-
HostIPC bool `json:"hostIPC,omitempty"`
68-
VolumeTypes []string `json:"volumeTypes,omitempty"`
69-
MountHostPaths []string `json:"mountedHostPath,omitempty"`
63+
Metadata Metadata `json:"metadata"`
64+
Namespace string `json:"namespace"`
65+
HostPID bool `json:"hostPID,omitempty"`
66+
HostNetwork bool `json:"hostMetwork,omitempty"`
67+
HostIPC bool `json:"hostIPC,omitempty"`
68+
VolumeTypes []string `json:"volumeTypes,omitempty"`
69+
MountHostPaths map[string]bool `json:"mountedHostPath,omitempty"`
7070
}
7171

7272
type Metadata struct {

test-yaml/base-busybox.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ spec:
1515
volumeMounts:
1616
- mountPath: /test-hostpath
1717
name: test-volume
18+
readOnly: true
1819
command:
1920
- sleep
2021
- "3600"
@@ -49,6 +50,7 @@ spec:
4950
volumeMounts:
5051
- mountPath: /test-hostpath
5152
name: test-volume
53+
readOnly: true
5254
command:
5355
- sleep
5456
- "3600"
@@ -140,6 +142,7 @@ spec:
140142
volumeMounts:
141143
- mountPath: /test-hostpath
142144
name: test-volume
145+
readOnly: true
143146
command:
144147
- sleep
145148
- "3600"

0 commit comments

Comments
 (0)