Skip to content

Commit

Permalink
Do not run nova services as root
Browse files Browse the repository at this point in the history
  • Loading branch information
gibizer committed Nov 20, 2023
1 parent 6cb8656 commit 14fb34d
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 33 deletions.
7 changes: 3 additions & 4 deletions pkg/nova/cellmapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"

common "github.com/openstack-k8s-operators/lib-common/modules/common"
"github.com/openstack-k8s-operators/lib-common/modules/common/env"
novav1 "github.com/openstack-k8s-operators/nova-operator/api/v1beta1"
)

const (
cellMappingCommand = "/usr/local/bin/kolla_set_configs && /var/lib/openstack/bin/ensure_cell_mapping.sh"
cellMappingCommand = "sudo /usr/local/bin/kolla_set_configs && /var/lib/openstack/bin/ensure_cell_mapping.sh"
)

func CellMappingJob(
Expand All @@ -22,8 +23,6 @@ func CellMappingJob(
inputHash string,
labels map[string]string,
) *batchv1.Job {
runAsUser := int64(0)

args := []string{"-c"}
if cell.Spec.Debug.StopJob {
args = append(args, common.DebugCommand)
Expand Down Expand Up @@ -69,7 +68,7 @@ func CellMappingJob(
Args: args,
Image: cell.Spec.ConductorServiceTemplate.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsUser: ptr.To(NovaUserID),
},
Env: env,
VolumeMounts: []corev1.VolumeMount{
Expand Down
5 changes: 4 additions & 1 deletion pkg/nova/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ import (

const (
// KollaServiceCommand - the command to start the service binary in the kolla container
KollaServiceCommand = "/usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start"
KollaServiceCommand = "sudo /usr/local/bin/kolla_set_configs && /usr/local/bin/kolla_start"
// NovaAPIDatabaseName - the name of the DB to store tha API schema
NovaAPIDatabaseName = "nova_api"
// NovaCell0DatabaseName - the name of the DB to store the cell schema for
// cell0
NovaCell0DatabaseName = "nova_cell0"
// NovaUserID is the linux user ID used by Kolla for the nova user
// in the service containers
NovaUserID int64 = 42436
)

// GetScriptSecretName returns the name of the Secret used for the
Expand Down
7 changes: 3 additions & 4 deletions pkg/nova/host_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import (
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
)

const (
discoverCommand = "/usr/local/bin/kolla_set_configs && /var/lib/openstack/bin/host_discover.sh"
discoverCommand = "sudo /usr/local/bin/kolla_set_configs && /var/lib/openstack/bin/host_discover.sh"
)

func HostDiscoveryJob(
Expand All @@ -35,8 +36,6 @@ func HostDiscoveryJob(
inputHash string,
labels map[string]string,
) *batchv1.Job {
runAsUser := int64(0)

args := []string{"-c"}
if instance.Spec.Debug.StopJob {
args = append(args, common.DebugCommand)
Expand Down Expand Up @@ -81,7 +80,7 @@ func HostDiscoveryJob(
Args: args,
Image: instance.Spec.ConductorServiceTemplate.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsUser: ptr.To(NovaUserID),
},
Env: env,
VolumeMounts: []corev1.VolumeMount{
Expand Down
7 changes: 3 additions & 4 deletions pkg/novaapi/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"
)

// StatefulSet - returns the StatefulSet definition for the nova-api service
Expand All @@ -36,8 +37,6 @@ func StatefulSet(
labels map[string]string,
annotations map[string]string,
) *appsv1.StatefulSet {
runAsUser := int64(0)

// This allows the pod to start up slowly. The pod will only be killed
// if it does not succeed a probe in 60 seconds.
startupProbe := &corev1.Probe{
Expand Down Expand Up @@ -135,7 +134,7 @@ func StatefulSet(
Args: []string{"-c", "tail -n+1 -F /var/log/nova/nova-api.log"},
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsUser: ptr.To(nova.NovaUserID),
},
Env: env,
VolumeMounts: []corev1.VolumeMount{nova.GetLogVolumeMount()},
Expand All @@ -152,7 +151,7 @@ func StatefulSet(
Args: args,
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsUser: ptr.To(nova.NovaUserID),
},
Env: env,
VolumeMounts: []corev1.VolumeMount{
Expand Down
5 changes: 2 additions & 3 deletions pkg/novacompute/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
)

// StatefulSet - returns the StatefulSet definition for the nova-compute service
Expand All @@ -35,8 +36,6 @@ func StatefulSet(
labels map[string]string,
annotations map[string]string,
) *appsv1.StatefulSet {
runAsUser := int64(0)

// After the first successful startupProbe, livenessProbe takes over
livenessProbe := &corev1.Probe{
// TODO might need tuning
Expand Down Expand Up @@ -122,7 +121,7 @@ func StatefulSet(
Args: args,
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsUser: ptr.To(nova.NovaUserID),
},
Env: env,
VolumeMounts: []corev1.VolumeMount{
Expand Down
7 changes: 3 additions & 4 deletions pkg/novaconductor/dbsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ import (
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
)

const (
// cellDBSyncCommand - the command to be used to run db sync for the cell DB
cellDBSyncCommand = "/usr/local/bin/kolla_set_configs && /bin/sh -c /var/lib/openstack/bin/dbsync.sh"
cellDBSyncCommand = "sudo /usr/local/bin/kolla_set_configs && /bin/sh -c /var/lib/openstack/bin/dbsync.sh"
)

// CellDBSyncJob - define a batchv1.Job to be run to apply the cel DB schema
Expand All @@ -39,8 +40,6 @@ func CellDBSyncJob(
labels map[string]string,
annotations map[string]string,
) *batchv1.Job {
runAsUser := int64(0)

args := []string{"-c"}
if instance.Spec.Debug.StopJob {
args = append(args, common.DebugCommand)
Expand Down Expand Up @@ -81,7 +80,7 @@ func CellDBSyncJob(
Args: args,
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsUser: ptr.To(nova.NovaUserID),
},
Env: env,
VolumeMounts: []corev1.VolumeMount{
Expand Down
5 changes: 2 additions & 3 deletions pkg/novaconductor/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
)

// StatefulSet - returns the StatefulSet definition for the nova-api service
Expand All @@ -35,8 +36,6 @@ func StatefulSet(
labels map[string]string,
annotations map[string]string,
) *appsv1.StatefulSet {
runAsUser := int64(0)

livenessProbe := &corev1.Probe{
// TODO might need tuning
TimeoutSeconds: 5,
Expand Down Expand Up @@ -128,7 +127,7 @@ func StatefulSet(
Args: args,
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsUser: ptr.To(nova.NovaUserID),
},
Env: env,
VolumeMounts: []corev1.VolumeMount{
Expand Down
7 changes: 3 additions & 4 deletions pkg/novametadata/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"
)

// StatefulSet - returns the StatefulSet definition for the nova-metadata service
Expand All @@ -36,8 +37,6 @@ func StatefulSet(
labels map[string]string,
annotations map[string]string,
) *appsv1.StatefulSet {
runAsUser := int64(0)

// This allows the pod to start up slowly. The pod will only be killed
// if it does not succeed a probe in 60 seconds.
startupProbe := &corev1.Probe{
Expand Down Expand Up @@ -135,7 +134,7 @@ func StatefulSet(
Args: []string{"-c", "tail -n+1 -F /var/log/nova/nova-metadata.log"},
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsUser: ptr.To(nova.NovaUserID),
},
Env: env,
VolumeMounts: []corev1.VolumeMount{
Expand All @@ -154,7 +153,7 @@ func StatefulSet(
Args: args,
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsUser: ptr.To(nova.NovaUserID),
},
Env: env,
VolumeMounts: []corev1.VolumeMount{
Expand Down
5 changes: 2 additions & 3 deletions pkg/novascheduler/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
)

// StatefulSet - returns the StatefulSet definition for the nova-scheduler service
Expand All @@ -35,8 +36,6 @@ func StatefulSet(
labels map[string]string,
annotations map[string]string,
) *appsv1.StatefulSet {
runAsUser := int64(0)

// This allows the pod to start up slowly. The pod will only be killed
// if it does not succeed a probe in 60 seconds.
startupProbe := &corev1.Probe{
Expand Down Expand Up @@ -137,7 +136,7 @@ func StatefulSet(
Args: args,
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsUser: ptr.To(nova.NovaUserID),
},
Env: env,
VolumeMounts: []corev1.VolumeMount{
Expand Down
5 changes: 2 additions & 3 deletions pkg/novncproxy/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"
)

// StatefulSet - returns the StatefulSet definition for the nova-novanovncproxy service
Expand All @@ -36,8 +37,6 @@ func StatefulSet(
labels map[string]string,
annotations map[string]string,
) *appsv1.StatefulSet {
runAsUser := int64(0)

// This allows the pod to start up slowly. The pod will only be killed
// if it does not succeed a probe in 60 seconds.
startupProbe := &corev1.Probe{
Expand Down Expand Up @@ -135,7 +134,7 @@ func StatefulSet(
Args: args,
Image: instance.Spec.ContainerImage,
SecurityContext: &corev1.SecurityContext{
RunAsUser: &runAsUser,
RunAsUser: ptr.To(nova.NovaUserID),
},
Env: env,
VolumeMounts: []corev1.VolumeMount{
Expand Down

0 comments on commit 14fb34d

Please sign in to comment.