Skip to content

Commit 331256c

Browse files
authored
Kh add readonly for hostpath (#12)
* add test service account * minor change * add readonly for allowedHostPath in k8s version greater than 1.11
1 parent 0e95d56 commit 331256c

File tree

5 files changed

+126
-51
lines changed

5 files changed

+126
-51
lines changed

Gopkg.lock

Lines changed: 70 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

advisor/processor/generate.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Processor struct {
1919
resourceNamePrefix map[string]bool
2020
namespace string
2121
serviceAccountMap map[string]v1.ServiceAccount
22+
serverGitVersion string
2223
}
2324

2425
func NewProcessor(kubeconfig string) (*Processor, error) {
@@ -31,9 +32,16 @@ func NewProcessor(kubeconfig string) (*Processor, error) {
3132
return nil, err
3233
}
3334

35+
info, err := clientset.ServerVersion()
36+
37+
if err != nil {
38+
return nil, err
39+
}
40+
3441
return &Processor{
3542
k8sClient: clientset,
3643
resourceNamePrefix: map[string]bool{},
44+
serverGitVersion: info.GitVersion,
3745
}, nil
3846
}
3947

@@ -149,9 +157,12 @@ func (p *Processor) GeneratePSP(cssList []types.ContainerSecuritySpec, pssList [
149157
// set allowed host path
150158
hostPathList := utils.MapToArray(hostPaths)
151159

160+
readOnly, _ := utils.CompareVersion(p.serverGitVersion, types.Version1_11)
161+
152162
for _, path := range hostPathList {
153163
psp.Spec.AllowedHostPaths = append(psp.Spec.AllowedHostPaths, v1beta1.AllowedHostPath{
154164
PathPrefix: path,
165+
ReadOnly: readOnly,
155166
})
156167
}
157168

advisor/types/securityspec.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ var (
1919
}
2020
)
2121

22+
const (
23+
Version1_11 = "v1.11"
24+
)
25+
2226
//PodSecurityPolicy Recommendation System help in the following attributes:
2327
// 1. allowPrivilegeEscalation - done
2428
// 2. allowedCapabilities - done

utils/version.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package utils
2+
3+
import (
4+
"github.com/hashicorp/go-version"
5+
)
6+
7+
// CompareVersion compare two versions
8+
func CompareVersion(v1, v2 string) (bool, error) {
9+
version1, err := version.NewVersion(v1)
10+
11+
if err != nil {
12+
return false, err
13+
}
14+
15+
version2, err := version.NewVersion(v2)
16+
17+
if err != nil {
18+
return false, err
19+
}
20+
21+
return version1.GreaterThan(version2), nil
22+
}

0 commit comments

Comments
 (0)