File tree Expand file tree Collapse file tree 5 files changed +108
-0
lines changed Expand file tree Collapse file tree 5 files changed +108
-0
lines changed Original file line number Diff line number Diff line change @@ -5,22 +5,28 @@ go 1.22.0
5
5
toolchain go1.22.5
6
6
7
7
require (
8
+ github.com/openshift/api v0.0.0-20240724184751-84047ef4a2ce
9
+ github.com/stretchr/testify v1.8.4
8
10
k8s.io/api v0.30.3
9
11
k8s.io/apimachinery v0.30.3
10
12
sigs.k8s.io/controller-runtime v0.18.4
13
+ sigs.k8s.io/yaml v1.3.0
11
14
)
12
15
13
16
require (
17
+ github.com/davecgh/go-spew v1.1.1 // indirect
14
18
github.com/go-logr/logr v1.4.1 // indirect
15
19
github.com/gogo/protobuf v1.3.2 // indirect
16
20
github.com/google/gofuzz v1.2.0 // indirect
17
21
github.com/json-iterator/go v1.1.12 // indirect
18
22
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
19
23
github.com/modern-go/reflect2 v1.0.2 // indirect
24
+ github.com/pmezard/go-difflib v1.0.0 // indirect
20
25
golang.org/x/net v0.23.0 // indirect
21
26
golang.org/x/text v0.14.0 // indirect
22
27
gopkg.in/inf.v0 v0.9.1 // indirect
23
28
gopkg.in/yaml.v2 v2.4.0 // indirect
29
+ gopkg.in/yaml.v3 v3.0.1 // indirect
24
30
k8s.io/klog/v2 v2.120.1 // indirect
25
31
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
26
32
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Original file line number Diff line number Diff line change @@ -32,6 +32,8 @@ github.com/onsi/ginkgo/v2 v2.17.1 h1:V++EzdbhI4ZV4ev0UTIj0PzhzOcReJFyJaLjtSF55M8
32
32
github.com/onsi/ginkgo/v2 v2.17.1 /go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs =
33
33
github.com/onsi/gomega v1.32.0 h1:JRYU78fJ1LPxlckP6Txi/EYqJvjtMrDC04/MM5XRHPk =
34
34
github.com/onsi/gomega v1.32.0 /go.mod h1:a4x4gW6Pz2yK1MAmvluYme5lvYTn61afQ2ETw/8n4Lg =
35
+ github.com/openshift/api v0.0.0-20240724184751-84047ef4a2ce h1:AR9XMlwc7akIN13KDx4L0tI04zHf8jEZ1z1RMRbz1J0 =
36
+ github.com/openshift/api v0.0.0-20240724184751-84047ef4a2ce /go.mod h1:OOh6Qopf21pSzqNVCB5gomomBXb8o5sGKZxG2KNpaXM =
35
37
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM =
36
38
github.com/pmezard/go-difflib v1.0.0 /go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4 =
37
39
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ =
Original file line number Diff line number Diff line change
1
+ package ocp
2
+
3
+ import (
4
+ _ "embed"
5
+ "fmt"
6
+ "strings"
7
+
8
+ secv1 "github.com/openshift/api/security/v1"
9
+ "sigs.k8s.io/yaml"
10
+ )
11
+
12
+ //go:embed scc.yaml
13
+ var sccYAMLTemplate string
14
+
15
+ // NewSecurityContextConstraints loads the embedded SCC YAML template, replaces the namespace,
16
+ // and returns it as a SecurityContextConstraints object
17
+ func NewSecurityContextConstraints (name string , namespace string ) (* secv1.SecurityContextConstraints , error ) {
18
+ scc := & secv1.SecurityContextConstraints {}
19
+ err := yaml .Unmarshal ([]byte (sccYAMLTemplate ), scc )
20
+ if err != nil {
21
+ return nil , fmt .Errorf ("error unmarshaling YAML: %v" , err )
22
+ }
23
+ scc .Name = name
24
+ scc .Namespace = namespace
25
+ var users []string
26
+ for _ , user := range scc .Users {
27
+ serviceAccount := strings .Split (user , ":" )
28
+ if len (serviceAccount ) != 4 {
29
+ return nil , fmt .Errorf ("invalid service account name" )
30
+ }
31
+ serviceAccount [2 ] = namespace
32
+ users = append (users , strings .Join (serviceAccount , ":" ))
33
+ }
34
+ scc .Users = users
35
+ return scc , nil
36
+ }
Original file line number Diff line number Diff line change
1
+ kind : SecurityContextConstraints
2
+ metadata :
3
+ name : csi-scc
4
+ namespace : ceph-csi-operator-system
5
+ allowHostDirVolumePlugin : true
6
+ allowHostIPC : true
7
+ allowHostNetwork : false
8
+ allowHostPID : true
9
+ allowHostPorts : true
10
+ allowPrivilegedContainer : true
11
+ allowedCapabilities :
12
+ - SYS_ADMIN
13
+ apiVersion : security.openshift.io/v1
14
+ defaultAddCapabilities : []
15
+ fsGroup :
16
+ type : RunAsAny
17
+ priority :
18
+ readOnlyRootFilesystem : false
19
+ requiredDropCapabilities :
20
+ - ALL
21
+ runAsUser :
22
+ type : RunAsAny
23
+ seLinuxContext :
24
+ type : RunAsAny
25
+ supplementalGroups :
26
+ type : RunAsAny
27
+ users :
28
+ - system:serviceaccount:ceph-csi-operator-system:csi-rbd-ctrlplugin-sa
29
+ - system:serviceaccount:ceph-csi-operator-system:csi-cephfs-ctrlplugin-sa
30
+ - system:serviceaccount:ceph-csi-operator-system:csi-nfs-ctrlplugin-sa
31
+ - system:serviceaccount:ceph-csi-operator-system:csi-rbd-nodeplugin-sa
32
+ - system:serviceaccount:ceph-csi-operator-system:csi-cephfs-nodeplugin-sa
33
+ - system:serviceaccount:ceph-csi-operator-system:csi-nfs-nodeplugin-sa
34
+ volumes :
35
+ - configMap
36
+ - emptyDir
37
+ - hostPath
38
+ - projected
Original file line number Diff line number Diff line change
1
+ package ocp
2
+
3
+ import (
4
+ "strings"
5
+ "testing"
6
+
7
+ "github.com/stretchr/testify/assert"
8
+ "github.com/stretchr/testify/require"
9
+ )
10
+
11
+ func TestNewSecurityContextConstraints (t * testing.T ) {
12
+ testNamespace := "test-namespace"
13
+ testName := "test"
14
+ scc , err := NewSecurityContextConstraints (testName , testNamespace )
15
+ require .NoError (t , err , "NewSecurityContextConstraints should not return an error" )
16
+ assert .NotNil (t , scc , "SCC should not be nil" )
17
+
18
+ assert .Equal (t , scc .Name , testName )
19
+ assert .NotEmpty (t , scc .Users , "Users should not be empty" )
20
+ for _ , user := range scc .Users {
21
+ assert .True (t , strings .Contains (user , testNamespace ),
22
+ "Each user should contain the specified namespace" )
23
+ assert .False (t , strings .Contains (user , "{{.Namespace}}" ),
24
+ "Template placeholders should be replaced" )
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments