Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config-reloader: change to non-root user #4235

Merged
merged 7 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,8 @@ v0.35.0 (2023-07-18)

- Add deployment spec options to describe operator's Prometheus Config Reloader image. (@alekseybb197)

- `config-reloader` container in agent no longer runs as root. (@rootmout)

- Update `module.git` with basic and SSH key authentication support. (@djcode)

- Support `clustering` block in `prometheus.operator.servicemonitors` and `prometheus.operator.podmonitors` components to distribute
Expand Down
8 changes: 7 additions & 1 deletion pkg/operator/resources_pod_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,20 @@ func generatePodTemplate(
imagePathConfigReloader = *d.Agent.Spec.ConfigReloaderImage
}

boolFalse := false
boolTrue := true
operatorContainers := []core_v1.Container{
{
Name: "config-reloader",
Image: imagePathConfigReloader,
VolumeMounts: volumeMounts,
Env: envVars,
SecurityContext: &core_v1.SecurityContext{
RunAsUser: pointer.Int64(0),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rfratto is there a specific reason why this was initially configured to run as root? do you see any problems with this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably just me being aggressive with copy and paste, I think it's safe to remove.

AllowPrivilegeEscalation: &boolFalse,
ReadOnlyRootFilesystem: &boolTrue,
Capabilities: &core_v1.Capabilities{
Drop: []core_v1.Capability{"ALL"},
},
},
Args: []string{
"--config-file=/var/lib/grafana-agent/config-in/agent.yml",
Expand Down
27 changes: 24 additions & 3 deletions pkg/operator/resources_pod_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func Test_generatePodTemplate(t *testing.T) {
assert.False(t, tmpl.Spec.Containers[i].SecurityContext.Privileged != nil &&
*tmpl.Spec.Containers[i].SecurityContext.Privileged,
"privileged is not required. Fargate cannot schedule privileged containers.")
assert.False(t, tmpl.Spec.Containers[i].SecurityContext.RunAsUser != nil &&
*tmpl.Spec.Containers[i].SecurityContext.RunAsUser == int64(0),
"force the container to run as root is not required.")
assert.False(t, tmpl.Spec.Containers[i].SecurityContext.AllowPrivilegeEscalation != nil &&
*tmpl.Spec.Containers[i].SecurityContext.AllowPrivilegeEscalation,
"allow privilege escalation is not required.")
}
})

Expand All @@ -110,9 +116,24 @@ func Test_generatePodTemplate(t *testing.T) {

tmpl, _, err := generatePodTemplate(cfg, "agent", deploy, podTemplateOptions{Privileged: true})
require.NoError(t, err)
assert.True(t, tmpl.Spec.Containers[1].SecurityContext.Privileged != nil &&
*tmpl.Spec.Containers[1].SecurityContext.Privileged,
"privileged is needed if pod options say so.")
for i := range tmpl.Spec.Containers {
// only grafana-agent container is supposed to be privileged
if tmpl.Spec.Containers[i].Name == "grafana-agent" {
assert.True(t, tmpl.Spec.Containers[i].SecurityContext.Privileged != nil &&
*tmpl.Spec.Containers[i].SecurityContext.Privileged,
"privileged is needed for grafana-agent if pod options say so.")
} else {
assert.False(t, tmpl.Spec.Containers[i].SecurityContext.Privileged != nil &&
*tmpl.Spec.Containers[i].SecurityContext.Privileged,
"privileged is not required for other containers.")
assert.False(t, tmpl.Spec.Containers[i].SecurityContext.RunAsUser != nil &&
*tmpl.Spec.Containers[i].SecurityContext.RunAsUser == int64(0),
"force the container to run as root is not required for other containers.")
assert.False(t, tmpl.Spec.Containers[i].SecurityContext.AllowPrivilegeEscalation != nil &&
*tmpl.Spec.Containers[i].SecurityContext.AllowPrivilegeEscalation,
"allow privilege escalation is not required for other containers.")
}
}
})

t.Run("runtimeclassname set if passed in", func(t *testing.T) {
Expand Down