Skip to content

Commit 8fdff26

Browse files
gibizeropenshift-merge-bot[bot]
authored andcommitted
Do not run placement service as root
This did not removed the root usage from the init container. We should get rid of the init container instead. (See #64) Implements: https://issues.redhat.com/browse/OSPRH-1374
1 parent 8e2ef61 commit 8fdff26

File tree

10 files changed

+46
-29
lines changed

10 files changed

+46
-29
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
k8s.io/api v0.26.11
1818
k8s.io/apimachinery v0.26.11
1919
k8s.io/client-go v0.26.11
20+
k8s.io/utils v0.0.0-20231127182322-b307cd553661
2021
sigs.k8s.io/controller-runtime v0.14.7
2122
)
2223

@@ -79,7 +80,6 @@ require (
7980
k8s.io/component-base v0.26.11 //indirect
8081
k8s.io/klog/v2 v2.100.1 // indirect
8182
k8s.io/kube-openapi v0.0.0-20230308215209-15aac26d736a //indirect
82-
k8s.io/utils v0.0.0-20231127182322-b307cd553661 //indirect
8383
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd //indirect
8484
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
8585
sigs.k8s.io/yaml v1.3.0 // indirect

pkg/placement/const.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ const (
2525
PlacementPublicPort int32 = 8778
2626
// PlacementInternalPort -
2727
PlacementInternalPort int32 = 8778
28+
29+
KollaServiceCommand = "/usr/local/bin/kolla_start"
30+
31+
// PlacementUserID is the linux user ID used by Kolla for the placement
32+
// user in the service containers
33+
PlacementUserID int64 = 42482
2834
)

pkg/placement/dbsync.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ import (
2424
batchv1 "k8s.io/api/batch/v1"
2525
corev1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27-
)
28-
29-
const (
30-
// DBSyncCommand -
31-
DBSyncCommand = "/usr/local/bin/kolla_set_configs && su -s /bin/sh -c \"placement-manage db sync\" placement"
27+
"k8s.io/utils/ptr"
3228
)
3329

3430
// DbSyncJob func
@@ -37,13 +33,11 @@ func DbSyncJob(
3733
labels map[string]string,
3834
annotations map[string]string,
3935
) *batchv1.Job {
40-
runAsUser := int64(0)
41-
4236
args := []string{"-c"}
4337
if instance.Spec.Debug.DBSync {
4438
args = append(args, common.DebugCommand)
4539
} else {
46-
args = append(args, DBSyncCommand)
40+
args = append(args, KollaServiceCommand)
4741
}
4842

4943
envVars := map[string]env.Setter{}
@@ -73,10 +67,10 @@ func DbSyncJob(
7367
Args: args,
7468
Image: instance.Spec.ContainerImage,
7569
SecurityContext: &corev1.SecurityContext{
76-
RunAsUser: &runAsUser,
70+
RunAsUser: ptr.To(PlacementUserID),
7771
},
7872
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
79-
VolumeMounts: getVolumeMounts(),
73+
VolumeMounts: getVolumeMounts("dbsync"),
8074
},
8175
},
8276
},

pkg/placement/deployment.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ import (
2626
corev1 "k8s.io/api/core/v1"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/util/intstr"
29-
)
30-
31-
const (
32-
// ServiceCommand -
33-
ServiceCommand = "/usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start"
29+
"k8s.io/utils/ptr"
3430
)
3531

