Skip to content

Implement Security Contexts for Kubernetes MCP Servers #304

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

teodor-yanev
Copy link
Contributor

Closes #92

@teodor-yanev teodor-yanev self-assigned this Apr 30, 2025
Copy link
Collaborator

@ChrisJBurns ChrisJBurns left a comment

Choose a reason for hiding this comment

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

Apologies for not reviewing this sooner @teodor-yanev! Was trying to get the operator announcement out ready for today!

groupID := int64(1000)
container.WithSecurityContext(
corev1apply.SecurityContext().
WithAllowPrivilegeEscalation(false).
Copy link
Collaborator

Choose a reason for hiding this comment

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

We able to add defaults for privileged as well?

If privileged is set to false, by default allowPrivilegeEscalation will also be false, if you wanted to add allowPrivilegeEscalation explicitly though, i don't mind.

@@ -1019,6 +1029,19 @@ func configureContainer(
WithStdin(attachStdio).
WithTTY(false).
WithEnv(envVars...)

// Add container security context if not already present
if container.SecurityContext == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We may want to do other checks too. Currently if users have specified a securityContext but say only added allowPrivilegeEscalation: false, this check technically wouldn't be true and we'd skip over it. We might want to add more checks so that if securityContext != nil, we check for specific fields and add the sensible defaults if they aren't provided.

For example:
User specifies:

securityContext:
   allowPrivilegeEscalation: false

We should read that but apply

securityContext:
   allowPrivilegeEscalation: false
   runAsUser: 1000
   runAsGroup: 1000
    ...
    ....

If that makes sense

@@ -19,10 +19,18 @@ spec:
app.kubernetes.io/name: toolhive
spec:
serviceAccountName: toolhive
securityContext:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Fun fact, now we've released our super duper funky wunky operator, we should probably deprecate these old YAMLs. Or at least make a decision on whether we want to support both operator and non operator installs!

@@ -968,6 +968,16 @@ func ensurePodTemplateConfig(
podTemplateSpec.Spec = podTemplateSpec.Spec.WithRestartPolicy(corev1.RestartPolicyAlways)
}

// Add pod-level security context if not already present
if podTemplateSpec.Spec.SecurityContext == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment below as doing more checks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Security Contexts for Kubernetes MCP Servers
2 participants