Skip to content

Commit 353bcdb

Browse files
committed
Update unpack job security
Signed-off-by: perdasilva <[email protected]>
1 parent 7e1e2f1 commit 353bcdb

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

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

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

10+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/security"
1011
"github.com/operator-framework/operator-registry/pkg/api"
1112
"github.com/operator-framework/operator-registry/pkg/configmap"
1213
"github.com/sirupsen/logrus"
@@ -190,6 +191,10 @@ func (c *ConfigMapUnpacker) job(cmRef *corev1.ObjectReference, bundlePath string
190191
},
191192
},
192193
}
194+
195+
// Apply Pod security
196+
security.ApplyPodSpecSecurity(&job.Spec.Template.Spec)
197+
193198
job.SetNamespace(cmRef.Namespace)
194199
job.SetName(cmRef.Name)
195200
job.SetOwnerReferences([]metav1.OwnerReference{ownerRef(cmRef)})

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

+4-22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"hash/fnv"
77
"strings"
88

9+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/security"
910
corev1 "k8s.io/api/core/v1"
1011
"k8s.io/apimachinery/pkg/api/resource"
1112
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -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+
// Apply 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/security/security.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package security
2+
3+
import corev1 "k8s.io/api/core/v1"
4+
5+
var readOnlyRootFilesystem = false
6+
var allowPrivilegeEscalation = false
7+
var runAsNonRoot = true
8+
var runAsUser int64 = 1001
9+
10+
var containerSecurityContext = &corev1.SecurityContext{
11+
ReadOnlyRootFilesystem: &readOnlyRootFilesystem,
12+
AllowPrivilegeEscalation: &allowPrivilegeEscalation,
13+
Capabilities: &corev1.Capabilities{
14+
Drop: []corev1.Capability{"ALL"},
15+
},
16+
}
17+
18+
var podSecurityContext = &corev1.PodSecurityContext{
19+
RunAsNonRoot: &runAsNonRoot,
20+
RunAsUser: &runAsUser,
21+
SeccompProfile: &corev1.SeccompProfile{
22+
Type: corev1.SeccompProfileTypeRuntimeDefault,
23+
},
24+
}
25+
26+
// ApplyPodSpecSecurity applies the standard security profile to a pod spec
27+
func ApplyPodSpecSecurity(spec *corev1.PodSpec) {
28+
spec.SecurityContext = podSecurityContext
29+
for idx := 0; idx < len(spec.Containers); idx++ {
30+
spec.Containers[idx].SecurityContext = containerSecurityContext
31+
}
32+
for idx := 0; idx < len(spec.InitContainers); idx++ {
33+
spec.InitContainers[idx].SecurityContext = containerSecurityContext
34+
}
35+
}

0 commit comments

Comments
 (0)