3632
// Deployment func
@@ -40,8 +36,6 @@ func Deployment(
4036
labels map[string]string,
4137
annotations map[string]string,
4238
) *appsv1.Deployment {
43-
runAsUser := int64(0)
44-
4539
livenessProbe := &corev1.Probe{
4640
// TODO might need tuning
4741
TimeoutSeconds: 5,
@@ -70,7 +64,7 @@ func Deployment(
7064
},
7165
}
7266
} else {
73-
args = append(args, ServiceCommand)
67+
args = append(args, KollaServiceCommand)
7468
//
7569
// https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/
7670
//
@@ -112,10 +106,10 @@ func Deployment(
112106
Args: args,
113107
Image: instance.Spec.ContainerImage,
114108
SecurityContext: &corev1.SecurityContext{
115-
RunAsUser: &runAsUser,
109+
RunAsUser: ptr.To(PlacementUserID),
116110
},
117111
Env: env.MergeEnvs([]corev1.EnvVar{}, envVars),
118-
VolumeMounts: getVolumeMounts(),
112+
VolumeMounts: getVolumeMounts("api"),
119113
Resources: instance.Spec.Resources,
120114
ReadinessProbe: readinessProbe,
121115
LivenessProbe: livenessProbe,

pkg/placement/volumes.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func getInitVolumeMounts() []corev1.VolumeMount {
7979
}
8080

8181
// getVolumeMounts - general VolumeMounts
82-
func getVolumeMounts() []corev1.VolumeMount {
82+
func getVolumeMounts(serviceName string) []corev1.VolumeMount {
8383
return []corev1.VolumeMount{
8484
{
8585
Name: "scripts",
@@ -94,7 +94,7 @@ func getVolumeMounts() []corev1.VolumeMount {
9494
{
9595
Name: "config-data-merged",
9696
MountPath: "/var/lib/kolla/config_files/config.json",
97-
SubPath: "placement-api-config.json",
97+
SubPath: "placement-" + serviceName + "-config.json",
9898
ReadOnly: true,
9999
},
100100
}

templates/placementapi/config/httpd.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combine
2323
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" proxy
2424

2525
SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" forwarded
26+
ErrorLog /dev/stderr
27+
TransferLog /dev/stdout
2628
CustomLog /dev/stdout combined env=!forwarded
2729
CustomLog /dev/stdout proxy env=forwarded
2830

templates/placementapi/config/placement-api-config.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,25 @@
1616
{
1717
"source": "/var/lib/config-data/merged/httpd.conf",
1818
"dest": "/etc/httpd/conf/httpd.conf",
19-
"owner": "root",
19+
"owner": "apache",
2020
"perm": "0644"
2121
},
2222
{
2323
"source": "/var/lib/config-data/merged/logging.conf",
2424
"dest": "/etc/placement/logging.conf",
25-
"owner": "root",
26-
"perm": "0644"
25+
"owner": "placement",
26+
"perm": "0600"
2727
}
2828
],
2929
"permissions": [
3030
{
3131
"path": "/var/log/placement",
32-
"owner": "placement:placement",
32+
"owner": "placement:apache",
33+
"recurse": true
34+
},
35+
{
36+
"path": "/etc/httpd/run/",
37+
"owner": "placement:apache",
3338
"recurse": true
3439
}
3540
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"command": "placement-manage db sync",
3+
"config_files": [
4+
{
5+
"source": "/var/lib/config-data/merged/placement.conf",
6+
"dest": "/etc/placement/placement.conf",
7+
"owner": "placement",
8+
"perm": "0600"
9+
},
10+
{
11+
"source": "/var/lib/config-data/merged/custom.conf",
12+
"dest": "/etc/placement/placement.conf.d/custom.conf",
13+
"owner": "placement",
14+
"perm": "0600"
15+
}
16+
]
17+
}

tests/functional/placementapi_controller_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ var _ = Describe("PlacementAPI controller", func() {
350350

351351
container := job.Spec.Template.Spec.Containers[0]
352352
Expect(container.VolumeMounts).To(HaveLen(3))
353-
Expect(container.Args[1]).To(ContainSubstring("placement-manage db sync"))
354353
Expect(container.Image).To(Equal("quay.io/podified-antelope-centos9/openstack-placement-api:current-podified"))
355354

356355
th.SimulateJobSuccess(names.DBSyncJobName)

tests/kuttl/common/assert_sample_deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ spec:
103103
containers:
104104
- args:
105105
- -c
106-
- /usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start
106+
- /usr/local/bin/kolla_start
107107
command:
108108
- /bin/bash
109109
imagePullPolicy: IfNotPresent

0 commit comments

Comments
 (0)