Skip to content

Commit 1ad888f

Browse files
committed
Enhance security context in deployment configurations by adding user and group settings, seccomp profiles, and disabling privilege escalation. Remove unnecessary logging flag from deployment arguments. Implement environment variable support for enabling Gateway API in main application logic.
1 parent 6a206f7 commit 1ad888f

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

charts/homer-operator/templates/deployment.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ spec:
8585
args:
8686
- --secure-listen-address=0.0.0.0:8443
8787
- --upstream=http://127.0.0.1:8080/
88-
- --logtostderr=true
8988
- --v=0
9089
ports:
9190
- containerPort: 8443

cmd/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"crypto/tls"
2121
"flag"
2222
"os"
23+
"strconv"
2324

2425
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2526
// to ensure that exec-entrypoint and run can make use of them.
@@ -77,6 +78,13 @@ func main() {
7778
opts.BindFlags(flag.CommandLine)
7879
flag.Parse()
7980

81+
// Override enableGatewayAPI from environment variable if set
82+
if envGateway := os.Getenv("ENABLE_GATEWAY_API"); envGateway != "" {
83+
if parsed, err := strconv.ParseBool(envGateway); err == nil {
84+
enableGatewayAPI = parsed
85+
}
86+
}
87+
8088
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
8189

8290
// if the enable-http2 flag is false (the default), http/2 should be disabled
@@ -143,13 +151,16 @@ func main() {
143151
os.Exit(1)
144152
}
145153
if enableGatewayAPI {
154+
setupLog.Info("Gateway API support enabled, setting up HTTPRoute controller")
146155
if err = (&controller.HTTPRouteReconciler{
147156
Client: mgr.GetClient(),
148157
Scheme: mgr.GetScheme(),
149158
}).SetupWithManager(mgr); err != nil {
150159
setupLog.Error(err, "unable to create controller", "controller", "HTTPRoute")
151160
os.Exit(1)
152161
}
162+
} else {
163+
setupLog.Info("Gateway API support disabled")
153164
}
154165
//+kubebuilder:scaffold:builder
155166

pkg/homer/config.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,15 @@ func CreateDeployment(name string, namespace string, replicas *int32, owner clie
273273
},
274274
},
275275
Spec: corev1.PodSpec{
276+
SecurityContext: &corev1.PodSecurityContext{
277+
RunAsNonRoot: &[]bool{true}[0],
278+
RunAsUser: &[]int64{1000}[0],
279+
RunAsGroup: &[]int64{1000}[0],
280+
FSGroup: &[]int64{1000}[0],
281+
SeccompProfile: &corev1.SeccompProfile{
282+
Type: corev1.SeccompProfileTypeRuntimeDefault,
283+
},
284+
},
276285
InitContainers: []corev1.Container{
277286
{
278287
Name: "init-assets",
@@ -282,6 +291,18 @@ func CreateDeployment(name string, namespace string, replicas *int32, owner clie
282291
"-c",
283292
"cp /config/config.yml /www/assets/config.yml && chown -R 1000:1000 /www/assets && chmod -R 755 /www/assets",
284293
},
294+
SecurityContext: &corev1.SecurityContext{
295+
AllowPrivilegeEscalation: &[]bool{false}[0],
296+
RunAsNonRoot: &[]bool{true}[0],
297+
RunAsUser: &[]int64{1000}[0],
298+
RunAsGroup: &[]int64{1000}[0],
299+
Capabilities: &corev1.Capabilities{
300+
Drop: []corev1.Capability{"ALL"},
301+
},
302+
SeccompProfile: &corev1.SeccompProfile{
303+
Type: corev1.SeccompProfileTypeRuntimeDefault,
304+
},
305+
},
285306
VolumeMounts: []corev1.VolumeMount{
286307
{
287308
Name: "config-volume",
@@ -298,6 +319,18 @@ func CreateDeployment(name string, namespace string, replicas *int32, owner clie
298319
{
299320
Name: name,
300321
Image: image,
322+
SecurityContext: &corev1.SecurityContext{
323+
AllowPrivilegeEscalation: &[]bool{false}[0],
324+
RunAsNonRoot: &[]bool{true}[0],
325+
RunAsUser: &[]int64{1000}[0],
326+
RunAsGroup: &[]int64{1000}[0],
327+
Capabilities: &corev1.Capabilities{
328+
Drop: []corev1.Capability{"ALL"},
329+
},
330+
SeccompProfile: &corev1.SeccompProfile{
331+
Type: corev1.SeccompProfileTypeRuntimeDefault,
332+
},
333+
},
301334
VolumeMounts: []corev1.VolumeMount{
302335
{
303336
Name: "assets-volume",
@@ -585,6 +618,15 @@ func CreateDeploymentWithAssets(name string, namespace string, replicas *int32,
585618
},
586619
},
587620
Spec: corev1.PodSpec{
621+
SecurityContext: &corev1.PodSecurityContext{
622+
RunAsNonRoot: &[]bool{true}[0],
623+
RunAsUser: &[]int64{1000}[0],
624+
RunAsGroup: &[]int64{1000}[0],
625+
FSGroup: &[]int64{1000}[0],
626+
SeccompProfile: &corev1.SeccompProfile{
627+
Type: corev1.SeccompProfileTypeRuntimeDefault,
628+
},
629+
},
588630
InitContainers: []corev1.Container{
589631
{
590632
Name: "init-assets",
@@ -594,13 +636,37 @@ func CreateDeploymentWithAssets(name string, namespace string, replicas *int32,
594636
"-c",
595637
initCommand,
596638
},
639+
SecurityContext: &corev1.SecurityContext{
640+
AllowPrivilegeEscalation: &[]bool{false}[0],
641+
RunAsNonRoot: &[]bool{true}[0],
642+
RunAsUser: &[]int64{1000}[0],
643+
RunAsGroup: &[]int64{1000}[0],
644+
Capabilities: &corev1.Capabilities{
645+
Drop: []corev1.Capability{"ALL"},
646+
},
647+
SeccompProfile: &corev1.SeccompProfile{
648+
Type: corev1.SeccompProfileTypeRuntimeDefault,
649+
},
650+
},
597651
VolumeMounts: initVolumeMounts,
598652
},
599653
},
600654
Containers: []corev1.Container{
601655
{
602656
Name: name,
603657
Image: image,
658+
SecurityContext: &corev1.SecurityContext{
659+
AllowPrivilegeEscalation: &[]bool{false}[0],
660+
RunAsNonRoot: &[]bool{true}[0],
661+
RunAsUser: &[]int64{1000}[0],
662+
RunAsGroup: &[]int64{1000}[0],
663+
Capabilities: &corev1.Capabilities{
664+
Drop: []corev1.Capability{"ALL"},
665+
},
666+
SeccompProfile: &corev1.SeccompProfile{
667+
Type: corev1.SeccompProfileTypeRuntimeDefault,
668+
},
669+
},
604670
VolumeMounts: []corev1.VolumeMount{
605671
{
606672
Name: "assets-volume",

0 commit comments

Comments
 (0)