Skip to content

Commit 1fc56db

Browse files
committed
Refactor catsrc pod creation to use security package
Signed-off-by: perdasilva <[email protected]>
1 parent f8737d1 commit 1fc56db

File tree

5 files changed

+35
-44
lines changed

5 files changed

+35
-44
lines changed

Diff for: pkg/controller/bundle/bundle_unpacker.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strings"
88
"time"
99

10-
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/security"
1110
"github.com/operator-framework/operator-registry/pkg/api"
1211
"github.com/operator-framework/operator-registry/pkg/configmap"
1312
"github.com/sirupsen/logrus"
@@ -29,6 +28,7 @@ import (
2928
listersoperatorsv1alpha1 "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/listers/operators/v1alpha1"
3029
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
3130
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/resolver/projection"
31+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/security"
3232
)
3333

3434
const (

Diff for: pkg/controller/bundle/bundle_unpacker_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ func TestConfigMapUnpacker(t *testing.T) {
7272
var expectedAllowPrivilegeEscalation = false
7373
var expectedRunAsNonRoot = true
7474
var expectedRunAsUser int64 = 1001
75+
var expectedPrivileged = false
7576

7677
var expectedContainerSecurityContext = &corev1.SecurityContext{
78+
Privileged: &expectedPrivileged,
7779
ReadOnlyRootFilesystem: &expectedReadOnlyRootFilesystem,
7880
AllowPrivilegeEscalation: &expectedAllowPrivilegeEscalation,
7981
Capabilities: &corev1.Capabilities{

Diff for: pkg/controller/registry/reconciler/reconciler.go

+4-22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"k8s.io/apimachinery/pkg/util/rand"
1313

1414
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
15+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/security"
1516
controllerclient "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/controller-runtime/client"
1617
hashutil "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/kubernetes/pkg/util/hash"
1718
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient"
@@ -113,14 +114,6 @@ func Pod(source *operatorsv1alpha1.CatalogSource, name string, image string, saN
113114
pullPolicy = corev1.PullAlways
114115
}
115116

116-
// Security context
117-
readOnlyRootFilesystem := false
118-
allowPrivilegeEscalation := false
119-
runAsNonRoot := true
120-
121-
// See: https://github.com/operator-framework/operator-registry/blob/master/Dockerfile#L27
122-
runAsUser := int64(1001)
123-
124117
pod := &corev1.Pod{
125118
ObjectMeta: metav1.ObjectMeta{
126119
GenerateName: source.GetName() + "-",
@@ -172,31 +165,20 @@ func Pod(source *operatorsv1alpha1.CatalogSource, name string, image string, saN
172165
corev1.ResourceMemory: resource.MustParse("50Mi"),
173166
},
174167
},
175-
SecurityContext: &corev1.SecurityContext{
176-
ReadOnlyRootFilesystem: &readOnlyRootFilesystem,
177-
AllowPrivilegeEscalation: &allowPrivilegeEscalation,
178-
Capabilities: &corev1.Capabilities{
179-
Drop: []corev1.Capability{"ALL"},
180-
},
181-
},
182168
ImagePullPolicy: pullPolicy,
183169
TerminationMessagePolicy: corev1.TerminationMessageFallbackToLogsOnError,
184170
},
185171
},
186-
SecurityContext: &corev1.PodSecurityContext{
187-
RunAsNonRoot: &runAsNonRoot,
188-
RunAsUser: &runAsUser,
189-
SeccompProfile: &corev1.SeccompProfile{
190-
Type: corev1.SeccompProfileTypeRuntimeDefault,
191-
},
192-
},
193172
NodeSelector: map[string]string{
194173
"kubernetes.io/os": "linux",
195174
},
196175
ServiceAccountName: saName,
197176
},
198177
}
199178

179+
// Update pod security
180+
security.ApplyPodSpecSecurity(&pod.Spec)
181+
200182
// Override scheduling options if specified
201183
if source.Spec.GrpcPodConfig != nil {
202184
grpcPodConfig := source.Spec.GrpcPodConfig

Diff for: pkg/controller/registry/reconciler/reconciler_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ func TestPodContainerSecurityContext(t *testing.T) {
8282
expectedAllowPrivilegeEscalation := false
8383
expectedRunAsNonRoot := true
8484
expectedRunAsUser := int64(1001)
85+
expectedPrivileged := false
8586

8687
expectedContainerSecCtx := &corev1.SecurityContext{
88+
Privileged: &expectedPrivileged,
8789
ReadOnlyRootFilesystem: &expectedReadOnlyRootFilesystem,
8890
AllowPrivilegeEscalation: &expectedAllowPrivilegeEscalation,
8991
Capabilities: &corev1.Capabilities{

Diff for: pkg/controller/security/security.go

+26-21
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
package security
22

3-
import corev1 "k8s.io/api/core/v1"
3+
import (
4+
corev1 "k8s.io/api/core/v1"
5+
"k8s.io/utils/pointer"
6+
)
47

5-
var readOnlyRootFilesystem = false
6-
var allowPrivilegeEscalation = false
7-
var runAsNonRoot = true
8+
const readOnlyRootFilesystem = false
9+
const allowPrivilegeEscalation = false
10+
const privileged = false
11+
const runAsNonRoot = true
812

913
// See: https://github.com/operator-framework/operator-registry/blob/master/Dockerfile#L27
10-
var runAsUser int64 = 1001
11-
12-
var containerSecurityContext = &corev1.SecurityContext{
13-
ReadOnlyRootFilesystem: &readOnlyRootFilesystem,
14-
AllowPrivilegeEscalation: &allowPrivilegeEscalation,
15-
Capabilities: &corev1.Capabilities{
16-
Drop: []corev1.Capability{"ALL"},
17-
},
18-
}
19-
20-
var podSecurityContext = &corev1.PodSecurityContext{
21-
RunAsNonRoot: &runAsNonRoot,
22-
RunAsUser: &runAsUser,
23-
SeccompProfile: &corev1.SeccompProfile{
24-
Type: corev1.SeccompProfileTypeRuntimeDefault,
25-
},
26-
}
14+
const runAsUser int64 = 1001
2715

2816
// ApplyPodSpecSecurity applies the standard security profile to a pod spec
2917
func ApplyPodSpecSecurity(spec *corev1.PodSpec) {
18+
var containerSecurityContext = &corev1.SecurityContext{
19+
Privileged: pointer.Bool(privileged),
20+
ReadOnlyRootFilesystem: pointer.Bool(readOnlyRootFilesystem),
21+
AllowPrivilegeEscalation: pointer.Bool(allowPrivilegeEscalation),
22+
Capabilities: &corev1.Capabilities{
23+
Drop: []corev1.Capability{"ALL"},
24+
},
25+
}
26+
27+
var podSecurityContext = &corev1.PodSecurityContext{
28+
RunAsNonRoot: pointer.Bool(runAsNonRoot),
29+
RunAsUser: pointer.Int64(runAsUser),
30+
SeccompProfile: &corev1.SeccompProfile{
31+
Type: corev1.SeccompProfileTypeRuntimeDefault,
32+
},
33+
}
34+
3035
spec.SecurityContext = podSecurityContext
3136
for idx := 0; idx < len(spec.Containers); idx++ {
3237
spec.Containers[idx].SecurityContext = containerSecurityContext

0 commit comments

Comments
 (0)